blob: 4a939a7510ded8c9c56317dc9c22ade1f2d21177 [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);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100454#ifdef FEAT_JOB
455static void job_free(job_T *job);
456#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100457static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
458static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
459static char_u *string_quote(char_u *str, int function);
460static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
461static int find_internal_func(char_u *name);
462static char_u *deref_func_name(char_u *name, int *lenp, int no_autoload);
463static 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 +0100464static void emsg_funcname(char *ermsg, char_u *name);
465static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000466
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100468static void f_abs(typval_T *argvars, typval_T *rettv);
469static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100471static void f_add(typval_T *argvars, typval_T *rettv);
472static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
473static void f_and(typval_T *argvars, typval_T *rettv);
474static void f_append(typval_T *argvars, typval_T *rettv);
475static void f_argc(typval_T *argvars, typval_T *rettv);
476static void f_argidx(typval_T *argvars, typval_T *rettv);
477static void f_arglistid(typval_T *argvars, typval_T *rettv);
478static void f_argv(typval_T *argvars, typval_T *rettv);
479static void f_assert_equal(typval_T *argvars, typval_T *rettv);
480static void f_assert_exception(typval_T *argvars, typval_T *rettv);
481static void f_assert_fails(typval_T *argvars, typval_T *rettv);
482static void f_assert_false(typval_T *argvars, typval_T *rettv);
483static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100485static void f_asin(typval_T *argvars, typval_T *rettv);
486static void f_atan(typval_T *argvars, typval_T *rettv);
487static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000488#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100489static void f_browse(typval_T *argvars, typval_T *rettv);
490static void f_browsedir(typval_T *argvars, typval_T *rettv);
491static void f_bufexists(typval_T *argvars, typval_T *rettv);
492static void f_buflisted(typval_T *argvars, typval_T *rettv);
493static void f_bufloaded(typval_T *argvars, typval_T *rettv);
494static void f_bufname(typval_T *argvars, typval_T *rettv);
495static void f_bufnr(typval_T *argvars, typval_T *rettv);
496static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
497static void f_byte2line(typval_T *argvars, typval_T *rettv);
498static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
499static void f_byteidx(typval_T *argvars, typval_T *rettv);
500static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
501static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000502#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100503static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000504#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100505#ifdef FEAT_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100506static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100507static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
508static void f_ch_open(typval_T *argvars, typval_T *rettv);
509static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100510static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
511static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
512#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100513static void f_changenr(typval_T *argvars, typval_T *rettv);
514static void f_char2nr(typval_T *argvars, typval_T *rettv);
515static void f_cindent(typval_T *argvars, typval_T *rettv);
516static void f_clearmatches(typval_T *argvars, typval_T *rettv);
517static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000518#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100519static void f_complete(typval_T *argvars, typval_T *rettv);
520static void f_complete_add(typval_T *argvars, typval_T *rettv);
521static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000522#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100523static void f_confirm(typval_T *argvars, typval_T *rettv);
524static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000525#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100526static void f_cos(typval_T *argvars, typval_T *rettv);
527static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000528#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100529static void f_count(typval_T *argvars, typval_T *rettv);
530static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
531static void f_cursor(typval_T *argsvars, typval_T *rettv);
532static void f_deepcopy(typval_T *argvars, typval_T *rettv);
533static void f_delete(typval_T *argvars, typval_T *rettv);
534static void f_did_filetype(typval_T *argvars, typval_T *rettv);
535static void f_diff_filler(typval_T *argvars, typval_T *rettv);
536static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100537static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100538static void f_empty(typval_T *argvars, typval_T *rettv);
539static void f_escape(typval_T *argvars, typval_T *rettv);
540static void f_eval(typval_T *argvars, typval_T *rettv);
541static void f_eventhandler(typval_T *argvars, typval_T *rettv);
542static void f_executable(typval_T *argvars, typval_T *rettv);
543static void f_exepath(typval_T *argvars, typval_T *rettv);
544static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200545#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100546static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200547#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100548static void f_expand(typval_T *argvars, typval_T *rettv);
549static void f_extend(typval_T *argvars, typval_T *rettv);
550static void f_feedkeys(typval_T *argvars, typval_T *rettv);
551static void f_filereadable(typval_T *argvars, typval_T *rettv);
552static void f_filewritable(typval_T *argvars, typval_T *rettv);
553static void f_filter(typval_T *argvars, typval_T *rettv);
554static void f_finddir(typval_T *argvars, typval_T *rettv);
555static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000556#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100557static void f_float2nr(typval_T *argvars, typval_T *rettv);
558static void f_floor(typval_T *argvars, typval_T *rettv);
559static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000560#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100561static void f_fnameescape(typval_T *argvars, typval_T *rettv);
562static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
563static void f_foldclosed(typval_T *argvars, typval_T *rettv);
564static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
565static void f_foldlevel(typval_T *argvars, typval_T *rettv);
566static void f_foldtext(typval_T *argvars, typval_T *rettv);
567static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
568static void f_foreground(typval_T *argvars, typval_T *rettv);
569static void f_function(typval_T *argvars, typval_T *rettv);
570static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
571static void f_get(typval_T *argvars, typval_T *rettv);
572static void f_getbufline(typval_T *argvars, typval_T *rettv);
573static void f_getbufvar(typval_T *argvars, typval_T *rettv);
574static void f_getchar(typval_T *argvars, typval_T *rettv);
575static void f_getcharmod(typval_T *argvars, typval_T *rettv);
576static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
577static void f_getcmdline(typval_T *argvars, typval_T *rettv);
578static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
579static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
580static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
581static void f_getcwd(typval_T *argvars, typval_T *rettv);
582static void f_getfontname(typval_T *argvars, typval_T *rettv);
583static void f_getfperm(typval_T *argvars, typval_T *rettv);
584static void f_getfsize(typval_T *argvars, typval_T *rettv);
585static void f_getftime(typval_T *argvars, typval_T *rettv);
586static void f_getftype(typval_T *argvars, typval_T *rettv);
587static void f_getline(typval_T *argvars, typval_T *rettv);
588static void f_getmatches(typval_T *argvars, typval_T *rettv);
589static void f_getpid(typval_T *argvars, typval_T *rettv);
590static void f_getcurpos(typval_T *argvars, typval_T *rettv);
591static void f_getpos(typval_T *argvars, typval_T *rettv);
592static void f_getqflist(typval_T *argvars, typval_T *rettv);
593static void f_getreg(typval_T *argvars, typval_T *rettv);
594static void f_getregtype(typval_T *argvars, typval_T *rettv);
595static void f_gettabvar(typval_T *argvars, typval_T *rettv);
596static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
597static void f_getwinposx(typval_T *argvars, typval_T *rettv);
598static void f_getwinposy(typval_T *argvars, typval_T *rettv);
599static void f_getwinvar(typval_T *argvars, typval_T *rettv);
600static void f_glob(typval_T *argvars, typval_T *rettv);
601static void f_globpath(typval_T *argvars, typval_T *rettv);
602static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
603static void f_has(typval_T *argvars, typval_T *rettv);
604static void f_has_key(typval_T *argvars, typval_T *rettv);
605static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
606static void f_hasmapto(typval_T *argvars, typval_T *rettv);
607static void f_histadd(typval_T *argvars, typval_T *rettv);
608static void f_histdel(typval_T *argvars, typval_T *rettv);
609static void f_histget(typval_T *argvars, typval_T *rettv);
610static void f_histnr(typval_T *argvars, typval_T *rettv);
611static void f_hlID(typval_T *argvars, typval_T *rettv);
612static void f_hlexists(typval_T *argvars, typval_T *rettv);
613static void f_hostname(typval_T *argvars, typval_T *rettv);
614static void f_iconv(typval_T *argvars, typval_T *rettv);
615static void f_indent(typval_T *argvars, typval_T *rettv);
616static void f_index(typval_T *argvars, typval_T *rettv);
617static void f_input(typval_T *argvars, typval_T *rettv);
618static void f_inputdialog(typval_T *argvars, typval_T *rettv);
619static void f_inputlist(typval_T *argvars, typval_T *rettv);
620static void f_inputrestore(typval_T *argvars, typval_T *rettv);
621static void f_inputsave(typval_T *argvars, typval_T *rettv);
622static void f_inputsecret(typval_T *argvars, typval_T *rettv);
623static void f_insert(typval_T *argvars, typval_T *rettv);
624static void f_invert(typval_T *argvars, typval_T *rettv);
625static void f_isdirectory(typval_T *argvars, typval_T *rettv);
626static void f_islocked(typval_T *argvars, typval_T *rettv);
627static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100628#ifdef FEAT_JOB
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100629static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100630static void f_job_start(typval_T *argvars, typval_T *rettv);
631static void f_job_stop(typval_T *argvars, typval_T *rettv);
632static void f_job_status(typval_T *argvars, typval_T *rettv);
633#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100634static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100635static void f_js_decode(typval_T *argvars, typval_T *rettv);
636static void f_js_encode(typval_T *argvars, typval_T *rettv);
637static void f_json_decode(typval_T *argvars, typval_T *rettv);
638static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100639static void f_keys(typval_T *argvars, typval_T *rettv);
640static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
641static void f_len(typval_T *argvars, typval_T *rettv);
642static void f_libcall(typval_T *argvars, typval_T *rettv);
643static void f_libcallnr(typval_T *argvars, typval_T *rettv);
644static void f_line(typval_T *argvars, typval_T *rettv);
645static void f_line2byte(typval_T *argvars, typval_T *rettv);
646static void f_lispindent(typval_T *argvars, typval_T *rettv);
647static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000648#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100649static void f_log(typval_T *argvars, typval_T *rettv);
650static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000651#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200652#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100653static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200654#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100655static void f_map(typval_T *argvars, typval_T *rettv);
656static void f_maparg(typval_T *argvars, typval_T *rettv);
657static void f_mapcheck(typval_T *argvars, typval_T *rettv);
658static void f_match(typval_T *argvars, typval_T *rettv);
659static void f_matchadd(typval_T *argvars, typval_T *rettv);
660static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
661static void f_matcharg(typval_T *argvars, typval_T *rettv);
662static void f_matchdelete(typval_T *argvars, typval_T *rettv);
663static void f_matchend(typval_T *argvars, typval_T *rettv);
664static void f_matchlist(typval_T *argvars, typval_T *rettv);
665static void f_matchstr(typval_T *argvars, typval_T *rettv);
666static void f_max(typval_T *argvars, typval_T *rettv);
667static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000668#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100669static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000670#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100671static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100672#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100673static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100674#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100675static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
676static void f_nr2char(typval_T *argvars, typval_T *rettv);
677static void f_or(typval_T *argvars, typval_T *rettv);
678static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100679#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100680static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100681#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000682#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100683static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000684#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100685static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
686static void f_printf(typval_T *argvars, typval_T *rettv);
687static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200688#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100689static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200690#endif
691#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100692static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200693#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100694static void f_range(typval_T *argvars, typval_T *rettv);
695static void f_readfile(typval_T *argvars, typval_T *rettv);
696static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100697#ifdef FEAT_FLOAT
698static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
699#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100700static void f_reltimestr(typval_T *argvars, typval_T *rettv);
701static void f_remote_expr(typval_T *argvars, typval_T *rettv);
702static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
703static void f_remote_peek(typval_T *argvars, typval_T *rettv);
704static void f_remote_read(typval_T *argvars, typval_T *rettv);
705static void f_remote_send(typval_T *argvars, typval_T *rettv);
706static void f_remove(typval_T *argvars, typval_T *rettv);
707static void f_rename(typval_T *argvars, typval_T *rettv);
708static void f_repeat(typval_T *argvars, typval_T *rettv);
709static void f_resolve(typval_T *argvars, typval_T *rettv);
710static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000711#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100712static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000713#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100714static void f_screenattr(typval_T *argvars, typval_T *rettv);
715static void f_screenchar(typval_T *argvars, typval_T *rettv);
716static void f_screencol(typval_T *argvars, typval_T *rettv);
717static void f_screenrow(typval_T *argvars, typval_T *rettv);
718static void f_search(typval_T *argvars, typval_T *rettv);
719static void f_searchdecl(typval_T *argvars, typval_T *rettv);
720static void f_searchpair(typval_T *argvars, typval_T *rettv);
721static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
722static void f_searchpos(typval_T *argvars, typval_T *rettv);
723static void f_server2client(typval_T *argvars, typval_T *rettv);
724static void f_serverlist(typval_T *argvars, typval_T *rettv);
725static void f_setbufvar(typval_T *argvars, typval_T *rettv);
726static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
727static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
728static void f_setline(typval_T *argvars, typval_T *rettv);
729static void f_setloclist(typval_T *argvars, typval_T *rettv);
730static void f_setmatches(typval_T *argvars, typval_T *rettv);
731static void f_setpos(typval_T *argvars, typval_T *rettv);
732static void f_setqflist(typval_T *argvars, typval_T *rettv);
733static void f_setreg(typval_T *argvars, typval_T *rettv);
734static void f_settabvar(typval_T *argvars, typval_T *rettv);
735static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
736static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100737#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100738static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100739#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100740static void f_shellescape(typval_T *argvars, typval_T *rettv);
741static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
742static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000743#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100744static void f_sin(typval_T *argvars, typval_T *rettv);
745static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000746#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100747static void f_sort(typval_T *argvars, typval_T *rettv);
748static void f_soundfold(typval_T *argvars, typval_T *rettv);
749static void f_spellbadword(typval_T *argvars, typval_T *rettv);
750static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
751static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000752#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100753static void f_sqrt(typval_T *argvars, typval_T *rettv);
754static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000755#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100756static void f_str2nr(typval_T *argvars, typval_T *rettv);
757static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000758#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100759static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000760#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100761static void f_stridx(typval_T *argvars, typval_T *rettv);
762static void f_string(typval_T *argvars, typval_T *rettv);
763static void f_strlen(typval_T *argvars, typval_T *rettv);
764static void f_strpart(typval_T *argvars, typval_T *rettv);
765static void f_strridx(typval_T *argvars, typval_T *rettv);
766static void f_strtrans(typval_T *argvars, typval_T *rettv);
767static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
768static void f_strwidth(typval_T *argvars, typval_T *rettv);
769static void f_submatch(typval_T *argvars, typval_T *rettv);
770static void f_substitute(typval_T *argvars, typval_T *rettv);
771static void f_synID(typval_T *argvars, typval_T *rettv);
772static void f_synIDattr(typval_T *argvars, typval_T *rettv);
773static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
774static void f_synstack(typval_T *argvars, typval_T *rettv);
775static void f_synconcealed(typval_T *argvars, typval_T *rettv);
776static void f_system(typval_T *argvars, typval_T *rettv);
777static void f_systemlist(typval_T *argvars, typval_T *rettv);
778static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
779static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
780static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
781static void f_taglist(typval_T *argvars, typval_T *rettv);
782static void f_tagfiles(typval_T *argvars, typval_T *rettv);
783static void f_tempname(typval_T *argvars, typval_T *rettv);
784static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200785#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100786static void f_tan(typval_T *argvars, typval_T *rettv);
787static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200788#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100789static void f_tolower(typval_T *argvars, typval_T *rettv);
790static void f_toupper(typval_T *argvars, typval_T *rettv);
791static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000792#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100793static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000794#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100795static void f_type(typval_T *argvars, typval_T *rettv);
796static void f_undofile(typval_T *argvars, typval_T *rettv);
797static void f_undotree(typval_T *argvars, typval_T *rettv);
798static void f_uniq(typval_T *argvars, typval_T *rettv);
799static void f_values(typval_T *argvars, typval_T *rettv);
800static void f_virtcol(typval_T *argvars, typval_T *rettv);
801static void f_visualmode(typval_T *argvars, typval_T *rettv);
802static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
803static void f_winbufnr(typval_T *argvars, typval_T *rettv);
804static void f_wincol(typval_T *argvars, typval_T *rettv);
805static void f_winheight(typval_T *argvars, typval_T *rettv);
806static void f_winline(typval_T *argvars, typval_T *rettv);
807static void f_winnr(typval_T *argvars, typval_T *rettv);
808static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
809static void f_winrestview(typval_T *argvars, typval_T *rettv);
810static void f_winsaveview(typval_T *argvars, typval_T *rettv);
811static void f_winwidth(typval_T *argvars, typval_T *rettv);
812static void f_writefile(typval_T *argvars, typval_T *rettv);
813static void f_wordcount(typval_T *argvars, typval_T *rettv);
814static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000815
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100816static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
817static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
818static int get_env_len(char_u **arg);
819static int get_id_len(char_u **arg);
820static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
821static 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 +0000822#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
823#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
824 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100825static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
826static int eval_isnamec(int c);
827static int eval_isnamec1(int c);
828static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
829static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100830static typval_T *alloc_string_tv(char_u *string);
831static void init_tv(typval_T *varp);
832static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100833#ifdef FEAT_FLOAT
834static float_T get_tv_float(typval_T *varp);
835#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100836static linenr_T get_tv_lnum(typval_T *argvars);
837static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
838static char_u *get_tv_string(typval_T *varp);
839static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
840static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
841static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
842static hashtab_T *find_var_ht(char_u *name, char_u **varname);
843static funccall_T *get_funccal(void);
844static void vars_clear_ext(hashtab_T *ht, int free_val);
845static void delete_var(hashtab_T *ht, hashitem_T *hi);
846static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
847static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
848static void set_var(char_u *name, typval_T *varp, int copy);
849static int var_check_ro(int flags, char_u *name, int use_gettext);
850static int var_check_fixed(int flags, char_u *name, int use_gettext);
851static int var_check_func_name(char_u *name, int new_var);
852static int valid_varname(char_u *varname);
853static int tv_check_lock(int lock, char_u *name, int use_gettext);
854static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
855static char_u *find_option_end(char_u **arg, int *opt_flags);
856static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
857static int eval_fname_script(char_u *p);
858static int eval_fname_sid(char_u *p);
859static void list_func_head(ufunc_T *fp, int indent);
860static ufunc_T *find_func(char_u *name);
861static int function_exists(char_u *name);
862static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000863#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100864static void func_do_profile(ufunc_T *fp);
865static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
866static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000867static int
868# ifdef __BORLANDC__
869 _RTLENTRYF
870# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100871 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000872static int
873# ifdef __BORLANDC__
874 _RTLENTRYF
875# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100876 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000877#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100878static int script_autoload(char_u *name, int reload);
879static char_u *autoload_name(char_u *name);
880static void cat_func_name(char_u *buf, ufunc_T *fp);
881static void func_free(ufunc_T *fp);
882static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
883static int can_free_funccal(funccall_T *fc, int copyID) ;
884static void free_funccal(funccall_T *fc, int free_val);
885static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
886static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
887static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
888static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
889static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
890static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
891static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
892static int write_list(FILE *fd, list_T *list, int binary);
893static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000894
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200895
896#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100897static int compare_func_name(const void *s1, const void *s2);
898static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200899#endif
900
Bram Moolenaar33570922005-01-25 22:26:29 +0000901/*
902 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000903 */
904 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100905eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000906{
Bram Moolenaar33570922005-01-25 22:26:29 +0000907 int i;
908 struct vimvar *p;
909
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200910 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
911 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200912 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000913 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000914 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000915
916 for (i = 0; i < VV_LEN; ++i)
917 {
918 p = &vimvars[i];
919 STRCPY(p->vv_di.di_key, p->vv_name);
920 if (p->vv_flags & VV_RO)
921 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
922 else if (p->vv_flags & VV_RO_SBX)
923 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
924 else
925 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000926
927 /* add to v: scope dict, unless the value is not always available */
928 if (p->vv_type != VAR_UNKNOWN)
929 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000930 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000931 /* add to compat scope dict */
932 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000933 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100934 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
935
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000936 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100937 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200938 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100939 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100940
941 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
942 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
943 set_vim_var_nr(VV_NONE, VVAL_NONE);
944 set_vim_var_nr(VV_NULL, VVAL_NULL);
945
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200946 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200947
948#ifdef EBCDIC
949 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100950 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200951 */
952 sortFunctions();
953#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000954}
955
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000956#if defined(EXITFREE) || defined(PROTO)
957 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100958eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000959{
960 int i;
961 struct vimvar *p;
962
963 for (i = 0; i < VV_LEN; ++i)
964 {
965 p = &vimvars[i];
966 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000967 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000968 vim_free(p->vv_str);
969 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000970 }
971 else if (p->vv_di.di_tv.v_type == VAR_LIST)
972 {
973 list_unref(p->vv_list);
974 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000975 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000976 }
977 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000978 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000979 hash_clear(&compat_hashtab);
980
Bram Moolenaard9fba312005-06-26 22:34:35 +0000981 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100982# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200983 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100984# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000985
986 /* global variables */
987 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000988
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000989 /* autoloaded script names */
990 ga_clear_strings(&ga_loaded);
991
Bram Moolenaarcca74132013-09-25 21:00:28 +0200992 /* Script-local variables. First clear all the variables and in a second
993 * loop free the scriptvar_T, because a variable in one script might hold
994 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200995 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200996 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200997 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200998 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200999 ga_clear(&ga_scripts);
1000
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001001 /* unreferenced lists and dicts */
1002 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001003
1004 /* functions */
1005 free_all_functions();
1006 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001007}
1008#endif
1009
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001010/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 * Return the name of the executed function.
1012 */
1013 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001014func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001016 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017}
1018
1019/*
1020 * Return the address holding the next breakpoint line for a funccall cookie.
1021 */
1022 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001023func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024{
Bram Moolenaar33570922005-01-25 22:26:29 +00001025 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026}
1027
1028/*
1029 * Return the address holding the debug tick for a funccall cookie.
1030 */
1031 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001032func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033{
Bram Moolenaar33570922005-01-25 22:26:29 +00001034 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035}
1036
1037/*
1038 * Return the nesting level for a funccall cookie.
1039 */
1040 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001041func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042{
Bram Moolenaar33570922005-01-25 22:26:29 +00001043 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044}
1045
1046/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001047funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001049/* pointer to list of previously used funccal, still around because some
1050 * item in it is still being used. */
1051funccall_T *previous_funccal = NULL;
1052
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053/*
1054 * Return TRUE when a function was ended by a ":return" command.
1055 */
1056 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001057current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058{
1059 return current_funccal->returned;
1060}
1061
1062
1063/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 * Set an internal variable to a string value. Creates the variable if it does
1065 * not already exist.
1066 */
1067 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001068set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001070 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001071 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072
1073 val = vim_strsave(value);
1074 if (val != NULL)
1075 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001076 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001077 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001079 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001080 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 }
1082 }
1083}
1084
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001085static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001086static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087static char_u *redir_endp = NULL;
1088static char_u *redir_varname = NULL;
1089
1090/*
1091 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001092 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093 * Returns OK if successfully completed the setup. FAIL otherwise.
1094 */
1095 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001096var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001097{
1098 int save_emsg;
1099 int err;
1100 typval_T tv;
1101
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001102 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001103 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104 {
1105 EMSG(_(e_invarg));
1106 return FAIL;
1107 }
1108
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001109 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 redir_varname = vim_strsave(name);
1111 if (redir_varname == NULL)
1112 return FAIL;
1113
1114 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1115 if (redir_lval == NULL)
1116 {
1117 var_redir_stop();
1118 return FAIL;
1119 }
1120
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001121 /* The output is stored in growarray "redir_ga" until redirection ends. */
1122 ga_init2(&redir_ga, (int)sizeof(char), 500);
1123
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001124 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001125 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001126 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1128 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001129 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130 if (redir_endp != NULL && *redir_endp != NUL)
1131 /* Trailing characters are present after the variable name */
1132 EMSG(_(e_trailing));
1133 else
1134 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001135 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136 var_redir_stop();
1137 return FAIL;
1138 }
1139
1140 /* check if we can write to the variable: set it to or append an empty
1141 * string */
1142 save_emsg = did_emsg;
1143 did_emsg = FALSE;
1144 tv.v_type = VAR_STRING;
1145 tv.vval.v_string = (char_u *)"";
1146 if (append)
1147 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1148 else
1149 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001150 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001151 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001152 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001153 if (err)
1154 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001155 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001156 var_redir_stop();
1157 return FAIL;
1158 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159
1160 return OK;
1161}
1162
1163/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001164 * Append "value[value_len]" to the variable set by var_redir_start().
1165 * The actual appending is postponed until redirection ends, because the value
1166 * appended may in fact be the string we write to, changing it may cause freed
1167 * memory to be used:
1168 * :redir => foo
1169 * :let foo
1170 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001171 */
1172 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001173var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001174{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001175 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001176
1177 if (redir_lval == NULL)
1178 return;
1179
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001180 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001181 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001182 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001183 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001184
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001185 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001186 {
1187 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001188 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001189 }
1190 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001191 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001192}
1193
1194/*
1195 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001196 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001197 */
1198 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001199var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001200{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001201 typval_T tv;
1202
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001203 if (redir_lval != NULL)
1204 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001205 /* If there was no error: assign the text to the variable. */
1206 if (redir_endp != NULL)
1207 {
1208 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1209 tv.v_type = VAR_STRING;
1210 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001211 /* Call get_lval() again, if it's inside a Dict or List it may
1212 * have changed. */
1213 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001214 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001215 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1216 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1217 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001218 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001219
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001220 /* free the collected output */
1221 vim_free(redir_ga.ga_data);
1222 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001223
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001224 vim_free(redir_lval);
1225 redir_lval = NULL;
1226 }
1227 vim_free(redir_varname);
1228 redir_varname = NULL;
1229}
1230
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231# if defined(FEAT_MBYTE) || defined(PROTO)
1232 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001233eval_charconvert(
1234 char_u *enc_from,
1235 char_u *enc_to,
1236 char_u *fname_from,
1237 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238{
1239 int err = FALSE;
1240
1241 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1242 set_vim_var_string(VV_CC_TO, enc_to, -1);
1243 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1244 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1245 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1246 err = TRUE;
1247 set_vim_var_string(VV_CC_FROM, NULL, -1);
1248 set_vim_var_string(VV_CC_TO, NULL, -1);
1249 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1250 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1251
1252 if (err)
1253 return FAIL;
1254 return OK;
1255}
1256# endif
1257
1258# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1259 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001260eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261{
1262 int err = FALSE;
1263
1264 set_vim_var_string(VV_FNAME_IN, fname, -1);
1265 set_vim_var_string(VV_CMDARG, args, -1);
1266 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1267 err = TRUE;
1268 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1269 set_vim_var_string(VV_CMDARG, NULL, -1);
1270
1271 if (err)
1272 {
1273 mch_remove(fname);
1274 return FAIL;
1275 }
1276 return OK;
1277}
1278# endif
1279
1280# if defined(FEAT_DIFF) || defined(PROTO)
1281 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001282eval_diff(
1283 char_u *origfile,
1284 char_u *newfile,
1285 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286{
1287 int err = FALSE;
1288
1289 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1290 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1291 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1292 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1293 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1294 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1295 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1296}
1297
1298 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001299eval_patch(
1300 char_u *origfile,
1301 char_u *difffile,
1302 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303{
1304 int err;
1305
1306 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1307 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1308 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1309 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1310 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1311 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1312 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1313}
1314# endif
1315
1316/*
1317 * Top level evaluation function, returning a boolean.
1318 * Sets "error" to TRUE if there was an error.
1319 * Return TRUE or FALSE.
1320 */
1321 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001322eval_to_bool(
1323 char_u *arg,
1324 int *error,
1325 char_u **nextcmd,
1326 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327{
Bram Moolenaar33570922005-01-25 22:26:29 +00001328 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 int retval = FALSE;
1330
1331 if (skip)
1332 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001333 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 else
1336 {
1337 *error = FALSE;
1338 if (!skip)
1339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001340 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001341 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 }
1343 }
1344 if (skip)
1345 --emsg_skip;
1346
1347 return retval;
1348}
1349
1350/*
1351 * Top level evaluation function, returning a string. If "skip" is TRUE,
1352 * only parsing to "nextcmd" is done, without reporting errors. Return
1353 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1354 */
1355 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001356eval_to_string_skip(
1357 char_u *arg,
1358 char_u **nextcmd,
1359 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360{
Bram Moolenaar33570922005-01-25 22:26:29 +00001361 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 char_u *retval;
1363
1364 if (skip)
1365 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001366 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 retval = NULL;
1368 else
1369 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001370 retval = vim_strsave(get_tv_string(&tv));
1371 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 }
1373 if (skip)
1374 --emsg_skip;
1375
1376 return retval;
1377}
1378
1379/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001380 * Skip over an expression at "*pp".
1381 * Return FAIL for an error, OK otherwise.
1382 */
1383 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001384skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001385{
Bram Moolenaar33570922005-01-25 22:26:29 +00001386 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001387
1388 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001389 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001390}
1391
1392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001394 * When "convert" is TRUE convert a List into a sequence of lines and convert
1395 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 * Return pointer to allocated memory, or NULL for failure.
1397 */
1398 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001399eval_to_string(
1400 char_u *arg,
1401 char_u **nextcmd,
1402 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403{
Bram Moolenaar33570922005-01-25 22:26:29 +00001404 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001406 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001407#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001408 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001411 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 retval = NULL;
1413 else
1414 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001415 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001416 {
1417 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001418 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001419 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001420 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001421 if (tv.vval.v_list->lv_len > 0)
1422 ga_append(&ga, NL);
1423 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001424 ga_append(&ga, NUL);
1425 retval = (char_u *)ga.ga_data;
1426 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001427#ifdef FEAT_FLOAT
1428 else if (convert && tv.v_type == VAR_FLOAT)
1429 {
1430 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1431 retval = vim_strsave(numbuf);
1432 }
1433#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001434 else
1435 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001436 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 }
1438
1439 return retval;
1440}
1441
1442/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001443 * Call eval_to_string() without using current local variables and using
1444 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 */
1446 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001447eval_to_string_safe(
1448 char_u *arg,
1449 char_u **nextcmd,
1450 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451{
1452 char_u *retval;
1453 void *save_funccalp;
1454
1455 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001456 if (use_sandbox)
1457 ++sandbox;
1458 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001459 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001460 if (use_sandbox)
1461 --sandbox;
1462 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 restore_funccal(save_funccalp);
1464 return retval;
1465}
1466
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467/*
1468 * Top level evaluation function, returning a number.
1469 * Evaluates "expr" silently.
1470 * Returns -1 for an error.
1471 */
1472 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001473eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474{
Bram Moolenaar33570922005-01-25 22:26:29 +00001475 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001477 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478
1479 ++emsg_off;
1480
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001481 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 retval = -1;
1483 else
1484 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001485 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001486 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 }
1488 --emsg_off;
1489
1490 return retval;
1491}
1492
Bram Moolenaara40058a2005-07-11 22:42:07 +00001493/*
1494 * Prepare v: variable "idx" to be used.
1495 * Save the current typeval in "save_tv".
1496 * When not used yet add the variable to the v: hashtable.
1497 */
1498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001499prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001500{
1501 *save_tv = vimvars[idx].vv_tv;
1502 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1503 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1504}
1505
1506/*
1507 * Restore v: variable "idx" to typeval "save_tv".
1508 * When no longer defined, remove the variable from the v: hashtable.
1509 */
1510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001511restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001512{
1513 hashitem_T *hi;
1514
Bram Moolenaara40058a2005-07-11 22:42:07 +00001515 vimvars[idx].vv_tv = *save_tv;
1516 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1517 {
1518 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1519 if (HASHITEM_EMPTY(hi))
1520 EMSG2(_(e_intern2), "restore_vimvar()");
1521 else
1522 hash_remove(&vimvarht, hi);
1523 }
1524}
1525
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001526#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001527/*
1528 * Evaluate an expression to a list with suggestions.
1529 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001530 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001531 */
1532 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001533eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001534{
1535 typval_T save_val;
1536 typval_T rettv;
1537 list_T *list = NULL;
1538 char_u *p = skipwhite(expr);
1539
1540 /* Set "v:val" to the bad word. */
1541 prepare_vimvar(VV_VAL, &save_val);
1542 vimvars[VV_VAL].vv_type = VAR_STRING;
1543 vimvars[VV_VAL].vv_str = badword;
1544 if (p_verbose == 0)
1545 ++emsg_off;
1546
1547 if (eval1(&p, &rettv, TRUE) == OK)
1548 {
1549 if (rettv.v_type != VAR_LIST)
1550 clear_tv(&rettv);
1551 else
1552 list = rettv.vval.v_list;
1553 }
1554
1555 if (p_verbose == 0)
1556 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001557 restore_vimvar(VV_VAL, &save_val);
1558
1559 return list;
1560}
1561
1562/*
1563 * "list" is supposed to contain two items: a word and a number. Return the
1564 * word in "pp" and the number as the return value.
1565 * Return -1 if anything isn't right.
1566 * Used to get the good word and score from the eval_spell_expr() result.
1567 */
1568 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001569get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001570{
1571 listitem_T *li;
1572
1573 li = list->lv_first;
1574 if (li == NULL)
1575 return -1;
1576 *pp = get_tv_string(&li->li_tv);
1577
1578 li = li->li_next;
1579 if (li == NULL)
1580 return -1;
1581 return get_tv_number(&li->li_tv);
1582}
1583#endif
1584
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001585/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001586 * Top level evaluation function.
1587 * Returns an allocated typval_T with the result.
1588 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001589 */
1590 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001591eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001592{
1593 typval_T *tv;
1594
1595 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001596 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001597 {
1598 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001599 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001600 }
1601
1602 return tv;
1603}
1604
1605
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001607 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001608 * Uses argv[argc] for the function arguments. Only Number and String
1609 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001610 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001612 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001613call_vim_function(
1614 char_u *func,
1615 int argc,
1616 char_u **argv,
1617 int safe, /* use the sandbox */
1618 int str_arg_only, /* all arguments are strings */
1619 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620{
Bram Moolenaar33570922005-01-25 22:26:29 +00001621 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 long n;
1623 int len;
1624 int i;
1625 int doesrange;
1626 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001627 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001629 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001631 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
1633 for (i = 0; i < argc; i++)
1634 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001635 /* Pass a NULL or empty argument as an empty string */
1636 if (argv[i] == NULL || *argv[i] == NUL)
1637 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001638 argvars[i].v_type = VAR_STRING;
1639 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001640 continue;
1641 }
1642
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001643 if (str_arg_only)
1644 len = 0;
1645 else
1646 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001647 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 if (len != 0 && len == (int)STRLEN(argv[i]))
1649 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001650 argvars[i].v_type = VAR_NUMBER;
1651 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 }
1653 else
1654 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001655 argvars[i].v_type = VAR_STRING;
1656 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 }
1658 }
1659
1660 if (safe)
1661 {
1662 save_funccalp = save_funccal();
1663 ++sandbox;
1664 }
1665
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001666 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1667 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001669 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 if (safe)
1671 {
1672 --sandbox;
1673 restore_funccal(save_funccalp);
1674 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001675 vim_free(argvars);
1676
1677 if (ret == FAIL)
1678 clear_tv(rettv);
1679
1680 return ret;
1681}
1682
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001683/*
1684 * Call vimL function "func" and return the result as a number.
1685 * Returns -1 when calling the function fails.
1686 * Uses argv[argc] for the function arguments.
1687 */
1688 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001689call_func_retnr(
1690 char_u *func,
1691 int argc,
1692 char_u **argv,
1693 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001694{
1695 typval_T rettv;
1696 long retval;
1697
1698 /* All arguments are passed as strings, no conversion to number. */
1699 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1700 return -1;
1701
1702 retval = get_tv_number_chk(&rettv, NULL);
1703 clear_tv(&rettv);
1704 return retval;
1705}
1706
1707#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1708 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1709
Bram Moolenaar4f688582007-07-24 12:34:30 +00001710# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001711/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001712 * Call vimL function "func" and return the result as a string.
1713 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001714 * Uses argv[argc] for the function arguments.
1715 */
1716 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001717call_func_retstr(
1718 char_u *func,
1719 int argc,
1720 char_u **argv,
1721 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001722{
1723 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001724 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001725
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001726 /* All arguments are passed as strings, no conversion to number. */
1727 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001728 return NULL;
1729
1730 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001731 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 return retval;
1733}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001734# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001735
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001736/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001737 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001738 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001739 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001740 */
1741 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001742call_func_retlist(
1743 char_u *func,
1744 int argc,
1745 char_u **argv,
1746 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001747{
1748 typval_T rettv;
1749
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001750 /* All arguments are passed as strings, no conversion to number. */
1751 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001752 return NULL;
1753
1754 if (rettv.v_type != VAR_LIST)
1755 {
1756 clear_tv(&rettv);
1757 return NULL;
1758 }
1759
1760 return rettv.vval.v_list;
1761}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762#endif
1763
1764/*
1765 * Save the current function call pointer, and set it to NULL.
1766 * Used when executing autocommands and for ":source".
1767 */
1768 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001769save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001771 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 current_funccal = NULL;
1774 return (void *)fc;
1775}
1776
1777 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001778restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001780 funccall_T *fc = (funccall_T *)vfc;
1781
1782 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783}
1784
Bram Moolenaar05159a02005-02-26 23:04:13 +00001785#if defined(FEAT_PROFILE) || defined(PROTO)
1786/*
1787 * Prepare profiling for entering a child or something else that is not
1788 * counted for the script/function itself.
1789 * Should always be called in pair with prof_child_exit().
1790 */
1791 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001792prof_child_enter(
1793 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001794{
1795 funccall_T *fc = current_funccal;
1796
1797 if (fc != NULL && fc->func->uf_profiling)
1798 profile_start(&fc->prof_child);
1799 script_prof_save(tm);
1800}
1801
1802/*
1803 * Take care of time spent in a child.
1804 * Should always be called after prof_child_enter().
1805 */
1806 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001807prof_child_exit(
1808 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001809{
1810 funccall_T *fc = current_funccal;
1811
1812 if (fc != NULL && fc->func->uf_profiling)
1813 {
1814 profile_end(&fc->prof_child);
1815 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1816 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1817 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1818 }
1819 script_prof_restore(tm);
1820}
1821#endif
1822
1823
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824#ifdef FEAT_FOLDING
1825/*
1826 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1827 * it in "*cp". Doesn't give error messages.
1828 */
1829 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001830eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831{
Bram Moolenaar33570922005-01-25 22:26:29 +00001832 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 int retval;
1834 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001835 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1836 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837
1838 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001839 if (use_sandbox)
1840 ++sandbox;
1841 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 retval = 0;
1845 else
1846 {
1847 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001848 if (tv.v_type == VAR_NUMBER)
1849 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001850 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 retval = 0;
1852 else
1853 {
1854 /* If the result is a string, check if there is a non-digit before
1855 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 if (!VIM_ISDIGIT(*s) && *s != '-')
1858 *cp = *s++;
1859 retval = atol((char *)s);
1860 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 }
1863 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001864 if (use_sandbox)
1865 --sandbox;
1866 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867
1868 return retval;
1869}
1870#endif
1871
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 * ":let" list all variable values
1874 * ":let var1 var2" list variable values
1875 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001876 * ":let var += expr" assignment command.
1877 * ":let var -= expr" assignment command.
1878 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001879 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 */
1881 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001882ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883{
1884 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001885 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001886 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888 int var_count = 0;
1889 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001890 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001891 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001892 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893
Bram Moolenaardb552d602006-03-23 22:59:57 +00001894 argend = skip_var_list(arg, &var_count, &semicolon);
1895 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001896 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001897 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1898 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001899 expr = skipwhite(argend);
1900 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1901 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001903 /*
1904 * ":let" without "=": list variables
1905 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 if (*arg == '[')
1907 EMSG(_(e_invarg));
1908 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001909 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001910 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001911 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001912 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001913 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001914 list_glob_vars(&first);
1915 list_buf_vars(&first);
1916 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001917#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001918 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001919#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001920 list_script_vars(&first);
1921 list_func_vars(&first);
1922 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 eap->nextcmd = check_nextcmd(arg);
1925 }
1926 else
1927 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001928 op[0] = '=';
1929 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001930 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001931 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001932 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1933 op[0] = *expr; /* +=, -= or .= */
1934 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001935 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001936 else
1937 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001938
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 if (eap->skip)
1940 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 if (eap->skip)
1943 {
1944 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001945 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 --emsg_skip;
1947 }
1948 else if (i != FAIL)
1949 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001951 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001952 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 }
1954 }
1955}
1956
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957/*
1958 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1959 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001960 * When "nextchars" is not NULL it points to a string with characters that
1961 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1962 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001963 * Returns OK or FAIL;
1964 */
1965 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001966ex_let_vars(
1967 char_u *arg_start,
1968 typval_T *tv,
1969 int copy, /* copy values from "tv", don't move */
1970 int semicolon, /* from skip_var_list() */
1971 int var_count, /* from skip_var_list() */
1972 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973{
1974 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001975 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001976 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001977 listitem_T *item;
1978 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979
1980 if (*arg != '[')
1981 {
1982 /*
1983 * ":let var = expr" or ":for var in list"
1984 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001985 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 return FAIL;
1987 return OK;
1988 }
1989
1990 /*
1991 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1992 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001993 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001994 {
1995 EMSG(_(e_listreq));
1996 return FAIL;
1997 }
1998
1999 i = list_len(l);
2000 if (semicolon == 0 && var_count < i)
2001 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002002 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002003 return FAIL;
2004 }
2005 if (var_count - semicolon > i)
2006 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002007 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002008 return FAIL;
2009 }
2010
2011 item = l->lv_first;
2012 while (*arg != ']')
2013 {
2014 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002015 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002016 item = item->li_next;
2017 if (arg == NULL)
2018 return FAIL;
2019
2020 arg = skipwhite(arg);
2021 if (*arg == ';')
2022 {
2023 /* Put the rest of the list (may be empty) in the var after ';'.
2024 * Create a new list for this. */
2025 l = list_alloc();
2026 if (l == NULL)
2027 return FAIL;
2028 while (item != NULL)
2029 {
2030 list_append_tv(l, &item->li_tv);
2031 item = item->li_next;
2032 }
2033
2034 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002035 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002036 ltv.vval.v_list = l;
2037 l->lv_refcount = 1;
2038
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002039 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2040 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002041 clear_tv(&ltv);
2042 if (arg == NULL)
2043 return FAIL;
2044 break;
2045 }
2046 else if (*arg != ',' && *arg != ']')
2047 {
2048 EMSG2(_(e_intern2), "ex_let_vars()");
2049 return FAIL;
2050 }
2051 }
2052
2053 return OK;
2054}
2055
2056/*
2057 * Skip over assignable variable "var" or list of variables "[var, var]".
2058 * Used for ":let varvar = expr" and ":for varvar in expr".
2059 * For "[var, var]" increment "*var_count" for each variable.
2060 * for "[var, var; var]" set "semicolon".
2061 * Return NULL for an error.
2062 */
2063 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002064skip_var_list(
2065 char_u *arg,
2066 int *var_count,
2067 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002068{
2069 char_u *p, *s;
2070
2071 if (*arg == '[')
2072 {
2073 /* "[var, var]": find the matching ']'. */
2074 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002075 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002076 {
2077 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2078 s = skip_var_one(p);
2079 if (s == p)
2080 {
2081 EMSG2(_(e_invarg2), p);
2082 return NULL;
2083 }
2084 ++*var_count;
2085
2086 p = skipwhite(s);
2087 if (*p == ']')
2088 break;
2089 else if (*p == ';')
2090 {
2091 if (*semicolon == 1)
2092 {
2093 EMSG(_("Double ; in list of variables"));
2094 return NULL;
2095 }
2096 *semicolon = 1;
2097 }
2098 else if (*p != ',')
2099 {
2100 EMSG2(_(e_invarg2), p);
2101 return NULL;
2102 }
2103 }
2104 return p + 1;
2105 }
2106 else
2107 return skip_var_one(arg);
2108}
2109
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002110/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002111 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002112 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002113 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002114 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002115skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002116{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002117 if (*arg == '@' && arg[1] != NUL)
2118 return arg + 2;
2119 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2120 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002121}
2122
Bram Moolenaara7043832005-01-21 11:56:39 +00002123/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002124 * List variables for hashtab "ht" with prefix "prefix".
2125 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002126 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002128list_hashtable_vars(
2129 hashtab_T *ht,
2130 char_u *prefix,
2131 int empty,
2132 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002133{
Bram Moolenaar33570922005-01-25 22:26:29 +00002134 hashitem_T *hi;
2135 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002136 int todo;
2137
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002138 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002139 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2140 {
2141 if (!HASHITEM_EMPTY(hi))
2142 {
2143 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002144 di = HI2DI(hi);
2145 if (empty || di->di_tv.v_type != VAR_STRING
2146 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002147 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002148 }
2149 }
2150}
2151
2152/*
2153 * List global variables.
2154 */
2155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002156list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002157{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002159}
2160
2161/*
2162 * List buffer variables.
2163 */
2164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002165list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002166{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002167 char_u numbuf[NUMBUFLEN];
2168
Bram Moolenaar429fa852013-04-15 12:27:36 +02002169 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002171
2172 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2174 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002175}
2176
2177/*
2178 * List window variables.
2179 */
2180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002181list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002182{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002183 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002185}
2186
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002187#ifdef FEAT_WINDOWS
2188/*
2189 * List tab page variables.
2190 */
2191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002192list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002193{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002194 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002195 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002196}
2197#endif
2198
Bram Moolenaara7043832005-01-21 11:56:39 +00002199/*
2200 * List Vim variables.
2201 */
2202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002203list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002205 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206}
2207
2208/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002209 * List script-local variables, if there is a script.
2210 */
2211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002212list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002213{
2214 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002215 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2216 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002217}
2218
2219/*
2220 * List function variables, if there is a function.
2221 */
2222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002223list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002224{
2225 if (current_funccal != NULL)
2226 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002227 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002228}
2229
2230/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 * List variables in "arg".
2232 */
2233 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002234list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235{
2236 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002237 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 char_u *name_start;
2240 char_u *arg_subsc;
2241 char_u *tofree;
2242 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243
2244 while (!ends_excmd(*arg) && !got_int)
2245 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002248 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2250 {
2251 emsg_severe = TRUE;
2252 EMSG(_(e_trailing));
2253 break;
2254 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002258 /* get_name_len() takes care of expanding curly braces */
2259 name_start = name = arg;
2260 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2261 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 /* This is mainly to keep test 49 working: when expanding
2264 * curly braces fails overrule the exception error message. */
2265 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 emsg_severe = TRUE;
2268 EMSG2(_(e_invarg2), arg);
2269 break;
2270 }
2271 error = TRUE;
2272 }
2273 else
2274 {
2275 if (tofree != NULL)
2276 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002277 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002278 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 else
2280 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002281 /* handle d.key, l[idx], f(expr) */
2282 arg_subsc = arg;
2283 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002284 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002286 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002287 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002288 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002289 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002290 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002291 case 'g': list_glob_vars(first); break;
2292 case 'b': list_buf_vars(first); break;
2293 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002294#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002295 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002296#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002297 case 'v': list_vim_vars(first); break;
2298 case 's': list_script_vars(first); break;
2299 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002300 default:
2301 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002302 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002303 }
2304 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002305 {
2306 char_u numbuf[NUMBUFLEN];
2307 char_u *tf;
2308 int c;
2309 char_u *s;
2310
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002311 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312 c = *arg;
2313 *arg = NUL;
2314 list_one_var_a((char_u *)"",
2315 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002316 tv.v_type,
2317 s == NULL ? (char_u *)"" : s,
2318 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002319 *arg = c;
2320 vim_free(tf);
2321 }
2322 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002323 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 }
2325 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002326
2327 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002329
2330 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 }
2332
2333 return arg;
2334}
2335
2336/*
2337 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2338 * Returns a pointer to the char just after the var name.
2339 * Returns NULL if there is an error.
2340 */
2341 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002342ex_let_one(
2343 char_u *arg, /* points to variable name */
2344 typval_T *tv, /* value to assign to variable */
2345 int copy, /* copy value from "tv" */
2346 char_u *endchars, /* valid chars after variable name or NULL */
2347 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002348{
2349 int c1;
2350 char_u *name;
2351 char_u *p;
2352 char_u *arg_end = NULL;
2353 int len;
2354 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002355 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002356
2357 /*
2358 * ":let $VAR = expr": Set environment variable.
2359 */
2360 if (*arg == '$')
2361 {
2362 /* Find the end of the name. */
2363 ++arg;
2364 name = arg;
2365 len = get_env_len(&arg);
2366 if (len == 0)
2367 EMSG2(_(e_invarg2), name - 1);
2368 else
2369 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002370 if (op != NULL && (*op == '+' || *op == '-'))
2371 EMSG2(_(e_letwrong), op);
2372 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002373 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002374 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002375 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376 {
2377 c1 = name[len];
2378 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002379 p = get_tv_string_chk(tv);
2380 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002381 {
2382 int mustfree = FALSE;
2383 char_u *s = vim_getenv(name, &mustfree);
2384
2385 if (s != NULL)
2386 {
2387 p = tofree = concat_str(s, p);
2388 if (mustfree)
2389 vim_free(s);
2390 }
2391 }
2392 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002393 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002394 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002395 if (STRICMP(name, "HOME") == 0)
2396 init_homedir();
2397 else if (didset_vim && STRICMP(name, "VIM") == 0)
2398 didset_vim = FALSE;
2399 else if (didset_vimruntime
2400 && STRICMP(name, "VIMRUNTIME") == 0)
2401 didset_vimruntime = FALSE;
2402 arg_end = arg;
2403 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002405 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002406 }
2407 }
2408 }
2409
2410 /*
2411 * ":let &option = expr": Set option value.
2412 * ":let &l:option = expr": Set local option value.
2413 * ":let &g:option = expr": Set global option value.
2414 */
2415 else if (*arg == '&')
2416 {
2417 /* Find the end of the name. */
2418 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002419 if (p == NULL || (endchars != NULL
2420 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002421 EMSG(_(e_letunexp));
2422 else
2423 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002424 long n;
2425 int opt_type;
2426 long numval;
2427 char_u *stringval = NULL;
2428 char_u *s;
2429
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002430 c1 = *p;
2431 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002432
2433 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002434 s = get_tv_string_chk(tv); /* != NULL if number or string */
2435 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002436 {
2437 opt_type = get_option_value(arg, &numval,
2438 &stringval, opt_flags);
2439 if ((opt_type == 1 && *op == '.')
2440 || (opt_type == 0 && *op != '.'))
2441 EMSG2(_(e_letwrong), op);
2442 else
2443 {
2444 if (opt_type == 1) /* number */
2445 {
2446 if (*op == '+')
2447 n = numval + n;
2448 else
2449 n = numval - n;
2450 }
2451 else if (opt_type == 0 && stringval != NULL) /* string */
2452 {
2453 s = concat_str(stringval, s);
2454 vim_free(stringval);
2455 stringval = s;
2456 }
2457 }
2458 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002459 if (s != NULL)
2460 {
2461 set_option_value(arg, n, s, opt_flags);
2462 arg_end = p;
2463 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002465 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 }
2467 }
2468
2469 /*
2470 * ":let @r = expr": Set register contents.
2471 */
2472 else if (*arg == '@')
2473 {
2474 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002475 if (op != NULL && (*op == '+' || *op == '-'))
2476 EMSG2(_(e_letwrong), op);
2477 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002478 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002479 EMSG(_(e_letunexp));
2480 else
2481 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002482 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002483 char_u *s;
2484
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002485 p = get_tv_string_chk(tv);
2486 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002487 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002488 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002489 if (s != NULL)
2490 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002491 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002492 vim_free(s);
2493 }
2494 }
2495 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002496 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002497 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002498 arg_end = arg + 1;
2499 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002500 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002501 }
2502 }
2503
2504 /*
2505 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002508 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002509 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002510 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002512 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2516 EMSG(_(e_letunexp));
2517 else
2518 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002519 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 arg_end = p;
2521 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 }
2525
2526 else
2527 EMSG2(_(e_invarg2), arg);
2528
2529 return arg_end;
2530}
2531
2532/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2534 */
2535 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002536check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002537{
2538 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2539 {
2540 EMSG2(_(e_readonlyvar), arg);
2541 return TRUE;
2542 }
2543 return FALSE;
2544}
2545
2546/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 * Get an lval: variable, Dict item or List item that can be assigned a value
2548 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2549 * "name.key", "name.key[expr]" etc.
2550 * Indexing only works if "name" is an existing List or Dictionary.
2551 * "name" points to the start of the name.
2552 * If "rettv" is not NULL it points to the value to be assigned.
2553 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2554 * wrong; must end in space or cmd separator.
2555 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002556 * flags:
2557 * GLV_QUIET: do not give error messages
2558 * GLV_NO_AUTOLOAD: do not use script autoloading
2559 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002561 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002563 */
2564 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002565get_lval(
2566 char_u *name,
2567 typval_T *rettv,
2568 lval_T *lp,
2569 int unlet,
2570 int skip,
2571 int flags, /* GLV_ values */
2572 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002573{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 char_u *expr_start, *expr_end;
2576 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002577 dictitem_T *v;
2578 typval_T var1;
2579 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002581 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002582 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002583 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002584 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002585 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002586
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002588 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589
2590 if (skip)
2591 {
2592 /* When skipping just find the end of the name. */
2593 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002594 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 }
2596
2597 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002598 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 if (expr_start != NULL)
2600 {
2601 /* Don't expand the name when we already know there is an error. */
2602 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2603 && *p != '[' && *p != '.')
2604 {
2605 EMSG(_(e_trailing));
2606 return NULL;
2607 }
2608
2609 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2610 if (lp->ll_exp_name == NULL)
2611 {
2612 /* Report an invalid expression in braces, unless the
2613 * expression evaluation has been cancelled due to an
2614 * aborting error, an interrupt, or an exception. */
2615 if (!aborting() && !quiet)
2616 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002617 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 EMSG2(_(e_invarg2), name);
2619 return NULL;
2620 }
2621 }
2622 lp->ll_name = lp->ll_exp_name;
2623 }
2624 else
2625 lp->ll_name = name;
2626
2627 /* Without [idx] or .key we are done. */
2628 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2629 return p;
2630
2631 cc = *p;
2632 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002633 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (v == NULL && !quiet)
2635 EMSG2(_(e_undefvar), lp->ll_name);
2636 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002637 if (v == NULL)
2638 return NULL;
2639
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 /*
2641 * Loop until no more [idx] or .key is following.
2642 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002643 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002645 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2647 && !(lp->ll_tv->v_type == VAR_DICT
2648 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (!quiet)
2651 EMSG(_("E689: Can only index a List or Dictionary"));
2652 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002653 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002655 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (!quiet)
2657 EMSG(_("E708: [:] must come last"));
2658 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002659 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002660
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 len = -1;
2662 if (*p == '.')
2663 {
2664 key = p + 1;
2665 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2666 ;
2667 if (len == 0)
2668 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 if (!quiet)
2670 EMSG(_(e_emptykey));
2671 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 }
2673 p = key + len;
2674 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002675 else
2676 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002678 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 if (*p == ':')
2680 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002681 else
2682 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002683 empty1 = FALSE;
2684 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002686 if (get_tv_string_chk(&var1) == NULL)
2687 {
2688 /* not a number or string */
2689 clear_tv(&var1);
2690 return NULL;
2691 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 }
2693
2694 /* Optionally get the second index [ :expr]. */
2695 if (*p == ':')
2696 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002700 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002701 if (!empty1)
2702 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002704 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (rettv != NULL && (rettv->v_type != VAR_LIST
2706 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (!quiet)
2709 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 if (!empty1)
2711 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 }
2714 p = skipwhite(p + 1);
2715 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 else
2718 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2721 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 if (!empty1)
2723 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002726 if (get_tv_string_chk(&var2) == NULL)
2727 {
2728 /* not a number or string */
2729 if (!empty1)
2730 clear_tv(&var1);
2731 clear_tv(&var2);
2732 return NULL;
2733 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002736 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002739
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002741 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 if (!quiet)
2743 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 if (!empty1)
2745 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 }
2750
2751 /* Skip to past ']'. */
2752 ++p;
2753 }
2754
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 {
2757 if (len == -1)
2758 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002760 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 if (*key == NUL)
2762 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 if (!quiet)
2764 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 }
2768 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002769 lp->ll_list = NULL;
2770 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002771 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002772
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002773 /* When assigning to a scope dictionary check that a function and
2774 * variable name is valid (only variable name unless it is l: or
2775 * g: dictionary). Disallow overwriting a builtin function. */
2776 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002777 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002778 int prevval;
2779 int wrong;
2780
2781 if (len != -1)
2782 {
2783 prevval = key[len];
2784 key[len] = NUL;
2785 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002786 else
2787 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002788 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2789 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002790 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002791 || !valid_varname(key);
2792 if (len != -1)
2793 key[len] = prevval;
2794 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002795 return NULL;
2796 }
2797
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002800 /* Can't add "v:" variable. */
2801 if (lp->ll_dict == &vimvardict)
2802 {
2803 EMSG2(_(e_illvar), name);
2804 return NULL;
2805 }
2806
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002807 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002811 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 if (len == -1)
2813 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 }
2816 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 if (len == -1)
2821 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 p = NULL;
2824 break;
2825 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002826 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002827 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002828 return NULL;
2829
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 if (len == -1)
2831 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 }
2834 else
2835 {
2836 /*
2837 * Get the number and item for the only or first index of the List.
2838 */
2839 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 else
2842 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002843 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 clear_tv(&var1);
2845 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002846 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 lp->ll_list = lp->ll_tv->vval.v_list;
2848 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2849 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002850 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002851 if (lp->ll_n1 < 0)
2852 {
2853 lp->ll_n1 = 0;
2854 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2855 }
2856 }
2857 if (lp->ll_li == NULL)
2858 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002861 if (!quiet)
2862 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 }
2865
2866 /*
2867 * May need to find the item or absolute index for the second
2868 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 * When no index given: "lp->ll_empty2" is TRUE.
2870 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002874 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002875 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002877 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002879 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002880 {
2881 if (!quiet)
2882 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002884 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002886 }
2887
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2889 if (lp->ll_n1 < 0)
2890 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2891 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002892 {
2893 if (!quiet)
2894 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002896 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002897 }
2898
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002900 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002901 }
2902
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 return p;
2904}
2905
2906/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002907 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 */
2909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002910clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911{
2912 vim_free(lp->ll_exp_name);
2913 vim_free(lp->ll_newkey);
2914}
2915
2916/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002917 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002919 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 */
2921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002922set_var_lval(
2923 lval_T *lp,
2924 char_u *endp,
2925 typval_T *rettv,
2926 int copy,
2927 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928{
2929 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002930 listitem_T *ri;
2931 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932
2933 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002934 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002935 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002936 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 cc = *endp;
2938 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002939 if (op != NULL && *op != '=')
2940 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002941 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002942
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002943 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002944 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002945 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002946 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002947 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002948 if ((di == NULL
2949 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2950 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2951 FALSE)))
2952 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002953 set_var(lp->ll_name, &tv, FALSE);
2954 clear_tv(&tv);
2955 }
2956 }
2957 else
2958 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002959 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002960 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002962 else if (tv_check_lock(lp->ll_newkey == NULL
2963 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002964 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002965 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 else if (lp->ll_range)
2967 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002968 listitem_T *ll_li = lp->ll_li;
2969 int ll_n1 = lp->ll_n1;
2970
2971 /*
2972 * Check whether any of the list items is locked
2973 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002974 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002975 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002976 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002977 return;
2978 ri = ri->li_next;
2979 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2980 break;
2981 ll_li = ll_li->li_next;
2982 ++ll_n1;
2983 }
2984
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002985 /*
2986 * Assign the List values to the list items.
2987 */
2988 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002989 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002990 if (op != NULL && *op != '=')
2991 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2992 else
2993 {
2994 clear_tv(&lp->ll_li->li_tv);
2995 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2996 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002997 ri = ri->li_next;
2998 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2999 break;
3000 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003001 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003003 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003004 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003005 ri = NULL;
3006 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003007 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003008 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003009 lp->ll_li = lp->ll_li->li_next;
3010 ++lp->ll_n1;
3011 }
3012 if (ri != NULL)
3013 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003014 else if (lp->ll_empty2
3015 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003016 : lp->ll_n1 != lp->ll_n2)
3017 EMSG(_("E711: List value has not enough items"));
3018 }
3019 else
3020 {
3021 /*
3022 * Assign to a List or Dictionary item.
3023 */
3024 if (lp->ll_newkey != NULL)
3025 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003026 if (op != NULL && *op != '=')
3027 {
3028 EMSG2(_(e_letwrong), op);
3029 return;
3030 }
3031
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003032 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003033 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003034 if (di == NULL)
3035 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003036 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3037 {
3038 vim_free(di);
3039 return;
3040 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003041 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003042 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003043 else if (op != NULL && *op != '=')
3044 {
3045 tv_op(lp->ll_tv, rettv, op);
3046 return;
3047 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003048 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003049 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003050
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003051 /*
3052 * Assign the value to the variable or list item.
3053 */
3054 if (copy)
3055 copy_tv(rettv, lp->ll_tv);
3056 else
3057 {
3058 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003059 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003060 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003061 }
3062 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003063}
3064
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003065/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003066 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3067 * Returns OK or FAIL.
3068 */
3069 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003070tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003071{
3072 long n;
3073 char_u numbuf[NUMBUFLEN];
3074 char_u *s;
3075
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003076 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3077 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3078 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003079 {
3080 switch (tv1->v_type)
3081 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003082 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003083 case VAR_DICT:
3084 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003085 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003086 case VAR_JOB:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003087 break;
3088
3089 case VAR_LIST:
3090 if (*op != '+' || tv2->v_type != VAR_LIST)
3091 break;
3092 /* List += List */
3093 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3094 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3095 return OK;
3096
3097 case VAR_NUMBER:
3098 case VAR_STRING:
3099 if (tv2->v_type == VAR_LIST)
3100 break;
3101 if (*op == '+' || *op == '-')
3102 {
3103 /* nr += nr or nr -= nr*/
3104 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003105#ifdef FEAT_FLOAT
3106 if (tv2->v_type == VAR_FLOAT)
3107 {
3108 float_T f = n;
3109
3110 if (*op == '+')
3111 f += tv2->vval.v_float;
3112 else
3113 f -= tv2->vval.v_float;
3114 clear_tv(tv1);
3115 tv1->v_type = VAR_FLOAT;
3116 tv1->vval.v_float = f;
3117 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003118 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003119#endif
3120 {
3121 if (*op == '+')
3122 n += get_tv_number(tv2);
3123 else
3124 n -= get_tv_number(tv2);
3125 clear_tv(tv1);
3126 tv1->v_type = VAR_NUMBER;
3127 tv1->vval.v_number = n;
3128 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003129 }
3130 else
3131 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003132 if (tv2->v_type == VAR_FLOAT)
3133 break;
3134
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003135 /* str .= str */
3136 s = get_tv_string(tv1);
3137 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3138 clear_tv(tv1);
3139 tv1->v_type = VAR_STRING;
3140 tv1->vval.v_string = s;
3141 }
3142 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003143
3144#ifdef FEAT_FLOAT
3145 case VAR_FLOAT:
3146 {
3147 float_T f;
3148
3149 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3150 && tv2->v_type != VAR_NUMBER
3151 && tv2->v_type != VAR_STRING))
3152 break;
3153 if (tv2->v_type == VAR_FLOAT)
3154 f = tv2->vval.v_float;
3155 else
3156 f = get_tv_number(tv2);
3157 if (*op == '+')
3158 tv1->vval.v_float += f;
3159 else
3160 tv1->vval.v_float -= f;
3161 }
3162 return OK;
3163#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003164 }
3165 }
3166
3167 EMSG2(_(e_letwrong), op);
3168 return FAIL;
3169}
3170
3171/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003172 * Add a watcher to a list.
3173 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003174 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003175list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003176{
3177 lw->lw_next = l->lv_watch;
3178 l->lv_watch = lw;
3179}
3180
3181/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003182 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003183 * No warning when it isn't found...
3184 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003185 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003186list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003187{
Bram Moolenaar33570922005-01-25 22:26:29 +00003188 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189
3190 lwp = &l->lv_watch;
3191 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3192 {
3193 if (lw == lwrem)
3194 {
3195 *lwp = lw->lw_next;
3196 break;
3197 }
3198 lwp = &lw->lw_next;
3199 }
3200}
3201
3202/*
3203 * Just before removing an item from a list: advance watchers to the next
3204 * item.
3205 */
3206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003207list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003208{
Bram Moolenaar33570922005-01-25 22:26:29 +00003209 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003210
3211 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3212 if (lw->lw_item == item)
3213 lw->lw_item = item->li_next;
3214}
3215
3216/*
3217 * Evaluate the expression used in a ":for var in expr" command.
3218 * "arg" points to "var".
3219 * Set "*errp" to TRUE for an error, FALSE otherwise;
3220 * Return a pointer that holds the info. Null when there is an error.
3221 */
3222 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003223eval_for_line(
3224 char_u *arg,
3225 int *errp,
3226 char_u **nextcmdp,
3227 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003228{
Bram Moolenaar33570922005-01-25 22:26:29 +00003229 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003230 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003231 typval_T tv;
3232 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233
3234 *errp = TRUE; /* default: there is an error */
3235
Bram Moolenaar33570922005-01-25 22:26:29 +00003236 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003237 if (fi == NULL)
3238 return NULL;
3239
3240 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3241 if (expr == NULL)
3242 return fi;
3243
3244 expr = skipwhite(expr);
3245 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3246 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003247 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003248 return fi;
3249 }
3250
3251 if (skip)
3252 ++emsg_skip;
3253 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3254 {
3255 *errp = FALSE;
3256 if (!skip)
3257 {
3258 l = tv.vval.v_list;
3259 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003260 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003261 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003262 clear_tv(&tv);
3263 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003264 else
3265 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003266 /* No need to increment the refcount, it's already set for the
3267 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003268 fi->fi_list = l;
3269 list_add_watch(l, &fi->fi_lw);
3270 fi->fi_lw.lw_item = l->lv_first;
3271 }
3272 }
3273 }
3274 if (skip)
3275 --emsg_skip;
3276
3277 return fi;
3278}
3279
3280/*
3281 * Use the first item in a ":for" list. Advance to the next.
3282 * Assign the values to the variable (list). "arg" points to the first one.
3283 * Return TRUE when a valid item was found, FALSE when at end of list or
3284 * something wrong.
3285 */
3286 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003287next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003288{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003289 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003290 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003291 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003292
3293 item = fi->fi_lw.lw_item;
3294 if (item == NULL)
3295 result = FALSE;
3296 else
3297 {
3298 fi->fi_lw.lw_item = item->li_next;
3299 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3300 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3301 }
3302 return result;
3303}
3304
3305/*
3306 * Free the structure used to store info used by ":for".
3307 */
3308 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003309free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003310{
Bram Moolenaar33570922005-01-25 22:26:29 +00003311 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003312
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003313 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003314 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003315 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003316 list_unref(fi->fi_list);
3317 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003318 vim_free(fi);
3319}
3320
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3322
3323 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003324set_context_for_expression(
3325 expand_T *xp,
3326 char_u *arg,
3327 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
3329 int got_eq = FALSE;
3330 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003331 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003333 if (cmdidx == CMD_let)
3334 {
3335 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003336 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003337 {
3338 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003339 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003340 {
3341 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003342 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003343 if (vim_iswhite(*p))
3344 break;
3345 }
3346 return;
3347 }
3348 }
3349 else
3350 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3351 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 while ((xp->xp_pattern = vim_strpbrk(arg,
3353 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3354 {
3355 c = *xp->xp_pattern;
3356 if (c == '&')
3357 {
3358 c = xp->xp_pattern[1];
3359 if (c == '&')
3360 {
3361 ++xp->xp_pattern;
3362 xp->xp_context = cmdidx != CMD_let || got_eq
3363 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3364 }
3365 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003366 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003368 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3369 xp->xp_pattern += 2;
3370
3371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 }
3373 else if (c == '$')
3374 {
3375 /* environment variable */
3376 xp->xp_context = EXPAND_ENV_VARS;
3377 }
3378 else if (c == '=')
3379 {
3380 got_eq = TRUE;
3381 xp->xp_context = EXPAND_EXPRESSION;
3382 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003383 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 && xp->xp_context == EXPAND_FUNCTIONS
3385 && vim_strchr(xp->xp_pattern, '(') == NULL)
3386 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003387 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 break;
3389 }
3390 else if (cmdidx != CMD_let || got_eq)
3391 {
3392 if (c == '"') /* string */
3393 {
3394 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3395 if (c == '\\' && xp->xp_pattern[1] != NUL)
3396 ++xp->xp_pattern;
3397 xp->xp_context = EXPAND_NOTHING;
3398 }
3399 else if (c == '\'') /* literal string */
3400 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003401 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3403 /* skip */ ;
3404 xp->xp_context = EXPAND_NOTHING;
3405 }
3406 else if (c == '|')
3407 {
3408 if (xp->xp_pattern[1] == '|')
3409 {
3410 ++xp->xp_pattern;
3411 xp->xp_context = EXPAND_EXPRESSION;
3412 }
3413 else
3414 xp->xp_context = EXPAND_COMMANDS;
3415 }
3416 else
3417 xp->xp_context = EXPAND_EXPRESSION;
3418 }
3419 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003420 /* Doesn't look like something valid, expand as an expression
3421 * anyway. */
3422 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 arg = xp->xp_pattern;
3424 if (*arg != NUL)
3425 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3426 /* skip */ ;
3427 }
3428 xp->xp_pattern = arg;
3429}
3430
3431#endif /* FEAT_CMDL_COMPL */
3432
3433/*
3434 * ":1,25call func(arg1, arg2)" function call.
3435 */
3436 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003437ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438{
3439 char_u *arg = eap->arg;
3440 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003442 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003444 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 linenr_T lnum;
3446 int doesrange;
3447 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003448 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003450 if (eap->skip)
3451 {
3452 /* trans_function_name() doesn't work well when skipping, use eval0()
3453 * instead to skip to any following command, e.g. for:
3454 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003455 ++emsg_skip;
3456 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3457 clear_tv(&rettv);
3458 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003459 return;
3460 }
3461
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003462 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003463 if (fudi.fd_newkey != NULL)
3464 {
3465 /* Still need to give an error message for missing key. */
3466 EMSG2(_(e_dictkey), fudi.fd_newkey);
3467 vim_free(fudi.fd_newkey);
3468 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003469 if (tofree == NULL)
3470 return;
3471
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003472 /* Increase refcount on dictionary, it could get deleted when evaluating
3473 * the arguments. */
3474 if (fudi.fd_dict != NULL)
3475 ++fudi.fd_dict->dv_refcount;
3476
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003477 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003478 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003479 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480
Bram Moolenaar532c7802005-01-27 14:44:31 +00003481 /* Skip white space to allow ":call func ()". Not good, but required for
3482 * backward compatibility. */
3483 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003484 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485
3486 if (*startarg != '(')
3487 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003488 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 goto end;
3490 }
3491
3492 /*
3493 * When skipping, evaluate the function once, to find the end of the
3494 * arguments.
3495 * When the function takes a range, this is discovered after the first
3496 * call, and the loop is broken.
3497 */
3498 if (eap->skip)
3499 {
3500 ++emsg_skip;
3501 lnum = eap->line2; /* do it once, also with an invalid range */
3502 }
3503 else
3504 lnum = eap->line1;
3505 for ( ; lnum <= eap->line2; ++lnum)
3506 {
3507 if (!eap->skip && eap->addr_count > 0)
3508 {
3509 curwin->w_cursor.lnum = lnum;
3510 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003511#ifdef FEAT_VIRTUALEDIT
3512 curwin->w_cursor.coladd = 0;
3513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 }
3515 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003516 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003517 eap->line1, eap->line2, &doesrange,
3518 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 {
3520 failed = TRUE;
3521 break;
3522 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003523
3524 /* Handle a function returning a Funcref, Dictionary or List. */
3525 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3526 {
3527 failed = TRUE;
3528 break;
3529 }
3530
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003531 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 if (doesrange || eap->skip)
3533 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003534
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003536 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003537 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003538 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 if (aborting())
3540 break;
3541 }
3542 if (eap->skip)
3543 --emsg_skip;
3544
3545 if (!failed)
3546 {
3547 /* Check for trailing illegal characters and a following command. */
3548 if (!ends_excmd(*arg))
3549 {
3550 emsg_severe = TRUE;
3551 EMSG(_(e_trailing));
3552 }
3553 else
3554 eap->nextcmd = check_nextcmd(arg);
3555 }
3556
3557end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003558 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003559 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560}
3561
3562/*
3563 * ":unlet[!] var1 ... " command.
3564 */
3565 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003566ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003568 ex_unletlock(eap, eap->arg, 0);
3569}
3570
3571/*
3572 * ":lockvar" and ":unlockvar" commands
3573 */
3574 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003575ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003576{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003578 int deep = 2;
3579
3580 if (eap->forceit)
3581 deep = -1;
3582 else if (vim_isdigit(*arg))
3583 {
3584 deep = getdigits(&arg);
3585 arg = skipwhite(arg);
3586 }
3587
3588 ex_unletlock(eap, arg, deep);
3589}
3590
3591/*
3592 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3593 */
3594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003595ex_unletlock(
3596 exarg_T *eap,
3597 char_u *argstart,
3598 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003599{
3600 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003603 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604
3605 do
3606 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003607 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003608 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003609 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003610 if (lv.ll_name == NULL)
3611 error = TRUE; /* error but continue parsing */
3612 if (name_end == NULL || (!vim_iswhite(*name_end)
3613 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003615 if (name_end != NULL)
3616 {
3617 emsg_severe = TRUE;
3618 EMSG(_(e_trailing));
3619 }
3620 if (!(eap->skip || error))
3621 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 break;
3623 }
3624
3625 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003626 {
3627 if (eap->cmdidx == CMD_unlet)
3628 {
3629 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3630 error = TRUE;
3631 }
3632 else
3633 {
3634 if (do_lock_var(&lv, name_end, deep,
3635 eap->cmdidx == CMD_lockvar) == FAIL)
3636 error = TRUE;
3637 }
3638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003640 if (!eap->skip)
3641 clear_lval(&lv);
3642
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 arg = skipwhite(name_end);
3644 } while (!ends_excmd(*arg));
3645
3646 eap->nextcmd = check_nextcmd(arg);
3647}
3648
Bram Moolenaar8c711452005-01-14 21:53:12 +00003649 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003650do_unlet_var(
3651 lval_T *lp,
3652 char_u *name_end,
3653 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003654{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003655 int ret = OK;
3656 int cc;
3657
3658 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003659 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003660 cc = *name_end;
3661 *name_end = NUL;
3662
3663 /* Normal name or expanded name. */
3664 if (check_changedtick(lp->ll_name))
3665 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003666 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003667 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003668 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003669 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003670 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003671 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003672 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003673 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003674 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003675 else if (lp->ll_range)
3676 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003677 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003678 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003679 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003680
3681 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3682 {
3683 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003684 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003685 return FAIL;
3686 ll_li = li;
3687 ++ll_n1;
3688 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003689
3690 /* Delete a range of List items. */
3691 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3692 {
3693 li = lp->ll_li->li_next;
3694 listitem_remove(lp->ll_list, lp->ll_li);
3695 lp->ll_li = li;
3696 ++lp->ll_n1;
3697 }
3698 }
3699 else
3700 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003701 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003702 /* unlet a List item. */
3703 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003704 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003705 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003706 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003707 }
3708
3709 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003710}
3711
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712/*
3713 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003714 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 */
3716 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003717do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718{
Bram Moolenaar33570922005-01-25 22:26:29 +00003719 hashtab_T *ht;
3720 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003721 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003722 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003723 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724
Bram Moolenaar33570922005-01-25 22:26:29 +00003725 ht = find_var_ht(name, &varname);
3726 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003728 if (ht == &globvarht)
3729 d = &globvardict;
3730 else if (current_funccal != NULL
3731 && ht == &current_funccal->l_vars.dv_hashtab)
3732 d = &current_funccal->l_vars;
3733 else if (ht == &compat_hashtab)
3734 d = &vimvardict;
3735 else
3736 {
3737 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3738 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3739 }
3740 if (d == NULL)
3741 {
3742 EMSG2(_(e_intern2), "do_unlet()");
3743 return FAIL;
3744 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003745 hi = hash_find(ht, varname);
3746 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003747 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003748 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003749 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003750 || var_check_ro(di->di_flags, name, FALSE)
3751 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003752 return FAIL;
3753
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003754 delete_var(ht, hi);
3755 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003758 if (forceit)
3759 return OK;
3760 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 return FAIL;
3762}
3763
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003764/*
3765 * Lock or unlock variable indicated by "lp".
3766 * "deep" is the levels to go (-1 for unlimited);
3767 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3768 */
3769 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003770do_lock_var(
3771 lval_T *lp,
3772 char_u *name_end,
3773 int deep,
3774 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003775{
3776 int ret = OK;
3777 int cc;
3778 dictitem_T *di;
3779
3780 if (deep == 0) /* nothing to do */
3781 return OK;
3782
3783 if (lp->ll_tv == NULL)
3784 {
3785 cc = *name_end;
3786 *name_end = NUL;
3787
3788 /* Normal name or expanded name. */
3789 if (check_changedtick(lp->ll_name))
3790 ret = FAIL;
3791 else
3792 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003793 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003794 if (di == NULL)
3795 ret = FAIL;
3796 else
3797 {
3798 if (lock)
3799 di->di_flags |= DI_FLAGS_LOCK;
3800 else
3801 di->di_flags &= ~DI_FLAGS_LOCK;
3802 item_lock(&di->di_tv, deep, lock);
3803 }
3804 }
3805 *name_end = cc;
3806 }
3807 else if (lp->ll_range)
3808 {
3809 listitem_T *li = lp->ll_li;
3810
3811 /* (un)lock a range of List items. */
3812 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3813 {
3814 item_lock(&li->li_tv, deep, lock);
3815 li = li->li_next;
3816 ++lp->ll_n1;
3817 }
3818 }
3819 else if (lp->ll_list != NULL)
3820 /* (un)lock a List item. */
3821 item_lock(&lp->ll_li->li_tv, deep, lock);
3822 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003823 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003824 item_lock(&lp->ll_di->di_tv, deep, lock);
3825
3826 return ret;
3827}
3828
3829/*
3830 * Lock or unlock an item. "deep" is nr of levels to go.
3831 */
3832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003833item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003834{
3835 static int recurse = 0;
3836 list_T *l;
3837 listitem_T *li;
3838 dict_T *d;
3839 hashitem_T *hi;
3840 int todo;
3841
3842 if (recurse >= DICT_MAXNEST)
3843 {
3844 EMSG(_("E743: variable nested too deep for (un)lock"));
3845 return;
3846 }
3847 if (deep == 0)
3848 return;
3849 ++recurse;
3850
3851 /* lock/unlock the item itself */
3852 if (lock)
3853 tv->v_lock |= VAR_LOCKED;
3854 else
3855 tv->v_lock &= ~VAR_LOCKED;
3856
3857 switch (tv->v_type)
3858 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003859 case VAR_UNKNOWN:
3860 case VAR_NUMBER:
3861 case VAR_STRING:
3862 case VAR_FUNC:
3863 case VAR_FLOAT:
3864 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003865 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003866 break;
3867
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003868 case VAR_LIST:
3869 if ((l = tv->vval.v_list) != NULL)
3870 {
3871 if (lock)
3872 l->lv_lock |= VAR_LOCKED;
3873 else
3874 l->lv_lock &= ~VAR_LOCKED;
3875 if (deep < 0 || deep > 1)
3876 /* recursive: lock/unlock the items the List contains */
3877 for (li = l->lv_first; li != NULL; li = li->li_next)
3878 item_lock(&li->li_tv, deep - 1, lock);
3879 }
3880 break;
3881 case VAR_DICT:
3882 if ((d = tv->vval.v_dict) != NULL)
3883 {
3884 if (lock)
3885 d->dv_lock |= VAR_LOCKED;
3886 else
3887 d->dv_lock &= ~VAR_LOCKED;
3888 if (deep < 0 || deep > 1)
3889 {
3890 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003891 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003892 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3893 {
3894 if (!HASHITEM_EMPTY(hi))
3895 {
3896 --todo;
3897 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3898 }
3899 }
3900 }
3901 }
3902 }
3903 --recurse;
3904}
3905
Bram Moolenaara40058a2005-07-11 22:42:07 +00003906/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003907 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3908 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003909 */
3910 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003911tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003912{
3913 return (tv->v_lock & VAR_LOCKED)
3914 || (tv->v_type == VAR_LIST
3915 && tv->vval.v_list != NULL
3916 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3917 || (tv->v_type == VAR_DICT
3918 && tv->vval.v_dict != NULL
3919 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3920}
3921
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3923/*
3924 * Delete all "menutrans_" variables.
3925 */
3926 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003927del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928{
Bram Moolenaar33570922005-01-25 22:26:29 +00003929 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003930 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931
Bram Moolenaar33570922005-01-25 22:26:29 +00003932 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003933 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003934 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003935 {
3936 if (!HASHITEM_EMPTY(hi))
3937 {
3938 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003939 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3940 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003941 }
3942 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003943 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944}
3945#endif
3946
3947#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3948
3949/*
3950 * Local string buffer for the next two functions to store a variable name
3951 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3952 * get_user_var_name().
3953 */
3954
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003955static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956
3957static char_u *varnamebuf = NULL;
3958static int varnamebuflen = 0;
3959
3960/*
3961 * Function to concatenate a prefix and a variable name.
3962 */
3963 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003964cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965{
3966 int len;
3967
3968 len = (int)STRLEN(name) + 3;
3969 if (len > varnamebuflen)
3970 {
3971 vim_free(varnamebuf);
3972 len += 10; /* some additional space */
3973 varnamebuf = alloc(len);
3974 if (varnamebuf == NULL)
3975 {
3976 varnamebuflen = 0;
3977 return NULL;
3978 }
3979 varnamebuflen = len;
3980 }
3981 *varnamebuf = prefix;
3982 varnamebuf[1] = ':';
3983 STRCPY(varnamebuf + 2, name);
3984 return varnamebuf;
3985}
3986
3987/*
3988 * Function given to ExpandGeneric() to obtain the list of user defined
3989 * (global/buffer/window/built-in) variable names.
3990 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003992get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003994 static long_u gdone;
3995 static long_u bdone;
3996 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003997#ifdef FEAT_WINDOWS
3998 static long_u tdone;
3999#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004000 static int vidx;
4001 static hashitem_T *hi;
4002 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003
4004 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004005 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004006 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004007#ifdef FEAT_WINDOWS
4008 tdone = 0;
4009#endif
4010 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004011
4012 /* Global variables */
4013 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004015 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004016 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004017 else
4018 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004019 while (HASHITEM_EMPTY(hi))
4020 ++hi;
4021 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4022 return cat_prefix_varname('g', hi->hi_key);
4023 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004025
4026 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004027 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004028 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004030 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004031 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004032 else
4033 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004034 while (HASHITEM_EMPTY(hi))
4035 ++hi;
4036 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004038 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004040 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 return (char_u *)"b:changedtick";
4042 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004043
4044 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004045 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004046 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004048 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004049 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004050 else
4051 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004052 while (HASHITEM_EMPTY(hi))
4053 ++hi;
4054 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004056
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004057#ifdef FEAT_WINDOWS
4058 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004059 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004060 if (tdone < ht->ht_used)
4061 {
4062 if (tdone++ == 0)
4063 hi = ht->ht_array;
4064 else
4065 ++hi;
4066 while (HASHITEM_EMPTY(hi))
4067 ++hi;
4068 return cat_prefix_varname('t', hi->hi_key);
4069 }
4070#endif
4071
Bram Moolenaar33570922005-01-25 22:26:29 +00004072 /* v: variables */
4073 if (vidx < VV_LEN)
4074 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075
4076 vim_free(varnamebuf);
4077 varnamebuf = NULL;
4078 varnamebuflen = 0;
4079 return NULL;
4080}
4081
4082#endif /* FEAT_CMDL_COMPL */
4083
4084/*
4085 * types for expressions.
4086 */
4087typedef enum
4088{
4089 TYPE_UNKNOWN = 0
4090 , TYPE_EQUAL /* == */
4091 , TYPE_NEQUAL /* != */
4092 , TYPE_GREATER /* > */
4093 , TYPE_GEQUAL /* >= */
4094 , TYPE_SMALLER /* < */
4095 , TYPE_SEQUAL /* <= */
4096 , TYPE_MATCH /* =~ */
4097 , TYPE_NOMATCH /* !~ */
4098} exptype_T;
4099
4100/*
4101 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004102 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4104 */
4105
4106/*
4107 * Handle zero level expression.
4108 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004109 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004110 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 * Return OK or FAIL.
4112 */
4113 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004114eval0(
4115 char_u *arg,
4116 typval_T *rettv,
4117 char_u **nextcmd,
4118 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119{
4120 int ret;
4121 char_u *p;
4122
4123 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004124 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 if (ret == FAIL || !ends_excmd(*p))
4126 {
4127 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004128 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 /*
4130 * Report the invalid expression unless the expression evaluation has
4131 * been cancelled due to an aborting error, an interrupt, or an
4132 * exception.
4133 */
4134 if (!aborting())
4135 EMSG2(_(e_invexpr2), arg);
4136 ret = FAIL;
4137 }
4138 if (nextcmd != NULL)
4139 *nextcmd = check_nextcmd(p);
4140
4141 return ret;
4142}
4143
4144/*
4145 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004146 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 *
4148 * "arg" must point to the first non-white of the expression.
4149 * "arg" is advanced to the next non-white after the recognized expression.
4150 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004151 * Note: "rettv.v_lock" is not set.
4152 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 * Return OK or FAIL.
4154 */
4155 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004156eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157{
4158 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004159 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160
4161 /*
4162 * Get the first variable.
4163 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004164 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 return FAIL;
4166
4167 if ((*arg)[0] == '?')
4168 {
4169 result = FALSE;
4170 if (evaluate)
4171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004172 int error = FALSE;
4173
4174 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004176 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004177 if (error)
4178 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 }
4180
4181 /*
4182 * Get the second variable.
4183 */
4184 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004185 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 return FAIL;
4187
4188 /*
4189 * Check for the ":".
4190 */
4191 if ((*arg)[0] != ':')
4192 {
4193 EMSG(_("E109: Missing ':' after '?'"));
4194 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004195 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 return FAIL;
4197 }
4198
4199 /*
4200 * Get the third variable.
4201 */
4202 *arg = skipwhite(*arg + 1);
4203 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4204 {
4205 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004206 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 return FAIL;
4208 }
4209 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004210 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 }
4212
4213 return OK;
4214}
4215
4216/*
4217 * Handle first level expression:
4218 * expr2 || expr2 || expr2 logical OR
4219 *
4220 * "arg" must point to the first non-white of the expression.
4221 * "arg" is advanced to the next non-white after the recognized expression.
4222 *
4223 * Return OK or FAIL.
4224 */
4225 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004226eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227{
Bram Moolenaar33570922005-01-25 22:26:29 +00004228 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 long result;
4230 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004231 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232
4233 /*
4234 * Get the first variable.
4235 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004236 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 return FAIL;
4238
4239 /*
4240 * Repeat until there is no following "||".
4241 */
4242 first = TRUE;
4243 result = FALSE;
4244 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4245 {
4246 if (evaluate && first)
4247 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004248 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004250 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004251 if (error)
4252 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 first = FALSE;
4254 }
4255
4256 /*
4257 * Get the second variable.
4258 */
4259 *arg = skipwhite(*arg + 2);
4260 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4261 return FAIL;
4262
4263 /*
4264 * Compute the result.
4265 */
4266 if (evaluate && !result)
4267 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004268 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004270 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004271 if (error)
4272 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
4274 if (evaluate)
4275 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004276 rettv->v_type = VAR_NUMBER;
4277 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 }
4279 }
4280
4281 return OK;
4282}
4283
4284/*
4285 * Handle second level expression:
4286 * expr3 && expr3 && expr3 logical AND
4287 *
4288 * "arg" must point to the first non-white of the expression.
4289 * "arg" is advanced to the next non-white after the recognized expression.
4290 *
4291 * Return OK or FAIL.
4292 */
4293 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004294eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295{
Bram Moolenaar33570922005-01-25 22:26:29 +00004296 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 long result;
4298 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004299 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300
4301 /*
4302 * Get the first variable.
4303 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004304 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 return FAIL;
4306
4307 /*
4308 * Repeat until there is no following "&&".
4309 */
4310 first = TRUE;
4311 result = TRUE;
4312 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4313 {
4314 if (evaluate && first)
4315 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004316 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004318 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004319 if (error)
4320 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 first = FALSE;
4322 }
4323
4324 /*
4325 * Get the second variable.
4326 */
4327 *arg = skipwhite(*arg + 2);
4328 if (eval4(arg, &var2, evaluate && result) == FAIL)
4329 return FAIL;
4330
4331 /*
4332 * Compute the result.
4333 */
4334 if (evaluate && result)
4335 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004336 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004338 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004339 if (error)
4340 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 }
4342 if (evaluate)
4343 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004344 rettv->v_type = VAR_NUMBER;
4345 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 }
4347 }
4348
4349 return OK;
4350}
4351
4352/*
4353 * Handle third level expression:
4354 * var1 == var2
4355 * var1 =~ var2
4356 * var1 != var2
4357 * var1 !~ var2
4358 * var1 > var2
4359 * var1 >= var2
4360 * var1 < var2
4361 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004362 * var1 is var2
4363 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 *
4365 * "arg" must point to the first non-white of the expression.
4366 * "arg" is advanced to the next non-white after the recognized expression.
4367 *
4368 * Return OK or FAIL.
4369 */
4370 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004371eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372{
Bram Moolenaar33570922005-01-25 22:26:29 +00004373 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 char_u *p;
4375 int i;
4376 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004377 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 int len = 2;
4379 long n1, n2;
4380 char_u *s1, *s2;
4381 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4382 regmatch_T regmatch;
4383 int ic;
4384 char_u *save_cpo;
4385
4386 /*
4387 * Get the first variable.
4388 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004389 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 return FAIL;
4391
4392 p = *arg;
4393 switch (p[0])
4394 {
4395 case '=': if (p[1] == '=')
4396 type = TYPE_EQUAL;
4397 else if (p[1] == '~')
4398 type = TYPE_MATCH;
4399 break;
4400 case '!': if (p[1] == '=')
4401 type = TYPE_NEQUAL;
4402 else if (p[1] == '~')
4403 type = TYPE_NOMATCH;
4404 break;
4405 case '>': if (p[1] != '=')
4406 {
4407 type = TYPE_GREATER;
4408 len = 1;
4409 }
4410 else
4411 type = TYPE_GEQUAL;
4412 break;
4413 case '<': if (p[1] != '=')
4414 {
4415 type = TYPE_SMALLER;
4416 len = 1;
4417 }
4418 else
4419 type = TYPE_SEQUAL;
4420 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004421 case 'i': if (p[1] == 's')
4422 {
4423 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4424 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004425 i = p[len];
4426 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004427 {
4428 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4429 type_is = TRUE;
4430 }
4431 }
4432 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 }
4434
4435 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004436 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 */
4438 if (type != TYPE_UNKNOWN)
4439 {
4440 /* extra question mark appended: ignore case */
4441 if (p[len] == '?')
4442 {
4443 ic = TRUE;
4444 ++len;
4445 }
4446 /* extra '#' appended: match case */
4447 else if (p[len] == '#')
4448 {
4449 ic = FALSE;
4450 ++len;
4451 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004452 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 else
4454 ic = p_ic;
4455
4456 /*
4457 * Get the second variable.
4458 */
4459 *arg = skipwhite(p + len);
4460 if (eval5(arg, &var2, evaluate) == FAIL)
4461 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004462 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 return FAIL;
4464 }
4465
4466 if (evaluate)
4467 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004468 if (type_is && rettv->v_type != var2.v_type)
4469 {
4470 /* For "is" a different type always means FALSE, for "notis"
4471 * it means TRUE. */
4472 n1 = (type == TYPE_NEQUAL);
4473 }
4474 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4475 {
4476 if (type_is)
4477 {
4478 n1 = (rettv->v_type == var2.v_type
4479 && rettv->vval.v_list == var2.vval.v_list);
4480 if (type == TYPE_NEQUAL)
4481 n1 = !n1;
4482 }
4483 else if (rettv->v_type != var2.v_type
4484 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4485 {
4486 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004487 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004488 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004489 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004490 clear_tv(rettv);
4491 clear_tv(&var2);
4492 return FAIL;
4493 }
4494 else
4495 {
4496 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004497 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4498 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004499 if (type == TYPE_NEQUAL)
4500 n1 = !n1;
4501 }
4502 }
4503
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004504 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4505 {
4506 if (type_is)
4507 {
4508 n1 = (rettv->v_type == var2.v_type
4509 && rettv->vval.v_dict == var2.vval.v_dict);
4510 if (type == TYPE_NEQUAL)
4511 n1 = !n1;
4512 }
4513 else if (rettv->v_type != var2.v_type
4514 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4515 {
4516 if (rettv->v_type != var2.v_type)
4517 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4518 else
4519 EMSG(_("E736: Invalid operation for Dictionary"));
4520 clear_tv(rettv);
4521 clear_tv(&var2);
4522 return FAIL;
4523 }
4524 else
4525 {
4526 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004527 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4528 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004529 if (type == TYPE_NEQUAL)
4530 n1 = !n1;
4531 }
4532 }
4533
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004534 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4535 {
4536 if (rettv->v_type != var2.v_type
4537 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4538 {
4539 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004540 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004541 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004542 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004543 clear_tv(rettv);
4544 clear_tv(&var2);
4545 return FAIL;
4546 }
4547 else
4548 {
4549 /* Compare two Funcrefs for being equal or unequal. */
4550 if (rettv->vval.v_string == NULL
4551 || var2.vval.v_string == NULL)
4552 n1 = FALSE;
4553 else
4554 n1 = STRCMP(rettv->vval.v_string,
4555 var2.vval.v_string) == 0;
4556 if (type == TYPE_NEQUAL)
4557 n1 = !n1;
4558 }
4559 }
4560
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004561#ifdef FEAT_FLOAT
4562 /*
4563 * If one of the two variables is a float, compare as a float.
4564 * When using "=~" or "!~", always compare as string.
4565 */
4566 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4567 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4568 {
4569 float_T f1, f2;
4570
4571 if (rettv->v_type == VAR_FLOAT)
4572 f1 = rettv->vval.v_float;
4573 else
4574 f1 = get_tv_number(rettv);
4575 if (var2.v_type == VAR_FLOAT)
4576 f2 = var2.vval.v_float;
4577 else
4578 f2 = get_tv_number(&var2);
4579 n1 = FALSE;
4580 switch (type)
4581 {
4582 case TYPE_EQUAL: n1 = (f1 == f2); break;
4583 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4584 case TYPE_GREATER: n1 = (f1 > f2); break;
4585 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4586 case TYPE_SMALLER: n1 = (f1 < f2); break;
4587 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4588 case TYPE_UNKNOWN:
4589 case TYPE_MATCH:
4590 case TYPE_NOMATCH: break; /* avoid gcc warning */
4591 }
4592 }
4593#endif
4594
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 /*
4596 * If one of the two variables is a number, compare as a number.
4597 * When using "=~" or "!~", always compare as string.
4598 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004599 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4601 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004602 n1 = get_tv_number(rettv);
4603 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 switch (type)
4605 {
4606 case TYPE_EQUAL: n1 = (n1 == n2); break;
4607 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4608 case TYPE_GREATER: n1 = (n1 > n2); break;
4609 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4610 case TYPE_SMALLER: n1 = (n1 < n2); break;
4611 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4612 case TYPE_UNKNOWN:
4613 case TYPE_MATCH:
4614 case TYPE_NOMATCH: break; /* avoid gcc warning */
4615 }
4616 }
4617 else
4618 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004619 s1 = get_tv_string_buf(rettv, buf1);
4620 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4622 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4623 else
4624 i = 0;
4625 n1 = FALSE;
4626 switch (type)
4627 {
4628 case TYPE_EQUAL: n1 = (i == 0); break;
4629 case TYPE_NEQUAL: n1 = (i != 0); break;
4630 case TYPE_GREATER: n1 = (i > 0); break;
4631 case TYPE_GEQUAL: n1 = (i >= 0); break;
4632 case TYPE_SMALLER: n1 = (i < 0); break;
4633 case TYPE_SEQUAL: n1 = (i <= 0); break;
4634
4635 case TYPE_MATCH:
4636 case TYPE_NOMATCH:
4637 /* avoid 'l' flag in 'cpoptions' */
4638 save_cpo = p_cpo;
4639 p_cpo = (char_u *)"";
4640 regmatch.regprog = vim_regcomp(s2,
4641 RE_MAGIC + RE_STRING);
4642 regmatch.rm_ic = ic;
4643 if (regmatch.regprog != NULL)
4644 {
4645 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004646 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 if (type == TYPE_NOMATCH)
4648 n1 = !n1;
4649 }
4650 p_cpo = save_cpo;
4651 break;
4652
4653 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4654 }
4655 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004656 clear_tv(rettv);
4657 clear_tv(&var2);
4658 rettv->v_type = VAR_NUMBER;
4659 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 }
4661 }
4662
4663 return OK;
4664}
4665
4666/*
4667 * Handle fourth level expression:
4668 * + number addition
4669 * - number subtraction
4670 * . string concatenation
4671 *
4672 * "arg" must point to the first non-white of the expression.
4673 * "arg" is advanced to the next non-white after the recognized expression.
4674 *
4675 * Return OK or FAIL.
4676 */
4677 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004678eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679{
Bram Moolenaar33570922005-01-25 22:26:29 +00004680 typval_T var2;
4681 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 int op;
4683 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004684#ifdef FEAT_FLOAT
4685 float_T f1 = 0, f2 = 0;
4686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 char_u *s1, *s2;
4688 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4689 char_u *p;
4690
4691 /*
4692 * Get the first variable.
4693 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004694 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 return FAIL;
4696
4697 /*
4698 * Repeat computing, until no '+', '-' or '.' is following.
4699 */
4700 for (;;)
4701 {
4702 op = **arg;
4703 if (op != '+' && op != '-' && op != '.')
4704 break;
4705
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004706 if ((op != '+' || rettv->v_type != VAR_LIST)
4707#ifdef FEAT_FLOAT
4708 && (op == '.' || rettv->v_type != VAR_FLOAT)
4709#endif
4710 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004711 {
4712 /* For "list + ...", an illegal use of the first operand as
4713 * a number cannot be determined before evaluating the 2nd
4714 * operand: if this is also a list, all is ok.
4715 * For "something . ...", "something - ..." or "non-list + ...",
4716 * we know that the first operand needs to be a string or number
4717 * without evaluating the 2nd operand. So check before to avoid
4718 * side effects after an error. */
4719 if (evaluate && get_tv_string_chk(rettv) == NULL)
4720 {
4721 clear_tv(rettv);
4722 return FAIL;
4723 }
4724 }
4725
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 /*
4727 * Get the second variable.
4728 */
4729 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004730 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004732 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 return FAIL;
4734 }
4735
4736 if (evaluate)
4737 {
4738 /*
4739 * Compute the result.
4740 */
4741 if (op == '.')
4742 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004743 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4744 s2 = get_tv_string_buf_chk(&var2, buf2);
4745 if (s2 == NULL) /* type error ? */
4746 {
4747 clear_tv(rettv);
4748 clear_tv(&var2);
4749 return FAIL;
4750 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004751 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004752 clear_tv(rettv);
4753 rettv->v_type = VAR_STRING;
4754 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004756 else if (op == '+' && rettv->v_type == VAR_LIST
4757 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004758 {
4759 /* concatenate Lists */
4760 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4761 &var3) == FAIL)
4762 {
4763 clear_tv(rettv);
4764 clear_tv(&var2);
4765 return FAIL;
4766 }
4767 clear_tv(rettv);
4768 *rettv = var3;
4769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 else
4771 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004772 int error = FALSE;
4773
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004774#ifdef FEAT_FLOAT
4775 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004776 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004777 f1 = rettv->vval.v_float;
4778 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004779 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004780 else
4781#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004782 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004783 n1 = get_tv_number_chk(rettv, &error);
4784 if (error)
4785 {
4786 /* This can only happen for "list + non-list". For
4787 * "non-list + ..." or "something - ...", we returned
4788 * before evaluating the 2nd operand. */
4789 clear_tv(rettv);
4790 return FAIL;
4791 }
4792#ifdef FEAT_FLOAT
4793 if (var2.v_type == VAR_FLOAT)
4794 f1 = n1;
4795#endif
4796 }
4797#ifdef FEAT_FLOAT
4798 if (var2.v_type == VAR_FLOAT)
4799 {
4800 f2 = var2.vval.v_float;
4801 n2 = 0;
4802 }
4803 else
4804#endif
4805 {
4806 n2 = get_tv_number_chk(&var2, &error);
4807 if (error)
4808 {
4809 clear_tv(rettv);
4810 clear_tv(&var2);
4811 return FAIL;
4812 }
4813#ifdef FEAT_FLOAT
4814 if (rettv->v_type == VAR_FLOAT)
4815 f2 = n2;
4816#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004817 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004818 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004819
4820#ifdef FEAT_FLOAT
4821 /* If there is a float on either side the result is a float. */
4822 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4823 {
4824 if (op == '+')
4825 f1 = f1 + f2;
4826 else
4827 f1 = f1 - f2;
4828 rettv->v_type = VAR_FLOAT;
4829 rettv->vval.v_float = f1;
4830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004832#endif
4833 {
4834 if (op == '+')
4835 n1 = n1 + n2;
4836 else
4837 n1 = n1 - n2;
4838 rettv->v_type = VAR_NUMBER;
4839 rettv->vval.v_number = n1;
4840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004842 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 }
4844 }
4845 return OK;
4846}
4847
4848/*
4849 * Handle fifth level expression:
4850 * * number multiplication
4851 * / number division
4852 * % number modulo
4853 *
4854 * "arg" must point to the first non-white of the expression.
4855 * "arg" is advanced to the next non-white after the recognized expression.
4856 *
4857 * Return OK or FAIL.
4858 */
4859 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004860eval6(
4861 char_u **arg,
4862 typval_T *rettv,
4863 int evaluate,
4864 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865{
Bram Moolenaar33570922005-01-25 22:26:29 +00004866 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 int op;
4868 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004869#ifdef FEAT_FLOAT
4870 int use_float = FALSE;
4871 float_T f1 = 0, f2;
4872#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004873 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874
4875 /*
4876 * Get the first variable.
4877 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004878 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 return FAIL;
4880
4881 /*
4882 * Repeat computing, until no '*', '/' or '%' is following.
4883 */
4884 for (;;)
4885 {
4886 op = **arg;
4887 if (op != '*' && op != '/' && op != '%')
4888 break;
4889
4890 if (evaluate)
4891 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004892#ifdef FEAT_FLOAT
4893 if (rettv->v_type == VAR_FLOAT)
4894 {
4895 f1 = rettv->vval.v_float;
4896 use_float = TRUE;
4897 n1 = 0;
4898 }
4899 else
4900#endif
4901 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004902 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004903 if (error)
4904 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 }
4906 else
4907 n1 = 0;
4908
4909 /*
4910 * Get the second variable.
4911 */
4912 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004913 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 return FAIL;
4915
4916 if (evaluate)
4917 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004918#ifdef FEAT_FLOAT
4919 if (var2.v_type == VAR_FLOAT)
4920 {
4921 if (!use_float)
4922 {
4923 f1 = n1;
4924 use_float = TRUE;
4925 }
4926 f2 = var2.vval.v_float;
4927 n2 = 0;
4928 }
4929 else
4930#endif
4931 {
4932 n2 = get_tv_number_chk(&var2, &error);
4933 clear_tv(&var2);
4934 if (error)
4935 return FAIL;
4936#ifdef FEAT_FLOAT
4937 if (use_float)
4938 f2 = n2;
4939#endif
4940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941
4942 /*
4943 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004944 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004946#ifdef FEAT_FLOAT
4947 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004949 if (op == '*')
4950 f1 = f1 * f2;
4951 else if (op == '/')
4952 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004953# ifdef VMS
4954 /* VMS crashes on divide by zero, work around it */
4955 if (f2 == 0.0)
4956 {
4957 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004958 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004959 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004960 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004961 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004962 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004963 }
4964 else
4965 f1 = f1 / f2;
4966# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004967 /* We rely on the floating point library to handle divide
4968 * by zero to result in "inf" and not a crash. */
4969 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004970# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004973 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004974 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004975 return FAIL;
4976 }
4977 rettv->v_type = VAR_FLOAT;
4978 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 }
4980 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004983 if (op == '*')
4984 n1 = n1 * n2;
4985 else if (op == '/')
4986 {
4987 if (n2 == 0) /* give an error message? */
4988 {
4989 if (n1 == 0)
4990 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4991 else if (n1 < 0)
4992 n1 = -0x7fffffffL;
4993 else
4994 n1 = 0x7fffffffL;
4995 }
4996 else
4997 n1 = n1 / n2;
4998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005000 {
5001 if (n2 == 0) /* give an error message? */
5002 n1 = 0;
5003 else
5004 n1 = n1 % n2;
5005 }
5006 rettv->v_type = VAR_NUMBER;
5007 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 }
5010 }
5011
5012 return OK;
5013}
5014
5015/*
5016 * Handle sixth level expression:
5017 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005018 * "string" string constant
5019 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 * &option-name option value
5021 * @r register contents
5022 * identifier variable value
5023 * function() function call
5024 * $VAR environment variable
5025 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005026 * [expr, expr] List
5027 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 *
5029 * Also handle:
5030 * ! in front logical NOT
5031 * - in front unary minus
5032 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005033 * trailing [] subscript in String or List
5034 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 *
5036 * "arg" must point to the first non-white of the expression.
5037 * "arg" is advanced to the next non-white after the recognized expression.
5038 *
5039 * Return OK or FAIL.
5040 */
5041 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005042eval7(
5043 char_u **arg,
5044 typval_T *rettv,
5045 int evaluate,
5046 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 long n;
5049 int len;
5050 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 char_u *start_leader, *end_leader;
5052 int ret = OK;
5053 char_u *alias;
5054
5055 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005056 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005057 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005059 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060
5061 /*
5062 * Skip '!' and '-' characters. They are handled later.
5063 */
5064 start_leader = *arg;
5065 while (**arg == '!' || **arg == '-' || **arg == '+')
5066 *arg = skipwhite(*arg + 1);
5067 end_leader = *arg;
5068
5069 switch (**arg)
5070 {
5071 /*
5072 * Number constant.
5073 */
5074 case '0':
5075 case '1':
5076 case '2':
5077 case '3':
5078 case '4':
5079 case '5':
5080 case '6':
5081 case '7':
5082 case '8':
5083 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005084 {
5085#ifdef FEAT_FLOAT
5086 char_u *p = skipdigits(*arg + 1);
5087 int get_float = FALSE;
5088
5089 /* We accept a float when the format matches
5090 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005091 * strict to avoid backwards compatibility problems.
5092 * Don't look for a float after the "." operator, so that
5093 * ":let vers = 1.2.3" doesn't fail. */
5094 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005096 get_float = TRUE;
5097 p = skipdigits(p + 2);
5098 if (*p == 'e' || *p == 'E')
5099 {
5100 ++p;
5101 if (*p == '-' || *p == '+')
5102 ++p;
5103 if (!vim_isdigit(*p))
5104 get_float = FALSE;
5105 else
5106 p = skipdigits(p + 1);
5107 }
5108 if (ASCII_ISALPHA(*p) || *p == '.')
5109 get_float = FALSE;
5110 }
5111 if (get_float)
5112 {
5113 float_T f;
5114
5115 *arg += string2float(*arg, &f);
5116 if (evaluate)
5117 {
5118 rettv->v_type = VAR_FLOAT;
5119 rettv->vval.v_float = f;
5120 }
5121 }
5122 else
5123#endif
5124 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005125 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005126 *arg += len;
5127 if (evaluate)
5128 {
5129 rettv->v_type = VAR_NUMBER;
5130 rettv->vval.v_number = n;
5131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 }
5133 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135
5136 /*
5137 * String constant: "string".
5138 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005139 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 break;
5141
5142 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005143 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005145 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005146 break;
5147
5148 /*
5149 * List: [expr, expr]
5150 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005151 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 break;
5153
5154 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 * Dictionary: {key: val, key: val}
5156 */
5157 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5158 break;
5159
5160 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005161 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005163 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 break;
5165
5166 /*
5167 * Environment variable: $VAR.
5168 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005169 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 break;
5171
5172 /*
5173 * Register contents: @r.
5174 */
5175 case '@': ++*arg;
5176 if (evaluate)
5177 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005178 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005179 rettv->vval.v_string = get_reg_contents(**arg,
5180 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 }
5182 if (**arg != NUL)
5183 ++*arg;
5184 break;
5185
5186 /*
5187 * nested expression: (expression).
5188 */
5189 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005190 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 if (**arg == ')')
5192 ++*arg;
5193 else if (ret == OK)
5194 {
5195 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005196 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 ret = FAIL;
5198 }
5199 break;
5200
Bram Moolenaar8c711452005-01-14 21:53:12 +00005201 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 break;
5203 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005204
5205 if (ret == NOTDONE)
5206 {
5207 /*
5208 * Must be a variable or function name.
5209 * Can also be a curly-braces kind of name: {expr}.
5210 */
5211 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005212 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005213 if (alias != NULL)
5214 s = alias;
5215
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005216 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005217 ret = FAIL;
5218 else
5219 {
5220 if (**arg == '(') /* recursive! */
5221 {
5222 /* If "s" is the name of a variable of type VAR_FUNC
5223 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005224 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225
5226 /* Invoke the function. */
5227 ret = get_func_tv(s, len, rettv, arg,
5228 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005229 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005230
5231 /* If evaluate is FALSE rettv->v_type was not set in
5232 * get_func_tv, but it's needed in handle_subscript() to parse
5233 * what follows. So set it here. */
5234 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5235 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005236 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005237 rettv->v_type = VAR_FUNC;
5238 }
5239
Bram Moolenaar8c711452005-01-14 21:53:12 +00005240 /* Stop the expression evaluation when immediately
5241 * aborting on error, or when an interrupt occurred or
5242 * an exception was thrown but not caught. */
5243 if (aborting())
5244 {
5245 if (ret == OK)
5246 clear_tv(rettv);
5247 ret = FAIL;
5248 }
5249 }
5250 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005251 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005252 else
5253 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005254 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005255 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005256 }
5257
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 *arg = skipwhite(*arg);
5259
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005260 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5261 * expr(expr). */
5262 if (ret == OK)
5263 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264
5265 /*
5266 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5267 */
5268 if (ret == OK && evaluate && end_leader > start_leader)
5269 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005270 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005271 int val = 0;
5272#ifdef FEAT_FLOAT
5273 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005274
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005275 if (rettv->v_type == VAR_FLOAT)
5276 f = rettv->vval.v_float;
5277 else
5278#endif
5279 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005280 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005282 clear_tv(rettv);
5283 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005285 else
5286 {
5287 while (end_leader > start_leader)
5288 {
5289 --end_leader;
5290 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005291 {
5292#ifdef FEAT_FLOAT
5293 if (rettv->v_type == VAR_FLOAT)
5294 f = !f;
5295 else
5296#endif
5297 val = !val;
5298 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005299 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005300 {
5301#ifdef FEAT_FLOAT
5302 if (rettv->v_type == VAR_FLOAT)
5303 f = -f;
5304 else
5305#endif
5306 val = -val;
5307 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005308 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005309#ifdef FEAT_FLOAT
5310 if (rettv->v_type == VAR_FLOAT)
5311 {
5312 clear_tv(rettv);
5313 rettv->vval.v_float = f;
5314 }
5315 else
5316#endif
5317 {
5318 clear_tv(rettv);
5319 rettv->v_type = VAR_NUMBER;
5320 rettv->vval.v_number = val;
5321 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 }
5324
5325 return ret;
5326}
5327
5328/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005329 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5330 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005331 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5332 */
5333 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005334eval_index(
5335 char_u **arg,
5336 typval_T *rettv,
5337 int evaluate,
5338 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005339{
5340 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005341 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005343 long len = -1;
5344 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005346 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005347
Bram Moolenaara03f2332016-02-06 18:09:59 +01005348 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005350 case VAR_FUNC:
5351 if (verbose)
5352 EMSG(_("E695: Cannot index a Funcref"));
5353 return FAIL;
5354 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005355#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005356 if (verbose)
5357 EMSG(_(e_float_as_string));
5358 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005359#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005360 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005361 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005362 if (verbose)
5363 EMSG(_("E909: Cannot index a special variable"));
5364 return FAIL;
5365 case VAR_UNKNOWN:
5366 if (evaluate)
5367 return FAIL;
5368 /* FALLTHROUGH */
5369
5370 case VAR_STRING:
5371 case VAR_NUMBER:
5372 case VAR_LIST:
5373 case VAR_DICT:
5374 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005375 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005376
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005377 init_tv(&var1);
5378 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005379 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005380 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005381 /*
5382 * dict.name
5383 */
5384 key = *arg + 1;
5385 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5386 ;
5387 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005388 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005389 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 }
5391 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005392 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005393 /*
5394 * something[idx]
5395 *
5396 * Get the (first) variable from inside the [].
5397 */
5398 *arg = skipwhite(*arg + 1);
5399 if (**arg == ':')
5400 empty1 = TRUE;
5401 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5402 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005403 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5404 {
5405 /* not a number or string */
5406 clear_tv(&var1);
5407 return FAIL;
5408 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005409
5410 /*
5411 * Get the second variable from inside the [:].
5412 */
5413 if (**arg == ':')
5414 {
5415 range = TRUE;
5416 *arg = skipwhite(*arg + 1);
5417 if (**arg == ']')
5418 empty2 = TRUE;
5419 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5420 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005421 if (!empty1)
5422 clear_tv(&var1);
5423 return FAIL;
5424 }
5425 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5426 {
5427 /* not a number or string */
5428 if (!empty1)
5429 clear_tv(&var1);
5430 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005431 return FAIL;
5432 }
5433 }
5434
5435 /* Check for the ']'. */
5436 if (**arg != ']')
5437 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005438 if (verbose)
5439 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005440 clear_tv(&var1);
5441 if (range)
5442 clear_tv(&var2);
5443 return FAIL;
5444 }
5445 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446 }
5447
5448 if (evaluate)
5449 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005450 n1 = 0;
5451 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005452 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005453 n1 = get_tv_number(&var1);
5454 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455 }
5456 if (range)
5457 {
5458 if (empty2)
5459 n2 = -1;
5460 else
5461 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005462 n2 = get_tv_number(&var2);
5463 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005464 }
5465 }
5466
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005467 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005469 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005470 case VAR_FUNC:
5471 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005472 case VAR_SPECIAL:
5473 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005474 break; /* not evaluating, skipping over subscript */
5475
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 case VAR_NUMBER:
5477 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005478 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 len = (long)STRLEN(s);
5480 if (range)
5481 {
5482 /* The resulting variable is a substring. If the indexes
5483 * are out of range the result is empty. */
5484 if (n1 < 0)
5485 {
5486 n1 = len + n1;
5487 if (n1 < 0)
5488 n1 = 0;
5489 }
5490 if (n2 < 0)
5491 n2 = len + n2;
5492 else if (n2 >= len)
5493 n2 = len;
5494 if (n1 >= len || n2 < 0 || n1 > n2)
5495 s = NULL;
5496 else
5497 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5498 }
5499 else
5500 {
5501 /* The resulting variable is a string of a single
5502 * character. If the index is too big or negative the
5503 * result is empty. */
5504 if (n1 >= len || n1 < 0)
5505 s = NULL;
5506 else
5507 s = vim_strnsave(s + n1, 1);
5508 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005509 clear_tv(rettv);
5510 rettv->v_type = VAR_STRING;
5511 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005512 break;
5513
5514 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005515 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005516 if (n1 < 0)
5517 n1 = len + n1;
5518 if (!empty1 && (n1 < 0 || n1 >= len))
5519 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005520 /* For a range we allow invalid values and return an empty
5521 * list. A list index out of range is an error. */
5522 if (!range)
5523 {
5524 if (verbose)
5525 EMSGN(_(e_listidx), n1);
5526 return FAIL;
5527 }
5528 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005529 }
5530 if (range)
5531 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005532 list_T *l;
5533 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005534
5535 if (n2 < 0)
5536 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005537 else if (n2 >= len)
5538 n2 = len - 1;
5539 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005540 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541 l = list_alloc();
5542 if (l == NULL)
5543 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005544 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005545 n1 <= n2; ++n1)
5546 {
5547 if (list_append_tv(l, &item->li_tv) == FAIL)
5548 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005549 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005550 return FAIL;
5551 }
5552 item = item->li_next;
5553 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005554 clear_tv(rettv);
5555 rettv->v_type = VAR_LIST;
5556 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005557 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005558 }
5559 else
5560 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005561 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005562 clear_tv(rettv);
5563 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005564 }
5565 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005566
5567 case VAR_DICT:
5568 if (range)
5569 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005570 if (verbose)
5571 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005572 if (len == -1)
5573 clear_tv(&var1);
5574 return FAIL;
5575 }
5576 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005577 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005578
5579 if (len == -1)
5580 {
5581 key = get_tv_string(&var1);
5582 if (*key == NUL)
5583 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005584 if (verbose)
5585 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005586 clear_tv(&var1);
5587 return FAIL;
5588 }
5589 }
5590
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005591 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005592
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005593 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005594 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005595 if (len == -1)
5596 clear_tv(&var1);
5597 if (item == NULL)
5598 return FAIL;
5599
5600 copy_tv(&item->di_tv, &var1);
5601 clear_tv(rettv);
5602 *rettv = var1;
5603 }
5604 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005605 }
5606 }
5607
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005608 return OK;
5609}
5610
5611/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 * Get an option value.
5613 * "arg" points to the '&' or '+' before the option name.
5614 * "arg" is advanced to character after the option name.
5615 * Return OK or FAIL.
5616 */
5617 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005618get_option_tv(
5619 char_u **arg,
5620 typval_T *rettv, /* when NULL, only check if option exists */
5621 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622{
5623 char_u *option_end;
5624 long numval;
5625 char_u *stringval;
5626 int opt_type;
5627 int c;
5628 int working = (**arg == '+'); /* has("+option") */
5629 int ret = OK;
5630 int opt_flags;
5631
5632 /*
5633 * Isolate the option name and find its value.
5634 */
5635 option_end = find_option_end(arg, &opt_flags);
5636 if (option_end == NULL)
5637 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005638 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 EMSG2(_("E112: Option name missing: %s"), *arg);
5640 return FAIL;
5641 }
5642
5643 if (!evaluate)
5644 {
5645 *arg = option_end;
5646 return OK;
5647 }
5648
5649 c = *option_end;
5650 *option_end = NUL;
5651 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005652 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653
5654 if (opt_type == -3) /* invalid name */
5655 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005656 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 EMSG2(_("E113: Unknown option: %s"), *arg);
5658 ret = FAIL;
5659 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005660 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 {
5662 if (opt_type == -2) /* hidden string option */
5663 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005664 rettv->v_type = VAR_STRING;
5665 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 }
5667 else if (opt_type == -1) /* hidden number option */
5668 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005669 rettv->v_type = VAR_NUMBER;
5670 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 }
5672 else if (opt_type == 1) /* number option */
5673 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005674 rettv->v_type = VAR_NUMBER;
5675 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 }
5677 else /* string option */
5678 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005679 rettv->v_type = VAR_STRING;
5680 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 }
5682 }
5683 else if (working && (opt_type == -2 || opt_type == -1))
5684 ret = FAIL;
5685
5686 *option_end = c; /* put back for error messages */
5687 *arg = option_end;
5688
5689 return ret;
5690}
5691
5692/*
5693 * Allocate a variable for a string constant.
5694 * Return OK or FAIL.
5695 */
5696 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005697get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698{
5699 char_u *p;
5700 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 int extra = 0;
5702
5703 /*
5704 * Find the end of the string, skipping backslashed characters.
5705 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005706 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 {
5708 if (*p == '\\' && p[1] != NUL)
5709 {
5710 ++p;
5711 /* A "\<x>" form occupies at least 4 characters, and produces up
5712 * to 6 characters: reserve space for 2 extra */
5713 if (*p == '<')
5714 extra += 2;
5715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 }
5717
5718 if (*p != '"')
5719 {
5720 EMSG2(_("E114: Missing quote: %s"), *arg);
5721 return FAIL;
5722 }
5723
5724 /* If only parsing, set *arg and return here */
5725 if (!evaluate)
5726 {
5727 *arg = p + 1;
5728 return OK;
5729 }
5730
5731 /*
5732 * Copy the string into allocated memory, handling backslashed
5733 * characters.
5734 */
5735 name = alloc((unsigned)(p - *arg + extra));
5736 if (name == NULL)
5737 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 rettv->v_type = VAR_STRING;
5739 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740
Bram Moolenaar8c711452005-01-14 21:53:12 +00005741 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 {
5743 if (*p == '\\')
5744 {
5745 switch (*++p)
5746 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005747 case 'b': *name++ = BS; ++p; break;
5748 case 'e': *name++ = ESC; ++p; break;
5749 case 'f': *name++ = FF; ++p; break;
5750 case 'n': *name++ = NL; ++p; break;
5751 case 'r': *name++ = CAR; ++p; break;
5752 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753
5754 case 'X': /* hex: "\x1", "\x12" */
5755 case 'x':
5756 case 'u': /* Unicode: "\u0023" */
5757 case 'U':
5758 if (vim_isxdigit(p[1]))
5759 {
5760 int n, nr;
5761 int c = toupper(*p);
5762
5763 if (c == 'X')
5764 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005765 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005767 else
5768 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 nr = 0;
5770 while (--n >= 0 && vim_isxdigit(p[1]))
5771 {
5772 ++p;
5773 nr = (nr << 4) + hex2nr(*p);
5774 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776#ifdef FEAT_MBYTE
5777 /* For "\u" store the number according to
5778 * 'encoding'. */
5779 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005780 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 else
5782#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005783 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 break;
5786
5787 /* octal: "\1", "\12", "\123" */
5788 case '0':
5789 case '1':
5790 case '2':
5791 case '3':
5792 case '4':
5793 case '5':
5794 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 case '7': *name = *p++ - '0';
5796 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 *name = (*name << 3) + *p++ - '0';
5799 if (*p >= '0' && *p <= '7')
5800 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 break;
5804
5805 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005806 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 if (extra != 0)
5808 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005809 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 break;
5811 }
5812 /* FALLTHROUGH */
5813
Bram Moolenaar8c711452005-01-14 21:53:12 +00005814 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 break;
5816 }
5817 }
5818 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005819 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005822 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 *arg = p + 1;
5824
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 return OK;
5826}
5827
5828/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005829 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 * Return OK or FAIL.
5831 */
5832 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005833get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834{
5835 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005836 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005837 int reduce = 0;
5838
5839 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005840 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005841 */
5842 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5843 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005844 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005845 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005847 break;
5848 ++reduce;
5849 ++p;
5850 }
5851 }
5852
Bram Moolenaar8c711452005-01-14 21:53:12 +00005853 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005854 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005855 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005856 return FAIL;
5857 }
5858
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005860 if (!evaluate)
5861 {
5862 *arg = p + 1;
5863 return OK;
5864 }
5865
5866 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005867 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005868 */
5869 str = alloc((unsigned)((p - *arg) - reduce));
5870 if (str == NULL)
5871 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005872 rettv->v_type = VAR_STRING;
5873 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005874
Bram Moolenaar8c711452005-01-14 21:53:12 +00005875 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005876 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005877 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005878 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005879 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005880 break;
5881 ++p;
5882 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005883 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005884 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005885 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005886 *arg = p + 1;
5887
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005888 return OK;
5889}
5890
5891/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005892 * Allocate a variable for a List and fill it from "*arg".
5893 * Return OK or FAIL.
5894 */
5895 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005896get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005897{
Bram Moolenaar33570922005-01-25 22:26:29 +00005898 list_T *l = NULL;
5899 typval_T tv;
5900 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901
5902 if (evaluate)
5903 {
5904 l = list_alloc();
5905 if (l == NULL)
5906 return FAIL;
5907 }
5908
5909 *arg = skipwhite(*arg + 1);
5910 while (**arg != ']' && **arg != NUL)
5911 {
5912 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5913 goto failret;
5914 if (evaluate)
5915 {
5916 item = listitem_alloc();
5917 if (item != NULL)
5918 {
5919 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005920 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921 list_append(l, item);
5922 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005923 else
5924 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925 }
5926
5927 if (**arg == ']')
5928 break;
5929 if (**arg != ',')
5930 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005931 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005932 goto failret;
5933 }
5934 *arg = skipwhite(*arg + 1);
5935 }
5936
5937 if (**arg != ']')
5938 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005939 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940failret:
5941 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005942 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005943 return FAIL;
5944 }
5945
5946 *arg = skipwhite(*arg + 1);
5947 if (evaluate)
5948 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005949 rettv->v_type = VAR_LIST;
5950 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951 ++l->lv_refcount;
5952 }
5953
5954 return OK;
5955}
5956
5957/*
5958 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005959 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005961 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005962list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005963{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005964 list_T *l;
5965
5966 l = (list_T *)alloc_clear(sizeof(list_T));
5967 if (l != NULL)
5968 {
5969 /* Prepend the list to the list of lists for garbage collection. */
5970 if (first_list != NULL)
5971 first_list->lv_used_prev = l;
5972 l->lv_used_prev = NULL;
5973 l->lv_used_next = first_list;
5974 first_list = l;
5975 }
5976 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977}
5978
5979/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005980 * Allocate an empty list for a return value.
5981 * Returns OK or FAIL.
5982 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005983 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005984rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005985{
5986 list_T *l = list_alloc();
5987
5988 if (l == NULL)
5989 return FAIL;
5990
5991 rettv->vval.v_list = l;
5992 rettv->v_type = VAR_LIST;
5993 ++l->lv_refcount;
5994 return OK;
5995}
5996
5997/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005998 * Unreference a list: decrement the reference count and free it when it
5999 * becomes zero.
6000 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006001 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006002list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006003{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006004 if (l != NULL && --l->lv_refcount <= 0)
6005 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006006}
6007
6008/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006009 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006010 * Ignores the reference count.
6011 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006012 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006013list_free(
6014 list_T *l,
6015 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006016{
Bram Moolenaar33570922005-01-25 22:26:29 +00006017 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006019 /* Remove the list from the list of lists for garbage collection. */
6020 if (l->lv_used_prev == NULL)
6021 first_list = l->lv_used_next;
6022 else
6023 l->lv_used_prev->lv_used_next = l->lv_used_next;
6024 if (l->lv_used_next != NULL)
6025 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6026
Bram Moolenaard9fba312005-06-26 22:34:35 +00006027 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006028 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006029 /* Remove the item before deleting it. */
6030 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006031 if (recurse || (item->li_tv.v_type != VAR_LIST
6032 && item->li_tv.v_type != VAR_DICT))
6033 clear_tv(&item->li_tv);
6034 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035 }
6036 vim_free(l);
6037}
6038
6039/*
6040 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006041 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006043 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006044listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006045{
Bram Moolenaar33570922005-01-25 22:26:29 +00006046 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006047}
6048
6049/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006050 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006051 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006052 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006053listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006055 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056 vim_free(item);
6057}
6058
6059/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006060 * Remove a list item from a List and free it. Also clears the value.
6061 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006062 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006063listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006064{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006065 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006066 listitem_free(item);
6067}
6068
6069/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006070 * Get the number of items in a list.
6071 */
6072 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006073list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006074{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006075 if (l == NULL)
6076 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006077 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006078}
6079
6080/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006081 * Return TRUE when two lists have exactly the same values.
6082 */
6083 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006084list_equal(
6085 list_T *l1,
6086 list_T *l2,
6087 int ic, /* ignore case for strings */
6088 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006089{
Bram Moolenaar33570922005-01-25 22:26:29 +00006090 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006091
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006092 if (l1 == NULL || l2 == NULL)
6093 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006094 if (l1 == l2)
6095 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006096 if (list_len(l1) != list_len(l2))
6097 return FALSE;
6098
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006099 for (item1 = l1->lv_first, item2 = l2->lv_first;
6100 item1 != NULL && item2 != NULL;
6101 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006102 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006103 return FALSE;
6104 return item1 == NULL && item2 == NULL;
6105}
6106
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006107/*
6108 * Return the dictitem that an entry in a hashtable points to.
6109 */
6110 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006111dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006112{
6113 return HI2DI(hi);
6114}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006115
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006116/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006117 * Return TRUE when two dictionaries have exactly the same key/values.
6118 */
6119 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006120dict_equal(
6121 dict_T *d1,
6122 dict_T *d2,
6123 int ic, /* ignore case for strings */
6124 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006125{
Bram Moolenaar33570922005-01-25 22:26:29 +00006126 hashitem_T *hi;
6127 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006128 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006129
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006130 if (d1 == NULL || d2 == NULL)
6131 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006132 if (d1 == d2)
6133 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006134 if (dict_len(d1) != dict_len(d2))
6135 return FALSE;
6136
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006137 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006138 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006139 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006140 if (!HASHITEM_EMPTY(hi))
6141 {
6142 item2 = dict_find(d2, hi->hi_key, -1);
6143 if (item2 == NULL)
6144 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006145 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006146 return FALSE;
6147 --todo;
6148 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006149 }
6150 return TRUE;
6151}
6152
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006153static int tv_equal_recurse_limit;
6154
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006155/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006156 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006157 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006158 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006159 */
6160 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006161tv_equal(
6162 typval_T *tv1,
6163 typval_T *tv2,
6164 int ic, /* ignore case */
6165 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006166{
6167 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006168 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006169 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006170 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006171
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006172 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006173 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006174
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006175 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006176 * recursiveness to a limit. We guess they are equal then.
6177 * A fixed limit has the problem of still taking an awful long time.
6178 * Reduce the limit every time running into it. That should work fine for
6179 * deeply linked structures that are not recursively linked and catch
6180 * recursiveness quickly. */
6181 if (!recursive)
6182 tv_equal_recurse_limit = 1000;
6183 if (recursive_cnt >= tv_equal_recurse_limit)
6184 {
6185 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006186 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006187 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006188
6189 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006190 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006191 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006192 ++recursive_cnt;
6193 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6194 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006195 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006196
6197 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006198 ++recursive_cnt;
6199 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6200 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006201 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006202
6203 case VAR_FUNC:
6204 return (tv1->vval.v_string != NULL
6205 && tv2->vval.v_string != NULL
6206 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6207
6208 case VAR_NUMBER:
6209 return tv1->vval.v_number == tv2->vval.v_number;
6210
6211 case VAR_STRING:
6212 s1 = get_tv_string_buf(tv1, buf1);
6213 s2 = get_tv_string_buf(tv2, buf2);
6214 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006215
6216 case VAR_SPECIAL:
6217 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006218
6219 case VAR_FLOAT:
6220#ifdef FEAT_FLOAT
6221 return tv1->vval.v_float == tv2->vval.v_float;
6222#endif
6223 case VAR_JOB:
6224#ifdef FEAT_JOB
6225 return tv1->vval.v_job == tv2->vval.v_job;
6226#endif
6227 case VAR_UNKNOWN:
6228 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006229 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006230
Bram Moolenaara03f2332016-02-06 18:09:59 +01006231 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6232 * does not equal anything, not even itself. */
6233 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006234}
6235
6236/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006237 * Locate item with index "n" in list "l" and return it.
6238 * A negative index is counted from the end; -1 is the last item.
6239 * Returns NULL when "n" is out of range.
6240 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006241 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006242list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006243{
Bram Moolenaar33570922005-01-25 22:26:29 +00006244 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006245 long idx;
6246
6247 if (l == NULL)
6248 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006249
6250 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006251 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006252 n = l->lv_len + n;
6253
6254 /* Check for index out of range. */
6255 if (n < 0 || n >= l->lv_len)
6256 return NULL;
6257
6258 /* When there is a cached index may start search from there. */
6259 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006260 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006261 if (n < l->lv_idx / 2)
6262 {
6263 /* closest to the start of the list */
6264 item = l->lv_first;
6265 idx = 0;
6266 }
6267 else if (n > (l->lv_idx + l->lv_len) / 2)
6268 {
6269 /* closest to the end of the list */
6270 item = l->lv_last;
6271 idx = l->lv_len - 1;
6272 }
6273 else
6274 {
6275 /* closest to the cached index */
6276 item = l->lv_idx_item;
6277 idx = l->lv_idx;
6278 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006279 }
6280 else
6281 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006282 if (n < l->lv_len / 2)
6283 {
6284 /* closest to the start of the list */
6285 item = l->lv_first;
6286 idx = 0;
6287 }
6288 else
6289 {
6290 /* closest to the end of the list */
6291 item = l->lv_last;
6292 idx = l->lv_len - 1;
6293 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006294 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006295
6296 while (n > idx)
6297 {
6298 /* search forward */
6299 item = item->li_next;
6300 ++idx;
6301 }
6302 while (n < idx)
6303 {
6304 /* search backward */
6305 item = item->li_prev;
6306 --idx;
6307 }
6308
6309 /* cache the used index */
6310 l->lv_idx = idx;
6311 l->lv_idx_item = item;
6312
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006313 return item;
6314}
6315
6316/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006317 * Get list item "l[idx]" as a number.
6318 */
6319 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006320list_find_nr(
6321 list_T *l,
6322 long idx,
6323 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006324{
6325 listitem_T *li;
6326
6327 li = list_find(l, idx);
6328 if (li == NULL)
6329 {
6330 if (errorp != NULL)
6331 *errorp = TRUE;
6332 return -1L;
6333 }
6334 return get_tv_number_chk(&li->li_tv, errorp);
6335}
6336
6337/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006338 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6339 */
6340 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006341list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006342{
6343 listitem_T *li;
6344
6345 li = list_find(l, idx - 1);
6346 if (li == NULL)
6347 {
6348 EMSGN(_(e_listidx), idx);
6349 return NULL;
6350 }
6351 return get_tv_string(&li->li_tv);
6352}
6353
6354/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006355 * Locate "item" list "l" and return its index.
6356 * Returns -1 when "item" is not in the list.
6357 */
6358 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006359list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006360{
6361 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006362 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006363
6364 if (l == NULL)
6365 return -1;
6366 idx = 0;
6367 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6368 ++idx;
6369 if (li == NULL)
6370 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006371 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006372}
6373
6374/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006375 * Append item "item" to the end of list "l".
6376 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006377 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006378list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006379{
6380 if (l->lv_last == NULL)
6381 {
6382 /* empty list */
6383 l->lv_first = item;
6384 l->lv_last = item;
6385 item->li_prev = NULL;
6386 }
6387 else
6388 {
6389 l->lv_last->li_next = item;
6390 item->li_prev = l->lv_last;
6391 l->lv_last = item;
6392 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006393 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006394 item->li_next = NULL;
6395}
6396
6397/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006398 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006399 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006400 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006401 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006402list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006403{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006404 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006405
Bram Moolenaar05159a02005-02-26 23:04:13 +00006406 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006407 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006408 copy_tv(tv, &li->li_tv);
6409 list_append(l, li);
6410 return OK;
6411}
6412
6413/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006414 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006415 * Return FAIL when out of memory.
6416 */
6417 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006418list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006419{
6420 listitem_T *li = listitem_alloc();
6421
6422 if (li == NULL)
6423 return FAIL;
6424 li->li_tv.v_type = VAR_DICT;
6425 li->li_tv.v_lock = 0;
6426 li->li_tv.vval.v_dict = dict;
6427 list_append(list, li);
6428 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006429 return OK;
6430}
6431
6432/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006433 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006434 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006435 * Returns FAIL when out of memory.
6436 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006437 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006438list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006439{
6440 listitem_T *li = listitem_alloc();
6441
6442 if (li == NULL)
6443 return FAIL;
6444 list_append(l, li);
6445 li->li_tv.v_type = VAR_STRING;
6446 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006447 if (str == NULL)
6448 li->li_tv.vval.v_string = NULL;
6449 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006450 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006451 return FAIL;
6452 return OK;
6453}
6454
6455/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006456 * Append "n" to list "l".
6457 * Returns FAIL when out of memory.
6458 */
6459 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006460list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006461{
6462 listitem_T *li;
6463
6464 li = listitem_alloc();
6465 if (li == NULL)
6466 return FAIL;
6467 li->li_tv.v_type = VAR_NUMBER;
6468 li->li_tv.v_lock = 0;
6469 li->li_tv.vval.v_number = n;
6470 list_append(l, li);
6471 return OK;
6472}
6473
6474/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006475 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006476 * If "item" is NULL append at the end.
6477 * Return FAIL when out of memory.
6478 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006479 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006480list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006481{
Bram Moolenaar33570922005-01-25 22:26:29 +00006482 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006483
6484 if (ni == NULL)
6485 return FAIL;
6486 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006487 list_insert(l, ni, item);
6488 return OK;
6489}
6490
6491 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006492list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006493{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006494 if (item == NULL)
6495 /* Append new item at end of list. */
6496 list_append(l, ni);
6497 else
6498 {
6499 /* Insert new item before existing item. */
6500 ni->li_prev = item->li_prev;
6501 ni->li_next = item;
6502 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006503 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006504 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006505 ++l->lv_idx;
6506 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006507 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006508 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006509 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006510 l->lv_idx_item = NULL;
6511 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006512 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006513 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006514 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006515}
6516
6517/*
6518 * Extend "l1" with "l2".
6519 * If "bef" is NULL append at the end, otherwise insert before this item.
6520 * Returns FAIL when out of memory.
6521 */
6522 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006523list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006524{
Bram Moolenaar33570922005-01-25 22:26:29 +00006525 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006526 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006528 /* We also quit the loop when we have inserted the original item count of
6529 * the list, avoid a hang when we extend a list with itself. */
6530 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006531 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6532 return FAIL;
6533 return OK;
6534}
6535
6536/*
6537 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6538 * Return FAIL when out of memory.
6539 */
6540 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006541list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542{
Bram Moolenaar33570922005-01-25 22:26:29 +00006543 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006544
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006545 if (l1 == NULL || l2 == NULL)
6546 return FAIL;
6547
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006548 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006549 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006550 if (l == NULL)
6551 return FAIL;
6552 tv->v_type = VAR_LIST;
6553 tv->vval.v_list = l;
6554
6555 /* append all items from the second list */
6556 return list_extend(l, l2, NULL);
6557}
6558
6559/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006560 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006561 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006562 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006563 * Returns NULL when out of memory.
6564 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006565 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006566list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006567{
Bram Moolenaar33570922005-01-25 22:26:29 +00006568 list_T *copy;
6569 listitem_T *item;
6570 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006571
6572 if (orig == NULL)
6573 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006574
6575 copy = list_alloc();
6576 if (copy != NULL)
6577 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006578 if (copyID != 0)
6579 {
6580 /* Do this before adding the items, because one of the items may
6581 * refer back to this list. */
6582 orig->lv_copyID = copyID;
6583 orig->lv_copylist = copy;
6584 }
6585 for (item = orig->lv_first; item != NULL && !got_int;
6586 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006587 {
6588 ni = listitem_alloc();
6589 if (ni == NULL)
6590 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006591 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006592 {
6593 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6594 {
6595 vim_free(ni);
6596 break;
6597 }
6598 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006599 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006600 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006601 list_append(copy, ni);
6602 }
6603 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006604 if (item != NULL)
6605 {
6606 list_unref(copy);
6607 copy = NULL;
6608 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006609 }
6610
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006611 return copy;
6612}
6613
6614/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006615 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006616 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006617 * This used to be called list_remove, but that conflicts with a Sun header
6618 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006620 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006621vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006622{
Bram Moolenaar33570922005-01-25 22:26:29 +00006623 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006624
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006625 /* notify watchers */
6626 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006627 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006628 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006629 list_fix_watch(l, ip);
6630 if (ip == item2)
6631 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006632 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006633
6634 if (item2->li_next == NULL)
6635 l->lv_last = item->li_prev;
6636 else
6637 item2->li_next->li_prev = item->li_prev;
6638 if (item->li_prev == NULL)
6639 l->lv_first = item2->li_next;
6640 else
6641 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006642 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006643}
6644
6645/*
6646 * Return an allocated string with the string representation of a list.
6647 * May return NULL.
6648 */
6649 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006650list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006651{
6652 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006653
6654 if (tv->vval.v_list == NULL)
6655 return NULL;
6656 ga_init2(&ga, (int)sizeof(char), 80);
6657 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006658 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006659 {
6660 vim_free(ga.ga_data);
6661 return NULL;
6662 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006663 ga_append(&ga, ']');
6664 ga_append(&ga, NUL);
6665 return (char_u *)ga.ga_data;
6666}
6667
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006668typedef struct join_S {
6669 char_u *s;
6670 char_u *tofree;
6671} join_T;
6672
6673 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006674list_join_inner(
6675 garray_T *gap, /* to store the result in */
6676 list_T *l,
6677 char_u *sep,
6678 int echo_style,
6679 int copyID,
6680 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006681{
6682 int i;
6683 join_T *p;
6684 int len;
6685 int sumlen = 0;
6686 int first = TRUE;
6687 char_u *tofree;
6688 char_u numbuf[NUMBUFLEN];
6689 listitem_T *item;
6690 char_u *s;
6691
6692 /* Stringify each item in the list. */
6693 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6694 {
6695 if (echo_style)
6696 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6697 else
6698 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6699 if (s == NULL)
6700 return FAIL;
6701
6702 len = (int)STRLEN(s);
6703 sumlen += len;
6704
Bram Moolenaarcde88542015-08-11 19:14:00 +02006705 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006706 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6707 if (tofree != NULL || s != numbuf)
6708 {
6709 p->s = s;
6710 p->tofree = tofree;
6711 }
6712 else
6713 {
6714 p->s = vim_strnsave(s, len);
6715 p->tofree = p->s;
6716 }
6717
6718 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006719 if (did_echo_string_emsg) /* recursion error, bail out */
6720 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006721 }
6722
6723 /* Allocate result buffer with its total size, avoid re-allocation and
6724 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6725 if (join_gap->ga_len >= 2)
6726 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6727 if (ga_grow(gap, sumlen + 2) == FAIL)
6728 return FAIL;
6729
6730 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6731 {
6732 if (first)
6733 first = FALSE;
6734 else
6735 ga_concat(gap, sep);
6736 p = ((join_T *)join_gap->ga_data) + i;
6737
6738 if (p->s != NULL)
6739 ga_concat(gap, p->s);
6740 line_breakcheck();
6741 }
6742
6743 return OK;
6744}
6745
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006746/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006747 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006748 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006749 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006750 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006751 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006752list_join(
6753 garray_T *gap,
6754 list_T *l,
6755 char_u *sep,
6756 int echo_style,
6757 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006758{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006759 garray_T join_ga;
6760 int retval;
6761 join_T *p;
6762 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006763
Bram Moolenaard39a7512015-04-16 22:51:22 +02006764 if (l->lv_len < 1)
6765 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006766 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6767 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6768
6769 /* Dispose each item in join_ga. */
6770 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006771 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006772 p = (join_T *)join_ga.ga_data;
6773 for (i = 0; i < join_ga.ga_len; ++i)
6774 {
6775 vim_free(p->tofree);
6776 ++p;
6777 }
6778 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006779 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006780
6781 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006782}
6783
6784/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006785 * Return the next (unique) copy ID.
6786 * Used for serializing nested structures.
6787 */
6788 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006789get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006790{
6791 current_copyID += COPYID_INC;
6792 return current_copyID;
6793}
6794
6795/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006796 * Garbage collection for lists and dictionaries.
6797 *
6798 * We use reference counts to be able to free most items right away when they
6799 * are no longer used. But for composite items it's possible that it becomes
6800 * unused while the reference count is > 0: When there is a recursive
6801 * reference. Example:
6802 * :let l = [1, 2, 3]
6803 * :let d = {9: l}
6804 * :let l[1] = d
6805 *
6806 * Since this is quite unusual we handle this with garbage collection: every
6807 * once in a while find out which lists and dicts are not referenced from any
6808 * variable.
6809 *
6810 * Here is a good reference text about garbage collection (refers to Python
6811 * but it applies to all reference-counting mechanisms):
6812 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006813 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006814
6815/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006816 * Do garbage collection for lists and dicts.
6817 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006818 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006819 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006820garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006821{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006822 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006823 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006824 buf_T *buf;
6825 win_T *wp;
6826 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006827 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006828 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006829 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006830#ifdef FEAT_WINDOWS
6831 tabpage_T *tp;
6832#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006833
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006834 /* Only do this once. */
6835 want_garbage_collect = FALSE;
6836 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006837 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006838
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006839 /* We advance by two because we add one for items referenced through
6840 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006841 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006842
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006843 /*
6844 * 1. Go through all accessible variables and mark all lists and dicts
6845 * with copyID.
6846 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006847
6848 /* Don't free variables in the previous_funccal list unless they are only
6849 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006850 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006851 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6852 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006853 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6854 NULL);
6855 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6856 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006857 }
6858
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006859 /* script-local variables */
6860 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006861 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006862
6863 /* buffer-local variables */
6864 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006865 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6866 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006867
6868 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006869 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006870 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6871 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006872#ifdef FEAT_AUTOCMD
6873 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006874 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6875 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006876#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006877
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006878#ifdef FEAT_WINDOWS
6879 /* tabpage-local variables */
6880 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006881 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6882 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006883#endif
6884
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006885 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006886 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006887
6888 /* function-local variables */
6889 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6890 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006891 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6892 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006893 }
6894
Bram Moolenaard812df62008-11-09 12:46:09 +00006895 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006896 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006897
Bram Moolenaar1dced572012-04-05 16:54:08 +02006898#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006899 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006900#endif
6901
Bram Moolenaardb913952012-06-29 12:54:53 +02006902#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006903 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006904#endif
6905
6906#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006907 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006908#endif
6909
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006910#ifdef FEAT_CHANNEL
6911 abort = abort || set_ref_in_channel(copyID);
6912#endif
6913
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006914 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006915 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006916 /*
6917 * 2. Free lists and dictionaries that are not referenced.
6918 */
6919 did_free = free_unref_items(copyID);
6920
6921 /*
6922 * 3. Check if any funccal can be freed now.
6923 */
6924 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006925 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006926 if (can_free_funccal(*pfc, copyID))
6927 {
6928 fc = *pfc;
6929 *pfc = fc->caller;
6930 free_funccal(fc, TRUE);
6931 did_free = TRUE;
6932 did_free_funccal = TRUE;
6933 }
6934 else
6935 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006936 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006937 if (did_free_funccal)
6938 /* When a funccal was freed some more items might be garbage
6939 * collected, so run again. */
6940 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006941 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006942 else if (p_verbose > 0)
6943 {
6944 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6945 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006946
6947 return did_free;
6948}
6949
6950/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006951 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006952 */
6953 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006954free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006955{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006956 dict_T *dd, *dd_next;
6957 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006958 int did_free = FALSE;
6959
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006960 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006961 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006962 */
6963 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006964 {
6965 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006966 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006967 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006968 /* Free the Dictionary and ordinary items it contains, but don't
6969 * recurse into Lists and Dictionaries, they will be in the list
6970 * of dicts or list of lists. */
6971 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006972 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006973 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006974 dd = dd_next;
6975 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006976
6977 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006978 * Go through the list of lists and free items without the copyID.
6979 * But don't free a list that has a watcher (used in a for loop), these
6980 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006981 */
6982 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006983 {
6984 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006985 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6986 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006987 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006988 /* Free the List and ordinary items it contains, but don't recurse
6989 * into Lists and Dictionaries, they will be in the list of dicts
6990 * or list of lists. */
6991 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006994 ll = ll_next;
6995 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01006996
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006997 return did_free;
6998}
6999
7000/*
7001 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007002 * "list_stack" is used to add lists to be marked. Can be NULL.
7003 *
7004 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007006 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007007set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008{
7009 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007010 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007012 hashtab_T *cur_ht;
7013 ht_stack_T *ht_stack = NULL;
7014 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007015
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007016 cur_ht = ht;
7017 for (;;)
7018 {
7019 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007020 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007021 /* Mark each item in the hashtab. If the item contains a hashtab
7022 * it is added to ht_stack, if it contains a list it is added to
7023 * list_stack. */
7024 todo = (int)cur_ht->ht_used;
7025 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7026 if (!HASHITEM_EMPTY(hi))
7027 {
7028 --todo;
7029 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7030 &ht_stack, list_stack);
7031 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007032 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007033
7034 if (ht_stack == NULL)
7035 break;
7036
7037 /* take an item from the stack */
7038 cur_ht = ht_stack->ht;
7039 tempitem = ht_stack;
7040 ht_stack = ht_stack->prev;
7041 free(tempitem);
7042 }
7043
7044 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007045}
7046
7047/*
7048 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007049 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7050 *
7051 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007052 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007053 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007054set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007055{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007056 listitem_T *li;
7057 int abort = FALSE;
7058 list_T *cur_l;
7059 list_stack_T *list_stack = NULL;
7060 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007061
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007062 cur_l = l;
7063 for (;;)
7064 {
7065 if (!abort)
7066 /* Mark each item in the list. If the item contains a hashtab
7067 * it is added to ht_stack, if it contains a list it is added to
7068 * list_stack. */
7069 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7070 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7071 ht_stack, &list_stack);
7072 if (list_stack == NULL)
7073 break;
7074
7075 /* take an item from the stack */
7076 cur_l = list_stack->list;
7077 tempitem = list_stack;
7078 list_stack = list_stack->prev;
7079 free(tempitem);
7080 }
7081
7082 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007083}
7084
7085/*
7086 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007087 * "list_stack" is used to add lists to be marked. Can be NULL.
7088 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7089 *
7090 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007091 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007092 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007093set_ref_in_item(
7094 typval_T *tv,
7095 int copyID,
7096 ht_stack_T **ht_stack,
7097 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007098{
7099 dict_T *dd;
7100 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007101 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007102
Bram Moolenaara03f2332016-02-06 18:09:59 +01007103 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007104 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007105 dd = tv->vval.v_dict;
7106 if (dd != NULL && dd->dv_copyID != copyID)
7107 {
7108 /* Didn't see this dict yet. */
7109 dd->dv_copyID = copyID;
7110 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007111 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007112 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7113 }
7114 else
7115 {
7116 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7117 if (newitem == NULL)
7118 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007119 else
7120 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007121 newitem->ht = &dd->dv_hashtab;
7122 newitem->prev = *ht_stack;
7123 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007124 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007125 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007126 }
7127 }
7128 else if (tv->v_type == VAR_LIST)
7129 {
7130 ll = tv->vval.v_list;
7131 if (ll != NULL && ll->lv_copyID != copyID)
7132 {
7133 /* Didn't see this list yet. */
7134 ll->lv_copyID = copyID;
7135 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007136 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007137 abort = set_ref_in_list(ll, copyID, ht_stack);
7138 }
7139 else
7140 {
7141 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007142 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007143 if (newitem == NULL)
7144 abort = TRUE;
7145 else
7146 {
7147 newitem->list = ll;
7148 newitem->prev = *list_stack;
7149 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007150 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007151 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007152 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007153 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007154 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007155}
7156
7157/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007158 * Allocate an empty header for a dictionary.
7159 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007160 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007161dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007162{
Bram Moolenaar33570922005-01-25 22:26:29 +00007163 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007164
Bram Moolenaar33570922005-01-25 22:26:29 +00007165 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007166 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007167 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007168 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007169 if (first_dict != NULL)
7170 first_dict->dv_used_prev = d;
7171 d->dv_used_next = first_dict;
7172 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007173 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007174
Bram Moolenaar33570922005-01-25 22:26:29 +00007175 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007176 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007177 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007178 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007179 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007180 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007181 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007182}
7183
7184/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007185 * Allocate an empty dict for a return value.
7186 * Returns OK or FAIL.
7187 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007188 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007189rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007190{
7191 dict_T *d = dict_alloc();
7192
7193 if (d == NULL)
7194 return FAIL;
7195
7196 rettv->vval.v_dict = d;
7197 rettv->v_type = VAR_DICT;
7198 ++d->dv_refcount;
7199 return OK;
7200}
7201
7202
7203/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007204 * Unreference a Dictionary: decrement the reference count and free it when it
7205 * becomes zero.
7206 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007207 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007208dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007209{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007210 if (d != NULL && --d->dv_refcount <= 0)
7211 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007212}
7213
7214/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007215 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007216 * Ignores the reference count.
7217 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007218 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007219dict_free(
7220 dict_T *d,
7221 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007223 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007224 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007225 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007226
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007227 /* Remove the dict from the list of dicts for garbage collection. */
7228 if (d->dv_used_prev == NULL)
7229 first_dict = d->dv_used_next;
7230 else
7231 d->dv_used_prev->dv_used_next = d->dv_used_next;
7232 if (d->dv_used_next != NULL)
7233 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7234
7235 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007236 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007237 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007238 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007239 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007240 if (!HASHITEM_EMPTY(hi))
7241 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007242 /* Remove the item before deleting it, just in case there is
7243 * something recursive causing trouble. */
7244 di = HI2DI(hi);
7245 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007246 if (recurse || (di->di_tv.v_type != VAR_LIST
7247 && di->di_tv.v_type != VAR_DICT))
7248 clear_tv(&di->di_tv);
7249 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007250 --todo;
7251 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007253 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007254 vim_free(d);
7255}
7256
7257/*
7258 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007259 * The "key" is copied to the new item.
7260 * Note that the value of the item "di_tv" still needs to be initialized!
7261 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007262 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007263 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007264dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265{
Bram Moolenaar33570922005-01-25 22:26:29 +00007266 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007267
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007268 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007269 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007270 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007271 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007272 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007273 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007274 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007275}
7276
7277/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007278 * Make a copy of a Dictionary item.
7279 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007280 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007281dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007282{
Bram Moolenaar33570922005-01-25 22:26:29 +00007283 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007284
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007285 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7286 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007287 if (di != NULL)
7288 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007289 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007290 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007291 copy_tv(&org->di_tv, &di->di_tv);
7292 }
7293 return di;
7294}
7295
7296/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007297 * Remove item "item" from Dictionary "dict" and free it.
7298 */
7299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007300dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007301{
Bram Moolenaar33570922005-01-25 22:26:29 +00007302 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007303
Bram Moolenaar33570922005-01-25 22:26:29 +00007304 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007305 if (HASHITEM_EMPTY(hi))
7306 EMSG2(_(e_intern2), "dictitem_remove()");
7307 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007308 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007309 dictitem_free(item);
7310}
7311
7312/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007313 * Free a dict item. Also clears the value.
7314 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007315 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007316dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007317{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007318 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007319 if (item->di_flags & DI_FLAGS_ALLOC)
7320 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007321}
7322
7323/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007324 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7325 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007326 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007327 * Returns NULL when out of memory.
7328 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007329 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007330dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007331{
Bram Moolenaar33570922005-01-25 22:26:29 +00007332 dict_T *copy;
7333 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007334 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007335 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007336
7337 if (orig == NULL)
7338 return NULL;
7339
7340 copy = dict_alloc();
7341 if (copy != NULL)
7342 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007343 if (copyID != 0)
7344 {
7345 orig->dv_copyID = copyID;
7346 orig->dv_copydict = copy;
7347 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007348 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007349 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007350 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007351 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007352 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007353 --todo;
7354
7355 di = dictitem_alloc(hi->hi_key);
7356 if (di == NULL)
7357 break;
7358 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007359 {
7360 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7361 copyID) == FAIL)
7362 {
7363 vim_free(di);
7364 break;
7365 }
7366 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007367 else
7368 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7369 if (dict_add(copy, di) == FAIL)
7370 {
7371 dictitem_free(di);
7372 break;
7373 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007374 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007375 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007376
Bram Moolenaare9a41262005-01-15 22:18:47 +00007377 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007378 if (todo > 0)
7379 {
7380 dict_unref(copy);
7381 copy = NULL;
7382 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007383 }
7384
7385 return copy;
7386}
7387
7388/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007389 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007390 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007391 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007392 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007393dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007394{
Bram Moolenaar33570922005-01-25 22:26:29 +00007395 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007396}
7397
Bram Moolenaar8c711452005-01-14 21:53:12 +00007398/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007399 * Add a number or string entry to dictionary "d".
7400 * When "str" is NULL use number "nr", otherwise use "str".
7401 * Returns FAIL when out of memory and when key already exists.
7402 */
7403 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007404dict_add_nr_str(
7405 dict_T *d,
7406 char *key,
7407 long nr,
7408 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007409{
7410 dictitem_T *item;
7411
7412 item = dictitem_alloc((char_u *)key);
7413 if (item == NULL)
7414 return FAIL;
7415 item->di_tv.v_lock = 0;
7416 if (str == NULL)
7417 {
7418 item->di_tv.v_type = VAR_NUMBER;
7419 item->di_tv.vval.v_number = nr;
7420 }
7421 else
7422 {
7423 item->di_tv.v_type = VAR_STRING;
7424 item->di_tv.vval.v_string = vim_strsave(str);
7425 }
7426 if (dict_add(d, item) == FAIL)
7427 {
7428 dictitem_free(item);
7429 return FAIL;
7430 }
7431 return OK;
7432}
7433
7434/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007435 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007436 * Returns FAIL when out of memory and when key already exists.
7437 */
7438 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007439dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007440{
7441 dictitem_T *item;
7442
7443 item = dictitem_alloc((char_u *)key);
7444 if (item == NULL)
7445 return FAIL;
7446 item->di_tv.v_lock = 0;
7447 item->di_tv.v_type = VAR_LIST;
7448 item->di_tv.vval.v_list = list;
7449 if (dict_add(d, item) == FAIL)
7450 {
7451 dictitem_free(item);
7452 return FAIL;
7453 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007454 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007455 return OK;
7456}
7457
7458/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007459 * Get the number of items in a Dictionary.
7460 */
7461 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007462dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007463{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007464 if (d == NULL)
7465 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007466 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007467}
7468
7469/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007470 * Find item "key[len]" in Dictionary "d".
7471 * If "len" is negative use strlen(key).
7472 * Returns NULL when not found.
7473 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007474 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007475dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007476{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007477#define AKEYLEN 200
7478 char_u buf[AKEYLEN];
7479 char_u *akey;
7480 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007481 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007482
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007483 if (len < 0)
7484 akey = key;
7485 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007486 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007487 tofree = akey = vim_strnsave(key, len);
7488 if (akey == NULL)
7489 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007490 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007491 else
7492 {
7493 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007494 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007495 akey = buf;
7496 }
7497
Bram Moolenaar33570922005-01-25 22:26:29 +00007498 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007499 vim_free(tofree);
7500 if (HASHITEM_EMPTY(hi))
7501 return NULL;
7502 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007503}
7504
7505/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007506 * Get a string item from a dictionary.
7507 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007508 * Returns NULL if the entry doesn't exist or out of memory.
7509 */
7510 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007511get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007512{
7513 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007514 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007515
7516 di = dict_find(d, key, -1);
7517 if (di == NULL)
7518 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007519 s = get_tv_string(&di->di_tv);
7520 if (save && s != NULL)
7521 s = vim_strsave(s);
7522 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007523}
7524
7525/*
7526 * Get a number item from a dictionary.
7527 * Returns 0 if the entry doesn't exist or out of memory.
7528 */
7529 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007530get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007531{
7532 dictitem_T *di;
7533
7534 di = dict_find(d, key, -1);
7535 if (di == NULL)
7536 return 0;
7537 return get_tv_number(&di->di_tv);
7538}
7539
7540/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007541 * Return an allocated string with the string representation of a Dictionary.
7542 * May return NULL.
7543 */
7544 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007545dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007546{
7547 garray_T ga;
7548 int first = TRUE;
7549 char_u *tofree;
7550 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007551 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007552 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007553 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007554 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007555
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007556 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007557 return NULL;
7558 ga_init2(&ga, (int)sizeof(char), 80);
7559 ga_append(&ga, '{');
7560
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007561 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007562 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007564 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007565 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007566 --todo;
7567
7568 if (first)
7569 first = FALSE;
7570 else
7571 ga_concat(&ga, (char_u *)", ");
7572
7573 tofree = string_quote(hi->hi_key, FALSE);
7574 if (tofree != NULL)
7575 {
7576 ga_concat(&ga, tofree);
7577 vim_free(tofree);
7578 }
7579 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007580 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007581 if (s != NULL)
7582 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007583 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007584 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007585 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007586 line_breakcheck();
7587
Bram Moolenaar8c711452005-01-14 21:53:12 +00007588 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007589 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007590 if (todo > 0)
7591 {
7592 vim_free(ga.ga_data);
7593 return NULL;
7594 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007595
7596 ga_append(&ga, '}');
7597 ga_append(&ga, NUL);
7598 return (char_u *)ga.ga_data;
7599}
7600
7601/*
7602 * Allocate a variable for a Dictionary and fill it from "*arg".
7603 * Return OK or FAIL. Returns NOTDONE for {expr}.
7604 */
7605 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007606get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007607{
Bram Moolenaar33570922005-01-25 22:26:29 +00007608 dict_T *d = NULL;
7609 typval_T tvkey;
7610 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007611 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007612 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007614 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007615
7616 /*
7617 * First check if it's not a curly-braces thing: {expr}.
7618 * Must do this without evaluating, otherwise a function may be called
7619 * twice. Unfortunately this means we need to call eval1() twice for the
7620 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007621 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007622 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007623 if (*start != '}')
7624 {
7625 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7626 return FAIL;
7627 if (*start == '}')
7628 return NOTDONE;
7629 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007630
7631 if (evaluate)
7632 {
7633 d = dict_alloc();
7634 if (d == NULL)
7635 return FAIL;
7636 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007637 tvkey.v_type = VAR_UNKNOWN;
7638 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007639
7640 *arg = skipwhite(*arg + 1);
7641 while (**arg != '}' && **arg != NUL)
7642 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007643 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007644 goto failret;
7645 if (**arg != ':')
7646 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007647 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007648 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007649 goto failret;
7650 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007651 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007652 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007653 key = get_tv_string_buf_chk(&tvkey, buf);
7654 if (key == NULL || *key == NUL)
7655 {
7656 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7657 if (key != NULL)
7658 EMSG(_(e_emptykey));
7659 clear_tv(&tvkey);
7660 goto failret;
7661 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663
7664 *arg = skipwhite(*arg + 1);
7665 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7666 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007667 if (evaluate)
7668 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007669 goto failret;
7670 }
7671 if (evaluate)
7672 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007673 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007674 if (item != NULL)
7675 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007676 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007677 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007678 clear_tv(&tv);
7679 goto failret;
7680 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007681 item = dictitem_alloc(key);
7682 clear_tv(&tvkey);
7683 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007684 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007685 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007686 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007687 if (dict_add(d, item) == FAIL)
7688 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007689 }
7690 }
7691
7692 if (**arg == '}')
7693 break;
7694 if (**arg != ',')
7695 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007696 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007697 goto failret;
7698 }
7699 *arg = skipwhite(*arg + 1);
7700 }
7701
7702 if (**arg != '}')
7703 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007704 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007705failret:
7706 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007707 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007708 return FAIL;
7709 }
7710
7711 *arg = skipwhite(*arg + 1);
7712 if (evaluate)
7713 {
7714 rettv->v_type = VAR_DICT;
7715 rettv->vval.v_dict = d;
7716 ++d->dv_refcount;
7717 }
7718
7719 return OK;
7720}
7721
Bram Moolenaar835dc632016-02-07 14:27:38 +01007722#ifdef FEAT_JOB
7723 static void
7724job_free(job_T *job)
7725{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01007726 if (job->jv_channel >= 0)
7727 channel_close(job->jv_channel);
Bram Moolenaar76467df2016-02-12 19:30:26 +01007728 mch_clear_job(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007729 vim_free(job);
7730}
7731
7732 static void
7733job_unref(job_T *job)
7734{
7735 if (job != NULL && --job->jv_refcount <= 0)
7736 job_free(job);
7737}
7738
7739/*
7740 * Allocate a job. Sets the refcount to one.
7741 */
7742 static job_T *
7743job_alloc(void)
7744{
7745 job_T *job;
7746
7747 job = (job_T *)alloc_clear(sizeof(job_T));
7748 if (job != NULL)
7749 {
7750 job->jv_refcount = 1;
7751 }
7752 return job;
7753}
7754
7755#endif
7756
Bram Moolenaar17a13432016-01-24 14:22:10 +01007757 static char *
7758get_var_special_name(int nr)
7759{
7760 switch (nr)
7761 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007762 case VVAL_FALSE: return "v:false";
7763 case VVAL_TRUE: return "v:true";
7764 case VVAL_NONE: return "v:none";
7765 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007766 }
7767 EMSG2(_(e_intern2), "get_var_special_name()");
7768 return "42";
7769}
7770
Bram Moolenaar8c711452005-01-14 21:53:12 +00007771/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007772 * Return a string with the string representation of a variable.
7773 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007774 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007775 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007776 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007777 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007778 */
7779 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007780echo_string(
7781 typval_T *tv,
7782 char_u **tofree,
7783 char_u *numbuf,
7784 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007785{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007786 static int recurse = 0;
7787 char_u *r = NULL;
7788
Bram Moolenaar33570922005-01-25 22:26:29 +00007789 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007790 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007791 if (!did_echo_string_emsg)
7792 {
7793 /* Only give this message once for a recursive call to avoid
7794 * flooding the user with errors. And stop iterating over lists
7795 * and dicts. */
7796 did_echo_string_emsg = TRUE;
7797 EMSG(_("E724: variable nested too deep for displaying"));
7798 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007799 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007800 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007801 }
7802 ++recurse;
7803
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007804 switch (tv->v_type)
7805 {
7806 case VAR_FUNC:
7807 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007808 r = tv->vval.v_string;
7809 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007810
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007811 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007812 if (tv->vval.v_list == NULL)
7813 {
7814 *tofree = NULL;
7815 r = NULL;
7816 }
7817 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7818 {
7819 *tofree = NULL;
7820 r = (char_u *)"[...]";
7821 }
7822 else
7823 {
7824 tv->vval.v_list->lv_copyID = copyID;
7825 *tofree = list2string(tv, copyID);
7826 r = *tofree;
7827 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007828 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007829
Bram Moolenaar8c711452005-01-14 21:53:12 +00007830 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007831 if (tv->vval.v_dict == NULL)
7832 {
7833 *tofree = NULL;
7834 r = NULL;
7835 }
7836 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7837 {
7838 *tofree = NULL;
7839 r = (char_u *)"{...}";
7840 }
7841 else
7842 {
7843 tv->vval.v_dict->dv_copyID = copyID;
7844 *tofree = dict2string(tv, copyID);
7845 r = *tofree;
7846 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007847 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007848
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007849 case VAR_STRING:
7850 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007851 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007852 case VAR_JOB:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007853 *tofree = NULL;
7854 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007855 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007856
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007857 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007858#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007859 *tofree = NULL;
7860 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7861 r = numbuf;
7862 break;
7863#endif
7864
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007865 case VAR_SPECIAL:
7866 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007867 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007868 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007869 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007870
Bram Moolenaar8502c702014-06-17 12:51:16 +02007871 if (--recurse == 0)
7872 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007873 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007874}
7875
7876/*
7877 * Return a string with the string representation of a variable.
7878 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7879 * "numbuf" is used for a number.
7880 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007881 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007882 */
7883 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007884tv2string(
7885 typval_T *tv,
7886 char_u **tofree,
7887 char_u *numbuf,
7888 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007889{
7890 switch (tv->v_type)
7891 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007892 case VAR_FUNC:
7893 *tofree = string_quote(tv->vval.v_string, TRUE);
7894 return *tofree;
7895 case VAR_STRING:
7896 *tofree = string_quote(tv->vval.v_string, FALSE);
7897 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007898#ifdef FEAT_FLOAT
7899 case VAR_FLOAT:
7900 *tofree = NULL;
7901 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7902 return numbuf;
7903#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007904 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007905 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007906 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007907 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007908 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007909 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007910 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007911 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007912 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007913}
7914
7915/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007916 * Return string "str" in ' quotes, doubling ' characters.
7917 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007918 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007919 */
7920 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007921string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007922{
Bram Moolenaar33570922005-01-25 22:26:29 +00007923 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007924 char_u *p, *r, *s;
7925
Bram Moolenaar33570922005-01-25 22:26:29 +00007926 len = (function ? 13 : 3);
7927 if (str != NULL)
7928 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007929 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007930 for (p = str; *p != NUL; mb_ptr_adv(p))
7931 if (*p == '\'')
7932 ++len;
7933 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007934 s = r = alloc(len);
7935 if (r != NULL)
7936 {
7937 if (function)
7938 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007939 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007940 r += 10;
7941 }
7942 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007943 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007944 if (str != NULL)
7945 for (p = str; *p != NUL; )
7946 {
7947 if (*p == '\'')
7948 *r++ = '\'';
7949 MB_COPY_CHAR(p, r);
7950 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007951 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007952 if (function)
7953 *r++ = ')';
7954 *r++ = NUL;
7955 }
7956 return s;
7957}
7958
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007959#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007960/*
7961 * Convert the string "text" to a floating point number.
7962 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7963 * this always uses a decimal point.
7964 * Returns the length of the text that was consumed.
7965 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007966 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007967string2float(
7968 char_u *text,
7969 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007970{
7971 char *s = (char *)text;
7972 float_T f;
7973
7974 f = strtod(s, &s);
7975 *value = f;
7976 return (int)((char_u *)s - text);
7977}
7978#endif
7979
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 * Get the value of an environment variable.
7982 * "arg" is pointing to the '$'. It is advanced to after the name.
7983 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007984 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 */
7986 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007987get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988{
7989 char_u *string = NULL;
7990 int len;
7991 int cc;
7992 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007993 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994
7995 ++*arg;
7996 name = *arg;
7997 len = get_env_len(arg);
7998 if (evaluate)
7999 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008000 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008001 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008002
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008003 cc = name[len];
8004 name[len] = NUL;
8005 /* first try vim_getenv(), fast for normal environment vars */
8006 string = vim_getenv(name, &mustfree);
8007 if (string != NULL && *string != NUL)
8008 {
8009 if (!mustfree)
8010 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008012 else
8013 {
8014 if (mustfree)
8015 vim_free(string);
8016
8017 /* next try expanding things like $VIM and ${HOME} */
8018 string = expand_env_save(name - 1);
8019 if (string != NULL && *string == '$')
8020 {
8021 vim_free(string);
8022 string = NULL;
8023 }
8024 }
8025 name[len] = cc;
8026
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008027 rettv->v_type = VAR_STRING;
8028 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 }
8030
8031 return OK;
8032}
8033
8034/*
8035 * Array with names and number of arguments of all internal functions
8036 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8037 */
8038static struct fst
8039{
8040 char *f_name; /* function name */
8041 char f_min_argc; /* minimal number of arguments */
8042 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008043 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008044 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045} functions[] =
8046{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008047#ifdef FEAT_FLOAT
8048 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008049 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008050#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008051 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008052 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008053 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 {"append", 2, 2, f_append},
8055 {"argc", 0, 0, f_argc},
8056 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008057 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008058 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008059#ifdef FEAT_FLOAT
8060 {"asin", 1, 1, f_asin}, /* WJMc */
8061#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008062 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008063 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008064 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008065 {"assert_false", 1, 2, f_assert_false},
8066 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008067#ifdef FEAT_FLOAT
8068 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008069 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008072 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 {"bufexists", 1, 1, f_bufexists},
8074 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8075 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8076 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8077 {"buflisted", 1, 1, f_buflisted},
8078 {"bufloaded", 1, 1, f_bufloaded},
8079 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008080 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 {"bufwinnr", 1, 1, f_bufwinnr},
8082 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008083 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008084 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008085 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008086#ifdef FEAT_FLOAT
8087 {"ceil", 1, 1, f_ceil},
8088#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008089#ifdef FEAT_CHANNEL
8090 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008091 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008092 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008093 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008094 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8095 {"ch_sendraw", 2, 3, f_ch_sendraw},
8096#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008097 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008098 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008100 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008102#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008103 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008104 {"complete_add", 1, 1, f_complete_add},
8105 {"complete_check", 0, 0, f_complete_check},
8106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008108 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008109#ifdef FEAT_FLOAT
8110 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008111 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008112#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008113 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008115 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008116 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008117 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008119 {"diff_filler", 1, 1, f_diff_filler},
8120 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008121 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008122 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008124 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {"eventhandler", 0, 0, f_eventhandler},
8126 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008127 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008129#ifdef FEAT_FLOAT
8130 {"exp", 1, 1, f_exp},
8131#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008132 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008133 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008134 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8136 {"filereadable", 1, 1, f_filereadable},
8137 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008138 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008139 {"finddir", 1, 3, f_finddir},
8140 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008141#ifdef FEAT_FLOAT
8142 {"float2nr", 1, 1, f_float2nr},
8143 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008144 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008145#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008146 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 {"fnamemodify", 2, 2, f_fnamemodify},
8148 {"foldclosed", 1, 1, f_foldclosed},
8149 {"foldclosedend", 1, 1, f_foldclosedend},
8150 {"foldlevel", 1, 1, f_foldlevel},
8151 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008152 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008154 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008155 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008156 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008157 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008158 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {"getchar", 0, 1, f_getchar},
8160 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008161 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {"getcmdline", 0, 0, f_getcmdline},
8163 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008164 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008165 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008166 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008167 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008168 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008169 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 {"getfsize", 1, 1, f_getfsize},
8171 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008172 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008173 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008174 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008175 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008176 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008177 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008178 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008179 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008181 {"gettabvar", 2, 3, f_gettabvar},
8182 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {"getwinposx", 0, 0, f_getwinposx},
8184 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008185 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008186 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008187 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008188 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008190 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008191 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008192 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8194 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8195 {"histadd", 2, 2, f_histadd},
8196 {"histdel", 1, 2, f_histdel},
8197 {"histget", 1, 2, f_histget},
8198 {"histnr", 1, 1, f_histnr},
8199 {"hlID", 1, 1, f_hlID},
8200 {"hlexists", 1, 1, f_hlexists},
8201 {"hostname", 0, 0, f_hostname},
8202 {"iconv", 3, 3, f_iconv},
8203 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008204 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008205 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008207 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 {"inputrestore", 0, 0, f_inputrestore},
8209 {"inputsave", 0, 0, f_inputsave},
8210 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008211 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008212 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008214 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008215 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008216#ifdef FEAT_JOB
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008217 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008218 {"job_start", 1, 2, f_job_start},
8219 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008220 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008221#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008222 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008223 {"js_decode", 1, 1, f_js_decode},
8224 {"js_encode", 1, 1, f_js_encode},
8225 {"json_decode", 1, 1, f_json_decode},
8226 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008227 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008229 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 {"libcall", 3, 3, f_libcall},
8231 {"libcallnr", 3, 3, f_libcallnr},
8232 {"line", 1, 1, f_line},
8233 {"line2byte", 1, 1, f_line2byte},
8234 {"lispindent", 1, 1, f_lispindent},
8235 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008236#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008237 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008238 {"log10", 1, 1, f_log10},
8239#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008240#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008241 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008242#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008243 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008244 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008245 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008246 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008247 {"matchadd", 2, 5, f_matchadd},
8248 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008249 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008250 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008251 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008252 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008253 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008254 {"max", 1, 1, f_max},
8255 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008256#ifdef vim_mkdir
8257 {"mkdir", 1, 3, f_mkdir},
8258#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008259 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008260#ifdef FEAT_MZSCHEME
8261 {"mzeval", 1, 1, f_mzeval},
8262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008264 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008265 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008266 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008267#ifdef FEAT_PERL
8268 {"perleval", 1, 1, f_perleval},
8269#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008270#ifdef FEAT_FLOAT
8271 {"pow", 2, 2, f_pow},
8272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008274 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008275 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008276#ifdef FEAT_PYTHON3
8277 {"py3eval", 1, 1, f_py3eval},
8278#endif
8279#ifdef FEAT_PYTHON
8280 {"pyeval", 1, 1, f_pyeval},
8281#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008282 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008283 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008284 {"reltime", 0, 2, f_reltime},
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008285 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008286 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 {"remote_expr", 2, 3, f_remote_expr},
8288 {"remote_foreground", 1, 1, f_remote_foreground},
8289 {"remote_peek", 1, 2, f_remote_peek},
8290 {"remote_read", 1, 1, f_remote_read},
8291 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008292 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008294 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008296 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008297#ifdef FEAT_FLOAT
8298 {"round", 1, 1, f_round},
8299#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008300 {"screenattr", 2, 2, f_screenattr},
8301 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008302 {"screencol", 0, 0, f_screencol},
8303 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008304 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008305 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008306 {"searchpair", 3, 7, f_searchpair},
8307 {"searchpairpos", 3, 7, f_searchpairpos},
8308 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 {"server2client", 2, 2, f_server2client},
8310 {"serverlist", 0, 0, f_serverlist},
8311 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008312 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 {"setcmdpos", 1, 1, f_setcmdpos},
8314 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008315 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008316 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008317 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008318 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008320 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008321 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008323#ifdef FEAT_CRYPT
8324 {"sha256", 1, 1, f_sha256},
8325#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008326 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008327 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008329#ifdef FEAT_FLOAT
8330 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008331 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008332#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008333 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008334 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008335 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008336 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008337 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008338#ifdef FEAT_FLOAT
8339 {"sqrt", 1, 1, f_sqrt},
8340 {"str2float", 1, 1, f_str2float},
8341#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008342 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008343 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008344 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345#ifdef HAVE_STRFTIME
8346 {"strftime", 1, 2, f_strftime},
8347#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008348 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008349 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 {"strlen", 1, 1, f_strlen},
8351 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008352 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008354 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008355 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 {"substitute", 4, 4, f_substitute},
8357 {"synID", 3, 3, f_synID},
8358 {"synIDattr", 2, 3, f_synIDattr},
8359 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008360 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008361 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008362 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008363 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008364 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008365 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008366 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008367 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008368 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008369#ifdef FEAT_FLOAT
8370 {"tan", 1, 1, f_tan},
8371 {"tanh", 1, 1, f_tanh},
8372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008374 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 {"tolower", 1, 1, f_tolower},
8376 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008377 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008378#ifdef FEAT_FLOAT
8379 {"trunc", 1, 1, f_trunc},
8380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008382 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008383 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008384 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008385 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 {"virtcol", 1, 1, f_virtcol},
8387 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008388 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 {"winbufnr", 1, 1, f_winbufnr},
8390 {"wincol", 0, 0, f_wincol},
8391 {"winheight", 1, 1, f_winheight},
8392 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008393 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008395 {"winrestview", 1, 1, f_winrestview},
8396 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008398 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008399 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008400 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401};
8402
8403#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8404
8405/*
8406 * Function given to ExpandGeneric() to obtain the list of internal
8407 * or user defined function names.
8408 */
8409 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008410get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411{
8412 static int intidx = -1;
8413 char_u *name;
8414
8415 if (idx == 0)
8416 intidx = -1;
8417 if (intidx < 0)
8418 {
8419 name = get_user_func_name(xp, idx);
8420 if (name != NULL)
8421 return name;
8422 }
8423 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8424 {
8425 STRCPY(IObuff, functions[intidx].f_name);
8426 STRCAT(IObuff, "(");
8427 if (functions[intidx].f_max_argc == 0)
8428 STRCAT(IObuff, ")");
8429 return IObuff;
8430 }
8431
8432 return NULL;
8433}
8434
8435/*
8436 * Function given to ExpandGeneric() to obtain the list of internal or
8437 * user defined variable or function names.
8438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008440get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441{
8442 static int intidx = -1;
8443 char_u *name;
8444
8445 if (idx == 0)
8446 intidx = -1;
8447 if (intidx < 0)
8448 {
8449 name = get_function_name(xp, idx);
8450 if (name != NULL)
8451 return name;
8452 }
8453 return get_user_var_name(xp, ++intidx);
8454}
8455
8456#endif /* FEAT_CMDL_COMPL */
8457
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008458#if defined(EBCDIC) || defined(PROTO)
8459/*
8460 * Compare struct fst by function name.
8461 */
8462 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008463compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008464{
8465 struct fst *p1 = (struct fst *)s1;
8466 struct fst *p2 = (struct fst *)s2;
8467
8468 return STRCMP(p1->f_name, p2->f_name);
8469}
8470
8471/*
8472 * Sort the function table by function name.
8473 * The sorting of the table above is ASCII dependant.
8474 * On machines using EBCDIC we have to sort it.
8475 */
8476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008477sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008478{
8479 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8480
8481 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8482}
8483#endif
8484
8485
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486/*
8487 * Find internal function in table above.
8488 * Return index, or -1 if not found
8489 */
8490 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008491find_internal_func(
8492 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493{
8494 int first = 0;
8495 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8496 int cmp;
8497 int x;
8498
8499 /*
8500 * Find the function name in the table. Binary search.
8501 */
8502 while (first <= last)
8503 {
8504 x = first + ((unsigned)(last - first) >> 1);
8505 cmp = STRCMP(name, functions[x].f_name);
8506 if (cmp < 0)
8507 last = x - 1;
8508 else if (cmp > 0)
8509 first = x + 1;
8510 else
8511 return x;
8512 }
8513 return -1;
8514}
8515
8516/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008517 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8518 * name it contains, otherwise return "name".
8519 */
8520 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008521deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008522{
Bram Moolenaar33570922005-01-25 22:26:29 +00008523 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008524 int cc;
8525
8526 cc = name[*lenp];
8527 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008528 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008529 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008530 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008531 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008532 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008533 {
8534 *lenp = 0;
8535 return (char_u *)""; /* just in case */
8536 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008537 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008538 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008539 }
8540
8541 return name;
8542}
8543
8544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 * Allocate a variable for the result of a function.
8546 * Return OK or FAIL.
8547 */
8548 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008549get_func_tv(
8550 char_u *name, /* name of the function */
8551 int len, /* length of "name" */
8552 typval_T *rettv,
8553 char_u **arg, /* argument, pointing to the '(' */
8554 linenr_T firstline, /* first line of range */
8555 linenr_T lastline, /* last line of range */
8556 int *doesrange, /* return: function handled range */
8557 int evaluate,
8558 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559{
8560 char_u *argp;
8561 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008562 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 int argcount = 0; /* number of arguments found */
8564
8565 /*
8566 * Get the arguments.
8567 */
8568 argp = *arg;
8569 while (argcount < MAX_FUNC_ARGS)
8570 {
8571 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8572 if (*argp == ')' || *argp == ',' || *argp == NUL)
8573 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8575 {
8576 ret = FAIL;
8577 break;
8578 }
8579 ++argcount;
8580 if (*argp != ',')
8581 break;
8582 }
8583 if (*argp == ')')
8584 ++argp;
8585 else
8586 ret = FAIL;
8587
8588 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008589 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008590 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008592 {
8593 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008594 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008595 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008596 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598
8599 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008600 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601
8602 *arg = skipwhite(argp);
8603 return ret;
8604}
8605
8606
8607/*
8608 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008609 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008610 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008612 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008613call_func(
8614 char_u *funcname, /* name of the function */
8615 int len, /* length of "name" */
8616 typval_T *rettv, /* return value goes here */
8617 int argcount, /* number of "argvars" */
8618 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008619 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008620 linenr_T firstline, /* first line of range */
8621 linenr_T lastline, /* last line of range */
8622 int *doesrange, /* return: function handled range */
8623 int evaluate,
8624 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625{
8626 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627#define ERROR_UNKNOWN 0
8628#define ERROR_TOOMANY 1
8629#define ERROR_TOOFEW 2
8630#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008631#define ERROR_DICT 4
8632#define ERROR_NONE 5
8633#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 int error = ERROR_NONE;
8635 int i;
8636 int llen;
8637 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638#define FLEN_FIXED 40
8639 char_u fname_buf[FLEN_FIXED + 1];
8640 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008641 char_u *name;
8642
8643 /* Make a copy of the name, if it comes from a funcref variable it could
8644 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008645 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008646 if (name == NULL)
8647 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648
8649 /*
8650 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8651 * Change <SNR>123_name() to K_SNR 123_name().
8652 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8653 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 llen = eval_fname_script(name);
8655 if (llen > 0)
8656 {
8657 fname_buf[0] = K_SPECIAL;
8658 fname_buf[1] = KS_EXTRA;
8659 fname_buf[2] = (int)KE_SNR;
8660 i = 3;
8661 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8662 {
8663 if (current_SID <= 0)
8664 error = ERROR_SCRIPT;
8665 else
8666 {
8667 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8668 i = (int)STRLEN(fname_buf);
8669 }
8670 }
8671 if (i + STRLEN(name + llen) < FLEN_FIXED)
8672 {
8673 STRCPY(fname_buf + i, name + llen);
8674 fname = fname_buf;
8675 }
8676 else
8677 {
8678 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8679 if (fname == NULL)
8680 error = ERROR_OTHER;
8681 else
8682 {
8683 mch_memmove(fname, fname_buf, (size_t)i);
8684 STRCPY(fname + i, name + llen);
8685 }
8686 }
8687 }
8688 else
8689 fname = name;
8690
8691 *doesrange = FALSE;
8692
8693
8694 /* execute the function if no errors detected and executing */
8695 if (evaluate && error == ERROR_NONE)
8696 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008697 char_u *rfname = fname;
8698
8699 /* Ignore "g:" before a function name. */
8700 if (fname[0] == 'g' && fname[1] == ':')
8701 rfname = fname + 2;
8702
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008703 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8704 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 error = ERROR_UNKNOWN;
8706
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008707 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 {
8709 /*
8710 * User defined function.
8711 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008712 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008713
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008715 /* Trigger FuncUndefined event, may load the function. */
8716 if (fp == NULL
8717 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008718 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008719 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008721 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008722 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 }
8724#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008725 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008726 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008727 {
8728 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008729 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008730 }
8731
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 if (fp != NULL)
8733 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008734 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008736 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008738 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008740 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008741 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 else
8743 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008744 int did_save_redo = FALSE;
8745
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 /*
8747 * Call the user function.
8748 * Save and restore search patterns, script variables and
8749 * redo buffer.
8750 */
8751 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008752#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008753 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008754#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008755 {
8756 saveRedobuff();
8757 did_save_redo = TRUE;
8758 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008759 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008760 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008761 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008762 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8763 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8764 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008765 /* Function was unreferenced while being used, free it
8766 * now. */
8767 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008768 if (did_save_redo)
8769 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 restore_search_patterns();
8771 error = ERROR_NONE;
8772 }
8773 }
8774 }
8775 else
8776 {
8777 /*
8778 * Find the function name in the table, call its implementation.
8779 */
8780 i = find_internal_func(fname);
8781 if (i >= 0)
8782 {
8783 if (argcount < functions[i].f_min_argc)
8784 error = ERROR_TOOFEW;
8785 else if (argcount > functions[i].f_max_argc)
8786 error = ERROR_TOOMANY;
8787 else
8788 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008789 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008790 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 error = ERROR_NONE;
8792 }
8793 }
8794 }
8795 /*
8796 * The function call (or "FuncUndefined" autocommand sequence) might
8797 * have been aborted by an error, an interrupt, or an explicitly thrown
8798 * exception that has not been caught so far. This situation can be
8799 * tested for by calling aborting(). For an error in an internal
8800 * function or for the "E132" error in call_user_func(), however, the
8801 * throw point at which the "force_abort" flag (temporarily reset by
8802 * emsg()) is normally updated has not been reached yet. We need to
8803 * update that flag first to make aborting() reliable.
8804 */
8805 update_force_abort();
8806 }
8807 if (error == ERROR_NONE)
8808 ret = OK;
8809
8810 /*
8811 * Report an error unless the argument evaluation or function call has been
8812 * cancelled due to an aborting error, an interrupt, or an exception.
8813 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008814 if (!aborting())
8815 {
8816 switch (error)
8817 {
8818 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008819 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008820 break;
8821 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008822 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008823 break;
8824 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008825 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008826 name);
8827 break;
8828 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008829 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008830 name);
8831 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008832 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008833 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008834 name);
8835 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008836 }
8837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 if (fname != name && fname != fname_buf)
8840 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008841 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842
8843 return ret;
8844}
8845
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008846/*
8847 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008848 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008849 */
8850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008851emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008852{
8853 char_u *p;
8854
8855 if (*name == K_SPECIAL)
8856 p = concat_str((char_u *)"<SNR>", name + 3);
8857 else
8858 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008859 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008860 if (p != name)
8861 vim_free(p);
8862}
8863
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008864/*
8865 * Return TRUE for a non-zero Number and a non-empty String.
8866 */
8867 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008868non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008869{
8870 return ((argvars[0].v_type == VAR_NUMBER
8871 && argvars[0].vval.v_number != 0)
8872 || (argvars[0].v_type == VAR_STRING
8873 && argvars[0].vval.v_string != NULL
8874 && *argvars[0].vval.v_string != NUL));
8875}
8876
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877/*********************************************
8878 * Implementation of the built-in functions
8879 */
8880
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008881#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008882static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008883
8884/*
8885 * Get the float value of "argvars[0]" into "f".
8886 * Returns FAIL when the argument is not a Number or Float.
8887 */
8888 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008889get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008890{
8891 if (argvars[0].v_type == VAR_FLOAT)
8892 {
8893 *f = argvars[0].vval.v_float;
8894 return OK;
8895 }
8896 if (argvars[0].v_type == VAR_NUMBER)
8897 {
8898 *f = (float_T)argvars[0].vval.v_number;
8899 return OK;
8900 }
8901 EMSG(_("E808: Number or Float required"));
8902 return FAIL;
8903}
8904
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008905/*
8906 * "abs(expr)" function
8907 */
8908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008909f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008910{
8911 if (argvars[0].v_type == VAR_FLOAT)
8912 {
8913 rettv->v_type = VAR_FLOAT;
8914 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8915 }
8916 else
8917 {
8918 varnumber_T n;
8919 int error = FALSE;
8920
8921 n = get_tv_number_chk(&argvars[0], &error);
8922 if (error)
8923 rettv->vval.v_number = -1;
8924 else if (n > 0)
8925 rettv->vval.v_number = n;
8926 else
8927 rettv->vval.v_number = -n;
8928 }
8929}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008930
8931/*
8932 * "acos()" function
8933 */
8934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008935f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008936{
8937 float_T f;
8938
8939 rettv->v_type = VAR_FLOAT;
8940 if (get_float_arg(argvars, &f) == OK)
8941 rettv->vval.v_float = acos(f);
8942 else
8943 rettv->vval.v_float = 0.0;
8944}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008945#endif
8946
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008948 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 */
8950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008951f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952{
Bram Moolenaar33570922005-01-25 22:26:29 +00008953 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008955 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008956 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008958 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008959 && !tv_check_lock(l->lv_lock,
8960 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008961 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008962 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008963 }
8964 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008965 EMSG(_(e_listreq));
8966}
8967
8968/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008969 * "alloc_fail(id, countdown, repeat)" function
8970 */
8971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008972f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008973{
8974 if (argvars[0].v_type != VAR_NUMBER
8975 || argvars[0].vval.v_number <= 0
8976 || argvars[1].v_type != VAR_NUMBER
8977 || argvars[1].vval.v_number < 0
8978 || argvars[2].v_type != VAR_NUMBER)
8979 EMSG(_(e_invarg));
8980 else
8981 {
8982 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008983 if (alloc_fail_id >= aid_last)
8984 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008985 alloc_fail_countdown = argvars[1].vval.v_number;
8986 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008987 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008988 }
8989}
8990
8991/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008992 * "and(expr, expr)" function
8993 */
8994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008995f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008996{
8997 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8998 & get_tv_number_chk(&argvars[1], NULL);
8999}
9000
9001/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009002 * "append(lnum, string/list)" function
9003 */
9004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009005f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009006{
9007 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009008 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009009 list_T *l = NULL;
9010 listitem_T *li = NULL;
9011 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009012 long added = 0;
9013
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009014 /* When coming here from Insert mode, sync undo, so that this can be
9015 * undone separately from what was previously inserted. */
9016 if (u_sync_once == 2)
9017 {
9018 u_sync_once = 1; /* notify that u_sync() was called */
9019 u_sync(TRUE);
9020 }
9021
Bram Moolenaar0d660222005-01-07 21:51:51 +00009022 lnum = get_tv_lnum(argvars);
9023 if (lnum >= 0
9024 && lnum <= curbuf->b_ml.ml_line_count
9025 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009026 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009027 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009028 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009029 l = argvars[1].vval.v_list;
9030 if (l == NULL)
9031 return;
9032 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009033 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009034 for (;;)
9035 {
9036 if (l == NULL)
9037 tv = &argvars[1]; /* append a string */
9038 else if (li == NULL)
9039 break; /* end of list */
9040 else
9041 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009042 line = get_tv_string_chk(tv);
9043 if (line == NULL) /* type error */
9044 {
9045 rettv->vval.v_number = 1; /* Failed */
9046 break;
9047 }
9048 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009049 ++added;
9050 if (l == NULL)
9051 break;
9052 li = li->li_next;
9053 }
9054
9055 appended_lines_mark(lnum, added);
9056 if (curwin->w_cursor.lnum > lnum)
9057 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009059 else
9060 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061}
9062
9063/*
9064 * "argc()" function
9065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009067f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009069 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070}
9071
9072/*
9073 * "argidx()" function
9074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009076f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009078 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079}
9080
9081/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009082 * "arglistid()" function
9083 */
9084 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009085f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009086{
9087 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009088
9089 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009090 wp = find_tabwin(&argvars[0], &argvars[1]);
9091 if (wp != NULL)
9092 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009093}
9094
9095/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 * "argv(nr)" function
9097 */
9098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009099f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100{
9101 int idx;
9102
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009103 if (argvars[0].v_type != VAR_UNKNOWN)
9104 {
9105 idx = get_tv_number_chk(&argvars[0], NULL);
9106 if (idx >= 0 && idx < ARGCOUNT)
9107 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9108 else
9109 rettv->vval.v_string = NULL;
9110 rettv->v_type = VAR_STRING;
9111 }
9112 else if (rettv_list_alloc(rettv) == OK)
9113 for (idx = 0; idx < ARGCOUNT; ++idx)
9114 list_append_string(rettv->vval.v_list,
9115 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116}
9117
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009118static void prepare_assert_error(garray_T*gap);
9119static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9120static void assert_error(garray_T *gap);
9121static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009122
9123/*
9124 * Prepare "gap" for an assert error and add the sourcing position.
9125 */
9126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009127prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009128{
9129 char buf[NUMBUFLEN];
9130
9131 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009132 if (sourcing_name != NULL)
9133 {
9134 ga_concat(gap, sourcing_name);
9135 if (sourcing_lnum > 0)
9136 ga_concat(gap, (char_u *)" ");
9137 }
9138 if (sourcing_lnum > 0)
9139 {
9140 sprintf(buf, "line %ld", (long)sourcing_lnum);
9141 ga_concat(gap, (char_u *)buf);
9142 }
9143 if (sourcing_name != NULL || sourcing_lnum > 0)
9144 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009145}
9146
9147/*
9148 * Fill "gap" with information about an assert error.
9149 */
9150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009151fill_assert_error(
9152 garray_T *gap,
9153 typval_T *opt_msg_tv,
9154 char_u *exp_str,
9155 typval_T *exp_tv,
9156 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009157{
9158 char_u numbuf[NUMBUFLEN];
9159 char_u *tofree;
9160
9161 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9162 {
9163 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9164 vim_free(tofree);
9165 }
9166 else
9167 {
9168 ga_concat(gap, (char_u *)"Expected ");
9169 if (exp_str == NULL)
9170 {
9171 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9172 vim_free(tofree);
9173 }
9174 else
9175 ga_concat(gap, exp_str);
9176 ga_concat(gap, (char_u *)" but got ");
9177 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9178 vim_free(tofree);
9179 }
9180}
Bram Moolenaar43345542015-11-29 17:35:35 +01009181
9182/*
9183 * Add an assert error to v:errors.
9184 */
9185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009186assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009187{
9188 struct vimvar *vp = &vimvars[VV_ERRORS];
9189
9190 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9191 /* Make sure v:errors is a list. */
9192 set_vim_var_list(VV_ERRORS, list_alloc());
9193 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9194}
9195
Bram Moolenaar43345542015-11-29 17:35:35 +01009196/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009197 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009198 */
9199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009200f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009201{
9202 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009203
9204 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9205 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009206 prepare_assert_error(&ga);
9207 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9208 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009209 ga_clear(&ga);
9210 }
9211}
9212
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009213/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009214 * "assert_exception(string[, msg])" function
9215 */
9216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009217f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009218{
9219 garray_T ga;
9220 char *error;
9221
9222 error = (char *)get_tv_string_chk(&argvars[0]);
9223 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9224 {
9225 prepare_assert_error(&ga);
9226 ga_concat(&ga, (char_u *)"v:exception is not set");
9227 assert_error(&ga);
9228 ga_clear(&ga);
9229 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009230 else if (error != NULL
9231 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009232 {
9233 prepare_assert_error(&ga);
9234 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9235 &vimvars[VV_EXCEPTION].vv_tv);
9236 assert_error(&ga);
9237 ga_clear(&ga);
9238 }
9239}
9240
9241/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009242 * "assert_fails(cmd [, error])" function
9243 */
9244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009245f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009246{
9247 char_u *cmd = get_tv_string_chk(&argvars[0]);
9248 garray_T ga;
9249
9250 called_emsg = FALSE;
9251 suppress_errthrow = TRUE;
9252 emsg_silent = TRUE;
9253 do_cmdline_cmd(cmd);
9254 if (!called_emsg)
9255 {
9256 prepare_assert_error(&ga);
9257 ga_concat(&ga, (char_u *)"command did not fail: ");
9258 ga_concat(&ga, cmd);
9259 assert_error(&ga);
9260 ga_clear(&ga);
9261 }
9262 else if (argvars[1].v_type != VAR_UNKNOWN)
9263 {
9264 char_u buf[NUMBUFLEN];
9265 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9266
9267 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9268 {
9269 prepare_assert_error(&ga);
9270 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9271 &vimvars[VV_ERRMSG].vv_tv);
9272 assert_error(&ga);
9273 ga_clear(&ga);
9274 }
9275 }
9276
9277 called_emsg = FALSE;
9278 suppress_errthrow = FALSE;
9279 emsg_silent = FALSE;
9280 emsg_on_display = FALSE;
9281 set_vim_var_string(VV_ERRMSG, NULL, 0);
9282}
9283
9284/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009285 * Common for assert_true() and assert_false().
9286 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009288assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009289{
9290 int error = FALSE;
9291 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009292
Bram Moolenaar37127922016-02-06 20:29:28 +01009293 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009294 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009295 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009296 if (argvars[0].v_type != VAR_NUMBER
9297 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9298 || error)
9299 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009300 prepare_assert_error(&ga);
9301 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009302 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009303 NULL, &argvars[0]);
9304 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009305 ga_clear(&ga);
9306 }
9307}
9308
9309/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009310 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009311 */
9312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009313f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009314{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009315 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009316}
9317
9318/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009319 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009320 */
9321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009322f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009323{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009324 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009325}
9326
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009327#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009328/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009329 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009330 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009332f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009333{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009334 float_T f;
9335
9336 rettv->v_type = VAR_FLOAT;
9337 if (get_float_arg(argvars, &f) == OK)
9338 rettv->vval.v_float = asin(f);
9339 else
9340 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009341}
9342
9343/*
9344 * "atan()" function
9345 */
9346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009347f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009348{
9349 float_T f;
9350
9351 rettv->v_type = VAR_FLOAT;
9352 if (get_float_arg(argvars, &f) == OK)
9353 rettv->vval.v_float = atan(f);
9354 else
9355 rettv->vval.v_float = 0.0;
9356}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009357
9358/*
9359 * "atan2()" function
9360 */
9361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009362f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009363{
9364 float_T fx, fy;
9365
9366 rettv->v_type = VAR_FLOAT;
9367 if (get_float_arg(argvars, &fx) == OK
9368 && get_float_arg(&argvars[1], &fy) == OK)
9369 rettv->vval.v_float = atan2(fx, fy);
9370 else
9371 rettv->vval.v_float = 0.0;
9372}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009373#endif
9374
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375/*
9376 * "browse(save, title, initdir, default)" function
9377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009379f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380{
9381#ifdef FEAT_BROWSE
9382 int save;
9383 char_u *title;
9384 char_u *initdir;
9385 char_u *defname;
9386 char_u buf[NUMBUFLEN];
9387 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009388 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009390 save = get_tv_number_chk(&argvars[0], &error);
9391 title = get_tv_string_chk(&argvars[1]);
9392 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9393 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009395 if (error || title == NULL || initdir == NULL || defname == NULL)
9396 rettv->vval.v_string = NULL;
9397 else
9398 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009399 do_browse(save ? BROWSE_SAVE : 0,
9400 title, defname, NULL, initdir, NULL, curbuf);
9401#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009402 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009403#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009404 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009405}
9406
9407/*
9408 * "browsedir(title, initdir)" function
9409 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009411f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009412{
9413#ifdef FEAT_BROWSE
9414 char_u *title;
9415 char_u *initdir;
9416 char_u buf[NUMBUFLEN];
9417
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009418 title = get_tv_string_chk(&argvars[0]);
9419 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009420
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009421 if (title == NULL || initdir == NULL)
9422 rettv->vval.v_string = NULL;
9423 else
9424 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009425 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009427 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009429 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430}
9431
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009432static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009433
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434/*
9435 * Find a buffer by number or exact name.
9436 */
9437 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009438find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439{
9440 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009442 if (avar->v_type == VAR_NUMBER)
9443 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009444 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009446 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009447 if (buf == NULL)
9448 {
9449 /* No full path name match, try a match with a URL or a "nofile"
9450 * buffer, these don't use the full path. */
9451 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9452 if (buf->b_fname != NULL
9453 && (path_with_url(buf->b_fname)
9454#ifdef FEAT_QUICKFIX
9455 || bt_nofile(buf)
9456#endif
9457 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009458 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009459 break;
9460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 }
9462 return buf;
9463}
9464
9465/*
9466 * "bufexists(expr)" function
9467 */
9468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009469f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009471 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472}
9473
9474/*
9475 * "buflisted(expr)" function
9476 */
9477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009478f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479{
9480 buf_T *buf;
9481
9482 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009483 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484}
9485
9486/*
9487 * "bufloaded(expr)" function
9488 */
9489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009490f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491{
9492 buf_T *buf;
9493
9494 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009495 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496}
9497
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009498static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009499
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500/*
9501 * Get buffer by number or pattern.
9502 */
9503 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009504get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009506 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 int save_magic;
9508 char_u *save_cpo;
9509 buf_T *buf;
9510
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009511 if (tv->v_type == VAR_NUMBER)
9512 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009513 if (tv->v_type != VAR_STRING)
9514 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 if (name == NULL || *name == NUL)
9516 return curbuf;
9517 if (name[0] == '$' && name[1] == NUL)
9518 return lastbuf;
9519
9520 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9521 save_magic = p_magic;
9522 p_magic = TRUE;
9523 save_cpo = p_cpo;
9524 p_cpo = (char_u *)"";
9525
9526 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009527 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528
9529 p_magic = save_magic;
9530 p_cpo = save_cpo;
9531
9532 /* If not found, try expanding the name, like done for bufexists(). */
9533 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009534 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535
9536 return buf;
9537}
9538
9539/*
9540 * "bufname(expr)" function
9541 */
9542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009543f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544{
9545 buf_T *buf;
9546
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009547 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009549 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009550 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009552 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009554 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 --emsg_off;
9556}
9557
9558/*
9559 * "bufnr(expr)" function
9560 */
9561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009562f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563{
9564 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009565 int error = FALSE;
9566 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009568 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009570 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009571 --emsg_off;
9572
9573 /* If the buffer isn't found and the second argument is not zero create a
9574 * new buffer. */
9575 if (buf == NULL
9576 && argvars[1].v_type != VAR_UNKNOWN
9577 && get_tv_number_chk(&argvars[1], &error) != 0
9578 && !error
9579 && (name = get_tv_string_chk(&argvars[0])) != NULL
9580 && !error)
9581 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9582
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009584 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009586 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587}
9588
9589/*
9590 * "bufwinnr(nr)" function
9591 */
9592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009593f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594{
9595#ifdef FEAT_WINDOWS
9596 win_T *wp;
9597 int winnr = 0;
9598#endif
9599 buf_T *buf;
9600
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009601 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009603 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604#ifdef FEAT_WINDOWS
9605 for (wp = firstwin; wp; wp = wp->w_next)
9606 {
9607 ++winnr;
9608 if (wp->w_buffer == buf)
9609 break;
9610 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009611 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009613 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614#endif
9615 --emsg_off;
9616}
9617
9618/*
9619 * "byte2line(byte)" function
9620 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009622f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623{
9624#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009625 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626#else
9627 long boff = 0;
9628
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009629 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009631 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009633 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634 (linenr_T)0, &boff);
9635#endif
9636}
9637
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009639byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009640{
9641#ifdef FEAT_MBYTE
9642 char_u *t;
9643#endif
9644 char_u *str;
9645 long idx;
9646
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009647 str = get_tv_string_chk(&argvars[0]);
9648 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009649 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009650 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009651 return;
9652
9653#ifdef FEAT_MBYTE
9654 t = str;
9655 for ( ; idx > 0; idx--)
9656 {
9657 if (*t == NUL) /* EOL reached */
9658 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009659 if (enc_utf8 && comp)
9660 t += utf_ptr2len(t);
9661 else
9662 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009663 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009664 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009665#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009666 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009667 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009668#endif
9669}
9670
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009671/*
9672 * "byteidx()" function
9673 */
9674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009675f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009676{
9677 byteidx(argvars, rettv, FALSE);
9678}
9679
9680/*
9681 * "byteidxcomp()" function
9682 */
9683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009684f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009685{
9686 byteidx(argvars, rettv, TRUE);
9687}
9688
Bram Moolenaardb913952012-06-29 12:54:53 +02009689 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009690func_call(
9691 char_u *name,
9692 typval_T *args,
9693 dict_T *selfdict,
9694 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009695{
9696 listitem_T *item;
9697 typval_T argv[MAX_FUNC_ARGS + 1];
9698 int argc = 0;
9699 int dummy;
9700 int r = 0;
9701
9702 for (item = args->vval.v_list->lv_first; item != NULL;
9703 item = item->li_next)
9704 {
9705 if (argc == MAX_FUNC_ARGS)
9706 {
9707 EMSG(_("E699: Too many arguments"));
9708 break;
9709 }
9710 /* Make a copy of each argument. This is needed to be able to set
9711 * v_lock to VAR_FIXED in the copy without changing the original list.
9712 */
9713 copy_tv(&item->li_tv, &argv[argc++]);
9714 }
9715
9716 if (item == NULL)
9717 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9718 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9719 &dummy, TRUE, selfdict);
9720
9721 /* Free the arguments. */
9722 while (argc > 0)
9723 clear_tv(&argv[--argc]);
9724
9725 return r;
9726}
9727
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009728/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009729 * "call(func, arglist)" function
9730 */
9731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009732f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009733{
9734 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009735 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009736
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009737 if (argvars[1].v_type != VAR_LIST)
9738 {
9739 EMSG(_(e_listreq));
9740 return;
9741 }
9742 if (argvars[1].vval.v_list == NULL)
9743 return;
9744
9745 if (argvars[0].v_type == VAR_FUNC)
9746 func = argvars[0].vval.v_string;
9747 else
9748 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009749 if (*func == NUL)
9750 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009751
Bram Moolenaare9a41262005-01-15 22:18:47 +00009752 if (argvars[2].v_type != VAR_UNKNOWN)
9753 {
9754 if (argvars[2].v_type != VAR_DICT)
9755 {
9756 EMSG(_(e_dictreq));
9757 return;
9758 }
9759 selfdict = argvars[2].vval.v_dict;
9760 }
9761
Bram Moolenaardb913952012-06-29 12:54:53 +02009762 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009763}
9764
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009765#ifdef FEAT_FLOAT
9766/*
9767 * "ceil({float})" function
9768 */
9769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009770f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009771{
9772 float_T f;
9773
9774 rettv->v_type = VAR_FLOAT;
9775 if (get_float_arg(argvars, &f) == OK)
9776 rettv->vval.v_float = ceil(f);
9777 else
9778 rettv->vval.v_float = 0.0;
9779}
9780#endif
9781
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009782#ifdef FEAT_CHANNEL
9783/*
9784 * Get the channel index from the handle argument.
9785 * Returns -1 if the handle is invalid or the channel is closed.
9786 */
9787 static int
9788get_channel_arg(typval_T *tv)
9789{
9790 int ch_idx;
9791
9792 if (tv->v_type != VAR_NUMBER)
9793 {
9794 EMSG2(_(e_invarg2), get_tv_string(tv));
9795 return -1;
9796 }
9797 ch_idx = tv->vval.v_number;
9798
Bram Moolenaar6463ca22016-02-13 17:04:46 +01009799 if (!channel_can_write_to(ch_idx))
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009800 {
9801 EMSGN(_("E906: not an open channel"), ch_idx);
9802 return -1;
9803 }
9804 return ch_idx;
9805}
9806
9807/*
9808 * "ch_close()" function
9809 */
9810 static void
9811f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9812{
9813 int ch_idx = get_channel_arg(&argvars[0]);
9814
9815 if (ch_idx >= 0)
9816 channel_close(ch_idx);
9817}
9818
9819/*
9820 * Get a callback from "arg". It can be a Funcref or a function name.
9821 * When "arg" is zero return an empty string.
9822 * Return NULL for an invalid argument.
9823 */
9824 static char_u *
9825get_callback(typval_T *arg)
9826{
9827 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9828 return arg->vval.v_string;
9829 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9830 return (char_u *)"";
9831 EMSG(_("E999: Invalid callback argument"));
9832 return NULL;
9833}
9834
9835/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +01009836 * "ch_logfile()" function
9837 */
9838 static void
9839f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
9840{
9841 char_u *fname;
9842 char_u *opt = (char_u *)"";
9843 char_u buf[NUMBUFLEN];
9844 FILE *file = NULL;
9845
9846 fname = get_tv_string(&argvars[0]);
9847 if (argvars[1].v_type == VAR_STRING)
9848 opt = get_tv_string_buf(&argvars[1], buf);
9849 if (*fname != NUL)
9850 {
9851 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
9852 if (file == NULL)
9853 {
9854 EMSG2(_(e_notopen), fname);
9855 return;
9856 }
9857 }
9858 ch_logfile(file);
9859}
9860
9861/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009862 * "ch_open()" function
9863 */
9864 static void
9865f_ch_open(typval_T *argvars, typval_T *rettv)
9866{
9867 char_u *address;
9868 char_u *mode;
9869 char_u *callback = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009870 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009871 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009872 int port;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009873 int waittime = 0;
9874 int timeout = 2000;
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009875 ch_mode_T ch_mode = MODE_JSON;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009876 int ch_idx;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009877
9878 /* default: fail */
9879 rettv->vval.v_number = -1;
9880
9881 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009882 if (argvars[1].v_type != VAR_UNKNOWN
9883 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009884 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009885 EMSG(_(e_invarg));
9886 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009887 }
9888
9889 /* parse address */
9890 p = vim_strchr(address, ':');
9891 if (p == NULL)
9892 {
9893 EMSG2(_(e_invarg2), address);
9894 return;
9895 }
9896 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009897 port = strtol((char *)p, &rest, 10);
9898 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009899 {
9900 p[-1] = ':';
9901 EMSG2(_(e_invarg2), address);
9902 return;
9903 }
9904
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009905 if (argvars[1].v_type == VAR_DICT)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009906 {
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +01009907 dict_T *dict = argvars[1].vval.v_dict;
9908 dictitem_T *item;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009909
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +01009910 /* parse argdict */
9911 if ((item = dict_find(dict, (char_u *)"mode", -1)) != NULL)
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009912 {
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +01009913 mode = get_tv_string(&item->di_tv);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009914 if (STRCMP(mode, "raw") == 0)
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009915 ch_mode = MODE_RAW;
9916 else if (STRCMP(mode, "js") == 0)
9917 ch_mode = MODE_JS;
9918 else if (STRCMP(mode, "json") == 0)
9919 ch_mode = MODE_JSON;
9920 else
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009921 {
9922 EMSG2(_(e_invarg2), mode);
9923 return;
9924 }
9925 }
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +01009926 if ((item = dict_find(dict, (char_u *)"waittime", -1)) != NULL)
9927 waittime = get_tv_number(&item->di_tv);
9928 if ((item = dict_find(dict, (char_u *)"timeout", -1)) != NULL)
9929 timeout = get_tv_number(&item->di_tv);
9930 if ((item = dict_find(dict, (char_u *)"callback", -1)) != NULL)
9931 callback = get_callback(&item->di_tv);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009932 }
9933 if (waittime < 0 || timeout < 0)
9934 {
9935 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009936 return;
9937 }
9938
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009939 ch_idx = channel_open((char *)address, port, waittime, NULL);
9940 if (ch_idx >= 0)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009941 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009942 channel_set_json_mode(ch_idx, ch_mode);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009943 channel_set_timeout(ch_idx, timeout);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009944 if (callback != NULL && *callback != NUL)
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009945 channel_set_callback(ch_idx, callback);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009946 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009947 rettv->vval.v_number = ch_idx;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009948}
9949
9950/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +01009951 * "ch_readraw()" function
9952 */
9953 static void
9954f_ch_readraw(typval_T *argvars, typval_T *rettv)
9955{
9956 int ch_idx;
9957
9958 /* return an empty string by default */
9959 rettv->v_type = VAR_STRING;
9960 rettv->vval.v_string = NULL;
9961
9962 ch_idx = get_channel_arg(&argvars[0]);
9963 if (ch_idx < 0)
9964 {
9965 EMSG(_(e_invarg));
9966 return;
9967 }
9968 rettv->vval.v_string = channel_read_block(ch_idx);
9969}
9970
9971/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009972 * common for "sendexpr()" and "sendraw()"
9973 * Returns the channel index if the caller should read the response.
9974 * Otherwise returns -1.
9975 */
9976 static int
Bram Moolenaara07fec92016-02-05 21:04:08 +01009977send_common(typval_T *argvars, char_u *text, int id, char *fun)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009978{
9979 int ch_idx;
9980 char_u *callback = NULL;
9981
9982 ch_idx = get_channel_arg(&argvars[0]);
9983 if (ch_idx < 0)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01009984 {
9985 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009986 return -1;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01009987 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009988
9989 if (argvars[2].v_type != VAR_UNKNOWN)
9990 {
9991 callback = get_callback(&argvars[2]);
9992 if (callback == NULL)
9993 return -1;
9994 }
Bram Moolenaara07fec92016-02-05 21:04:08 +01009995 /* Set the callback. An empty callback means no callback and not reading
9996 * the response. */
9997 if (callback != NULL && *callback != NUL)
9998 channel_set_req_callback(ch_idx, callback, id);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009999
10000 if (channel_send(ch_idx, text, fun) == OK && callback == NULL)
10001 return ch_idx;
10002 return -1;
10003}
10004
10005/*
10006 * "ch_sendexpr()" function
10007 */
10008 static void
10009f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10010{
10011 char_u *text;
10012 typval_T *listtv;
10013 int ch_idx;
10014 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010015 ch_mode_T ch_mode;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010016
10017 /* return an empty string by default */
10018 rettv->v_type = VAR_STRING;
10019 rettv->vval.v_string = NULL;
10020
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010021 ch_idx = get_channel_arg(&argvars[0]);
10022 if (ch_idx < 0)
10023 {
10024 EMSG(_(e_invarg));
10025 return;
10026 }
10027
10028 ch_mode = channel_get_mode(ch_idx);
10029 if (ch_mode == MODE_RAW)
10030 {
10031 EMSG(_("E912: cannot use ch_sendexpr() with a raw channel"));
10032 return;
10033 }
10034
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010035 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010036 text = json_encode_nr_expr(id, &argvars[1],
10037 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010038 if (text == NULL)
10039 return;
10040
Bram Moolenaara07fec92016-02-05 21:04:08 +010010041 ch_idx = send_common(argvars, text, id, "sendexpr");
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010042 vim_free(text);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010043 if (ch_idx >= 0)
10044 {
10045 if (channel_read_json_block(ch_idx, id, &listtv) == OK)
10046 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010047 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010048
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010049 /* Move the item from the list and then change the type to
10050 * avoid the value being freed. */
10051 *rettv = list->lv_last->li_tv;
10052 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010053 clear_tv(listtv);
10054 }
10055 }
10056}
10057
10058/*
10059 * "ch_sendraw()" function
10060 */
10061 static void
10062f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10063{
10064 char_u buf[NUMBUFLEN];
10065 char_u *text;
10066 int ch_idx;
10067
10068 /* return an empty string by default */
10069 rettv->v_type = VAR_STRING;
10070 rettv->vval.v_string = NULL;
10071
10072 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaara07fec92016-02-05 21:04:08 +010010073 ch_idx = send_common(argvars, text, 0, "sendraw");
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010074 if (ch_idx >= 0)
10075 rettv->vval.v_string = channel_read_block(ch_idx);
10076}
10077#endif
10078
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010079/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010080 * "changenr()" function
10081 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010083f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010084{
10085 rettv->vval.v_number = curbuf->b_u_seq_cur;
10086}
10087
10088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 * "char2nr(string)" function
10090 */
10091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010092f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093{
10094#ifdef FEAT_MBYTE
10095 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010096 {
10097 int utf8 = 0;
10098
10099 if (argvars[1].v_type != VAR_UNKNOWN)
10100 utf8 = get_tv_number_chk(&argvars[1], NULL);
10101
10102 if (utf8)
10103 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10104 else
10105 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 else
10108#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010109 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110}
10111
10112/*
10113 * "cindent(lnum)" function
10114 */
10115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010116f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117{
10118#ifdef FEAT_CINDENT
10119 pos_T pos;
10120 linenr_T lnum;
10121
10122 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010123 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10125 {
10126 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010127 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 curwin->w_cursor = pos;
10129 }
10130 else
10131#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010132 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133}
10134
10135/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010136 * "clearmatches()" function
10137 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010139f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010140{
10141#ifdef FEAT_SEARCH_EXTRA
10142 clear_matches(curwin);
10143#endif
10144}
10145
10146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 * "col(string)" function
10148 */
10149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010150f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151{
10152 colnr_T col = 0;
10153 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010154 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010156 fp = var2fpos(&argvars[0], FALSE, &fnum);
10157 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 {
10159 if (fp->col == MAXCOL)
10160 {
10161 /* '> can be MAXCOL, get the length of the line then */
10162 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010163 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164 else
10165 col = MAXCOL;
10166 }
10167 else
10168 {
10169 col = fp->col + 1;
10170#ifdef FEAT_VIRTUALEDIT
10171 /* col(".") when the cursor is on the NUL at the end of the line
10172 * because of "coladd" can be seen as an extra column. */
10173 if (virtual_active() && fp == &curwin->w_cursor)
10174 {
10175 char_u *p = ml_get_cursor();
10176
10177 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10178 curwin->w_virtcol - curwin->w_cursor.coladd))
10179 {
10180# ifdef FEAT_MBYTE
10181 int l;
10182
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010183 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184 col += l;
10185# else
10186 if (*p != NUL && p[1] == NUL)
10187 ++col;
10188# endif
10189 }
10190 }
10191#endif
10192 }
10193 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010194 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195}
10196
Bram Moolenaar572cb562005-08-05 21:35:02 +000010197#if defined(FEAT_INS_EXPAND)
10198/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010199 * "complete()" function
10200 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010202f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010203{
10204 int startcol;
10205
10206 if ((State & INSERT) == 0)
10207 {
10208 EMSG(_("E785: complete() can only be used in Insert mode"));
10209 return;
10210 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010211
10212 /* Check for undo allowed here, because if something was already inserted
10213 * the line was already saved for undo and this check isn't done. */
10214 if (!undo_allowed())
10215 return;
10216
Bram Moolenaarade00832006-03-10 21:46:58 +000010217 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10218 {
10219 EMSG(_(e_invarg));
10220 return;
10221 }
10222
10223 startcol = get_tv_number_chk(&argvars[0], NULL);
10224 if (startcol <= 0)
10225 return;
10226
10227 set_completion(startcol - 1, argvars[1].vval.v_list);
10228}
10229
10230/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010231 * "complete_add()" function
10232 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010234f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010235{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010236 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010237}
10238
10239/*
10240 * "complete_check()" function
10241 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010243f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010244{
10245 int saved = RedrawingDisabled;
10246
10247 RedrawingDisabled = 0;
10248 ins_compl_check_keys(0);
10249 rettv->vval.v_number = compl_interrupted;
10250 RedrawingDisabled = saved;
10251}
10252#endif
10253
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254/*
10255 * "confirm(message, buttons[, default [, type]])" function
10256 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010258f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259{
10260#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10261 char_u *message;
10262 char_u *buttons = NULL;
10263 char_u buf[NUMBUFLEN];
10264 char_u buf2[NUMBUFLEN];
10265 int def = 1;
10266 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010267 char_u *typestr;
10268 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010270 message = get_tv_string_chk(&argvars[0]);
10271 if (message == NULL)
10272 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010273 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010275 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10276 if (buttons == NULL)
10277 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010278 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010280 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010281 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010283 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10284 if (typestr == NULL)
10285 error = TRUE;
10286 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010288 switch (TOUPPER_ASC(*typestr))
10289 {
10290 case 'E': type = VIM_ERROR; break;
10291 case 'Q': type = VIM_QUESTION; break;
10292 case 'I': type = VIM_INFO; break;
10293 case 'W': type = VIM_WARNING; break;
10294 case 'G': type = VIM_GENERIC; break;
10295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296 }
10297 }
10298 }
10299 }
10300
10301 if (buttons == NULL || *buttons == NUL)
10302 buttons = (char_u *)_("&Ok");
10303
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010304 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010305 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010306 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307#endif
10308}
10309
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010310/*
10311 * "copy()" function
10312 */
10313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010314f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010315{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010316 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010317}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010319#ifdef FEAT_FLOAT
10320/*
10321 * "cos()" function
10322 */
10323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010324f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010325{
10326 float_T f;
10327
10328 rettv->v_type = VAR_FLOAT;
10329 if (get_float_arg(argvars, &f) == OK)
10330 rettv->vval.v_float = cos(f);
10331 else
10332 rettv->vval.v_float = 0.0;
10333}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010334
10335/*
10336 * "cosh()" function
10337 */
10338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010339f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010340{
10341 float_T f;
10342
10343 rettv->v_type = VAR_FLOAT;
10344 if (get_float_arg(argvars, &f) == OK)
10345 rettv->vval.v_float = cosh(f);
10346 else
10347 rettv->vval.v_float = 0.0;
10348}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010349#endif
10350
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010352 * "count()" function
10353 */
10354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010355f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010356{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010357 long n = 0;
10358 int ic = FALSE;
10359
Bram Moolenaare9a41262005-01-15 22:18:47 +000010360 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010361 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010362 listitem_T *li;
10363 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010364 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010365
Bram Moolenaare9a41262005-01-15 22:18:47 +000010366 if ((l = argvars[0].vval.v_list) != NULL)
10367 {
10368 li = l->lv_first;
10369 if (argvars[2].v_type != VAR_UNKNOWN)
10370 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010371 int error = FALSE;
10372
10373 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010374 if (argvars[3].v_type != VAR_UNKNOWN)
10375 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010376 idx = get_tv_number_chk(&argvars[3], &error);
10377 if (!error)
10378 {
10379 li = list_find(l, idx);
10380 if (li == NULL)
10381 EMSGN(_(e_listidx), idx);
10382 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010383 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010384 if (error)
10385 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010386 }
10387
10388 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010389 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010390 ++n;
10391 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010392 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010393 else if (argvars[0].v_type == VAR_DICT)
10394 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010395 int todo;
10396 dict_T *d;
10397 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010398
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010399 if ((d = argvars[0].vval.v_dict) != NULL)
10400 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010401 int error = FALSE;
10402
Bram Moolenaare9a41262005-01-15 22:18:47 +000010403 if (argvars[2].v_type != VAR_UNKNOWN)
10404 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010405 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010406 if (argvars[3].v_type != VAR_UNKNOWN)
10407 EMSG(_(e_invarg));
10408 }
10409
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010410 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010411 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010412 {
10413 if (!HASHITEM_EMPTY(hi))
10414 {
10415 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010416 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010417 ++n;
10418 }
10419 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010420 }
10421 }
10422 else
10423 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010424 rettv->vval.v_number = n;
10425}
10426
10427/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10429 *
10430 * Checks the existence of a cscope connection.
10431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010433f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434{
10435#ifdef FEAT_CSCOPE
10436 int num = 0;
10437 char_u *dbpath = NULL;
10438 char_u *prepend = NULL;
10439 char_u buf[NUMBUFLEN];
10440
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010441 if (argvars[0].v_type != VAR_UNKNOWN
10442 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010444 num = (int)get_tv_number(&argvars[0]);
10445 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010446 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010447 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448 }
10449
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010450 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451#endif
10452}
10453
10454/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010455 * "cursor(lnum, col)" function, or
10456 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010458 * Moves the cursor to the specified line and column.
10459 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010462f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463{
10464 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010465#ifdef FEAT_VIRTUALEDIT
10466 long coladd = 0;
10467#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010468 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010470 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010471 if (argvars[1].v_type == VAR_UNKNOWN)
10472 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010473 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010474 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010475
Bram Moolenaar493c1782014-05-28 14:34:46 +020010476 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010477 {
10478 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010479 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010480 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010481 line = pos.lnum;
10482 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010483#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010484 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010485#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010486 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010487 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010488 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010489 set_curswant = FALSE;
10490 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010491 }
10492 else
10493 {
10494 line = get_tv_lnum(argvars);
10495 col = get_tv_number_chk(&argvars[1], NULL);
10496#ifdef FEAT_VIRTUALEDIT
10497 if (argvars[2].v_type != VAR_UNKNOWN)
10498 coladd = get_tv_number_chk(&argvars[2], NULL);
10499#endif
10500 }
10501 if (line < 0 || col < 0
10502#ifdef FEAT_VIRTUALEDIT
10503 || coladd < 0
10504#endif
10505 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010506 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507 if (line > 0)
10508 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 if (col > 0)
10510 curwin->w_cursor.col = col - 1;
10511#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010512 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513#endif
10514
10515 /* Make sure the cursor is in a valid position. */
10516 check_cursor();
10517#ifdef FEAT_MBYTE
10518 /* Correct cursor for multi-byte character. */
10519 if (has_mbyte)
10520 mb_adjust_cursor();
10521#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010522
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010523 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010524 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525}
10526
10527/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010528 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 */
10530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010531f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010533 int noref = 0;
10534
10535 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010536 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010537 if (noref < 0 || noref > 1)
10538 EMSG(_(e_invarg));
10539 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010540 {
10541 current_copyID += COPYID_INC;
10542 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544}
10545
10546/*
10547 * "delete()" function
10548 */
10549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010550f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010552 char_u nbuf[NUMBUFLEN];
10553 char_u *name;
10554 char_u *flags;
10555
10556 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010558 return;
10559
10560 name = get_tv_string(&argvars[0]);
10561 if (name == NULL || *name == NUL)
10562 {
10563 EMSG(_(e_invarg));
10564 return;
10565 }
10566
10567 if (argvars[1].v_type != VAR_UNKNOWN)
10568 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010570 flags = (char_u *)"";
10571
10572 if (*flags == NUL)
10573 /* delete a file */
10574 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10575 else if (STRCMP(flags, "d") == 0)
10576 /* delete an empty directory */
10577 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10578 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010579 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010580 rettv->vval.v_number = delete_recursive(name);
10581 else
10582 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583}
10584
10585/*
10586 * "did_filetype()" function
10587 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010589f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590{
10591#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010592 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593#endif
10594}
10595
10596/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010597 * "diff_filler()" function
10598 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010600f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010601{
10602#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010603 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010604#endif
10605}
10606
10607/*
10608 * "diff_hlID()" function
10609 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010611f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010612{
10613#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010614 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010615 static linenr_T prev_lnum = 0;
10616 static int changedtick = 0;
10617 static int fnum = 0;
10618 static int change_start = 0;
10619 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010620 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010621 int filler_lines;
10622 int col;
10623
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010624 if (lnum < 0) /* ignore type error in {lnum} arg */
10625 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010626 if (lnum != prev_lnum
10627 || changedtick != curbuf->b_changedtick
10628 || fnum != curbuf->b_fnum)
10629 {
10630 /* New line, buffer, change: need to get the values. */
10631 filler_lines = diff_check(curwin, lnum);
10632 if (filler_lines < 0)
10633 {
10634 if (filler_lines == -1)
10635 {
10636 change_start = MAXCOL;
10637 change_end = -1;
10638 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10639 hlID = HLF_ADD; /* added line */
10640 else
10641 hlID = HLF_CHD; /* changed line */
10642 }
10643 else
10644 hlID = HLF_ADD; /* added line */
10645 }
10646 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010647 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010648 prev_lnum = lnum;
10649 changedtick = curbuf->b_changedtick;
10650 fnum = curbuf->b_fnum;
10651 }
10652
10653 if (hlID == HLF_CHD || hlID == HLF_TXD)
10654 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010655 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010656 if (col >= change_start && col <= change_end)
10657 hlID = HLF_TXD; /* changed text */
10658 else
10659 hlID = HLF_CHD; /* changed line */
10660 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010661 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010662#endif
10663}
10664
10665/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010666 * "disable_char_avail_for_testing({expr})" function
10667 */
10668 static void
10669f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10670{
10671 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10672}
10673
10674/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010675 * "empty({expr})" function
10676 */
10677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010678f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010679{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010680 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010681
10682 switch (argvars[0].v_type)
10683 {
10684 case VAR_STRING:
10685 case VAR_FUNC:
10686 n = argvars[0].vval.v_string == NULL
10687 || *argvars[0].vval.v_string == NUL;
10688 break;
10689 case VAR_NUMBER:
10690 n = argvars[0].vval.v_number == 0;
10691 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010692 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010693#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010694 n = argvars[0].vval.v_float == 0.0;
10695 break;
10696#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010697 case VAR_LIST:
10698 n = argvars[0].vval.v_list == NULL
10699 || argvars[0].vval.v_list->lv_first == NULL;
10700 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010701 case VAR_DICT:
10702 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010703 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010704 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010705 case VAR_SPECIAL:
10706 n = argvars[0].vval.v_number != VVAL_TRUE;
10707 break;
10708
Bram Moolenaar835dc632016-02-07 14:27:38 +010010709 case VAR_JOB:
10710#ifdef FEAT_JOB
10711 n = argvars[0].vval.v_job->jv_status != JOB_STARTED;
10712 break;
10713#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010714 case VAR_UNKNOWN:
10715 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10716 n = TRUE;
10717 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010718 }
10719
10720 rettv->vval.v_number = n;
10721}
10722
10723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 * "escape({string}, {chars})" function
10725 */
10726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010727f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728{
10729 char_u buf[NUMBUFLEN];
10730
Bram Moolenaar758711c2005-02-02 23:11:38 +000010731 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10732 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010733 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734}
10735
10736/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010737 * "eval()" function
10738 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010740f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010741{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010742 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010744 s = get_tv_string_chk(&argvars[0]);
10745 if (s != NULL)
10746 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010747
Bram Moolenaar615b9972015-01-14 17:15:05 +010010748 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010749 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10750 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010751 if (p != NULL && !aborting())
10752 EMSG2(_(e_invexpr2), p);
10753 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010754 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010755 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010756 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010757 else if (*s != NUL)
10758 EMSG(_(e_trailing));
10759}
10760
10761/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762 * "eventhandler()" function
10763 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010765f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010767 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768}
10769
10770/*
10771 * "executable()" function
10772 */
10773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010774f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010776 char_u *name = get_tv_string(&argvars[0]);
10777
10778 /* Check in $PATH and also check directly if there is a directory name. */
10779 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10780 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010781}
10782
10783/*
10784 * "exepath()" function
10785 */
10786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010787f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010788{
10789 char_u *p = NULL;
10790
Bram Moolenaarb5971142015-03-21 17:32:19 +010010791 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010792 rettv->v_type = VAR_STRING;
10793 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794}
10795
10796/*
10797 * "exists()" function
10798 */
10799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010800f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801{
10802 char_u *p;
10803 char_u *name;
10804 int n = FALSE;
10805 int len = 0;
10806
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010807 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808 if (*p == '$') /* environment variable */
10809 {
10810 /* first try "normal" environment variables (fast) */
10811 if (mch_getenv(p + 1) != NULL)
10812 n = TRUE;
10813 else
10814 {
10815 /* try expanding things like $VIM and ${HOME} */
10816 p = expand_env_save(p);
10817 if (p != NULL && *p != '$')
10818 n = TRUE;
10819 vim_free(p);
10820 }
10821 }
10822 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010823 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010824 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010825 if (*skipwhite(p) != NUL)
10826 n = FALSE; /* trailing garbage */
10827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 else if (*p == '*') /* internal or user defined function */
10829 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010830 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831 }
10832 else if (*p == ':')
10833 {
10834 n = cmd_exists(p + 1);
10835 }
10836 else if (*p == '#')
10837 {
10838#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010839 if (p[1] == '#')
10840 n = autocmd_supported(p + 2);
10841 else
10842 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843#endif
10844 }
10845 else /* internal variable */
10846 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010847 char_u *tofree;
10848 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010850 /* get_name_len() takes care of expanding curly braces */
10851 name = p;
10852 len = get_name_len(&p, &tofree, TRUE, FALSE);
10853 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010855 if (tofree != NULL)
10856 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010857 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010858 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010860 /* handle d.key, l[idx], f(expr) */
10861 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10862 if (n)
10863 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 }
10865 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010866 if (*p != NUL)
10867 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010869 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 }
10871
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010872 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873}
10874
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010875#ifdef FEAT_FLOAT
10876/*
10877 * "exp()" function
10878 */
10879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010880f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010881{
10882 float_T f;
10883
10884 rettv->v_type = VAR_FLOAT;
10885 if (get_float_arg(argvars, &f) == OK)
10886 rettv->vval.v_float = exp(f);
10887 else
10888 rettv->vval.v_float = 0.0;
10889}
10890#endif
10891
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892/*
10893 * "expand()" function
10894 */
10895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010896f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010897{
10898 char_u *s;
10899 int len;
10900 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010901 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010903 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010904 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010906 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010907 if (argvars[1].v_type != VAR_UNKNOWN
10908 && argvars[2].v_type != VAR_UNKNOWN
10909 && get_tv_number_chk(&argvars[2], &error)
10910 && !error)
10911 {
10912 rettv->v_type = VAR_LIST;
10913 rettv->vval.v_list = NULL;
10914 }
10915
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010916 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917 if (*s == '%' || *s == '#' || *s == '<')
10918 {
10919 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010920 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010922 if (rettv->v_type == VAR_LIST)
10923 {
10924 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10925 list_append_string(rettv->vval.v_list, result, -1);
10926 else
10927 vim_free(result);
10928 }
10929 else
10930 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931 }
10932 else
10933 {
10934 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010935 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010936 if (argvars[1].v_type != VAR_UNKNOWN
10937 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010938 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010939 if (!error)
10940 {
10941 ExpandInit(&xpc);
10942 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010943 if (p_wic)
10944 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010945 if (rettv->v_type == VAR_STRING)
10946 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10947 options, WILD_ALL);
10948 else if (rettv_list_alloc(rettv) != FAIL)
10949 {
10950 int i;
10951
10952 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10953 for (i = 0; i < xpc.xp_numfiles; i++)
10954 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10955 ExpandCleanup(&xpc);
10956 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010957 }
10958 else
10959 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960 }
10961}
10962
10963/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010964 * Go over all entries in "d2" and add them to "d1".
10965 * When "action" is "error" then a duplicate key is an error.
10966 * When "action" is "force" then a duplicate key is overwritten.
10967 * Otherwise duplicate keys are ignored ("action" is "keep").
10968 */
10969 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010970dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020010971{
10972 dictitem_T *di1;
10973 hashitem_T *hi2;
10974 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010975 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010976
10977 todo = (int)d2->dv_hashtab.ht_used;
10978 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10979 {
10980 if (!HASHITEM_EMPTY(hi2))
10981 {
10982 --todo;
10983 di1 = dict_find(d1, hi2->hi_key, -1);
10984 if (d1->dv_scope != 0)
10985 {
10986 /* Disallow replacing a builtin function in l: and g:.
10987 * Check the key to be valid when adding to any
10988 * scope. */
10989 if (d1->dv_scope == VAR_DEF_SCOPE
10990 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10991 && var_check_func_name(hi2->hi_key,
10992 di1 == NULL))
10993 break;
10994 if (!valid_varname(hi2->hi_key))
10995 break;
10996 }
10997 if (di1 == NULL)
10998 {
10999 di1 = dictitem_copy(HI2DI(hi2));
11000 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11001 dictitem_free(di1);
11002 }
11003 else if (*action == 'e')
11004 {
11005 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11006 break;
11007 }
11008 else if (*action == 'f' && HI2DI(hi2) != di1)
11009 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011010 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11011 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011012 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011013 clear_tv(&di1->di_tv);
11014 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11015 }
11016 }
11017 }
11018}
11019
11020/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011021 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011022 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011023 */
11024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011025f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011026{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011027 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011028
Bram Moolenaare9a41262005-01-15 22:18:47 +000011029 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011030 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011031 list_T *l1, *l2;
11032 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011033 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011034 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011035
Bram Moolenaare9a41262005-01-15 22:18:47 +000011036 l1 = argvars[0].vval.v_list;
11037 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011038 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011039 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011040 {
11041 if (argvars[2].v_type != VAR_UNKNOWN)
11042 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011043 before = get_tv_number_chk(&argvars[2], &error);
11044 if (error)
11045 return; /* type error; errmsg already given */
11046
Bram Moolenaar758711c2005-02-02 23:11:38 +000011047 if (before == l1->lv_len)
11048 item = NULL;
11049 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011050 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011051 item = list_find(l1, before);
11052 if (item == NULL)
11053 {
11054 EMSGN(_(e_listidx), before);
11055 return;
11056 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011057 }
11058 }
11059 else
11060 item = NULL;
11061 list_extend(l1, l2, item);
11062
Bram Moolenaare9a41262005-01-15 22:18:47 +000011063 copy_tv(&argvars[0], rettv);
11064 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011065 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011066 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11067 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011068 dict_T *d1, *d2;
11069 char_u *action;
11070 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011071
11072 d1 = argvars[0].vval.v_dict;
11073 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011074 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011075 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011076 {
11077 /* Check the third argument. */
11078 if (argvars[2].v_type != VAR_UNKNOWN)
11079 {
11080 static char *(av[]) = {"keep", "force", "error"};
11081
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011082 action = get_tv_string_chk(&argvars[2]);
11083 if (action == NULL)
11084 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011085 for (i = 0; i < 3; ++i)
11086 if (STRCMP(action, av[i]) == 0)
11087 break;
11088 if (i == 3)
11089 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011090 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011091 return;
11092 }
11093 }
11094 else
11095 action = (char_u *)"force";
11096
Bram Moolenaara9922d62013-05-30 13:01:18 +020011097 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011098
Bram Moolenaare9a41262005-01-15 22:18:47 +000011099 copy_tv(&argvars[0], rettv);
11100 }
11101 }
11102 else
11103 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011104}
11105
11106/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011107 * "feedkeys()" function
11108 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011110f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011111{
11112 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011113 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011114 char_u *keys, *flags;
11115 char_u nbuf[NUMBUFLEN];
11116 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011117 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011118 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011119
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011120 /* This is not allowed in the sandbox. If the commands would still be
11121 * executed in the sandbox it would be OK, but it probably happens later,
11122 * when "sandbox" is no longer set. */
11123 if (check_secure())
11124 return;
11125
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011126 keys = get_tv_string(&argvars[0]);
11127 if (*keys != NUL)
11128 {
11129 if (argvars[1].v_type != VAR_UNKNOWN)
11130 {
11131 flags = get_tv_string_buf(&argvars[1], nbuf);
11132 for ( ; *flags != NUL; ++flags)
11133 {
11134 switch (*flags)
11135 {
11136 case 'n': remap = FALSE; break;
11137 case 'm': remap = TRUE; break;
11138 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011139 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011140 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011141 }
11142 }
11143 }
11144
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011145 /* Need to escape K_SPECIAL and CSI before putting the string in the
11146 * typeahead buffer. */
11147 keys_esc = vim_strsave_escape_csi(keys);
11148 if (keys_esc != NULL)
11149 {
11150 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011151 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011152 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011153 if (vgetc_busy)
11154 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011155 if (execute)
11156 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011157 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011158 }
11159}
11160
11161/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162 * "filereadable()" function
11163 */
11164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011165f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011167 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168 char_u *p;
11169 int n;
11170
Bram Moolenaarc236c162008-07-13 17:41:49 +000011171#ifndef O_NONBLOCK
11172# define O_NONBLOCK 0
11173#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011174 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011175 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11176 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177 {
11178 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011179 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180 }
11181 else
11182 n = FALSE;
11183
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011184 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185}
11186
11187/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011188 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011189 * rights to write into.
11190 */
11191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011192f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011194 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195}
11196
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011197 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011198findfilendir(
11199 typval_T *argvars UNUSED,
11200 typval_T *rettv,
11201 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011202{
11203#ifdef FEAT_SEARCHPATH
11204 char_u *fname;
11205 char_u *fresult = NULL;
11206 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11207 char_u *p;
11208 char_u pathbuf[NUMBUFLEN];
11209 int count = 1;
11210 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011211 int error = FALSE;
11212#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011213
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011214 rettv->vval.v_string = NULL;
11215 rettv->v_type = VAR_STRING;
11216
11217#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011218 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011219
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011220 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011221 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011222 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11223 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011224 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011225 else
11226 {
11227 if (*p != NUL)
11228 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011229
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011230 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011231 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011232 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011233 }
11234
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011235 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11236 error = TRUE;
11237
11238 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011239 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011240 do
11241 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011242 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011243 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011244 fresult = find_file_in_path_option(first ? fname : NULL,
11245 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011246 0, first, path,
11247 find_what,
11248 curbuf->b_ffname,
11249 find_what == FINDFILE_DIR
11250 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011251 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011252
11253 if (fresult != NULL && rettv->v_type == VAR_LIST)
11254 list_append_string(rettv->vval.v_list, fresult, -1);
11255
11256 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011257 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011258
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011259 if (rettv->v_type == VAR_STRING)
11260 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011261#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011262}
11263
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011264static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11265static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011266
11267/*
11268 * Implementation of map() and filter().
11269 */
11270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011271filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011272{
11273 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011274 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011275 listitem_T *li, *nli;
11276 list_T *l = NULL;
11277 dictitem_T *di;
11278 hashtab_T *ht;
11279 hashitem_T *hi;
11280 dict_T *d = NULL;
11281 typval_T save_val;
11282 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011283 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011284 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011285 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011286 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011287 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011288 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011289 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011290
Bram Moolenaare9a41262005-01-15 22:18:47 +000011291 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011292 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011293 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011294 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011295 return;
11296 }
11297 else if (argvars[0].v_type == VAR_DICT)
11298 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011299 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011300 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011301 return;
11302 }
11303 else
11304 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011305 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011306 return;
11307 }
11308
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011309 expr = get_tv_string_buf_chk(&argvars[1], buf);
11310 /* On type errors, the preceding call has already displayed an error
11311 * message. Avoid a misleading error message for an empty string that
11312 * was not passed as argument. */
11313 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011314 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011315 prepare_vimvar(VV_VAL, &save_val);
11316 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011317
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011318 /* We reset "did_emsg" to be able to detect whether an error
11319 * occurred during evaluation of the expression. */
11320 save_did_emsg = did_emsg;
11321 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011322
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011323 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011324 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011325 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011326 vimvars[VV_KEY].vv_type = VAR_STRING;
11327
11328 ht = &d->dv_hashtab;
11329 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011330 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011331 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011332 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011333 if (!HASHITEM_EMPTY(hi))
11334 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011335 int r;
11336
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011337 --todo;
11338 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011339 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011340 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11341 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011342 break;
11343 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011344 r = filter_map_one(&di->di_tv, expr, map, &rem);
11345 clear_tv(&vimvars[VV_KEY].vv_tv);
11346 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011347 break;
11348 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011349 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011350 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11351 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011352 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011353 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011354 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011355 }
11356 }
11357 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011358 }
11359 else
11360 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011361 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11362
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011363 for (li = l->lv_first; li != NULL; li = nli)
11364 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011365 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011366 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011367 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011368 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011369 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011370 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011371 break;
11372 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011373 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011374 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011375 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011376 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011377
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011378 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011379 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011380
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011381 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011382 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011383
11384 copy_tv(&argvars[0], rettv);
11385}
11386
11387 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011388filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011389{
Bram Moolenaar33570922005-01-25 22:26:29 +000011390 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011391 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011392 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011393
Bram Moolenaar33570922005-01-25 22:26:29 +000011394 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011395 s = expr;
11396 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011397 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011398 if (*s != NUL) /* check for trailing chars after expr */
11399 {
11400 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011401 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011402 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011403 }
11404 if (map)
11405 {
11406 /* map(): replace the list item value */
11407 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011408 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011409 *tv = rettv;
11410 }
11411 else
11412 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011413 int error = FALSE;
11414
Bram Moolenaare9a41262005-01-15 22:18:47 +000011415 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011416 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011417 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011418 /* On type error, nothing has been removed; return FAIL to stop the
11419 * loop. The error message was given by get_tv_number_chk(). */
11420 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011421 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011422 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011423 retval = OK;
11424theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011425 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011426 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011427}
11428
11429/*
11430 * "filter()" function
11431 */
11432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011433f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011434{
11435 filter_map(argvars, rettv, FALSE);
11436}
11437
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011438/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011439 * "finddir({fname}[, {path}[, {count}]])" function
11440 */
11441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011442f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011443{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011444 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011445}
11446
11447/*
11448 * "findfile({fname}[, {path}[, {count}]])" function
11449 */
11450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011451f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011452{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011453 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011454}
11455
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011456#ifdef FEAT_FLOAT
11457/*
11458 * "float2nr({float})" function
11459 */
11460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011461f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011462{
11463 float_T f;
11464
11465 if (get_float_arg(argvars, &f) == OK)
11466 {
11467 if (f < -0x7fffffff)
11468 rettv->vval.v_number = -0x7fffffff;
11469 else if (f > 0x7fffffff)
11470 rettv->vval.v_number = 0x7fffffff;
11471 else
11472 rettv->vval.v_number = (varnumber_T)f;
11473 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011474}
11475
11476/*
11477 * "floor({float})" function
11478 */
11479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011480f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011481{
11482 float_T f;
11483
11484 rettv->v_type = VAR_FLOAT;
11485 if (get_float_arg(argvars, &f) == OK)
11486 rettv->vval.v_float = floor(f);
11487 else
11488 rettv->vval.v_float = 0.0;
11489}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011490
11491/*
11492 * "fmod()" function
11493 */
11494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011495f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011496{
11497 float_T fx, fy;
11498
11499 rettv->v_type = VAR_FLOAT;
11500 if (get_float_arg(argvars, &fx) == OK
11501 && get_float_arg(&argvars[1], &fy) == OK)
11502 rettv->vval.v_float = fmod(fx, fy);
11503 else
11504 rettv->vval.v_float = 0.0;
11505}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011506#endif
11507
Bram Moolenaar0d660222005-01-07 21:51:51 +000011508/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011509 * "fnameescape({string})" function
11510 */
11511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011512f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011513{
11514 rettv->vval.v_string = vim_strsave_fnameescape(
11515 get_tv_string(&argvars[0]), FALSE);
11516 rettv->v_type = VAR_STRING;
11517}
11518
11519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520 * "fnamemodify({fname}, {mods})" function
11521 */
11522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011523f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524{
11525 char_u *fname;
11526 char_u *mods;
11527 int usedlen = 0;
11528 int len;
11529 char_u *fbuf = NULL;
11530 char_u buf[NUMBUFLEN];
11531
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011532 fname = get_tv_string_chk(&argvars[0]);
11533 mods = get_tv_string_buf_chk(&argvars[1], buf);
11534 if (fname == NULL || mods == NULL)
11535 fname = NULL;
11536 else
11537 {
11538 len = (int)STRLEN(fname);
11539 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011542 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011543 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011544 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011546 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011547 vim_free(fbuf);
11548}
11549
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011550static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551
11552/*
11553 * "foldclosed()" function
11554 */
11555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011556foldclosed_both(
11557 typval_T *argvars UNUSED,
11558 typval_T *rettv,
11559 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011560{
11561#ifdef FEAT_FOLDING
11562 linenr_T lnum;
11563 linenr_T first, last;
11564
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011565 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11567 {
11568 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11569 {
11570 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011571 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011573 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011574 return;
11575 }
11576 }
11577#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011578 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011579}
11580
11581/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011582 * "foldclosed()" function
11583 */
11584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011585f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011586{
11587 foldclosed_both(argvars, rettv, FALSE);
11588}
11589
11590/*
11591 * "foldclosedend()" function
11592 */
11593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011594f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011595{
11596 foldclosed_both(argvars, rettv, TRUE);
11597}
11598
11599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600 * "foldlevel()" function
11601 */
11602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011603f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604{
11605#ifdef FEAT_FOLDING
11606 linenr_T lnum;
11607
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011608 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011609 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011610 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612}
11613
11614/*
11615 * "foldtext()" function
11616 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011618f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011619{
11620#ifdef FEAT_FOLDING
11621 linenr_T lnum;
11622 char_u *s;
11623 char_u *r;
11624 int len;
11625 char *txt;
11626#endif
11627
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011628 rettv->v_type = VAR_STRING;
11629 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011630#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011631 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11632 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11633 <= curbuf->b_ml.ml_line_count
11634 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011635 {
11636 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011637 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11638 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639 {
11640 if (!linewhite(lnum))
11641 break;
11642 ++lnum;
11643 }
11644
11645 /* Find interesting text in this line. */
11646 s = skipwhite(ml_get(lnum));
11647 /* skip C comment-start */
11648 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011649 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011650 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011651 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011652 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011653 {
11654 s = skipwhite(ml_get(lnum + 1));
11655 if (*s == '*')
11656 s = skipwhite(s + 1);
11657 }
11658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659 txt = _("+-%s%3ld lines: ");
11660 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011661 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662 + 20 /* for %3ld */
11663 + STRLEN(s))); /* concatenated */
11664 if (r != NULL)
11665 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011666 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11667 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11668 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669 len = (int)STRLEN(r);
11670 STRCAT(r, s);
11671 /* remove 'foldmarker' and 'commentstring' */
11672 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011673 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674 }
11675 }
11676#endif
11677}
11678
11679/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011680 * "foldtextresult(lnum)" function
11681 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011683f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011684{
11685#ifdef FEAT_FOLDING
11686 linenr_T lnum;
11687 char_u *text;
11688 char_u buf[51];
11689 foldinfo_T foldinfo;
11690 int fold_count;
11691#endif
11692
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011693 rettv->v_type = VAR_STRING;
11694 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011695#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011696 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011697 /* treat illegal types and illegal string values for {lnum} the same */
11698 if (lnum < 0)
11699 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011700 fold_count = foldedCount(curwin, lnum, &foldinfo);
11701 if (fold_count > 0)
11702 {
11703 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11704 &foldinfo, buf);
11705 if (text == buf)
11706 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011707 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011708 }
11709#endif
11710}
11711
11712/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011713 * "foreground()" function
11714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011716f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011717{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718#ifdef FEAT_GUI
11719 if (gui.in_use)
11720 gui_mch_set_foreground();
11721#else
11722# ifdef WIN32
11723 win32_set_foreground();
11724# endif
11725#endif
11726}
11727
11728/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011729 * "function()" function
11730 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011732f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011733{
11734 char_u *s;
11735
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011736 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011737 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011738 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011739 /* Don't check an autoload name for existence here. */
11740 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011741 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011742 else
11743 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011744 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011745 {
11746 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011747 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011748
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011749 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11750 * also be called from another script. Using trans_function_name()
11751 * would also work, but some plugins depend on the name being
11752 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011753 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011754 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011755 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011756 if (rettv->vval.v_string != NULL)
11757 {
11758 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011759 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011760 }
11761 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011762 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011763 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011764 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011765 }
11766}
11767
11768/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011769 * "garbagecollect()" function
11770 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011772f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011773{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011774 /* This is postponed until we are back at the toplevel, because we may be
11775 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11776 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011777
11778 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11779 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011780}
11781
11782/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011783 * "get()" function
11784 */
11785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011786f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011787{
Bram Moolenaar33570922005-01-25 22:26:29 +000011788 listitem_T *li;
11789 list_T *l;
11790 dictitem_T *di;
11791 dict_T *d;
11792 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011793
Bram Moolenaare9a41262005-01-15 22:18:47 +000011794 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011795 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011796 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011797 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011798 int error = FALSE;
11799
11800 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11801 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011802 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011803 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011804 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011805 else if (argvars[0].v_type == VAR_DICT)
11806 {
11807 if ((d = argvars[0].vval.v_dict) != NULL)
11808 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011809 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011810 if (di != NULL)
11811 tv = &di->di_tv;
11812 }
11813 }
11814 else
11815 EMSG2(_(e_listdictarg), "get()");
11816
11817 if (tv == NULL)
11818 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011819 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011820 copy_tv(&argvars[2], rettv);
11821 }
11822 else
11823 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011824}
11825
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011826static 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 +000011827
11828/*
11829 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011830 * Return a range (from start to end) of lines in rettv from the specified
11831 * buffer.
11832 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011833 */
11834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011835get_buffer_lines(
11836 buf_T *buf,
11837 linenr_T start,
11838 linenr_T end,
11839 int retlist,
11840 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011841{
11842 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011843
Bram Moolenaar959a1432013-12-14 12:17:38 +010011844 rettv->v_type = VAR_STRING;
11845 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011846 if (retlist && rettv_list_alloc(rettv) == FAIL)
11847 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011848
11849 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11850 return;
11851
11852 if (!retlist)
11853 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011854 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11855 p = ml_get_buf(buf, start, FALSE);
11856 else
11857 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011858 rettv->vval.v_string = vim_strsave(p);
11859 }
11860 else
11861 {
11862 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011863 return;
11864
11865 if (start < 1)
11866 start = 1;
11867 if (end > buf->b_ml.ml_line_count)
11868 end = buf->b_ml.ml_line_count;
11869 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011870 if (list_append_string(rettv->vval.v_list,
11871 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011872 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011873 }
11874}
11875
11876/*
11877 * "getbufline()" function
11878 */
11879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011880f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011881{
11882 linenr_T lnum;
11883 linenr_T end;
11884 buf_T *buf;
11885
11886 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11887 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011888 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011889 --emsg_off;
11890
Bram Moolenaar661b1822005-07-28 22:36:45 +000011891 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011892 if (argvars[2].v_type == VAR_UNKNOWN)
11893 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011894 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011895 end = get_tv_lnum_buf(&argvars[2], buf);
11896
Bram Moolenaar342337a2005-07-21 21:11:17 +000011897 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011898}
11899
Bram Moolenaar0d660222005-01-07 21:51:51 +000011900/*
11901 * "getbufvar()" function
11902 */
11903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011904f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011905{
11906 buf_T *buf;
11907 buf_T *save_curbuf;
11908 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011909 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011910 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011911
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011912 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11913 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011914 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011915 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011916
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011917 rettv->v_type = VAR_STRING;
11918 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011919
11920 if (buf != NULL && varname != NULL)
11921 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011922 /* set curbuf to be our buf, temporarily */
11923 save_curbuf = curbuf;
11924 curbuf = buf;
11925
Bram Moolenaar0d660222005-01-07 21:51:51 +000011926 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011927 {
11928 if (get_option_tv(&varname, rettv, TRUE) == OK)
11929 done = TRUE;
11930 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011931 else if (STRCMP(varname, "changedtick") == 0)
11932 {
11933 rettv->v_type = VAR_NUMBER;
11934 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011935 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011936 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011937 else
11938 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011939 /* Look up the variable. */
11940 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11941 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11942 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011943 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011944 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011945 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011946 done = TRUE;
11947 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011948 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011949
11950 /* restore previous notion of curbuf */
11951 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011952 }
11953
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011954 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11955 /* use the default value */
11956 copy_tv(&argvars[2], rettv);
11957
Bram Moolenaar0d660222005-01-07 21:51:51 +000011958 --emsg_off;
11959}
11960
11961/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011962 * "getchar()" function
11963 */
11964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011965f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966{
11967 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011968 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011970 /* Position the cursor. Needed after a message that ends in a space. */
11971 windgoto(msg_row, msg_col);
11972
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973 ++no_mapping;
11974 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011975 for (;;)
11976 {
11977 if (argvars[0].v_type == VAR_UNKNOWN)
11978 /* getchar(): blocking wait. */
11979 n = safe_vgetc();
11980 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11981 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011982 n = vpeekc_any();
11983 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011984 /* illegal argument or getchar(0) and no char avail: return zero */
11985 n = 0;
11986 else
11987 /* getchar(0) and char avail: return char */
11988 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011989
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011990 if (n == K_IGNORE)
11991 continue;
11992 break;
11993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994 --no_mapping;
11995 --allow_keys;
11996
Bram Moolenaar219b8702006-11-01 14:32:36 +000011997 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11998 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11999 vimvars[VV_MOUSE_COL].vv_nr = 0;
12000
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012001 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002 if (IS_SPECIAL(n) || mod_mask != 0)
12003 {
12004 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12005 int i = 0;
12006
12007 /* Turn a special key into three bytes, plus modifier. */
12008 if (mod_mask != 0)
12009 {
12010 temp[i++] = K_SPECIAL;
12011 temp[i++] = KS_MODIFIER;
12012 temp[i++] = mod_mask;
12013 }
12014 if (IS_SPECIAL(n))
12015 {
12016 temp[i++] = K_SPECIAL;
12017 temp[i++] = K_SECOND(n);
12018 temp[i++] = K_THIRD(n);
12019 }
12020#ifdef FEAT_MBYTE
12021 else if (has_mbyte)
12022 i += (*mb_char2bytes)(n, temp + i);
12023#endif
12024 else
12025 temp[i++] = n;
12026 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012027 rettv->v_type = VAR_STRING;
12028 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012029
12030#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012031 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012032 {
12033 int row = mouse_row;
12034 int col = mouse_col;
12035 win_T *win;
12036 linenr_T lnum;
12037# ifdef FEAT_WINDOWS
12038 win_T *wp;
12039# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012040 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012041
12042 if (row >= 0 && col >= 0)
12043 {
12044 /* Find the window at the mouse coordinates and compute the
12045 * text position. */
12046 win = mouse_find_win(&row, &col);
12047 (void)mouse_comp_pos(win, &row, &col, &lnum);
12048# ifdef FEAT_WINDOWS
12049 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012050 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012051# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012052 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012053 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12054 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12055 }
12056 }
12057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058 }
12059}
12060
12061/*
12062 * "getcharmod()" function
12063 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012065f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012067 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068}
12069
12070/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012071 * "getcharsearch()" function
12072 */
12073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012074f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012075{
12076 if (rettv_dict_alloc(rettv) != FAIL)
12077 {
12078 dict_T *dict = rettv->vval.v_dict;
12079
12080 dict_add_nr_str(dict, "char", 0L, last_csearch());
12081 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12082 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12083 }
12084}
12085
12086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087 * "getcmdline()" function
12088 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012090f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012092 rettv->v_type = VAR_STRING;
12093 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012094}
12095
12096/*
12097 * "getcmdpos()" function
12098 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012100f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012102 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012103}
12104
12105/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012106 * "getcmdtype()" function
12107 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012109f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012110{
12111 rettv->v_type = VAR_STRING;
12112 rettv->vval.v_string = alloc(2);
12113 if (rettv->vval.v_string != NULL)
12114 {
12115 rettv->vval.v_string[0] = get_cmdline_type();
12116 rettv->vval.v_string[1] = NUL;
12117 }
12118}
12119
12120/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012121 * "getcmdwintype()" function
12122 */
12123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012124f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012125{
12126 rettv->v_type = VAR_STRING;
12127 rettv->vval.v_string = NULL;
12128#ifdef FEAT_CMDWIN
12129 rettv->vval.v_string = alloc(2);
12130 if (rettv->vval.v_string != NULL)
12131 {
12132 rettv->vval.v_string[0] = cmdwin_type;
12133 rettv->vval.v_string[1] = NUL;
12134 }
12135#endif
12136}
12137
12138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012139 * "getcwd()" function
12140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012142f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012143{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012144 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012145 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012147 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012148 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012149
12150 wp = find_tabwin(&argvars[0], &argvars[1]);
12151 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012153 if (wp->w_localdir != NULL)
12154 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12155 else if(globaldir != NULL)
12156 rettv->vval.v_string = vim_strsave(globaldir);
12157 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012158 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012159 cwd = alloc(MAXPATHL);
12160 if (cwd != NULL)
12161 {
12162 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12163 rettv->vval.v_string = vim_strsave(cwd);
12164 vim_free(cwd);
12165 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012166 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012167#ifdef BACKSLASH_IN_FILENAME
12168 if (rettv->vval.v_string != NULL)
12169 slash_adjust(rettv->vval.v_string);
12170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012171 }
12172}
12173
12174/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012175 * "getfontname()" function
12176 */
12177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012178f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012179{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012180 rettv->v_type = VAR_STRING;
12181 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012182#ifdef FEAT_GUI
12183 if (gui.in_use)
12184 {
12185 GuiFont font;
12186 char_u *name = NULL;
12187
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012188 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012189 {
12190 /* Get the "Normal" font. Either the name saved by
12191 * hl_set_font_name() or from the font ID. */
12192 font = gui.norm_font;
12193 name = hl_get_font_name();
12194 }
12195 else
12196 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012197 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012198 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12199 return;
12200 font = gui_mch_get_font(name, FALSE);
12201 if (font == NOFONT)
12202 return; /* Invalid font name, return empty string. */
12203 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012204 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012205 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012206 gui_mch_free_font(font);
12207 }
12208#endif
12209}
12210
12211/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012212 * "getfperm({fname})" function
12213 */
12214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012215f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012216{
12217 char_u *fname;
12218 struct stat st;
12219 char_u *perm = NULL;
12220 char_u flags[] = "rwx";
12221 int i;
12222
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012223 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012224
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012225 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012226 if (mch_stat((char *)fname, &st) >= 0)
12227 {
12228 perm = vim_strsave((char_u *)"---------");
12229 if (perm != NULL)
12230 {
12231 for (i = 0; i < 9; i++)
12232 {
12233 if (st.st_mode & (1 << (8 - i)))
12234 perm[i] = flags[i % 3];
12235 }
12236 }
12237 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012238 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012239}
12240
12241/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012242 * "getfsize({fname})" function
12243 */
12244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012245f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246{
12247 char_u *fname;
12248 struct stat st;
12249
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012250 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012252 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253
12254 if (mch_stat((char *)fname, &st) >= 0)
12255 {
12256 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012257 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012258 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012259 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012260 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012261
12262 /* non-perfect check for overflow */
12263 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12264 rettv->vval.v_number = -2;
12265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266 }
12267 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012268 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012269}
12270
12271/*
12272 * "getftime({fname})" function
12273 */
12274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012275f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276{
12277 char_u *fname;
12278 struct stat st;
12279
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012280 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012281
12282 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012283 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012285 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012286}
12287
12288/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012289 * "getftype({fname})" function
12290 */
12291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012292f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012293{
12294 char_u *fname;
12295 struct stat st;
12296 char_u *type = NULL;
12297 char *t;
12298
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012299 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012300
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012301 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012302 if (mch_lstat((char *)fname, &st) >= 0)
12303 {
12304#ifdef S_ISREG
12305 if (S_ISREG(st.st_mode))
12306 t = "file";
12307 else if (S_ISDIR(st.st_mode))
12308 t = "dir";
12309# ifdef S_ISLNK
12310 else if (S_ISLNK(st.st_mode))
12311 t = "link";
12312# endif
12313# ifdef S_ISBLK
12314 else if (S_ISBLK(st.st_mode))
12315 t = "bdev";
12316# endif
12317# ifdef S_ISCHR
12318 else if (S_ISCHR(st.st_mode))
12319 t = "cdev";
12320# endif
12321# ifdef S_ISFIFO
12322 else if (S_ISFIFO(st.st_mode))
12323 t = "fifo";
12324# endif
12325# ifdef S_ISSOCK
12326 else if (S_ISSOCK(st.st_mode))
12327 t = "fifo";
12328# endif
12329 else
12330 t = "other";
12331#else
12332# ifdef S_IFMT
12333 switch (st.st_mode & S_IFMT)
12334 {
12335 case S_IFREG: t = "file"; break;
12336 case S_IFDIR: t = "dir"; break;
12337# ifdef S_IFLNK
12338 case S_IFLNK: t = "link"; break;
12339# endif
12340# ifdef S_IFBLK
12341 case S_IFBLK: t = "bdev"; break;
12342# endif
12343# ifdef S_IFCHR
12344 case S_IFCHR: t = "cdev"; break;
12345# endif
12346# ifdef S_IFIFO
12347 case S_IFIFO: t = "fifo"; break;
12348# endif
12349# ifdef S_IFSOCK
12350 case S_IFSOCK: t = "socket"; break;
12351# endif
12352 default: t = "other";
12353 }
12354# else
12355 if (mch_isdir(fname))
12356 t = "dir";
12357 else
12358 t = "file";
12359# endif
12360#endif
12361 type = vim_strsave((char_u *)t);
12362 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012363 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012364}
12365
12366/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012367 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012368 */
12369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012370f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012371{
12372 linenr_T lnum;
12373 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012374 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012375
12376 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012377 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012378 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012379 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012380 retlist = FALSE;
12381 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012382 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012383 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012384 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012385 retlist = TRUE;
12386 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012387
Bram Moolenaar342337a2005-07-21 21:11:17 +000012388 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012389}
12390
12391/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012392 * "getmatches()" function
12393 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012395f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012396{
12397#ifdef FEAT_SEARCH_EXTRA
12398 dict_T *dict;
12399 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012400 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012401
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012402 if (rettv_list_alloc(rettv) == OK)
12403 {
12404 while (cur != NULL)
12405 {
12406 dict = dict_alloc();
12407 if (dict == NULL)
12408 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012409 if (cur->match.regprog == NULL)
12410 {
12411 /* match added with matchaddpos() */
12412 for (i = 0; i < MAXPOSMATCH; ++i)
12413 {
12414 llpos_T *llpos;
12415 char buf[6];
12416 list_T *l;
12417
12418 llpos = &cur->pos.pos[i];
12419 if (llpos->lnum == 0)
12420 break;
12421 l = list_alloc();
12422 if (l == NULL)
12423 break;
12424 list_append_number(l, (varnumber_T)llpos->lnum);
12425 if (llpos->col > 0)
12426 {
12427 list_append_number(l, (varnumber_T)llpos->col);
12428 list_append_number(l, (varnumber_T)llpos->len);
12429 }
12430 sprintf(buf, "pos%d", i + 1);
12431 dict_add_list(dict, buf, l);
12432 }
12433 }
12434 else
12435 {
12436 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12437 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012438 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012439 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12440 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012441# ifdef FEAT_CONCEAL
12442 if (cur->conceal_char)
12443 {
12444 char_u buf[MB_MAXBYTES + 1];
12445
12446 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12447 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12448 }
12449# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012450 list_append_dict(rettv->vval.v_list, dict);
12451 cur = cur->next;
12452 }
12453 }
12454#endif
12455}
12456
12457/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012458 * "getpid()" function
12459 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012461f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012462{
12463 rettv->vval.v_number = mch_get_pid();
12464}
12465
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012466static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012467
12468/*
12469 * "getcurpos()" function
12470 */
12471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012472f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012473{
12474 getpos_both(argvars, rettv, TRUE);
12475}
12476
Bram Moolenaar18081e32008-02-20 19:11:07 +000012477/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012478 * "getpos(string)" function
12479 */
12480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012481f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012482{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012483 getpos_both(argvars, rettv, FALSE);
12484}
12485
12486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012487getpos_both(
12488 typval_T *argvars,
12489 typval_T *rettv,
12490 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012491{
Bram Moolenaara5525202006-03-02 22:52:09 +000012492 pos_T *fp;
12493 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012494 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012495
12496 if (rettv_list_alloc(rettv) == OK)
12497 {
12498 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012499 if (getcurpos)
12500 fp = &curwin->w_cursor;
12501 else
12502 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012503 if (fnum != -1)
12504 list_append_number(l, (varnumber_T)fnum);
12505 else
12506 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012507 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12508 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012509 list_append_number(l, (fp != NULL)
12510 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012511 : (varnumber_T)0);
12512 list_append_number(l,
12513#ifdef FEAT_VIRTUALEDIT
12514 (fp != NULL) ? (varnumber_T)fp->coladd :
12515#endif
12516 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012517 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012518 {
12519 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012520 list_append_number(l, curwin->w_curswant == MAXCOL ?
12521 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012522 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012523 }
12524 else
12525 rettv->vval.v_number = FALSE;
12526}
12527
12528/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012529 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012530 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012532f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012533{
12534#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012535 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012536#endif
12537
Bram Moolenaar2641f772005-03-25 21:58:17 +000012538#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012539 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012540 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012541 wp = NULL;
12542 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12543 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012544 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012545 if (wp == NULL)
12546 return;
12547 }
12548
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012549 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012550 }
12551#endif
12552}
12553
12554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555 * "getreg()" function
12556 */
12557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012558f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012559{
12560 char_u *strregname;
12561 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012562 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012563 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012564 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012565
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012566 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012567 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012568 strregname = get_tv_string_chk(&argvars[0]);
12569 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012570 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012571 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012572 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012573 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12574 return_list = get_tv_number_chk(&argvars[2], &error);
12575 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012577 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012578 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012579
12580 if (error)
12581 return;
12582
Bram Moolenaar071d4272004-06-13 20:20:40 +000012583 regname = (strregname == NULL ? '"' : *strregname);
12584 if (regname == 0)
12585 regname = '"';
12586
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012587 if (return_list)
12588 {
12589 rettv->v_type = VAR_LIST;
12590 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12591 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012592 if (rettv->vval.v_list != NULL)
12593 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012594 }
12595 else
12596 {
12597 rettv->v_type = VAR_STRING;
12598 rettv->vval.v_string = get_reg_contents(regname,
12599 arg2 ? GREG_EXPR_SRC : 0);
12600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012601}
12602
12603/*
12604 * "getregtype()" function
12605 */
12606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012607f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012608{
12609 char_u *strregname;
12610 int regname;
12611 char_u buf[NUMBUFLEN + 2];
12612 long reglen = 0;
12613
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012614 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012615 {
12616 strregname = get_tv_string_chk(&argvars[0]);
12617 if (strregname == NULL) /* type error; errmsg already given */
12618 {
12619 rettv->v_type = VAR_STRING;
12620 rettv->vval.v_string = NULL;
12621 return;
12622 }
12623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012624 else
12625 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012626 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012627
12628 regname = (strregname == NULL ? '"' : *strregname);
12629 if (regname == 0)
12630 regname = '"';
12631
12632 buf[0] = NUL;
12633 buf[1] = NUL;
12634 switch (get_reg_type(regname, &reglen))
12635 {
12636 case MLINE: buf[0] = 'V'; break;
12637 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012638 case MBLOCK:
12639 buf[0] = Ctrl_V;
12640 sprintf((char *)buf + 1, "%ld", reglen + 1);
12641 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012642 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012643 rettv->v_type = VAR_STRING;
12644 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645}
12646
12647/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012648 * "gettabvar()" function
12649 */
12650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012651f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012652{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012653 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012654 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012655 dictitem_T *v;
12656 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012657 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012658
12659 rettv->v_type = VAR_STRING;
12660 rettv->vval.v_string = NULL;
12661
12662 varname = get_tv_string_chk(&argvars[1]);
12663 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12664 if (tp != NULL && varname != NULL)
12665 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012666 /* Set tp to be our tabpage, temporarily. Also set the window to the
12667 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012668 if (switch_win(&oldcurwin, &oldtabpage,
12669 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012670 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012671 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012672 /* look up the variable */
12673 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12674 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12675 if (v != NULL)
12676 {
12677 copy_tv(&v->di_tv, rettv);
12678 done = TRUE;
12679 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012680 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012681
12682 /* restore previous notion of curwin */
12683 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012684 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012685
12686 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012687 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012688}
12689
12690/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012691 * "gettabwinvar()" function
12692 */
12693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012694f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012695{
12696 getwinvar(argvars, rettv, 1);
12697}
12698
12699/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012700 * "getwinposx()" function
12701 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012703f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012704{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012705 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706#ifdef FEAT_GUI
12707 if (gui.in_use)
12708 {
12709 int x, y;
12710
12711 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012712 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713 }
12714#endif
12715}
12716
12717/*
12718 * "getwinposy()" function
12719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012721f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012723 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724#ifdef FEAT_GUI
12725 if (gui.in_use)
12726 {
12727 int x, y;
12728
12729 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012730 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012731 }
12732#endif
12733}
12734
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012735/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012736 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012737 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012738 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012739find_win_by_nr(
12740 typval_T *vp,
12741 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012742{
12743#ifdef FEAT_WINDOWS
12744 win_T *wp;
12745#endif
12746 int nr;
12747
12748 nr = get_tv_number_chk(vp, NULL);
12749
12750#ifdef FEAT_WINDOWS
12751 if (nr < 0)
12752 return NULL;
12753 if (nr == 0)
12754 return curwin;
12755
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012756 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12757 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012758 if (--nr <= 0)
12759 break;
12760 return wp;
12761#else
12762 if (nr == 0 || nr == 1)
12763 return curwin;
12764 return NULL;
12765#endif
12766}
12767
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012769 * Find window specified by "wvp" in tabpage "tvp".
12770 */
12771 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012772find_tabwin(
12773 typval_T *wvp, /* VAR_UNKNOWN for current window */
12774 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012775{
12776 win_T *wp = NULL;
12777 tabpage_T *tp = NULL;
12778 long n;
12779
12780 if (wvp->v_type != VAR_UNKNOWN)
12781 {
12782 if (tvp->v_type != VAR_UNKNOWN)
12783 {
12784 n = get_tv_number(tvp);
12785 if (n >= 0)
12786 tp = find_tabpage(n);
12787 }
12788 else
12789 tp = curtab;
12790
12791 if (tp != NULL)
12792 wp = find_win_by_nr(wvp, tp);
12793 }
12794 else
12795 wp = curwin;
12796
12797 return wp;
12798}
12799
12800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012801 * "getwinvar()" function
12802 */
12803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012804f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012805{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012806 getwinvar(argvars, rettv, 0);
12807}
12808
12809/*
12810 * getwinvar() and gettabwinvar()
12811 */
12812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012813getwinvar(
12814 typval_T *argvars,
12815 typval_T *rettv,
12816 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012817{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012818 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012820 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012821 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012822 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012823#ifdef FEAT_WINDOWS
12824 win_T *oldcurwin;
12825 tabpage_T *oldtabpage;
12826 int need_switch_win;
12827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012829#ifdef FEAT_WINDOWS
12830 if (off == 1)
12831 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12832 else
12833 tp = curtab;
12834#endif
12835 win = find_win_by_nr(&argvars[off], tp);
12836 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012837 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012839 rettv->v_type = VAR_STRING;
12840 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841
12842 if (win != NULL && varname != NULL)
12843 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012844#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012845 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012846 * otherwise the window is not valid. Only do this when needed,
12847 * autocommands get blocked. */
12848 need_switch_win = !(tp == curtab && win == curwin);
12849 if (!need_switch_win
12850 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12851#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012852 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012853 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012854 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012855 if (get_option_tv(&varname, rettv, 1) == OK)
12856 done = TRUE;
12857 }
12858 else
12859 {
12860 /* Look up the variable. */
12861 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12862 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12863 varname, FALSE);
12864 if (v != NULL)
12865 {
12866 copy_tv(&v->di_tv, rettv);
12867 done = TRUE;
12868 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012870 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012871
Bram Moolenaarba117c22015-09-29 16:53:22 +020012872#ifdef FEAT_WINDOWS
12873 if (need_switch_win)
12874 /* restore previous notion of curwin */
12875 restore_win(oldcurwin, oldtabpage, TRUE);
12876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877 }
12878
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012879 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12880 /* use the default return value */
12881 copy_tv(&argvars[off + 2], rettv);
12882
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883 --emsg_off;
12884}
12885
12886/*
12887 * "glob()" function
12888 */
12889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012890f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012892 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012894 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012896 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012897 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012898 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012899 if (argvars[1].v_type != VAR_UNKNOWN)
12900 {
12901 if (get_tv_number_chk(&argvars[1], &error))
12902 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012903 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012904 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012905 if (get_tv_number_chk(&argvars[2], &error))
12906 {
12907 rettv->v_type = VAR_LIST;
12908 rettv->vval.v_list = NULL;
12909 }
12910 if (argvars[3].v_type != VAR_UNKNOWN
12911 && get_tv_number_chk(&argvars[3], &error))
12912 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012913 }
12914 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012915 if (!error)
12916 {
12917 ExpandInit(&xpc);
12918 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012919 if (p_wic)
12920 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012921 if (rettv->v_type == VAR_STRING)
12922 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012923 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012924 else if (rettv_list_alloc(rettv) != FAIL)
12925 {
12926 int i;
12927
12928 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12929 NULL, options, WILD_ALL_KEEP);
12930 for (i = 0; i < xpc.xp_numfiles; i++)
12931 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12932
12933 ExpandCleanup(&xpc);
12934 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012935 }
12936 else
12937 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938}
12939
12940/*
12941 * "globpath()" function
12942 */
12943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012944f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012946 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012948 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012949 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012950 garray_T ga;
12951 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012953 /* When the optional second argument is non-zero, don't remove matches
12954 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012955 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012956 if (argvars[2].v_type != VAR_UNKNOWN)
12957 {
12958 if (get_tv_number_chk(&argvars[2], &error))
12959 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012960 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012961 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012962 if (get_tv_number_chk(&argvars[3], &error))
12963 {
12964 rettv->v_type = VAR_LIST;
12965 rettv->vval.v_list = NULL;
12966 }
12967 if (argvars[4].v_type != VAR_UNKNOWN
12968 && get_tv_number_chk(&argvars[4], &error))
12969 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012970 }
12971 }
12972 if (file != NULL && !error)
12973 {
12974 ga_init2(&ga, (int)sizeof(char_u *), 10);
12975 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12976 if (rettv->v_type == VAR_STRING)
12977 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12978 else if (rettv_list_alloc(rettv) != FAIL)
12979 for (i = 0; i < ga.ga_len; ++i)
12980 list_append_string(rettv->vval.v_list,
12981 ((char_u **)(ga.ga_data))[i], -1);
12982 ga_clear_strings(&ga);
12983 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012984 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012985 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012986}
12987
12988/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012989 * "glob2regpat()" function
12990 */
12991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012992f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012993{
12994 char_u *pat = get_tv_string_chk(&argvars[0]);
12995
12996 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010012997 rettv->vval.v_string = (pat == NULL)
12998 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012999}
13000
13001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013002 * "has()" function
13003 */
13004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013005f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013006{
13007 int i;
13008 char_u *name;
13009 int n = FALSE;
13010 static char *(has_list[]) =
13011 {
13012#ifdef AMIGA
13013 "amiga",
13014# ifdef FEAT_ARP
13015 "arp",
13016# endif
13017#endif
13018#ifdef __BEOS__
13019 "beos",
13020#endif
13021#ifdef MSDOS
13022# ifdef DJGPP
13023 "dos32",
13024# else
13025 "dos16",
13026# endif
13027#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013028#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029 "mac",
13030#endif
13031#if defined(MACOS_X_UNIX)
13032 "macunix",
13033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013034#ifdef __QNX__
13035 "qnx",
13036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013037#ifdef UNIX
13038 "unix",
13039#endif
13040#ifdef VMS
13041 "vms",
13042#endif
13043#ifdef WIN16
13044 "win16",
13045#endif
13046#ifdef WIN32
13047 "win32",
13048#endif
13049#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13050 "win32unix",
13051#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013052#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053 "win64",
13054#endif
13055#ifdef EBCDIC
13056 "ebcdic",
13057#endif
13058#ifndef CASE_INSENSITIVE_FILENAME
13059 "fname_case",
13060#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013061#ifdef HAVE_ACL
13062 "acl",
13063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013064#ifdef FEAT_ARABIC
13065 "arabic",
13066#endif
13067#ifdef FEAT_AUTOCMD
13068 "autocmd",
13069#endif
13070#ifdef FEAT_BEVAL
13071 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013072# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13073 "balloon_multiline",
13074# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075#endif
13076#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13077 "builtin_terms",
13078# ifdef ALL_BUILTIN_TCAPS
13079 "all_builtin_terms",
13080# endif
13081#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013082#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13083 || defined(FEAT_GUI_W32) \
13084 || defined(FEAT_GUI_MOTIF))
13085 "browsefilter",
13086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013087#ifdef FEAT_BYTEOFF
13088 "byte_offset",
13089#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013090#ifdef FEAT_CHANNEL
13091 "channel",
13092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013093#ifdef FEAT_CINDENT
13094 "cindent",
13095#endif
13096#ifdef FEAT_CLIENTSERVER
13097 "clientserver",
13098#endif
13099#ifdef FEAT_CLIPBOARD
13100 "clipboard",
13101#endif
13102#ifdef FEAT_CMDL_COMPL
13103 "cmdline_compl",
13104#endif
13105#ifdef FEAT_CMDHIST
13106 "cmdline_hist",
13107#endif
13108#ifdef FEAT_COMMENTS
13109 "comments",
13110#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013111#ifdef FEAT_CONCEAL
13112 "conceal",
13113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013114#ifdef FEAT_CRYPT
13115 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013116 "crypt-blowfish",
13117 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013118#endif
13119#ifdef FEAT_CSCOPE
13120 "cscope",
13121#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013122#ifdef FEAT_CURSORBIND
13123 "cursorbind",
13124#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013125#ifdef CURSOR_SHAPE
13126 "cursorshape",
13127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013128#ifdef DEBUG
13129 "debug",
13130#endif
13131#ifdef FEAT_CON_DIALOG
13132 "dialog_con",
13133#endif
13134#ifdef FEAT_GUI_DIALOG
13135 "dialog_gui",
13136#endif
13137#ifdef FEAT_DIFF
13138 "diff",
13139#endif
13140#ifdef FEAT_DIGRAPHS
13141 "digraphs",
13142#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013143#ifdef FEAT_DIRECTX
13144 "directx",
13145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146#ifdef FEAT_DND
13147 "dnd",
13148#endif
13149#ifdef FEAT_EMACS_TAGS
13150 "emacs_tags",
13151#endif
13152 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013153 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154#ifdef FEAT_SEARCH_EXTRA
13155 "extra_search",
13156#endif
13157#ifdef FEAT_FKMAP
13158 "farsi",
13159#endif
13160#ifdef FEAT_SEARCHPATH
13161 "file_in_path",
13162#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013163#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013164 "filterpipe",
13165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166#ifdef FEAT_FIND_ID
13167 "find_in_path",
13168#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013169#ifdef FEAT_FLOAT
13170 "float",
13171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013172#ifdef FEAT_FOLDING
13173 "folding",
13174#endif
13175#ifdef FEAT_FOOTER
13176 "footer",
13177#endif
13178#if !defined(USE_SYSTEM) && defined(UNIX)
13179 "fork",
13180#endif
13181#ifdef FEAT_GETTEXT
13182 "gettext",
13183#endif
13184#ifdef FEAT_GUI
13185 "gui",
13186#endif
13187#ifdef FEAT_GUI_ATHENA
13188# ifdef FEAT_GUI_NEXTAW
13189 "gui_neXtaw",
13190# else
13191 "gui_athena",
13192# endif
13193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013194#ifdef FEAT_GUI_GTK
13195 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013198#ifdef FEAT_GUI_GNOME
13199 "gui_gnome",
13200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201#ifdef FEAT_GUI_MAC
13202 "gui_mac",
13203#endif
13204#ifdef FEAT_GUI_MOTIF
13205 "gui_motif",
13206#endif
13207#ifdef FEAT_GUI_PHOTON
13208 "gui_photon",
13209#endif
13210#ifdef FEAT_GUI_W16
13211 "gui_win16",
13212#endif
13213#ifdef FEAT_GUI_W32
13214 "gui_win32",
13215#endif
13216#ifdef FEAT_HANGULIN
13217 "hangul_input",
13218#endif
13219#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13220 "iconv",
13221#endif
13222#ifdef FEAT_INS_EXPAND
13223 "insert_expand",
13224#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013225#ifdef FEAT_JOB
13226 "job",
13227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228#ifdef FEAT_JUMPLIST
13229 "jumplist",
13230#endif
13231#ifdef FEAT_KEYMAP
13232 "keymap",
13233#endif
13234#ifdef FEAT_LANGMAP
13235 "langmap",
13236#endif
13237#ifdef FEAT_LIBCALL
13238 "libcall",
13239#endif
13240#ifdef FEAT_LINEBREAK
13241 "linebreak",
13242#endif
13243#ifdef FEAT_LISP
13244 "lispindent",
13245#endif
13246#ifdef FEAT_LISTCMDS
13247 "listcmds",
13248#endif
13249#ifdef FEAT_LOCALMAP
13250 "localmap",
13251#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013252#ifdef FEAT_LUA
13253# ifndef DYNAMIC_LUA
13254 "lua",
13255# endif
13256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013257#ifdef FEAT_MENU
13258 "menu",
13259#endif
13260#ifdef FEAT_SESSION
13261 "mksession",
13262#endif
13263#ifdef FEAT_MODIFY_FNAME
13264 "modify_fname",
13265#endif
13266#ifdef FEAT_MOUSE
13267 "mouse",
13268#endif
13269#ifdef FEAT_MOUSESHAPE
13270 "mouseshape",
13271#endif
13272#if defined(UNIX) || defined(VMS)
13273# ifdef FEAT_MOUSE_DEC
13274 "mouse_dec",
13275# endif
13276# ifdef FEAT_MOUSE_GPM
13277 "mouse_gpm",
13278# endif
13279# ifdef FEAT_MOUSE_JSB
13280 "mouse_jsbterm",
13281# endif
13282# ifdef FEAT_MOUSE_NET
13283 "mouse_netterm",
13284# endif
13285# ifdef FEAT_MOUSE_PTERM
13286 "mouse_pterm",
13287# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013288# ifdef FEAT_MOUSE_SGR
13289 "mouse_sgr",
13290# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013291# ifdef FEAT_SYSMOUSE
13292 "mouse_sysmouse",
13293# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013294# ifdef FEAT_MOUSE_URXVT
13295 "mouse_urxvt",
13296# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297# ifdef FEAT_MOUSE_XTERM
13298 "mouse_xterm",
13299# endif
13300#endif
13301#ifdef FEAT_MBYTE
13302 "multi_byte",
13303#endif
13304#ifdef FEAT_MBYTE_IME
13305 "multi_byte_ime",
13306#endif
13307#ifdef FEAT_MULTI_LANG
13308 "multi_lang",
13309#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013310#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013311#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013312 "mzscheme",
13313#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013315#ifdef FEAT_OLE
13316 "ole",
13317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013318#ifdef FEAT_PATH_EXTRA
13319 "path_extra",
13320#endif
13321#ifdef FEAT_PERL
13322#ifndef DYNAMIC_PERL
13323 "perl",
13324#endif
13325#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013326#ifdef FEAT_PERSISTENT_UNDO
13327 "persistent_undo",
13328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013329#ifdef FEAT_PYTHON
13330#ifndef DYNAMIC_PYTHON
13331 "python",
13332#endif
13333#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013334#ifdef FEAT_PYTHON3
13335#ifndef DYNAMIC_PYTHON3
13336 "python3",
13337#endif
13338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013339#ifdef FEAT_POSTSCRIPT
13340 "postscript",
13341#endif
13342#ifdef FEAT_PRINTER
13343 "printer",
13344#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013345#ifdef FEAT_PROFILE
13346 "profile",
13347#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013348#ifdef FEAT_RELTIME
13349 "reltime",
13350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013351#ifdef FEAT_QUICKFIX
13352 "quickfix",
13353#endif
13354#ifdef FEAT_RIGHTLEFT
13355 "rightleft",
13356#endif
13357#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13358 "ruby",
13359#endif
13360#ifdef FEAT_SCROLLBIND
13361 "scrollbind",
13362#endif
13363#ifdef FEAT_CMDL_INFO
13364 "showcmd",
13365 "cmdline_info",
13366#endif
13367#ifdef FEAT_SIGNS
13368 "signs",
13369#endif
13370#ifdef FEAT_SMARTINDENT
13371 "smartindent",
13372#endif
13373#ifdef FEAT_SNIFF
13374 "sniff",
13375#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013376#ifdef STARTUPTIME
13377 "startuptime",
13378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379#ifdef FEAT_STL_OPT
13380 "statusline",
13381#endif
13382#ifdef FEAT_SUN_WORKSHOP
13383 "sun_workshop",
13384#endif
13385#ifdef FEAT_NETBEANS_INTG
13386 "netbeans_intg",
13387#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013388#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013389 "spell",
13390#endif
13391#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013392 "syntax",
13393#endif
13394#if defined(USE_SYSTEM) || !defined(UNIX)
13395 "system",
13396#endif
13397#ifdef FEAT_TAG_BINS
13398 "tag_binary",
13399#endif
13400#ifdef FEAT_TAG_OLDSTATIC
13401 "tag_old_static",
13402#endif
13403#ifdef FEAT_TAG_ANYWHITE
13404 "tag_any_white",
13405#endif
13406#ifdef FEAT_TCL
13407# ifndef DYNAMIC_TCL
13408 "tcl",
13409# endif
13410#endif
13411#ifdef TERMINFO
13412 "terminfo",
13413#endif
13414#ifdef FEAT_TERMRESPONSE
13415 "termresponse",
13416#endif
13417#ifdef FEAT_TEXTOBJ
13418 "textobjects",
13419#endif
13420#ifdef HAVE_TGETENT
13421 "tgetent",
13422#endif
13423#ifdef FEAT_TITLE
13424 "title",
13425#endif
13426#ifdef FEAT_TOOLBAR
13427 "toolbar",
13428#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013429#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13430 "unnamedplus",
13431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432#ifdef FEAT_USR_CMDS
13433 "user-commands", /* was accidentally included in 5.4 */
13434 "user_commands",
13435#endif
13436#ifdef FEAT_VIMINFO
13437 "viminfo",
13438#endif
13439#ifdef FEAT_VERTSPLIT
13440 "vertsplit",
13441#endif
13442#ifdef FEAT_VIRTUALEDIT
13443 "virtualedit",
13444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013445 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013446#ifdef FEAT_VISUALEXTRA
13447 "visualextra",
13448#endif
13449#ifdef FEAT_VREPLACE
13450 "vreplace",
13451#endif
13452#ifdef FEAT_WILDIGN
13453 "wildignore",
13454#endif
13455#ifdef FEAT_WILDMENU
13456 "wildmenu",
13457#endif
13458#ifdef FEAT_WINDOWS
13459 "windows",
13460#endif
13461#ifdef FEAT_WAK
13462 "winaltkeys",
13463#endif
13464#ifdef FEAT_WRITEBACKUP
13465 "writebackup",
13466#endif
13467#ifdef FEAT_XIM
13468 "xim",
13469#endif
13470#ifdef FEAT_XFONTSET
13471 "xfontset",
13472#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013473#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013474 "xpm",
13475 "xpm_w32", /* for backward compatibility */
13476#else
13477# if defined(HAVE_XPM)
13478 "xpm",
13479# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481#ifdef USE_XSMP
13482 "xsmp",
13483#endif
13484#ifdef USE_XSMP_INTERACT
13485 "xsmp_interact",
13486#endif
13487#ifdef FEAT_XCLIPBOARD
13488 "xterm_clipboard",
13489#endif
13490#ifdef FEAT_XTERM_SAVE
13491 "xterm_save",
13492#endif
13493#if defined(UNIX) && defined(FEAT_X11)
13494 "X11",
13495#endif
13496 NULL
13497 };
13498
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013499 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500 for (i = 0; has_list[i] != NULL; ++i)
13501 if (STRICMP(name, has_list[i]) == 0)
13502 {
13503 n = TRUE;
13504 break;
13505 }
13506
13507 if (n == FALSE)
13508 {
13509 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013510 {
13511 if (name[5] == '-'
13512 && STRLEN(name) > 11
13513 && vim_isdigit(name[6])
13514 && vim_isdigit(name[8])
13515 && vim_isdigit(name[10]))
13516 {
13517 int major = atoi((char *)name + 6);
13518 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013519
13520 /* Expect "patch-9.9.01234". */
13521 n = (major < VIM_VERSION_MAJOR
13522 || (major == VIM_VERSION_MAJOR
13523 && (minor < VIM_VERSION_MINOR
13524 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013525 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013526 }
13527 else
13528 n = has_patch(atoi((char *)name + 5));
13529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530 else if (STRICMP(name, "vim_starting") == 0)
13531 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013532#ifdef FEAT_MBYTE
13533 else if (STRICMP(name, "multi_byte_encoding") == 0)
13534 n = has_mbyte;
13535#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013536#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13537 else if (STRICMP(name, "balloon_multiline") == 0)
13538 n = multiline_balloon_available();
13539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013540#ifdef DYNAMIC_TCL
13541 else if (STRICMP(name, "tcl") == 0)
13542 n = tcl_enabled(FALSE);
13543#endif
13544#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13545 else if (STRICMP(name, "iconv") == 0)
13546 n = iconv_enabled(FALSE);
13547#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013548#ifdef DYNAMIC_LUA
13549 else if (STRICMP(name, "lua") == 0)
13550 n = lua_enabled(FALSE);
13551#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013552#ifdef DYNAMIC_MZSCHEME
13553 else if (STRICMP(name, "mzscheme") == 0)
13554 n = mzscheme_enabled(FALSE);
13555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013556#ifdef DYNAMIC_RUBY
13557 else if (STRICMP(name, "ruby") == 0)
13558 n = ruby_enabled(FALSE);
13559#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013560#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013561#ifdef DYNAMIC_PYTHON
13562 else if (STRICMP(name, "python") == 0)
13563 n = python_enabled(FALSE);
13564#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013565#endif
13566#ifdef FEAT_PYTHON3
13567#ifdef DYNAMIC_PYTHON3
13568 else if (STRICMP(name, "python3") == 0)
13569 n = python3_enabled(FALSE);
13570#endif
13571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013572#ifdef DYNAMIC_PERL
13573 else if (STRICMP(name, "perl") == 0)
13574 n = perl_enabled(FALSE);
13575#endif
13576#ifdef FEAT_GUI
13577 else if (STRICMP(name, "gui_running") == 0)
13578 n = (gui.in_use || gui.starting);
13579# ifdef FEAT_GUI_W32
13580 else if (STRICMP(name, "gui_win32s") == 0)
13581 n = gui_is_win32s();
13582# endif
13583# ifdef FEAT_BROWSE
13584 else if (STRICMP(name, "browse") == 0)
13585 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13586# endif
13587#endif
13588#ifdef FEAT_SYN_HL
13589 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013590 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591#endif
13592#if defined(WIN3264)
13593 else if (STRICMP(name, "win95") == 0)
13594 n = mch_windows95();
13595#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013596#ifdef FEAT_NETBEANS_INTG
13597 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013598 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013600 }
13601
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013602 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013603}
13604
13605/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013606 * "has_key()" function
13607 */
13608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013609f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013610{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013611 if (argvars[0].v_type != VAR_DICT)
13612 {
13613 EMSG(_(e_dictreq));
13614 return;
13615 }
13616 if (argvars[0].vval.v_dict == NULL)
13617 return;
13618
13619 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013620 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013621}
13622
13623/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013624 * "haslocaldir()" function
13625 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013627f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013628{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013629 win_T *wp = NULL;
13630
13631 wp = find_tabwin(&argvars[0], &argvars[1]);
13632 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013633}
13634
13635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636 * "hasmapto()" function
13637 */
13638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013639f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640{
13641 char_u *name;
13642 char_u *mode;
13643 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013644 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013646 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013647 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648 mode = (char_u *)"nvo";
13649 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013650 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013651 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013652 if (argvars[2].v_type != VAR_UNKNOWN)
13653 abbr = get_tv_number(&argvars[2]);
13654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013655
Bram Moolenaar2c932302006-03-18 21:42:09 +000013656 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013657 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013658 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013659 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660}
13661
13662/*
13663 * "histadd()" function
13664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013666f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667{
13668#ifdef FEAT_CMDHIST
13669 int histype;
13670 char_u *str;
13671 char_u buf[NUMBUFLEN];
13672#endif
13673
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013674 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675 if (check_restricted() || check_secure())
13676 return;
13677#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013678 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13679 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680 if (histype >= 0)
13681 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013682 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683 if (*str != NUL)
13684 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013685 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013687 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688 return;
13689 }
13690 }
13691#endif
13692}
13693
13694/*
13695 * "histdel()" function
13696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013698f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699{
13700#ifdef FEAT_CMDHIST
13701 int n;
13702 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013703 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013705 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13706 if (str == NULL)
13707 n = 0;
13708 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013710 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013711 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013712 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013713 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013714 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013715 else
13716 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013717 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013718 get_tv_string_buf(&argvars[1], buf));
13719 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720#endif
13721}
13722
13723/*
13724 * "histget()" function
13725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013727f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728{
13729#ifdef FEAT_CMDHIST
13730 int type;
13731 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013732 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013734 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13735 if (str == NULL)
13736 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013738 {
13739 type = get_histtype(str);
13740 if (argvars[1].v_type == VAR_UNKNOWN)
13741 idx = get_history_idx(type);
13742 else
13743 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13744 /* -1 on type error */
13745 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013747#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013748 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013750 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751}
13752
13753/*
13754 * "histnr()" function
13755 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013757f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758{
13759 int i;
13760
13761#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013762 char_u *history = get_tv_string_chk(&argvars[0]);
13763
13764 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013765 if (i >= HIST_CMD && i < HIST_COUNT)
13766 i = get_history_idx(i);
13767 else
13768#endif
13769 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013770 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013771}
13772
13773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774 * "highlightID(name)" function
13775 */
13776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013777f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013778{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013779 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780}
13781
13782/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013783 * "highlight_exists()" function
13784 */
13785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013786f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013787{
13788 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13789}
13790
13791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792 * "hostname()" function
13793 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013795f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796{
13797 char_u hostname[256];
13798
13799 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013800 rettv->v_type = VAR_STRING;
13801 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802}
13803
13804/*
13805 * iconv() function
13806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013808f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809{
13810#ifdef FEAT_MBYTE
13811 char_u buf1[NUMBUFLEN];
13812 char_u buf2[NUMBUFLEN];
13813 char_u *from, *to, *str;
13814 vimconv_T vimconv;
13815#endif
13816
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013817 rettv->v_type = VAR_STRING;
13818 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819
13820#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013821 str = get_tv_string(&argvars[0]);
13822 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13823 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824 vimconv.vc_type = CONV_NONE;
13825 convert_setup(&vimconv, from, to);
13826
13827 /* If the encodings are equal, no conversion needed. */
13828 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013829 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013831 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832
13833 convert_setup(&vimconv, NULL, NULL);
13834 vim_free(from);
13835 vim_free(to);
13836#endif
13837}
13838
13839/*
13840 * "indent()" function
13841 */
13842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013843f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844{
13845 linenr_T lnum;
13846
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013847 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013849 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013851 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013852}
13853
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013854/*
13855 * "index()" function
13856 */
13857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013858f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013859{
Bram Moolenaar33570922005-01-25 22:26:29 +000013860 list_T *l;
13861 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013862 long idx = 0;
13863 int ic = FALSE;
13864
13865 rettv->vval.v_number = -1;
13866 if (argvars[0].v_type != VAR_LIST)
13867 {
13868 EMSG(_(e_listreq));
13869 return;
13870 }
13871 l = argvars[0].vval.v_list;
13872 if (l != NULL)
13873 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013874 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013875 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013876 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013877 int error = FALSE;
13878
Bram Moolenaar758711c2005-02-02 23:11:38 +000013879 /* Start at specified item. Use the cached index that list_find()
13880 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013881 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013882 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013883 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013884 ic = get_tv_number_chk(&argvars[3], &error);
13885 if (error)
13886 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013887 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013888
Bram Moolenaar758711c2005-02-02 23:11:38 +000013889 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013890 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013891 {
13892 rettv->vval.v_number = idx;
13893 break;
13894 }
13895 }
13896}
13897
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898static int inputsecret_flag = 0;
13899
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013900static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013901
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013903 * This function is used by f_input() and f_inputdialog() functions. The third
13904 * argument to f_input() specifies the type of completion to use at the
13905 * prompt. The third argument to f_inputdialog() specifies the value to return
13906 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907 */
13908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013909get_user_input(
13910 typval_T *argvars,
13911 typval_T *rettv,
13912 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013914 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915 char_u *p = NULL;
13916 int c;
13917 char_u buf[NUMBUFLEN];
13918 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013919 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013920 int xp_type = EXPAND_NOTHING;
13921 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013922
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013923 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013924 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013925
13926#ifdef NO_CONSOLE_INPUT
13927 /* While starting up, there is no place to enter text. */
13928 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013929 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930#endif
13931
13932 cmd_silent = FALSE; /* Want to see the prompt. */
13933 if (prompt != NULL)
13934 {
13935 /* Only the part of the message after the last NL is considered as
13936 * prompt for the command line */
13937 p = vim_strrchr(prompt, '\n');
13938 if (p == NULL)
13939 p = prompt;
13940 else
13941 {
13942 ++p;
13943 c = *p;
13944 *p = NUL;
13945 msg_start();
13946 msg_clr_eos();
13947 msg_puts_attr(prompt, echo_attr);
13948 msg_didout = FALSE;
13949 msg_starthere();
13950 *p = c;
13951 }
13952 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013954 if (argvars[1].v_type != VAR_UNKNOWN)
13955 {
13956 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13957 if (defstr != NULL)
13958 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013960 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013961 {
13962 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013963 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013964 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013965
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013966 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013967 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013968
Bram Moolenaar4463f292005-09-25 22:20:24 +000013969 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13970 if (xp_name == NULL)
13971 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013972
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013973 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013974
Bram Moolenaar4463f292005-09-25 22:20:24 +000013975 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13976 &xp_arg) == FAIL)
13977 return;
13978 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013979 }
13980
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013981 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013982 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013983 int save_ex_normal_busy = ex_normal_busy;
13984 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013985 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013986 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13987 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013988 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013989 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013990 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013991 && argvars[1].v_type != VAR_UNKNOWN
13992 && argvars[2].v_type != VAR_UNKNOWN)
13993 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13994 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013995
13996 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013998 /* since the user typed this, no need to wait for return */
13999 need_wait_return = FALSE;
14000 msg_didout = FALSE;
14001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002 cmd_silent = cmd_silent_save;
14003}
14004
14005/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014006 * "input()" function
14007 * Also handles inputsecret() when inputsecret is set.
14008 */
14009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014010f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014011{
14012 get_user_input(argvars, rettv, FALSE);
14013}
14014
14015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016 * "inputdialog()" function
14017 */
14018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014019f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014020{
14021#if defined(FEAT_GUI_TEXTDIALOG)
14022 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14023 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14024 {
14025 char_u *message;
14026 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014027 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014029 message = get_tv_string_chk(&argvars[0]);
14030 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014031 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014032 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033 else
14034 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014035 if (message != NULL && defstr != NULL
14036 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014037 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014038 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014039 else
14040 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014041 if (message != NULL && defstr != NULL
14042 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014043 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014044 rettv->vval.v_string = vim_strsave(
14045 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014046 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014047 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014049 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014050 }
14051 else
14052#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014053 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054}
14055
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014056/*
14057 * "inputlist()" function
14058 */
14059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014060f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014061{
14062 listitem_T *li;
14063 int selected;
14064 int mouse_used;
14065
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014066#ifdef NO_CONSOLE_INPUT
14067 /* While starting up, there is no place to enter text. */
14068 if (no_console_input())
14069 return;
14070#endif
14071 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14072 {
14073 EMSG2(_(e_listarg), "inputlist()");
14074 return;
14075 }
14076
14077 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014078 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014079 lines_left = Rows; /* avoid more prompt */
14080 msg_scroll = TRUE;
14081 msg_clr_eos();
14082
14083 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14084 {
14085 msg_puts(get_tv_string(&li->li_tv));
14086 msg_putchar('\n');
14087 }
14088
14089 /* Ask for choice. */
14090 selected = prompt_for_number(&mouse_used);
14091 if (mouse_used)
14092 selected -= lines_left;
14093
14094 rettv->vval.v_number = selected;
14095}
14096
14097
Bram Moolenaar071d4272004-06-13 20:20:40 +000014098static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14099
14100/*
14101 * "inputrestore()" function
14102 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014104f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014105{
14106 if (ga_userinput.ga_len > 0)
14107 {
14108 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014109 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14110 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014111 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014112 }
14113 else if (p_verbose > 1)
14114 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014115 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014116 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014117 }
14118}
14119
14120/*
14121 * "inputsave()" function
14122 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014124f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014125{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014126 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127 if (ga_grow(&ga_userinput, 1) == OK)
14128 {
14129 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14130 + ga_userinput.ga_len);
14131 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014132 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014133 }
14134 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014135 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136}
14137
14138/*
14139 * "inputsecret()" function
14140 */
14141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014142f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143{
14144 ++cmdline_star;
14145 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014146 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014147 --cmdline_star;
14148 --inputsecret_flag;
14149}
14150
14151/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014152 * "insert()" function
14153 */
14154 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014155f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014156{
14157 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014158 listitem_T *item;
14159 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014160 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014161
14162 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014163 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014164 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014165 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014166 {
14167 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014168 before = get_tv_number_chk(&argvars[2], &error);
14169 if (error)
14170 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014171
Bram Moolenaar758711c2005-02-02 23:11:38 +000014172 if (before == l->lv_len)
14173 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014174 else
14175 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014176 item = list_find(l, before);
14177 if (item == NULL)
14178 {
14179 EMSGN(_(e_listidx), before);
14180 l = NULL;
14181 }
14182 }
14183 if (l != NULL)
14184 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014185 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014186 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014187 }
14188 }
14189}
14190
14191/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014192 * "invert(expr)" function
14193 */
14194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014195f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014196{
14197 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14198}
14199
14200/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014201 * "isdirectory()" function
14202 */
14203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014204f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014205{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014206 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014207}
14208
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014209/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014210 * "islocked()" function
14211 */
14212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014213f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014214{
14215 lval_T lv;
14216 char_u *end;
14217 dictitem_T *di;
14218
14219 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014220 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14221 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014222 if (end != NULL && lv.ll_name != NULL)
14223 {
14224 if (*end != NUL)
14225 EMSG(_(e_trailing));
14226 else
14227 {
14228 if (lv.ll_tv == NULL)
14229 {
14230 if (check_changedtick(lv.ll_name))
14231 rettv->vval.v_number = 1; /* always locked */
14232 else
14233 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014234 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014235 if (di != NULL)
14236 {
14237 /* Consider a variable locked when:
14238 * 1. the variable itself is locked
14239 * 2. the value of the variable is locked.
14240 * 3. the List or Dict value is locked.
14241 */
14242 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14243 || tv_islocked(&di->di_tv));
14244 }
14245 }
14246 }
14247 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014248 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014249 else if (lv.ll_newkey != NULL)
14250 EMSG2(_(e_dictkey), lv.ll_newkey);
14251 else if (lv.ll_list != NULL)
14252 /* List item. */
14253 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14254 else
14255 /* Dictionary item. */
14256 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14257 }
14258 }
14259
14260 clear_lval(&lv);
14261}
14262
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014263static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014264
14265/*
14266 * Turn a dict into a list:
14267 * "what" == 0: list of keys
14268 * "what" == 1: list of values
14269 * "what" == 2: list of items
14270 */
14271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014272dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014273{
Bram Moolenaar33570922005-01-25 22:26:29 +000014274 list_T *l2;
14275 dictitem_T *di;
14276 hashitem_T *hi;
14277 listitem_T *li;
14278 listitem_T *li2;
14279 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014280 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014281
Bram Moolenaar8c711452005-01-14 21:53:12 +000014282 if (argvars[0].v_type != VAR_DICT)
14283 {
14284 EMSG(_(e_dictreq));
14285 return;
14286 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014287 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014288 return;
14289
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014290 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014291 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014292
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014293 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014294 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014295 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014296 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014297 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014298 --todo;
14299 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014300
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014301 li = listitem_alloc();
14302 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014303 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014304 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014305
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014306 if (what == 0)
14307 {
14308 /* keys() */
14309 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014310 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014311 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14312 }
14313 else if (what == 1)
14314 {
14315 /* values() */
14316 copy_tv(&di->di_tv, &li->li_tv);
14317 }
14318 else
14319 {
14320 /* items() */
14321 l2 = list_alloc();
14322 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014323 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014324 li->li_tv.vval.v_list = l2;
14325 if (l2 == NULL)
14326 break;
14327 ++l2->lv_refcount;
14328
14329 li2 = listitem_alloc();
14330 if (li2 == NULL)
14331 break;
14332 list_append(l2, li2);
14333 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014334 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014335 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14336
14337 li2 = listitem_alloc();
14338 if (li2 == NULL)
14339 break;
14340 list_append(l2, li2);
14341 copy_tv(&di->di_tv, &li2->li_tv);
14342 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014343 }
14344 }
14345}
14346
14347/*
14348 * "items(dict)" function
14349 */
14350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014351f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014352{
14353 dict_list(argvars, rettv, 2);
14354}
14355
Bram Moolenaar835dc632016-02-07 14:27:38 +010014356#ifdef FEAT_JOB
14357/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014358 * "job_getchannel()" function
14359 */
14360 static void
14361f_job_getchannel(typval_T *argvars, typval_T *rettv)
14362{
14363 if (argvars[0].v_type != VAR_JOB)
14364 EMSG(_(e_invarg));
14365 else
14366 {
14367 job_T *job = argvars[0].vval.v_job;
14368
14369 rettv->v_type = VAR_NUMBER;
14370 rettv->vval.v_number = job->jv_channel;
14371 }
14372}
14373
14374/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014375 * "job_start()" function
14376 */
14377 static void
14378f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
14379{
14380 job_T *job;
14381 char_u *cmd = NULL;
14382#if defined(UNIX)
14383# define USE_ARGV
14384 char **argv = NULL;
14385 int argc = 0;
14386#else
14387 garray_T ga;
14388#endif
14389
14390 rettv->v_type = VAR_JOB;
14391 job = job_alloc();
14392 rettv->vval.v_job = job;
14393 if (job == NULL)
14394 return;
14395
14396 rettv->vval.v_job->jv_status = JOB_FAILED;
14397#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010014398 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014399#endif
14400
14401 if (argvars[0].v_type == VAR_STRING)
14402 {
14403 /* Command is a string. */
14404 cmd = argvars[0].vval.v_string;
14405#ifdef USE_ARGV
14406 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
14407 return;
14408 argv[argc] = NULL;
14409#endif
14410 }
14411 else if (argvars[0].v_type != VAR_LIST
14412 || argvars[0].vval.v_list == NULL
14413 || argvars[0].vval.v_list->lv_len < 1)
14414 {
14415 EMSG(_(e_invarg));
14416 return;
14417 }
14418 else
14419 {
14420 list_T *l = argvars[0].vval.v_list;
14421 listitem_T *li;
14422 char_u *s;
14423
14424#ifdef USE_ARGV
14425 /* Pass argv[] to mch_call_shell(). */
14426 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
14427 if (argv == NULL)
14428 return;
14429#endif
14430 for (li = l->lv_first; li != NULL; li = li->li_next)
14431 {
14432 s = get_tv_string_chk(&li->li_tv);
14433 if (s == NULL)
14434 goto theend;
14435#ifdef USE_ARGV
14436 argv[argc++] = (char *)s;
14437#else
14438 if (li != l->lv_first)
14439 {
14440 s = vim_strsave_shellescape(s, FALSE, TRUE);
14441 if (s == NULL)
14442 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010014443 ga_concat(&ga, s);
14444 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014445 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010014446 else
14447 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014448 if (li->li_next != NULL)
14449 ga_append(&ga, ' ');
14450#endif
14451 }
14452#ifdef USE_ARGV
14453 argv[argc] = NULL;
14454#else
14455 cmd = ga.ga_data;
14456#endif
14457 }
14458#ifdef USE_ARGV
14459 mch_start_job(argv, job);
14460#else
14461 mch_start_job(cmd, job);
14462#endif
14463
14464theend:
14465#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010014466 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014467#else
14468 vim_free(ga.ga_data);
14469#endif
14470}
14471
14472/*
14473 * "job_status()" function
14474 */
14475 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014476f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014477{
14478 char *result;
14479
14480 if (argvars[0].v_type != VAR_JOB)
14481 EMSG(_(e_invarg));
14482 else
14483 {
14484 job_T *job = argvars[0].vval.v_job;
14485
14486 if (job->jv_status == JOB_ENDED)
14487 /* No need to check, dead is dead. */
14488 result = "dead";
14489 else if (job->jv_status == JOB_FAILED)
14490 result = "fail";
14491 else
14492 result = mch_job_status(job);
14493 rettv->v_type = VAR_STRING;
14494 rettv->vval.v_string = vim_strsave((char_u *)result);
14495 }
14496}
14497
14498/*
14499 * "job_stop()" function
14500 */
14501 static void
14502f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14503{
14504 if (argvars[0].v_type != VAR_JOB)
14505 EMSG(_(e_invarg));
14506 else
14507 {
14508 char_u *arg;
14509
14510 if (argvars[1].v_type == VAR_UNKNOWN)
14511 arg = (char_u *)"";
14512 else
14513 {
14514 arg = get_tv_string_chk(&argvars[1]);
14515 if (arg == NULL)
14516 {
14517 EMSG(_(e_invarg));
14518 return;
14519 }
14520 }
14521 if (mch_stop_job(argvars[0].vval.v_job, arg) == FAIL)
14522 rettv->vval.v_number = 0;
14523 else
14524 rettv->vval.v_number = 1;
14525 }
14526}
14527#endif
14528
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014530 * "join()" function
14531 */
14532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014533f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014534{
14535 garray_T ga;
14536 char_u *sep;
14537
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014538 if (argvars[0].v_type != VAR_LIST)
14539 {
14540 EMSG(_(e_listreq));
14541 return;
14542 }
14543 if (argvars[0].vval.v_list == NULL)
14544 return;
14545 if (argvars[1].v_type == VAR_UNKNOWN)
14546 sep = (char_u *)" ";
14547 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014548 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014549
14550 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014551
14552 if (sep != NULL)
14553 {
14554 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014555 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014556 ga_append(&ga, NUL);
14557 rettv->vval.v_string = (char_u *)ga.ga_data;
14558 }
14559 else
14560 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014561}
14562
14563/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014564 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014565 */
14566 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014567f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014568{
14569 js_read_T reader;
14570
14571 reader.js_buf = get_tv_string(&argvars[0]);
14572 reader.js_fill = NULL;
14573 reader.js_used = 0;
14574 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14575 EMSG(_(e_invarg));
14576}
14577
14578/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014579 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014580 */
14581 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014582f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014583{
14584 rettv->v_type = VAR_STRING;
14585 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14586}
14587
14588/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014589 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014590 */
14591 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014592f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014593{
14594 js_read_T reader;
14595
14596 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014597 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014598 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014599 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014600 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014601}
14602
14603/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014604 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014605 */
14606 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014607f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014608{
14609 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014610 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014611}
14612
14613/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014614 * "keys()" function
14615 */
14616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014617f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014618{
14619 dict_list(argvars, rettv, 0);
14620}
14621
14622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014623 * "last_buffer_nr()" function.
14624 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014626f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014627{
14628 int n = 0;
14629 buf_T *buf;
14630
14631 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14632 if (n < buf->b_fnum)
14633 n = buf->b_fnum;
14634
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014635 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014636}
14637
14638/*
14639 * "len()" function
14640 */
14641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014642f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014643{
14644 switch (argvars[0].v_type)
14645 {
14646 case VAR_STRING:
14647 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014648 rettv->vval.v_number = (varnumber_T)STRLEN(
14649 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014650 break;
14651 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014652 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014653 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014654 case VAR_DICT:
14655 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14656 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014657 case VAR_UNKNOWN:
14658 case VAR_SPECIAL:
14659 case VAR_FLOAT:
14660 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014661 case VAR_JOB:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014662 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014663 break;
14664 }
14665}
14666
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014667static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014668
14669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014670libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014671{
14672#ifdef FEAT_LIBCALL
14673 char_u *string_in;
14674 char_u **string_result;
14675 int nr_result;
14676#endif
14677
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014678 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014679 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014680 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014681
14682 if (check_restricted() || check_secure())
14683 return;
14684
14685#ifdef FEAT_LIBCALL
14686 /* The first two args must be strings, otherwise its meaningless */
14687 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14688 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014689 string_in = NULL;
14690 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014691 string_in = argvars[2].vval.v_string;
14692 if (type == VAR_NUMBER)
14693 string_result = NULL;
14694 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014695 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014696 if (mch_libcall(argvars[0].vval.v_string,
14697 argvars[1].vval.v_string,
14698 string_in,
14699 argvars[2].vval.v_number,
14700 string_result,
14701 &nr_result) == OK
14702 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014703 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014704 }
14705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014706}
14707
14708/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014709 * "libcall()" function
14710 */
14711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014712f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014713{
14714 libcall_common(argvars, rettv, VAR_STRING);
14715}
14716
14717/*
14718 * "libcallnr()" function
14719 */
14720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014721f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014722{
14723 libcall_common(argvars, rettv, VAR_NUMBER);
14724}
14725
14726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014727 * "line(string)" function
14728 */
14729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014730f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014731{
14732 linenr_T lnum = 0;
14733 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014734 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014735
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014736 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014737 if (fp != NULL)
14738 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014739 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014740}
14741
14742/*
14743 * "line2byte(lnum)" function
14744 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014746f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014747{
14748#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014749 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750#else
14751 linenr_T lnum;
14752
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014753 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014754 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014755 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014757 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14758 if (rettv->vval.v_number >= 0)
14759 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014760#endif
14761}
14762
14763/*
14764 * "lispindent(lnum)" function
14765 */
14766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014767f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014768{
14769#ifdef FEAT_LISP
14770 pos_T pos;
14771 linenr_T lnum;
14772
14773 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014774 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014775 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14776 {
14777 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014778 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014779 curwin->w_cursor = pos;
14780 }
14781 else
14782#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014783 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014784}
14785
14786/*
14787 * "localtime()" function
14788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014790f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014791{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014792 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014793}
14794
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014795static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014796
14797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014798get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014799{
14800 char_u *keys;
14801 char_u *which;
14802 char_u buf[NUMBUFLEN];
14803 char_u *keys_buf = NULL;
14804 char_u *rhs;
14805 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014806 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014807 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014808 mapblock_T *mp;
14809 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014810
14811 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014812 rettv->v_type = VAR_STRING;
14813 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014814
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014815 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014816 if (*keys == NUL)
14817 return;
14818
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014819 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014820 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014821 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014822 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014823 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014824 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014825 if (argvars[3].v_type != VAR_UNKNOWN)
14826 get_dict = get_tv_number(&argvars[3]);
14827 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014829 else
14830 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014831 if (which == NULL)
14832 return;
14833
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834 mode = get_map_mode(&which, 0);
14835
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014836 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014837 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014838 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014839
14840 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014841 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014842 /* Return a string. */
14843 if (rhs != NULL)
14844 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014845
Bram Moolenaarbd743252010-10-20 21:23:33 +020014846 }
14847 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14848 {
14849 /* Return a dictionary. */
14850 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14851 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14852 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014853
Bram Moolenaarbd743252010-10-20 21:23:33 +020014854 dict_add_nr_str(dict, "lhs", 0L, lhs);
14855 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14856 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14857 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14858 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14859 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14860 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014861 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014862 dict_add_nr_str(dict, "mode", 0L, mapmode);
14863
14864 vim_free(lhs);
14865 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014866 }
14867}
14868
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014869#ifdef FEAT_FLOAT
14870/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014871 * "log()" function
14872 */
14873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014874f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014875{
14876 float_T f;
14877
14878 rettv->v_type = VAR_FLOAT;
14879 if (get_float_arg(argvars, &f) == OK)
14880 rettv->vval.v_float = log(f);
14881 else
14882 rettv->vval.v_float = 0.0;
14883}
14884
14885/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014886 * "log10()" function
14887 */
14888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014889f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014890{
14891 float_T f;
14892
14893 rettv->v_type = VAR_FLOAT;
14894 if (get_float_arg(argvars, &f) == OK)
14895 rettv->vval.v_float = log10(f);
14896 else
14897 rettv->vval.v_float = 0.0;
14898}
14899#endif
14900
Bram Moolenaar1dced572012-04-05 16:54:08 +020014901#ifdef FEAT_LUA
14902/*
14903 * "luaeval()" function
14904 */
14905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014906f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020014907{
14908 char_u *str;
14909 char_u buf[NUMBUFLEN];
14910
14911 str = get_tv_string_buf(&argvars[0], buf);
14912 do_luaeval(str, argvars + 1, rettv);
14913}
14914#endif
14915
Bram Moolenaar071d4272004-06-13 20:20:40 +000014916/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014917 * "map()" function
14918 */
14919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014920f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014921{
14922 filter_map(argvars, rettv, TRUE);
14923}
14924
14925/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014926 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014927 */
14928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014929f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014930{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014931 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014932}
14933
14934/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014935 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014936 */
14937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014938f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014939{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014940 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014941}
14942
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014943static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014944
14945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014946find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014947{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014948 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014949 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014950 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014951 char_u *pat;
14952 regmatch_T regmatch;
14953 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014954 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014955 char_u *save_cpo;
14956 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014957 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014958 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014959 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014960 list_T *l = NULL;
14961 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014962 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014963 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014964
14965 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14966 save_cpo = p_cpo;
14967 p_cpo = (char_u *)"";
14968
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014969 rettv->vval.v_number = -1;
14970 if (type == 3)
14971 {
14972 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014973 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014974 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014975 }
14976 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014977 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014978 rettv->v_type = VAR_STRING;
14979 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014981
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014982 if (argvars[0].v_type == VAR_LIST)
14983 {
14984 if ((l = argvars[0].vval.v_list) == NULL)
14985 goto theend;
14986 li = l->lv_first;
14987 }
14988 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014989 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014990 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014991 len = (long)STRLEN(str);
14992 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014993
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014994 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14995 if (pat == NULL)
14996 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014997
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014998 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014999 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015000 int error = FALSE;
15001
15002 start = get_tv_number_chk(&argvars[2], &error);
15003 if (error)
15004 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015005 if (l != NULL)
15006 {
15007 li = list_find(l, start);
15008 if (li == NULL)
15009 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015010 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015011 }
15012 else
15013 {
15014 if (start < 0)
15015 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015016 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015017 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015018 /* When "count" argument is there ignore matches before "start",
15019 * otherwise skip part of the string. Differs when pattern is "^"
15020 * or "\<". */
15021 if (argvars[3].v_type != VAR_UNKNOWN)
15022 startcol = start;
15023 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015024 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015025 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015026 len -= start;
15027 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015028 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015029
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015030 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015031 nth = get_tv_number_chk(&argvars[3], &error);
15032 if (error)
15033 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034 }
15035
15036 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15037 if (regmatch.regprog != NULL)
15038 {
15039 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015040
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015041 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015042 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015043 if (l != NULL)
15044 {
15045 if (li == NULL)
15046 {
15047 match = FALSE;
15048 break;
15049 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015050 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015051 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015052 if (str == NULL)
15053 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015054 }
15055
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015056 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015057
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015058 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015059 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015060 if (l == NULL && !match)
15061 break;
15062
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015063 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015064 if (l != NULL)
15065 {
15066 li = li->li_next;
15067 ++idx;
15068 }
15069 else
15070 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015071#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015072 startcol = (colnr_T)(regmatch.startp[0]
15073 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015074#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015075 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015076#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015077 if (startcol > (colnr_T)len
15078 || str + startcol <= regmatch.startp[0])
15079 {
15080 match = FALSE;
15081 break;
15082 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015083 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015084 }
15085
15086 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015087 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015088 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015089 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015090 int i;
15091
15092 /* return list with matched string and submatches */
15093 for (i = 0; i < NSUBEXP; ++i)
15094 {
15095 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015096 {
15097 if (list_append_string(rettv->vval.v_list,
15098 (char_u *)"", 0) == FAIL)
15099 break;
15100 }
15101 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015102 regmatch.startp[i],
15103 (int)(regmatch.endp[i] - regmatch.startp[i]))
15104 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015105 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015106 }
15107 }
15108 else if (type == 2)
15109 {
15110 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015111 if (l != NULL)
15112 copy_tv(&li->li_tv, rettv);
15113 else
15114 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015115 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015116 }
15117 else if (l != NULL)
15118 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015119 else
15120 {
15121 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015122 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015123 (varnumber_T)(regmatch.startp[0] - str);
15124 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015125 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015126 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015127 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015128 }
15129 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015130 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131 }
15132
15133theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015134 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015135 p_cpo = save_cpo;
15136}
15137
15138/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015139 * "match()" function
15140 */
15141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015142f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015143{
15144 find_some_match(argvars, rettv, 1);
15145}
15146
15147/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015148 * "matchadd()" function
15149 */
15150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015151f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015152{
15153#ifdef FEAT_SEARCH_EXTRA
15154 char_u buf[NUMBUFLEN];
15155 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15156 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15157 int prio = 10; /* default priority */
15158 int id = -1;
15159 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015160 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015161
15162 rettv->vval.v_number = -1;
15163
15164 if (grp == NULL || pat == NULL)
15165 return;
15166 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015167 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015168 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015169 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015170 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015171 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015172 if (argvars[4].v_type != VAR_UNKNOWN)
15173 {
15174 if (argvars[4].v_type != VAR_DICT)
15175 {
15176 EMSG(_(e_dictreq));
15177 return;
15178 }
15179 if (dict_find(argvars[4].vval.v_dict,
15180 (char_u *)"conceal", -1) != NULL)
15181 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15182 (char_u *)"conceal", FALSE);
15183 }
15184 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015185 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015186 if (error == TRUE)
15187 return;
15188 if (id >= 1 && id <= 3)
15189 {
15190 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15191 return;
15192 }
15193
Bram Moolenaar6561d522015-07-21 15:48:27 +020015194 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15195 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015196#endif
15197}
15198
15199/*
15200 * "matchaddpos()" function
15201 */
15202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015203f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015204{
15205#ifdef FEAT_SEARCH_EXTRA
15206 char_u buf[NUMBUFLEN];
15207 char_u *group;
15208 int prio = 10;
15209 int id = -1;
15210 int error = FALSE;
15211 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015212 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015213
15214 rettv->vval.v_number = -1;
15215
15216 group = get_tv_string_buf_chk(&argvars[0], buf);
15217 if (group == NULL)
15218 return;
15219
15220 if (argvars[1].v_type != VAR_LIST)
15221 {
15222 EMSG2(_(e_listarg), "matchaddpos()");
15223 return;
15224 }
15225 l = argvars[1].vval.v_list;
15226 if (l == NULL)
15227 return;
15228
15229 if (argvars[2].v_type != VAR_UNKNOWN)
15230 {
15231 prio = get_tv_number_chk(&argvars[2], &error);
15232 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015233 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015234 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015235 if (argvars[4].v_type != VAR_UNKNOWN)
15236 {
15237 if (argvars[4].v_type != VAR_DICT)
15238 {
15239 EMSG(_(e_dictreq));
15240 return;
15241 }
15242 if (dict_find(argvars[4].vval.v_dict,
15243 (char_u *)"conceal", -1) != NULL)
15244 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15245 (char_u *)"conceal", FALSE);
15246 }
15247 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015248 }
15249 if (error == TRUE)
15250 return;
15251
15252 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15253 if (id == 1 || id == 2)
15254 {
15255 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15256 return;
15257 }
15258
Bram Moolenaar6561d522015-07-21 15:48:27 +020015259 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15260 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015261#endif
15262}
15263
15264/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015265 * "matcharg()" function
15266 */
15267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015268f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015269{
15270 if (rettv_list_alloc(rettv) == OK)
15271 {
15272#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015273 int id = get_tv_number(&argvars[0]);
15274 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015275
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015276 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015277 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015278 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15279 {
15280 list_append_string(rettv->vval.v_list,
15281 syn_id2name(m->hlg_id), -1);
15282 list_append_string(rettv->vval.v_list, m->pattern, -1);
15283 }
15284 else
15285 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015286 list_append_string(rettv->vval.v_list, NULL, -1);
15287 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015288 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015289 }
15290#endif
15291 }
15292}
15293
15294/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015295 * "matchdelete()" function
15296 */
15297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015298f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015299{
15300#ifdef FEAT_SEARCH_EXTRA
15301 rettv->vval.v_number = match_delete(curwin,
15302 (int)get_tv_number(&argvars[0]), TRUE);
15303#endif
15304}
15305
15306/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015307 * "matchend()" function
15308 */
15309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015310f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015311{
15312 find_some_match(argvars, rettv, 0);
15313}
15314
15315/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015316 * "matchlist()" function
15317 */
15318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015319f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015320{
15321 find_some_match(argvars, rettv, 3);
15322}
15323
15324/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015325 * "matchstr()" function
15326 */
15327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015328f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015329{
15330 find_some_match(argvars, rettv, 2);
15331}
15332
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015333static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015334
15335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015336max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015337{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015338 long n = 0;
15339 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015340 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015341
15342 if (argvars[0].v_type == VAR_LIST)
15343 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015344 list_T *l;
15345 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015346
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015347 l = argvars[0].vval.v_list;
15348 if (l != NULL)
15349 {
15350 li = l->lv_first;
15351 if (li != NULL)
15352 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015353 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015354 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015355 {
15356 li = li->li_next;
15357 if (li == NULL)
15358 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015359 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015360 if (domax ? i > n : i < n)
15361 n = i;
15362 }
15363 }
15364 }
15365 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015366 else if (argvars[0].v_type == VAR_DICT)
15367 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015368 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015369 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015370 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015371 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015372
15373 d = argvars[0].vval.v_dict;
15374 if (d != NULL)
15375 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015376 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015377 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015378 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015379 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015380 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015381 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015382 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015383 if (first)
15384 {
15385 n = i;
15386 first = FALSE;
15387 }
15388 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015389 n = i;
15390 }
15391 }
15392 }
15393 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015394 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015395 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015396 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015397}
15398
15399/*
15400 * "max()" function
15401 */
15402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015403f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015404{
15405 max_min(argvars, rettv, TRUE);
15406}
15407
15408/*
15409 * "min()" function
15410 */
15411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015412f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015413{
15414 max_min(argvars, rettv, FALSE);
15415}
15416
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015417static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015418
15419/*
15420 * Create the directory in which "dir" is located, and higher levels when
15421 * needed.
15422 */
15423 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015424mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015425{
15426 char_u *p;
15427 char_u *updir;
15428 int r = FAIL;
15429
15430 /* Get end of directory name in "dir".
15431 * We're done when it's "/" or "c:/". */
15432 p = gettail_sep(dir);
15433 if (p <= get_past_head(dir))
15434 return OK;
15435
15436 /* If the directory exists we're done. Otherwise: create it.*/
15437 updir = vim_strnsave(dir, (int)(p - dir));
15438 if (updir == NULL)
15439 return FAIL;
15440 if (mch_isdir(updir))
15441 r = OK;
15442 else if (mkdir_recurse(updir, prot) == OK)
15443 r = vim_mkdir_emsg(updir, prot);
15444 vim_free(updir);
15445 return r;
15446}
15447
15448#ifdef vim_mkdir
15449/*
15450 * "mkdir()" function
15451 */
15452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015453f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015454{
15455 char_u *dir;
15456 char_u buf[NUMBUFLEN];
15457 int prot = 0755;
15458
15459 rettv->vval.v_number = FAIL;
15460 if (check_restricted() || check_secure())
15461 return;
15462
15463 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015464 if (*dir == NUL)
15465 rettv->vval.v_number = FAIL;
15466 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015467 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015468 if (*gettail(dir) == NUL)
15469 /* remove trailing slashes */
15470 *gettail_sep(dir) = NUL;
15471
15472 if (argvars[1].v_type != VAR_UNKNOWN)
15473 {
15474 if (argvars[2].v_type != VAR_UNKNOWN)
15475 prot = get_tv_number_chk(&argvars[2], NULL);
15476 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15477 mkdir_recurse(dir, prot);
15478 }
15479 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015480 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015481}
15482#endif
15483
Bram Moolenaar0d660222005-01-07 21:51:51 +000015484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015485 * "mode()" function
15486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015488f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015489{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015490 char_u buf[3];
15491
15492 buf[1] = NUL;
15493 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015494
Bram Moolenaar071d4272004-06-13 20:20:40 +000015495 if (VIsual_active)
15496 {
15497 if (VIsual_select)
15498 buf[0] = VIsual_mode + 's' - 'v';
15499 else
15500 buf[0] = VIsual_mode;
15501 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015502 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015503 || State == CONFIRM)
15504 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015505 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015506 if (State == ASKMORE)
15507 buf[1] = 'm';
15508 else if (State == CONFIRM)
15509 buf[1] = '?';
15510 }
15511 else if (State == EXTERNCMD)
15512 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015513 else if (State & INSERT)
15514 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015515#ifdef FEAT_VREPLACE
15516 if (State & VREPLACE_FLAG)
15517 {
15518 buf[0] = 'R';
15519 buf[1] = 'v';
15520 }
15521 else
15522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523 if (State & REPLACE_FLAG)
15524 buf[0] = 'R';
15525 else
15526 buf[0] = 'i';
15527 }
15528 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015529 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015530 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015531 if (exmode_active)
15532 buf[1] = 'v';
15533 }
15534 else if (exmode_active)
15535 {
15536 buf[0] = 'c';
15537 buf[1] = 'e';
15538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015540 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015542 if (finish_op)
15543 buf[1] = 'o';
15544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015545
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015546 /* Clear out the minor mode when the argument is not a non-zero number or
15547 * non-empty string. */
15548 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015549 buf[1] = NUL;
15550
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015551 rettv->vval.v_string = vim_strsave(buf);
15552 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553}
15554
Bram Moolenaar429fa852013-04-15 12:27:36 +020015555#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015556/*
15557 * "mzeval()" function
15558 */
15559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015560f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015561{
15562 char_u *str;
15563 char_u buf[NUMBUFLEN];
15564
15565 str = get_tv_string_buf(&argvars[0], buf);
15566 do_mzeval(str, rettv);
15567}
Bram Moolenaar75676462013-01-30 14:55:42 +010015568
15569 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015570mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015571{
15572 typval_T argvars[3];
15573
15574 argvars[0].v_type = VAR_STRING;
15575 argvars[0].vval.v_string = name;
15576 copy_tv(args, &argvars[1]);
15577 argvars[2].v_type = VAR_UNKNOWN;
15578 f_call(argvars, rettv);
15579 clear_tv(&argvars[1]);
15580}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015581#endif
15582
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015584 * "nextnonblank()" function
15585 */
15586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015587f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015588{
15589 linenr_T lnum;
15590
15591 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15592 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015593 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015594 {
15595 lnum = 0;
15596 break;
15597 }
15598 if (*skipwhite(ml_get(lnum)) != NUL)
15599 break;
15600 }
15601 rettv->vval.v_number = lnum;
15602}
15603
15604/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015605 * "nr2char()" function
15606 */
15607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015608f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015609{
15610 char_u buf[NUMBUFLEN];
15611
15612#ifdef FEAT_MBYTE
15613 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015614 {
15615 int utf8 = 0;
15616
15617 if (argvars[1].v_type != VAR_UNKNOWN)
15618 utf8 = get_tv_number_chk(&argvars[1], NULL);
15619 if (utf8)
15620 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15621 else
15622 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015624 else
15625#endif
15626 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015627 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015628 buf[1] = NUL;
15629 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015630 rettv->v_type = VAR_STRING;
15631 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015632}
15633
15634/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015635 * "or(expr, expr)" function
15636 */
15637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015638f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015639{
15640 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15641 | get_tv_number_chk(&argvars[1], NULL);
15642}
15643
15644/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015645 * "pathshorten()" function
15646 */
15647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015648f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015649{
15650 char_u *p;
15651
15652 rettv->v_type = VAR_STRING;
15653 p = get_tv_string_chk(&argvars[0]);
15654 if (p == NULL)
15655 rettv->vval.v_string = NULL;
15656 else
15657 {
15658 p = vim_strsave(p);
15659 rettv->vval.v_string = p;
15660 if (p != NULL)
15661 shorten_dir(p);
15662 }
15663}
15664
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015665#ifdef FEAT_PERL
15666/*
15667 * "perleval()" function
15668 */
15669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015670f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015671{
15672 char_u *str;
15673 char_u buf[NUMBUFLEN];
15674
15675 str = get_tv_string_buf(&argvars[0], buf);
15676 do_perleval(str, rettv);
15677}
15678#endif
15679
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015680#ifdef FEAT_FLOAT
15681/*
15682 * "pow()" function
15683 */
15684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015685f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015686{
15687 float_T fx, fy;
15688
15689 rettv->v_type = VAR_FLOAT;
15690 if (get_float_arg(argvars, &fx) == OK
15691 && get_float_arg(&argvars[1], &fy) == OK)
15692 rettv->vval.v_float = pow(fx, fy);
15693 else
15694 rettv->vval.v_float = 0.0;
15695}
15696#endif
15697
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015698/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015699 * "prevnonblank()" function
15700 */
15701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015702f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015703{
15704 linenr_T lnum;
15705
15706 lnum = get_tv_lnum(argvars);
15707 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15708 lnum = 0;
15709 else
15710 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15711 --lnum;
15712 rettv->vval.v_number = lnum;
15713}
15714
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015715/* This dummy va_list is here because:
15716 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15717 * - locally in the function results in a "used before set" warning
15718 * - using va_start() to initialize it gives "function with fixed args" error */
15719static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015720
Bram Moolenaar8c711452005-01-14 21:53:12 +000015721/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015722 * "printf()" function
15723 */
15724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015725f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015726{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015727 char_u buf[NUMBUFLEN];
15728 int len;
15729 char_u *s;
15730 int saved_did_emsg = did_emsg;
15731 char *fmt;
15732
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015733 rettv->v_type = VAR_STRING;
15734 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015735
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015736 /* Get the required length, allocate the buffer and do it for real. */
15737 did_emsg = FALSE;
15738 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15739 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15740 if (!did_emsg)
15741 {
15742 s = alloc(len + 1);
15743 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015744 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015745 rettv->vval.v_string = s;
15746 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015747 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015748 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015749 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015750}
15751
15752/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015753 * "pumvisible()" function
15754 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015756f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015757{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015758#ifdef FEAT_INS_EXPAND
15759 if (pum_visible())
15760 rettv->vval.v_number = 1;
15761#endif
15762}
15763
Bram Moolenaardb913952012-06-29 12:54:53 +020015764#ifdef FEAT_PYTHON3
15765/*
15766 * "py3eval()" function
15767 */
15768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015769f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015770{
15771 char_u *str;
15772 char_u buf[NUMBUFLEN];
15773
15774 str = get_tv_string_buf(&argvars[0], buf);
15775 do_py3eval(str, rettv);
15776}
15777#endif
15778
15779#ifdef FEAT_PYTHON
15780/*
15781 * "pyeval()" function
15782 */
15783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015784f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015785{
15786 char_u *str;
15787 char_u buf[NUMBUFLEN];
15788
15789 str = get_tv_string_buf(&argvars[0], buf);
15790 do_pyeval(str, rettv);
15791}
15792#endif
15793
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015794/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015795 * "range()" function
15796 */
15797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015798f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015799{
15800 long start;
15801 long end;
15802 long stride = 1;
15803 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015804 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015805
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015806 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015807 if (argvars[1].v_type == VAR_UNKNOWN)
15808 {
15809 end = start - 1;
15810 start = 0;
15811 }
15812 else
15813 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015814 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015815 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015816 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015817 }
15818
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015819 if (error)
15820 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015821 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015822 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015823 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015824 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015825 else
15826 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015827 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015828 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015829 if (list_append_number(rettv->vval.v_list,
15830 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015831 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015832 }
15833}
15834
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015835/*
15836 * "readfile()" function
15837 */
15838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015839f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015840{
15841 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015842 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015843 char_u *fname;
15844 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015845 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15846 int io_size = sizeof(buf);
15847 int readlen; /* size of last fread() */
15848 char_u *prev = NULL; /* previously read bytes, if any */
15849 long prevlen = 0; /* length of data in prev */
15850 long prevsize = 0; /* size of prev buffer */
15851 long maxline = MAXLNUM;
15852 long cnt = 0;
15853 char_u *p; /* position in buf */
15854 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015855
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015856 if (argvars[1].v_type != VAR_UNKNOWN)
15857 {
15858 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15859 binary = TRUE;
15860 if (argvars[2].v_type != VAR_UNKNOWN)
15861 maxline = get_tv_number(&argvars[2]);
15862 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015863
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015864 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015865 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015866
15867 /* Always open the file in binary mode, library functions have a mind of
15868 * their own about CR-LF conversion. */
15869 fname = get_tv_string(&argvars[0]);
15870 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15871 {
15872 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15873 return;
15874 }
15875
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015876 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015877 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015878 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015879
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015880 /* This for loop processes what was read, but is also entered at end
15881 * of file so that either:
15882 * - an incomplete line gets written
15883 * - a "binary" file gets an empty line at the end if it ends in a
15884 * newline. */
15885 for (p = buf, start = buf;
15886 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15887 ++p)
15888 {
15889 if (*p == '\n' || readlen <= 0)
15890 {
15891 listitem_T *li;
15892 char_u *s = NULL;
15893 long_u len = p - start;
15894
15895 /* Finished a line. Remove CRs before NL. */
15896 if (readlen > 0 && !binary)
15897 {
15898 while (len > 0 && start[len - 1] == '\r')
15899 --len;
15900 /* removal may cross back to the "prev" string */
15901 if (len == 0)
15902 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15903 --prevlen;
15904 }
15905 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015906 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015907 else
15908 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015909 /* Change "prev" buffer to be the right size. This way
15910 * the bytes are only copied once, and very long lines are
15911 * allocated only once. */
15912 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015913 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015914 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015915 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015916 prev = NULL; /* the list will own the string */
15917 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015918 }
15919 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015920 if (s == NULL)
15921 {
15922 do_outofmem_msg((long_u) prevlen + len + 1);
15923 failed = TRUE;
15924 break;
15925 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015926
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015927 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015928 {
15929 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015930 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015931 break;
15932 }
15933 li->li_tv.v_type = VAR_STRING;
15934 li->li_tv.v_lock = 0;
15935 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015936 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015937
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015938 start = p + 1; /* step over newline */
15939 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015940 break;
15941 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015942 else if (*p == NUL)
15943 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015944#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015945 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15946 * when finding the BF and check the previous two bytes. */
15947 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015948 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015949 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15950 * + 1, these may be in the "prev" string. */
15951 char_u back1 = p >= buf + 1 ? p[-1]
15952 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15953 char_u back2 = p >= buf + 2 ? p[-2]
15954 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15955 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015956
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015957 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015958 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015959 char_u *dest = p - 2;
15960
15961 /* Usually a BOM is at the beginning of a file, and so at
15962 * the beginning of a line; then we can just step over it.
15963 */
15964 if (start == dest)
15965 start = p + 1;
15966 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015967 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015968 /* have to shuffle buf to close gap */
15969 int adjust_prevlen = 0;
15970
15971 if (dest < buf)
15972 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015973 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015974 dest = buf;
15975 }
15976 if (readlen > p - buf + 1)
15977 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15978 readlen -= 3 - adjust_prevlen;
15979 prevlen -= adjust_prevlen;
15980 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015981 }
15982 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015983 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015984#endif
15985 } /* for */
15986
15987 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15988 break;
15989 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015990 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015991 /* There's part of a line in buf, store it in "prev". */
15992 if (p - start + prevlen >= prevsize)
15993 {
15994 /* need bigger "prev" buffer */
15995 char_u *newprev;
15996
15997 /* A common use case is ordinary text files and "prev" gets a
15998 * fragment of a line, so the first allocation is made
15999 * small, to avoid repeatedly 'allocing' large and
16000 * 'reallocing' small. */
16001 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016002 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016003 else
16004 {
16005 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016006 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016007 prevsize = grow50pc > growmin ? grow50pc : growmin;
16008 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016009 newprev = prev == NULL ? alloc(prevsize)
16010 : vim_realloc(prev, prevsize);
16011 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016012 {
16013 do_outofmem_msg((long_u)prevsize);
16014 failed = TRUE;
16015 break;
16016 }
16017 prev = newprev;
16018 }
16019 /* Add the line part to end of "prev". */
16020 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016021 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016022 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016023 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016024
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016025 /*
16026 * For a negative line count use only the lines at the end of the file,
16027 * free the rest.
16028 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016029 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016030 while (cnt > -maxline)
16031 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016032 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016033 --cnt;
16034 }
16035
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016036 if (failed)
16037 {
16038 list_free(rettv->vval.v_list, TRUE);
16039 /* readfile doc says an empty list is returned on error */
16040 rettv->vval.v_list = list_alloc();
16041 }
16042
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016043 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016044 fclose(fd);
16045}
16046
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016047#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016048static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016049
16050/*
16051 * Convert a List to proftime_T.
16052 * Return FAIL when there is something wrong.
16053 */
16054 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016055list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016056{
16057 long n1, n2;
16058 int error = FALSE;
16059
16060 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16061 || arg->vval.v_list->lv_len != 2)
16062 return FAIL;
16063 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16064 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16065# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016066 tm->HighPart = n1;
16067 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016068# else
16069 tm->tv_sec = n1;
16070 tm->tv_usec = n2;
16071# endif
16072 return error ? FAIL : OK;
16073}
16074#endif /* FEAT_RELTIME */
16075
16076/*
16077 * "reltime()" function
16078 */
16079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016080f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016081{
16082#ifdef FEAT_RELTIME
16083 proftime_T res;
16084 proftime_T start;
16085
16086 if (argvars[0].v_type == VAR_UNKNOWN)
16087 {
16088 /* No arguments: get current time. */
16089 profile_start(&res);
16090 }
16091 else if (argvars[1].v_type == VAR_UNKNOWN)
16092 {
16093 if (list2proftime(&argvars[0], &res) == FAIL)
16094 return;
16095 profile_end(&res);
16096 }
16097 else
16098 {
16099 /* Two arguments: compute the difference. */
16100 if (list2proftime(&argvars[0], &start) == FAIL
16101 || list2proftime(&argvars[1], &res) == FAIL)
16102 return;
16103 profile_sub(&res, &start);
16104 }
16105
16106 if (rettv_list_alloc(rettv) == OK)
16107 {
16108 long n1, n2;
16109
16110# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016111 n1 = res.HighPart;
16112 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016113# else
16114 n1 = res.tv_sec;
16115 n2 = res.tv_usec;
16116# endif
16117 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16118 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16119 }
16120#endif
16121}
16122
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016123#ifdef FEAT_FLOAT
16124/*
16125 * "reltimefloat()" function
16126 */
16127 static void
16128f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16129{
16130# ifdef FEAT_RELTIME
16131 proftime_T tm;
16132# endif
16133
16134 rettv->v_type = VAR_FLOAT;
16135 rettv->vval.v_float = 0;
16136# ifdef FEAT_RELTIME
16137 if (list2proftime(&argvars[0], &tm) == OK)
16138 rettv->vval.v_float = profile_float(&tm);
16139# endif
16140}
16141#endif
16142
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016143/*
16144 * "reltimestr()" function
16145 */
16146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016147f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016148{
16149#ifdef FEAT_RELTIME
16150 proftime_T tm;
16151#endif
16152
16153 rettv->v_type = VAR_STRING;
16154 rettv->vval.v_string = NULL;
16155#ifdef FEAT_RELTIME
16156 if (list2proftime(&argvars[0], &tm) == OK)
16157 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16158#endif
16159}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016160
Bram Moolenaar0d660222005-01-07 21:51:51 +000016161#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016162static void make_connection(void);
16163static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016164
16165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016166make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016167{
16168 if (X_DISPLAY == NULL
16169# ifdef FEAT_GUI
16170 && !gui.in_use
16171# endif
16172 )
16173 {
16174 x_force_connect = TRUE;
16175 setup_term_clip();
16176 x_force_connect = FALSE;
16177 }
16178}
16179
16180 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016181check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016182{
16183 make_connection();
16184 if (X_DISPLAY == NULL)
16185 {
16186 EMSG(_("E240: No connection to Vim server"));
16187 return FAIL;
16188 }
16189 return OK;
16190}
16191#endif
16192
16193#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016194static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016195
16196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016197remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016198{
16199 char_u *server_name;
16200 char_u *keys;
16201 char_u *r = NULL;
16202 char_u buf[NUMBUFLEN];
16203# ifdef WIN32
16204 HWND w;
16205# else
16206 Window w;
16207# endif
16208
16209 if (check_restricted() || check_secure())
16210 return;
16211
16212# ifdef FEAT_X11
16213 if (check_connection() == FAIL)
16214 return;
16215# endif
16216
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016217 server_name = get_tv_string_chk(&argvars[0]);
16218 if (server_name == NULL)
16219 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016220 keys = get_tv_string_buf(&argvars[1], buf);
16221# ifdef WIN32
16222 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16223# else
16224 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16225 < 0)
16226# endif
16227 {
16228 if (r != NULL)
16229 EMSG(r); /* sending worked but evaluation failed */
16230 else
16231 EMSG2(_("E241: Unable to send to %s"), server_name);
16232 return;
16233 }
16234
16235 rettv->vval.v_string = r;
16236
16237 if (argvars[2].v_type != VAR_UNKNOWN)
16238 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016239 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016240 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016241 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016242
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016243 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016244 v.di_tv.v_type = VAR_STRING;
16245 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016246 idvar = get_tv_string_chk(&argvars[2]);
16247 if (idvar != NULL)
16248 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016249 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016250 }
16251}
16252#endif
16253
16254/*
16255 * "remote_expr()" function
16256 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016258f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016259{
16260 rettv->v_type = VAR_STRING;
16261 rettv->vval.v_string = NULL;
16262#ifdef FEAT_CLIENTSERVER
16263 remote_common(argvars, rettv, TRUE);
16264#endif
16265}
16266
16267/*
16268 * "remote_foreground()" function
16269 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016271f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016272{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016273#ifdef FEAT_CLIENTSERVER
16274# ifdef WIN32
16275 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016276 {
16277 char_u *server_name = get_tv_string_chk(&argvars[0]);
16278
16279 if (server_name != NULL)
16280 serverForeground(server_name);
16281 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016282# else
16283 /* Send a foreground() expression to the server. */
16284 argvars[1].v_type = VAR_STRING;
16285 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16286 argvars[2].v_type = VAR_UNKNOWN;
16287 remote_common(argvars, rettv, TRUE);
16288 vim_free(argvars[1].vval.v_string);
16289# endif
16290#endif
16291}
16292
Bram Moolenaar0d660222005-01-07 21:51:51 +000016293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016294f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016295{
16296#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016297 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016298 char_u *s = NULL;
16299# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016300 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016301# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016302 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016303
16304 if (check_restricted() || check_secure())
16305 {
16306 rettv->vval.v_number = -1;
16307 return;
16308 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016309 serverid = get_tv_string_chk(&argvars[0]);
16310 if (serverid == NULL)
16311 {
16312 rettv->vval.v_number = -1;
16313 return; /* type error; errmsg already given */
16314 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016315# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016316 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016317 if (n == 0)
16318 rettv->vval.v_number = -1;
16319 else
16320 {
16321 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16322 rettv->vval.v_number = (s != NULL);
16323 }
16324# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016325 if (check_connection() == FAIL)
16326 return;
16327
16328 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016329 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016330# endif
16331
16332 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016334 char_u *retvar;
16335
Bram Moolenaar33570922005-01-25 22:26:29 +000016336 v.di_tv.v_type = VAR_STRING;
16337 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016338 retvar = get_tv_string_chk(&argvars[1]);
16339 if (retvar != NULL)
16340 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016341 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016342 }
16343#else
16344 rettv->vval.v_number = -1;
16345#endif
16346}
16347
Bram Moolenaar0d660222005-01-07 21:51:51 +000016348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016349f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016350{
16351 char_u *r = NULL;
16352
16353#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016354 char_u *serverid = get_tv_string_chk(&argvars[0]);
16355
16356 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016357 {
16358# ifdef WIN32
16359 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016360 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016361
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016362 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016363 if (n != 0)
16364 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16365 if (r == NULL)
16366# else
16367 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016368 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016369# endif
16370 EMSG(_("E277: Unable to read a server reply"));
16371 }
16372#endif
16373 rettv->v_type = VAR_STRING;
16374 rettv->vval.v_string = r;
16375}
16376
16377/*
16378 * "remote_send()" function
16379 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016381f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016382{
16383 rettv->v_type = VAR_STRING;
16384 rettv->vval.v_string = NULL;
16385#ifdef FEAT_CLIENTSERVER
16386 remote_common(argvars, rettv, FALSE);
16387#endif
16388}
16389
16390/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016391 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016392 */
16393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016394f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016395{
Bram Moolenaar33570922005-01-25 22:26:29 +000016396 list_T *l;
16397 listitem_T *item, *item2;
16398 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016399 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016400 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016401 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016402 dict_T *d;
16403 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016404 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016405
Bram Moolenaar8c711452005-01-14 21:53:12 +000016406 if (argvars[0].v_type == VAR_DICT)
16407 {
16408 if (argvars[2].v_type != VAR_UNKNOWN)
16409 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016410 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016411 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016412 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016413 key = get_tv_string_chk(&argvars[1]);
16414 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016415 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016416 di = dict_find(d, key, -1);
16417 if (di == NULL)
16418 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016419 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16420 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016421 {
16422 *rettv = di->di_tv;
16423 init_tv(&di->di_tv);
16424 dictitem_remove(d, di);
16425 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016426 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016427 }
16428 }
16429 else if (argvars[0].v_type != VAR_LIST)
16430 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016431 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016432 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016434 int error = FALSE;
16435
16436 idx = get_tv_number_chk(&argvars[1], &error);
16437 if (error)
16438 ; /* type error: do nothing, errmsg already given */
16439 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016440 EMSGN(_(e_listidx), idx);
16441 else
16442 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016443 if (argvars[2].v_type == VAR_UNKNOWN)
16444 {
16445 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016446 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016447 *rettv = item->li_tv;
16448 vim_free(item);
16449 }
16450 else
16451 {
16452 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016453 end = get_tv_number_chk(&argvars[2], &error);
16454 if (error)
16455 ; /* type error: do nothing */
16456 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016457 EMSGN(_(e_listidx), end);
16458 else
16459 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016460 int cnt = 0;
16461
16462 for (li = item; li != NULL; li = li->li_next)
16463 {
16464 ++cnt;
16465 if (li == item2)
16466 break;
16467 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016468 if (li == NULL) /* didn't find "item2" after "item" */
16469 EMSG(_(e_invrange));
16470 else
16471 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016472 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016473 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016474 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016475 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016476 l->lv_first = item;
16477 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016478 item->li_prev = NULL;
16479 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016480 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016481 }
16482 }
16483 }
16484 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016485 }
16486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016487}
16488
16489/*
16490 * "rename({from}, {to})" function
16491 */
16492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016493f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016494{
16495 char_u buf[NUMBUFLEN];
16496
16497 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016498 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016499 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016500 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16501 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016502}
16503
16504/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016505 * "repeat()" function
16506 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016508f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016509{
16510 char_u *p;
16511 int n;
16512 int slen;
16513 int len;
16514 char_u *r;
16515 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016516
16517 n = get_tv_number(&argvars[1]);
16518 if (argvars[0].v_type == VAR_LIST)
16519 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016520 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016521 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016522 if (list_extend(rettv->vval.v_list,
16523 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016524 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016525 }
16526 else
16527 {
16528 p = get_tv_string(&argvars[0]);
16529 rettv->v_type = VAR_STRING;
16530 rettv->vval.v_string = NULL;
16531
16532 slen = (int)STRLEN(p);
16533 len = slen * n;
16534 if (len <= 0)
16535 return;
16536
16537 r = alloc(len + 1);
16538 if (r != NULL)
16539 {
16540 for (i = 0; i < n; i++)
16541 mch_memmove(r + i * slen, p, (size_t)slen);
16542 r[len] = NUL;
16543 }
16544
16545 rettv->vval.v_string = r;
16546 }
16547}
16548
16549/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016550 * "resolve()" function
16551 */
16552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016553f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016554{
16555 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016556#ifdef HAVE_READLINK
16557 char_u *buf = NULL;
16558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016559
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016560 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016561#ifdef FEAT_SHORTCUT
16562 {
16563 char_u *v = NULL;
16564
16565 v = mch_resolve_shortcut(p);
16566 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016567 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016568 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016569 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570 }
16571#else
16572# ifdef HAVE_READLINK
16573 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016574 char_u *cpy;
16575 int len;
16576 char_u *remain = NULL;
16577 char_u *q;
16578 int is_relative_to_current = FALSE;
16579 int has_trailing_pathsep = FALSE;
16580 int limit = 100;
16581
16582 p = vim_strsave(p);
16583
16584 if (p[0] == '.' && (vim_ispathsep(p[1])
16585 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16586 is_relative_to_current = TRUE;
16587
16588 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016589 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016590 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016592 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016594
16595 q = getnextcomp(p);
16596 if (*q != NUL)
16597 {
16598 /* Separate the first path component in "p", and keep the
16599 * remainder (beginning with the path separator). */
16600 remain = vim_strsave(q - 1);
16601 q[-1] = NUL;
16602 }
16603
Bram Moolenaard9462e32011-04-11 21:35:11 +020016604 buf = alloc(MAXPATHL + 1);
16605 if (buf == NULL)
16606 goto fail;
16607
Bram Moolenaar071d4272004-06-13 20:20:40 +000016608 for (;;)
16609 {
16610 for (;;)
16611 {
16612 len = readlink((char *)p, (char *)buf, MAXPATHL);
16613 if (len <= 0)
16614 break;
16615 buf[len] = NUL;
16616
16617 if (limit-- == 0)
16618 {
16619 vim_free(p);
16620 vim_free(remain);
16621 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016622 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016623 goto fail;
16624 }
16625
16626 /* Ensure that the result will have a trailing path separator
16627 * if the argument has one. */
16628 if (remain == NULL && has_trailing_pathsep)
16629 add_pathsep(buf);
16630
16631 /* Separate the first path component in the link value and
16632 * concatenate the remainders. */
16633 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16634 if (*q != NUL)
16635 {
16636 if (remain == NULL)
16637 remain = vim_strsave(q - 1);
16638 else
16639 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016640 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016641 if (cpy != NULL)
16642 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016643 vim_free(remain);
16644 remain = cpy;
16645 }
16646 }
16647 q[-1] = NUL;
16648 }
16649
16650 q = gettail(p);
16651 if (q > p && *q == NUL)
16652 {
16653 /* Ignore trailing path separator. */
16654 q[-1] = NUL;
16655 q = gettail(p);
16656 }
16657 if (q > p && !mch_isFullName(buf))
16658 {
16659 /* symlink is relative to directory of argument */
16660 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16661 if (cpy != NULL)
16662 {
16663 STRCPY(cpy, p);
16664 STRCPY(gettail(cpy), buf);
16665 vim_free(p);
16666 p = cpy;
16667 }
16668 }
16669 else
16670 {
16671 vim_free(p);
16672 p = vim_strsave(buf);
16673 }
16674 }
16675
16676 if (remain == NULL)
16677 break;
16678
16679 /* Append the first path component of "remain" to "p". */
16680 q = getnextcomp(remain + 1);
16681 len = q - remain - (*q != NUL);
16682 cpy = vim_strnsave(p, STRLEN(p) + len);
16683 if (cpy != NULL)
16684 {
16685 STRNCAT(cpy, remain, len);
16686 vim_free(p);
16687 p = cpy;
16688 }
16689 /* Shorten "remain". */
16690 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016691 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016692 else
16693 {
16694 vim_free(remain);
16695 remain = NULL;
16696 }
16697 }
16698
16699 /* If the result is a relative path name, make it explicitly relative to
16700 * the current directory if and only if the argument had this form. */
16701 if (!vim_ispathsep(*p))
16702 {
16703 if (is_relative_to_current
16704 && *p != NUL
16705 && !(p[0] == '.'
16706 && (p[1] == NUL
16707 || vim_ispathsep(p[1])
16708 || (p[1] == '.'
16709 && (p[2] == NUL
16710 || vim_ispathsep(p[2]))))))
16711 {
16712 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016713 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016714 if (cpy != NULL)
16715 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716 vim_free(p);
16717 p = cpy;
16718 }
16719 }
16720 else if (!is_relative_to_current)
16721 {
16722 /* Strip leading "./". */
16723 q = p;
16724 while (q[0] == '.' && vim_ispathsep(q[1]))
16725 q += 2;
16726 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016727 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016728 }
16729 }
16730
16731 /* Ensure that the result will have no trailing path separator
16732 * if the argument had none. But keep "/" or "//". */
16733 if (!has_trailing_pathsep)
16734 {
16735 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016736 if (after_pathsep(p, q))
16737 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016738 }
16739
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016740 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016741 }
16742# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016743 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016744# endif
16745#endif
16746
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016747 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748
16749#ifdef HAVE_READLINK
16750fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016751 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016752#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016753 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016754}
16755
16756/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016757 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016758 */
16759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016760f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016761{
Bram Moolenaar33570922005-01-25 22:26:29 +000016762 list_T *l;
16763 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016764
Bram Moolenaar0d660222005-01-07 21:51:51 +000016765 if (argvars[0].v_type != VAR_LIST)
16766 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016767 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016768 && !tv_check_lock(l->lv_lock,
16769 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016770 {
16771 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016772 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016773 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016774 while (li != NULL)
16775 {
16776 ni = li->li_prev;
16777 list_append(l, li);
16778 li = ni;
16779 }
16780 rettv->vval.v_list = l;
16781 rettv->v_type = VAR_LIST;
16782 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016783 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016785}
16786
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016787#define SP_NOMOVE 0x01 /* don't move cursor */
16788#define SP_REPEAT 0x02 /* repeat to find outer pair */
16789#define SP_RETCOUNT 0x04 /* return matchcount */
16790#define SP_SETPCMARK 0x08 /* set previous context mark */
16791#define SP_START 0x10 /* accept match at start position */
16792#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16793#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016794#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016795
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016796static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016797
16798/*
16799 * Get flags for a search function.
16800 * Possibly sets "p_ws".
16801 * Returns BACKWARD, FORWARD or zero (for an error).
16802 */
16803 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016804get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016805{
16806 int dir = FORWARD;
16807 char_u *flags;
16808 char_u nbuf[NUMBUFLEN];
16809 int mask;
16810
16811 if (varp->v_type != VAR_UNKNOWN)
16812 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016813 flags = get_tv_string_buf_chk(varp, nbuf);
16814 if (flags == NULL)
16815 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016816 while (*flags != NUL)
16817 {
16818 switch (*flags)
16819 {
16820 case 'b': dir = BACKWARD; break;
16821 case 'w': p_ws = TRUE; break;
16822 case 'W': p_ws = FALSE; break;
16823 default: mask = 0;
16824 if (flagsp != NULL)
16825 switch (*flags)
16826 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016827 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016828 case 'e': mask = SP_END; break;
16829 case 'm': mask = SP_RETCOUNT; break;
16830 case 'n': mask = SP_NOMOVE; break;
16831 case 'p': mask = SP_SUBPAT; break;
16832 case 'r': mask = SP_REPEAT; break;
16833 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016834 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016835 }
16836 if (mask == 0)
16837 {
16838 EMSG2(_(e_invarg2), flags);
16839 dir = 0;
16840 }
16841 else
16842 *flagsp |= mask;
16843 }
16844 if (dir == 0)
16845 break;
16846 ++flags;
16847 }
16848 }
16849 return dir;
16850}
16851
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016853 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016854 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016855 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016856search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016857{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016858 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016859 char_u *pat;
16860 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016861 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862 int save_p_ws = p_ws;
16863 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016864 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016865 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016866 proftime_T tm;
16867#ifdef FEAT_RELTIME
16868 long time_limit = 0;
16869#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016870 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016871 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016872
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016873 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016874 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016875 if (dir == 0)
16876 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016877 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016878 if (flags & SP_START)
16879 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016880 if (flags & SP_END)
16881 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016882 if (flags & SP_COLUMN)
16883 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016884
Bram Moolenaar76929292008-01-06 19:07:36 +000016885 /* Optional arguments: line number to stop searching and timeout. */
16886 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016887 {
16888 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16889 if (lnum_stop < 0)
16890 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016891#ifdef FEAT_RELTIME
16892 if (argvars[3].v_type != VAR_UNKNOWN)
16893 {
16894 time_limit = get_tv_number_chk(&argvars[3], NULL);
16895 if (time_limit < 0)
16896 goto theend;
16897 }
16898#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016899 }
16900
Bram Moolenaar76929292008-01-06 19:07:36 +000016901#ifdef FEAT_RELTIME
16902 /* Set the time limit, if there is one. */
16903 profile_setlimit(time_limit, &tm);
16904#endif
16905
Bram Moolenaar231334e2005-07-25 20:46:57 +000016906 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016907 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016908 * Check to make sure only those flags are set.
16909 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16910 * flags cannot be set. Check for that condition also.
16911 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016912 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016913 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016914 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016915 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016916 goto theend;
16917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016918
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016919 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016920 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016921 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016922 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016923 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016924 if (flags & SP_SUBPAT)
16925 retval = subpatnum;
16926 else
16927 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016928 if (flags & SP_SETPCMARK)
16929 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016931 if (match_pos != NULL)
16932 {
16933 /* Store the match cursor position */
16934 match_pos->lnum = pos.lnum;
16935 match_pos->col = pos.col + 1;
16936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016937 /* "/$" will put the cursor after the end of the line, may need to
16938 * correct that here */
16939 check_cursor();
16940 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016941
16942 /* If 'n' flag is used: restore cursor position. */
16943 if (flags & SP_NOMOVE)
16944 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016945 else
16946 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016947theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016948 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016949
16950 return retval;
16951}
16952
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016953#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016954
16955/*
16956 * round() is not in C90, use ceil() or floor() instead.
16957 */
16958 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010016959vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016960{
16961 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16962}
16963
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016964/*
16965 * "round({float})" function
16966 */
16967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016968f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016969{
16970 float_T f;
16971
16972 rettv->v_type = VAR_FLOAT;
16973 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016974 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016975 else
16976 rettv->vval.v_float = 0.0;
16977}
16978#endif
16979
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016980/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016981 * "screenattr()" function
16982 */
16983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016984f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016985{
16986 int row;
16987 int col;
16988 int c;
16989
16990 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16991 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16992 if (row < 0 || row >= screen_Rows
16993 || col < 0 || col >= screen_Columns)
16994 c = -1;
16995 else
16996 c = ScreenAttrs[LineOffset[row] + col];
16997 rettv->vval.v_number = c;
16998}
16999
17000/*
17001 * "screenchar()" function
17002 */
17003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017004f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017005{
17006 int row;
17007 int col;
17008 int off;
17009 int c;
17010
17011 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17012 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17013 if (row < 0 || row >= screen_Rows
17014 || col < 0 || col >= screen_Columns)
17015 c = -1;
17016 else
17017 {
17018 off = LineOffset[row] + col;
17019#ifdef FEAT_MBYTE
17020 if (enc_utf8 && ScreenLinesUC[off] != 0)
17021 c = ScreenLinesUC[off];
17022 else
17023#endif
17024 c = ScreenLines[off];
17025 }
17026 rettv->vval.v_number = c;
17027}
17028
17029/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017030 * "screencol()" function
17031 *
17032 * First column is 1 to be consistent with virtcol().
17033 */
17034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017035f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017036{
17037 rettv->vval.v_number = screen_screencol() + 1;
17038}
17039
17040/*
17041 * "screenrow()" function
17042 */
17043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017044f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017045{
17046 rettv->vval.v_number = screen_screenrow() + 1;
17047}
17048
17049/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017050 * "search()" function
17051 */
17052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017053f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017054{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017055 int flags = 0;
17056
17057 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058}
17059
Bram Moolenaar071d4272004-06-13 20:20:40 +000017060/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017061 * "searchdecl()" function
17062 */
17063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017064f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017065{
17066 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017067 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017068 int error = FALSE;
17069 char_u *name;
17070
17071 rettv->vval.v_number = 1; /* default: FAIL */
17072
17073 name = get_tv_string_chk(&argvars[0]);
17074 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017075 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017076 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017077 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17078 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17079 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017080 if (!error && name != NULL)
17081 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017082 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017083}
17084
17085/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017086 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017088 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017089searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090{
17091 char_u *spat, *mpat, *epat;
17092 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017093 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017094 int dir;
17095 int flags = 0;
17096 char_u nbuf1[NUMBUFLEN];
17097 char_u nbuf2[NUMBUFLEN];
17098 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017099 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017100 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017101 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102
Bram Moolenaar071d4272004-06-13 20:20:40 +000017103 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017104 spat = get_tv_string_chk(&argvars[0]);
17105 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17106 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17107 if (spat == NULL || mpat == NULL || epat == NULL)
17108 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110 /* Handle the optional fourth argument: flags */
17111 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017112 if (dir == 0)
17113 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017114
17115 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017116 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17117 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017118 if ((flags & (SP_END | SP_SUBPAT)) != 0
17119 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017120 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017121 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017122 goto theend;
17123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017124
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017125 /* Using 'r' implies 'W', otherwise it doesn't work. */
17126 if (flags & SP_REPEAT)
17127 p_ws = FALSE;
17128
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017129 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017130 if (argvars[3].v_type == VAR_UNKNOWN
17131 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017132 skip = (char_u *)"";
17133 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017134 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017135 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017136 if (argvars[5].v_type != VAR_UNKNOWN)
17137 {
17138 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17139 if (lnum_stop < 0)
17140 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017141#ifdef FEAT_RELTIME
17142 if (argvars[6].v_type != VAR_UNKNOWN)
17143 {
17144 time_limit = get_tv_number_chk(&argvars[6], NULL);
17145 if (time_limit < 0)
17146 goto theend;
17147 }
17148#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017149 }
17150 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017151 if (skip == NULL)
17152 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017153
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017154 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017155 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017156
17157theend:
17158 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017159
17160 return retval;
17161}
17162
17163/*
17164 * "searchpair()" function
17165 */
17166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017167f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017168{
17169 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17170}
17171
17172/*
17173 * "searchpairpos()" function
17174 */
17175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017176f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017177{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017178 pos_T match_pos;
17179 int lnum = 0;
17180 int col = 0;
17181
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017182 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017183 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017184
17185 if (searchpair_cmn(argvars, &match_pos) > 0)
17186 {
17187 lnum = match_pos.lnum;
17188 col = match_pos.col;
17189 }
17190
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017191 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17192 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017193}
17194
17195/*
17196 * Search for a start/middle/end thing.
17197 * Used by searchpair(), see its documentation for the details.
17198 * Returns 0 or -1 for no match,
17199 */
17200 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017201do_searchpair(
17202 char_u *spat, /* start pattern */
17203 char_u *mpat, /* middle pattern */
17204 char_u *epat, /* end pattern */
17205 int dir, /* BACKWARD or FORWARD */
17206 char_u *skip, /* skip expression */
17207 int flags, /* SP_SETPCMARK and other SP_ values */
17208 pos_T *match_pos,
17209 linenr_T lnum_stop, /* stop at this line if not zero */
17210 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017211{
17212 char_u *save_cpo;
17213 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17214 long retval = 0;
17215 pos_T pos;
17216 pos_T firstpos;
17217 pos_T foundpos;
17218 pos_T save_cursor;
17219 pos_T save_pos;
17220 int n;
17221 int r;
17222 int nest = 1;
17223 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017224 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017225 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017226
17227 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17228 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017229 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017230
Bram Moolenaar76929292008-01-06 19:07:36 +000017231#ifdef FEAT_RELTIME
17232 /* Set the time limit, if there is one. */
17233 profile_setlimit(time_limit, &tm);
17234#endif
17235
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017236 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17237 * start/middle/end (pat3, for the top pair). */
17238 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17239 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17240 if (pat2 == NULL || pat3 == NULL)
17241 goto theend;
17242 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17243 if (*mpat == NUL)
17244 STRCPY(pat3, pat2);
17245 else
17246 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17247 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017248 if (flags & SP_START)
17249 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017250
Bram Moolenaar071d4272004-06-13 20:20:40 +000017251 save_cursor = curwin->w_cursor;
17252 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017253 clearpos(&firstpos);
17254 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017255 pat = pat3;
17256 for (;;)
17257 {
17258 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017259 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017260 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17261 /* didn't find it or found the first match again: FAIL */
17262 break;
17263
17264 if (firstpos.lnum == 0)
17265 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017266 if (equalpos(pos, foundpos))
17267 {
17268 /* Found the same position again. Can happen with a pattern that
17269 * has "\zs" at the end and searching backwards. Advance one
17270 * character and try again. */
17271 if (dir == BACKWARD)
17272 decl(&pos);
17273 else
17274 incl(&pos);
17275 }
17276 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017277
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017278 /* clear the start flag to avoid getting stuck here */
17279 options &= ~SEARCH_START;
17280
Bram Moolenaar071d4272004-06-13 20:20:40 +000017281 /* If the skip pattern matches, ignore this match. */
17282 if (*skip != NUL)
17283 {
17284 save_pos = curwin->w_cursor;
17285 curwin->w_cursor = pos;
17286 r = eval_to_bool(skip, &err, NULL, FALSE);
17287 curwin->w_cursor = save_pos;
17288 if (err)
17289 {
17290 /* Evaluating {skip} caused an error, break here. */
17291 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017292 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017293 break;
17294 }
17295 if (r)
17296 continue;
17297 }
17298
17299 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17300 {
17301 /* Found end when searching backwards or start when searching
17302 * forward: nested pair. */
17303 ++nest;
17304 pat = pat2; /* nested, don't search for middle */
17305 }
17306 else
17307 {
17308 /* Found end when searching forward or start when searching
17309 * backward: end of (nested) pair; or found middle in outer pair. */
17310 if (--nest == 1)
17311 pat = pat3; /* outer level, search for middle */
17312 }
17313
17314 if (nest == 0)
17315 {
17316 /* Found the match: return matchcount or line number. */
17317 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017318 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017319 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017320 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017321 if (flags & SP_SETPCMARK)
17322 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017323 curwin->w_cursor = pos;
17324 if (!(flags & SP_REPEAT))
17325 break;
17326 nest = 1; /* search for next unmatched */
17327 }
17328 }
17329
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017330 if (match_pos != NULL)
17331 {
17332 /* Store the match cursor position */
17333 match_pos->lnum = curwin->w_cursor.lnum;
17334 match_pos->col = curwin->w_cursor.col + 1;
17335 }
17336
Bram Moolenaar071d4272004-06-13 20:20:40 +000017337 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017338 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339 curwin->w_cursor = save_cursor;
17340
17341theend:
17342 vim_free(pat2);
17343 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017344 if (p_cpo == empty_option)
17345 p_cpo = save_cpo;
17346 else
17347 /* Darn, evaluating the {skip} expression changed the value. */
17348 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017349
17350 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351}
17352
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017353/*
17354 * "searchpos()" function
17355 */
17356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017357f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017358{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017359 pos_T match_pos;
17360 int lnum = 0;
17361 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017362 int n;
17363 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017364
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017365 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017366 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017367
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017368 n = search_cmn(argvars, &match_pos, &flags);
17369 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017370 {
17371 lnum = match_pos.lnum;
17372 col = match_pos.col;
17373 }
17374
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017375 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17376 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017377 if (flags & SP_SUBPAT)
17378 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017379}
17380
Bram Moolenaar0d660222005-01-07 21:51:51 +000017381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017382f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017384#ifdef FEAT_CLIENTSERVER
17385 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017386 char_u *server = get_tv_string_chk(&argvars[0]);
17387 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017388
Bram Moolenaar0d660222005-01-07 21:51:51 +000017389 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017390 if (server == NULL || reply == NULL)
17391 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017392 if (check_restricted() || check_secure())
17393 return;
17394# ifdef FEAT_X11
17395 if (check_connection() == FAIL)
17396 return;
17397# endif
17398
17399 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017400 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017401 EMSG(_("E258: Unable to send to client"));
17402 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017404 rettv->vval.v_number = 0;
17405#else
17406 rettv->vval.v_number = -1;
17407#endif
17408}
17409
Bram Moolenaar0d660222005-01-07 21:51:51 +000017410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017411f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017412{
17413 char_u *r = NULL;
17414
17415#ifdef FEAT_CLIENTSERVER
17416# ifdef WIN32
17417 r = serverGetVimNames();
17418# else
17419 make_connection();
17420 if (X_DISPLAY != NULL)
17421 r = serverGetVimNames(X_DISPLAY);
17422# endif
17423#endif
17424 rettv->v_type = VAR_STRING;
17425 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426}
17427
17428/*
17429 * "setbufvar()" function
17430 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017432f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017433{
17434 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017435 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017436 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017437 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017438 char_u nbuf[NUMBUFLEN];
17439
17440 if (check_restricted() || check_secure())
17441 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017442 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17443 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017444 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017445 varp = &argvars[2];
17446
17447 if (buf != NULL && varname != NULL && varp != NULL)
17448 {
17449 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017450 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017451
17452 if (*varname == '&')
17453 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017454 long numval;
17455 char_u *strval;
17456 int error = FALSE;
17457
Bram Moolenaar071d4272004-06-13 20:20:40 +000017458 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017459 numval = get_tv_number_chk(varp, &error);
17460 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017461 if (!error && strval != NULL)
17462 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017463 }
17464 else
17465 {
17466 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17467 if (bufvarname != NULL)
17468 {
17469 STRCPY(bufvarname, "b:");
17470 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017471 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017472 vim_free(bufvarname);
17473 }
17474 }
17475
17476 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017479}
17480
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017482f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017483{
17484 dict_T *d;
17485 dictitem_T *di;
17486 char_u *csearch;
17487
17488 if (argvars[0].v_type != VAR_DICT)
17489 {
17490 EMSG(_(e_dictreq));
17491 return;
17492 }
17493
17494 if ((d = argvars[0].vval.v_dict) != NULL)
17495 {
17496 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17497 if (csearch != NULL)
17498 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017499#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017500 if (enc_utf8)
17501 {
17502 int pcc[MAX_MCO];
17503 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017504
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017505 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17506 }
17507 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017508#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017509 set_last_csearch(PTR2CHAR(csearch),
17510 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017511 }
17512
17513 di = dict_find(d, (char_u *)"forward", -1);
17514 if (di != NULL)
17515 set_csearch_direction(get_tv_number(&di->di_tv)
17516 ? FORWARD : BACKWARD);
17517
17518 di = dict_find(d, (char_u *)"until", -1);
17519 if (di != NULL)
17520 set_csearch_until(!!get_tv_number(&di->di_tv));
17521 }
17522}
17523
Bram Moolenaar071d4272004-06-13 20:20:40 +000017524/*
17525 * "setcmdpos()" function
17526 */
17527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017528f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017529{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017530 int pos = (int)get_tv_number(&argvars[0]) - 1;
17531
17532 if (pos >= 0)
17533 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017534}
17535
17536/*
17537 * "setline()" function
17538 */
17539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017540f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017541{
17542 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017543 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017544 list_T *l = NULL;
17545 listitem_T *li = NULL;
17546 long added = 0;
17547 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017548
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017549 lnum = get_tv_lnum(&argvars[0]);
17550 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017551 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017552 l = argvars[1].vval.v_list;
17553 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017554 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017555 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017556 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017557
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017558 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017559 for (;;)
17560 {
17561 if (l != NULL)
17562 {
17563 /* list argument, get next string */
17564 if (li == NULL)
17565 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017566 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017567 li = li->li_next;
17568 }
17569
17570 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017571 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017572 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017573
17574 /* When coming here from Insert mode, sync undo, so that this can be
17575 * undone separately from what was previously inserted. */
17576 if (u_sync_once == 2)
17577 {
17578 u_sync_once = 1; /* notify that u_sync() was called */
17579 u_sync(TRUE);
17580 }
17581
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017582 if (lnum <= curbuf->b_ml.ml_line_count)
17583 {
17584 /* existing line, replace it */
17585 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17586 {
17587 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017588 if (lnum == curwin->w_cursor.lnum)
17589 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017590 rettv->vval.v_number = 0; /* OK */
17591 }
17592 }
17593 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17594 {
17595 /* lnum is one past the last line, append the line */
17596 ++added;
17597 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17598 rettv->vval.v_number = 0; /* OK */
17599 }
17600
17601 if (l == NULL) /* only one string argument */
17602 break;
17603 ++lnum;
17604 }
17605
17606 if (added > 0)
17607 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017608}
17609
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017610static 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 +000017611
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017613 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017614 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017616set_qf_ll_list(
17617 win_T *wp UNUSED,
17618 typval_T *list_arg UNUSED,
17619 typval_T *action_arg UNUSED,
17620 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017621{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017622#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017623 char_u *act;
17624 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017625#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017626
Bram Moolenaar2641f772005-03-25 21:58:17 +000017627 rettv->vval.v_number = -1;
17628
17629#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017630 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017631 EMSG(_(e_listreq));
17632 else
17633 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017634 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017635
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017636 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017637 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017638 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017639 if (act == NULL)
17640 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017641 if (*act == 'a' || *act == 'r')
17642 action = *act;
17643 }
17644
Bram Moolenaar81484f42012-12-05 15:16:47 +010017645 if (l != NULL && set_errorlist(wp, l, action,
17646 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017647 rettv->vval.v_number = 0;
17648 }
17649#endif
17650}
17651
17652/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017653 * "setloclist()" function
17654 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017656f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017657{
17658 win_T *win;
17659
17660 rettv->vval.v_number = -1;
17661
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017662 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017663 if (win != NULL)
17664 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17665}
17666
17667/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017668 * "setmatches()" function
17669 */
17670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017671f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017672{
17673#ifdef FEAT_SEARCH_EXTRA
17674 list_T *l;
17675 listitem_T *li;
17676 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017677 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017678
17679 rettv->vval.v_number = -1;
17680 if (argvars[0].v_type != VAR_LIST)
17681 {
17682 EMSG(_(e_listreq));
17683 return;
17684 }
17685 if ((l = argvars[0].vval.v_list) != NULL)
17686 {
17687
17688 /* To some extent make sure that we are dealing with a list from
17689 * "getmatches()". */
17690 li = l->lv_first;
17691 while (li != NULL)
17692 {
17693 if (li->li_tv.v_type != VAR_DICT
17694 || (d = li->li_tv.vval.v_dict) == NULL)
17695 {
17696 EMSG(_(e_invarg));
17697 return;
17698 }
17699 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017700 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17701 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017702 && dict_find(d, (char_u *)"priority", -1) != NULL
17703 && dict_find(d, (char_u *)"id", -1) != NULL))
17704 {
17705 EMSG(_(e_invarg));
17706 return;
17707 }
17708 li = li->li_next;
17709 }
17710
17711 clear_matches(curwin);
17712 li = l->lv_first;
17713 while (li != NULL)
17714 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017715 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017716 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017717 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017718 char_u *group;
17719 int priority;
17720 int id;
17721 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017722
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017723 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017724 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17725 {
17726 if (s == NULL)
17727 {
17728 s = list_alloc();
17729 if (s == NULL)
17730 return;
17731 }
17732
17733 /* match from matchaddpos() */
17734 for (i = 1; i < 9; i++)
17735 {
17736 sprintf((char *)buf, (char *)"pos%d", i);
17737 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17738 {
17739 if (di->di_tv.v_type != VAR_LIST)
17740 return;
17741
17742 list_append_tv(s, &di->di_tv);
17743 s->lv_refcount++;
17744 }
17745 else
17746 break;
17747 }
17748 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017749
17750 group = get_dict_string(d, (char_u *)"group", FALSE);
17751 priority = (int)get_dict_number(d, (char_u *)"priority");
17752 id = (int)get_dict_number(d, (char_u *)"id");
17753 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17754 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17755 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017756 if (i == 0)
17757 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017758 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017759 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017760 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017761 }
17762 else
17763 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017764 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017765 list_unref(s);
17766 s = NULL;
17767 }
17768
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017769 li = li->li_next;
17770 }
17771 rettv->vval.v_number = 0;
17772 }
17773#endif
17774}
17775
17776/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017777 * "setpos()" function
17778 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017780f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017781{
17782 pos_T pos;
17783 int fnum;
17784 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017785 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017786
Bram Moolenaar08250432008-02-13 11:42:46 +000017787 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017788 name = get_tv_string_chk(argvars);
17789 if (name != NULL)
17790 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017791 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017792 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017793 if (--pos.col < 0)
17794 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017795 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017796 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017797 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017798 if (fnum == curbuf->b_fnum)
17799 {
17800 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017801 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017802 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017803 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017804 curwin->w_set_curswant = FALSE;
17805 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017806 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017807 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017808 }
17809 else
17810 EMSG(_(e_invarg));
17811 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017812 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17813 {
17814 /* set mark */
17815 if (setmark_pos(name[1], &pos, fnum) == OK)
17816 rettv->vval.v_number = 0;
17817 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017818 else
17819 EMSG(_(e_invarg));
17820 }
17821 }
17822}
17823
17824/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017825 * "setqflist()" function
17826 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017828f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017829{
17830 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17831}
17832
17833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017834 * "setreg()" function
17835 */
17836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017837f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017838{
17839 int regname;
17840 char_u *strregname;
17841 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017842 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017843 int append;
17844 char_u yank_type;
17845 long block_len;
17846
17847 block_len = -1;
17848 yank_type = MAUTO;
17849 append = FALSE;
17850
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017851 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017852 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017853
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017854 if (strregname == NULL)
17855 return; /* type error; errmsg already given */
17856 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017857 if (regname == 0 || regname == '@')
17858 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017859
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017860 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017861 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017862 stropt = get_tv_string_chk(&argvars[2]);
17863 if (stropt == NULL)
17864 return; /* type error */
17865 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017866 switch (*stropt)
17867 {
17868 case 'a': case 'A': /* append */
17869 append = TRUE;
17870 break;
17871 case 'v': case 'c': /* character-wise selection */
17872 yank_type = MCHAR;
17873 break;
17874 case 'V': case 'l': /* line-wise selection */
17875 yank_type = MLINE;
17876 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017877 case 'b': case Ctrl_V: /* block-wise selection */
17878 yank_type = MBLOCK;
17879 if (VIM_ISDIGIT(stropt[1]))
17880 {
17881 ++stropt;
17882 block_len = getdigits(&stropt) - 1;
17883 --stropt;
17884 }
17885 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017886 }
17887 }
17888
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017889 if (argvars[1].v_type == VAR_LIST)
17890 {
17891 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017892 char_u **allocval;
17893 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017894 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017895 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017896 int len = argvars[1].vval.v_list->lv_len;
17897 listitem_T *li;
17898
Bram Moolenaar7d647822014-04-05 21:28:56 +020017899 /* First half: use for pointers to result lines; second half: use for
17900 * pointers to allocated copies. */
17901 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017902 if (lstval == NULL)
17903 return;
17904 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017905 allocval = lstval + len + 2;
17906 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017907
17908 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17909 li = li->li_next)
17910 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017911 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017912 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017913 goto free_lstval;
17914 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017915 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017916 /* Need to make a copy, next get_tv_string_buf_chk() will
17917 * overwrite the string. */
17918 strval = vim_strsave(buf);
17919 if (strval == NULL)
17920 goto free_lstval;
17921 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017922 }
17923 *curval++ = strval;
17924 }
17925 *curval++ = NULL;
17926
17927 write_reg_contents_lst(regname, lstval, -1,
17928 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017929free_lstval:
17930 while (curallocval > allocval)
17931 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017932 vim_free(lstval);
17933 }
17934 else
17935 {
17936 strval = get_tv_string_chk(&argvars[1]);
17937 if (strval == NULL)
17938 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017939 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017940 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017941 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017942 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943}
17944
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017945/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017946 * "settabvar()" function
17947 */
17948 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017949f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017950{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017951#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017952 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017953 tabpage_T *tp;
17954#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017955 char_u *varname, *tabvarname;
17956 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017957
17958 rettv->vval.v_number = 0;
17959
17960 if (check_restricted() || check_secure())
17961 return;
17962
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017963#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017964 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017965#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017966 varname = get_tv_string_chk(&argvars[1]);
17967 varp = &argvars[2];
17968
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017969 if (varname != NULL && varp != NULL
17970#ifdef FEAT_WINDOWS
17971 && tp != NULL
17972#endif
17973 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017974 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017975#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017976 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017977 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017978#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017979
17980 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17981 if (tabvarname != NULL)
17982 {
17983 STRCPY(tabvarname, "t:");
17984 STRCPY(tabvarname + 2, varname);
17985 set_var(tabvarname, varp, TRUE);
17986 vim_free(tabvarname);
17987 }
17988
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017989#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017990 /* Restore current tabpage */
17991 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017992 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017993#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017994 }
17995}
17996
17997/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017998 * "settabwinvar()" function
17999 */
18000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018001f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018002{
18003 setwinvar(argvars, rettv, 1);
18004}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005
18006/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018007 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018010f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018012 setwinvar(argvars, rettv, 0);
18013}
18014
18015/*
18016 * "setwinvar()" and "settabwinvar()" functions
18017 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018018
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018020setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018021{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018022 win_T *win;
18023#ifdef FEAT_WINDOWS
18024 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018025 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018026 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027#endif
18028 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018029 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018030 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018031 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032
18033 if (check_restricted() || check_secure())
18034 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018035
18036#ifdef FEAT_WINDOWS
18037 if (off == 1)
18038 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18039 else
18040 tp = curtab;
18041#endif
18042 win = find_win_by_nr(&argvars[off], tp);
18043 varname = get_tv_string_chk(&argvars[off + 1]);
18044 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018045
18046 if (win != NULL && varname != NULL && varp != NULL)
18047 {
18048#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018049 need_switch_win = !(tp == curtab && win == curwin);
18050 if (!need_switch_win
18051 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018054 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018056 long numval;
18057 char_u *strval;
18058 int error = FALSE;
18059
18060 ++varname;
18061 numval = get_tv_number_chk(varp, &error);
18062 strval = get_tv_string_buf_chk(varp, nbuf);
18063 if (!error && strval != NULL)
18064 set_option_value(varname, numval, strval, OPT_LOCAL);
18065 }
18066 else
18067 {
18068 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18069 if (winvarname != NULL)
18070 {
18071 STRCPY(winvarname, "w:");
18072 STRCPY(winvarname + 2, varname);
18073 set_var(winvarname, varp, TRUE);
18074 vim_free(winvarname);
18075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018076 }
18077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018078#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018079 if (need_switch_win)
18080 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018081#endif
18082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018083}
18084
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018085#ifdef FEAT_CRYPT
18086/*
18087 * "sha256({string})" function
18088 */
18089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018090f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018091{
18092 char_u *p;
18093
18094 p = get_tv_string(&argvars[0]);
18095 rettv->vval.v_string = vim_strsave(
18096 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18097 rettv->v_type = VAR_STRING;
18098}
18099#endif /* FEAT_CRYPT */
18100
Bram Moolenaar071d4272004-06-13 20:20:40 +000018101/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018102 * "shellescape({string})" function
18103 */
18104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018105f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018106{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018107 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018108 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018109 rettv->v_type = VAR_STRING;
18110}
18111
18112/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018113 * shiftwidth() function
18114 */
18115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018116f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018117{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018118 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018119}
18120
18121/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018122 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018123 */
18124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018125f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018126{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018127 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018128
Bram Moolenaar0d660222005-01-07 21:51:51 +000018129 p = get_tv_string(&argvars[0]);
18130 rettv->vval.v_string = vim_strsave(p);
18131 simplify_filename(rettv->vval.v_string); /* simplify in place */
18132 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018133}
18134
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018135#ifdef FEAT_FLOAT
18136/*
18137 * "sin()" function
18138 */
18139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018140f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018141{
18142 float_T f;
18143
18144 rettv->v_type = VAR_FLOAT;
18145 if (get_float_arg(argvars, &f) == OK)
18146 rettv->vval.v_float = sin(f);
18147 else
18148 rettv->vval.v_float = 0.0;
18149}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018150
18151/*
18152 * "sinh()" function
18153 */
18154 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018155f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018156{
18157 float_T f;
18158
18159 rettv->v_type = VAR_FLOAT;
18160 if (get_float_arg(argvars, &f) == OK)
18161 rettv->vval.v_float = sinh(f);
18162 else
18163 rettv->vval.v_float = 0.0;
18164}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018165#endif
18166
Bram Moolenaar0d660222005-01-07 21:51:51 +000018167static int
18168#ifdef __BORLANDC__
18169 _RTLENTRYF
18170#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018171 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018172static int
18173#ifdef __BORLANDC__
18174 _RTLENTRYF
18175#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018176 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018177
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018178/* struct used in the array that's given to qsort() */
18179typedef struct
18180{
18181 listitem_T *item;
18182 int idx;
18183} sortItem_T;
18184
Bram Moolenaar0d660222005-01-07 21:51:51 +000018185static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018186static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018187static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018188#ifdef FEAT_FLOAT
18189static int item_compare_float;
18190#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018191static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018192static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018193static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018194static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018195static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018196#define ITEM_COMPARE_FAIL 999
18197
Bram Moolenaar071d4272004-06-13 20:20:40 +000018198/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018199 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018201 static int
18202#ifdef __BORLANDC__
18203_RTLENTRYF
18204#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018205item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018206{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018207 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018208 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018209 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018210 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018211 int res;
18212 char_u numbuf1[NUMBUFLEN];
18213 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018214
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018215 si1 = (sortItem_T *)s1;
18216 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018217 tv1 = &si1->item->li_tv;
18218 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018219
18220 if (item_compare_numbers)
18221 {
18222 long v1 = get_tv_number(tv1);
18223 long v2 = get_tv_number(tv2);
18224
18225 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18226 }
18227
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018228#ifdef FEAT_FLOAT
18229 if (item_compare_float)
18230 {
18231 float_T v1 = get_tv_float(tv1);
18232 float_T v2 = get_tv_float(tv2);
18233
18234 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18235 }
18236#endif
18237
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018238 /* tv2string() puts quotes around a string and allocates memory. Don't do
18239 * that for string variables. Use a single quote when comparing with a
18240 * non-string to do what the docs promise. */
18241 if (tv1->v_type == VAR_STRING)
18242 {
18243 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18244 p1 = (char_u *)"'";
18245 else
18246 p1 = tv1->vval.v_string;
18247 }
18248 else
18249 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18250 if (tv2->v_type == VAR_STRING)
18251 {
18252 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18253 p2 = (char_u *)"'";
18254 else
18255 p2 = tv2->vval.v_string;
18256 }
18257 else
18258 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018259 if (p1 == NULL)
18260 p1 = (char_u *)"";
18261 if (p2 == NULL)
18262 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018263 if (!item_compare_numeric)
18264 {
18265 if (item_compare_ic)
18266 res = STRICMP(p1, p2);
18267 else
18268 res = STRCMP(p1, p2);
18269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018270 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018271 {
18272 double n1, n2;
18273 n1 = strtod((char *)p1, (char **)&p1);
18274 n2 = strtod((char *)p2, (char **)&p2);
18275 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18276 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018277
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018278 /* When the result would be zero, compare the item indexes. Makes the
18279 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018280 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018281 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018282
Bram Moolenaar0d660222005-01-07 21:51:51 +000018283 vim_free(tofree1);
18284 vim_free(tofree2);
18285 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018286}
18287
18288 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018289#ifdef __BORLANDC__
18290_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018291#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018292item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018293{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018294 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018295 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018296 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018297 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018298 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018299
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018300 /* shortcut after failure in previous call; compare all items equal */
18301 if (item_compare_func_err)
18302 return 0;
18303
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018304 si1 = (sortItem_T *)s1;
18305 si2 = (sortItem_T *)s2;
18306
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018307 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018308 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018309 copy_tv(&si1->item->li_tv, &argv[0]);
18310 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018311
18312 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018313 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018314 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18315 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018316 clear_tv(&argv[0]);
18317 clear_tv(&argv[1]);
18318
18319 if (res == FAIL)
18320 res = ITEM_COMPARE_FAIL;
18321 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018322 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18323 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018324 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018325 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018326
18327 /* When the result would be zero, compare the pointers themselves. Makes
18328 * the sort stable. */
18329 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018330 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018331
Bram Moolenaar0d660222005-01-07 21:51:51 +000018332 return res;
18333}
18334
18335/*
18336 * "sort({list})" function
18337 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018339do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018340{
Bram Moolenaar33570922005-01-25 22:26:29 +000018341 list_T *l;
18342 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018343 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018344 long len;
18345 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018346
Bram Moolenaar0d660222005-01-07 21:51:51 +000018347 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018348 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018349 else
18350 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018351 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018352 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018353 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18354 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018355 return;
18356 rettv->vval.v_list = l;
18357 rettv->v_type = VAR_LIST;
18358 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018359
Bram Moolenaar0d660222005-01-07 21:51:51 +000018360 len = list_len(l);
18361 if (len <= 1)
18362 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018363
Bram Moolenaar0d660222005-01-07 21:51:51 +000018364 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018365 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018366 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018367#ifdef FEAT_FLOAT
18368 item_compare_float = FALSE;
18369#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018370 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018371 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018372 if (argvars[1].v_type != VAR_UNKNOWN)
18373 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018374 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018375 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018376 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018377 else
18378 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018379 int error = FALSE;
18380
18381 i = get_tv_number_chk(&argvars[1], &error);
18382 if (error)
18383 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018384 if (i == 1)
18385 item_compare_ic = TRUE;
18386 else
18387 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018388 if (item_compare_func != NULL)
18389 {
18390 if (STRCMP(item_compare_func, "n") == 0)
18391 {
18392 item_compare_func = NULL;
18393 item_compare_numeric = TRUE;
18394 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018395 else if (STRCMP(item_compare_func, "N") == 0)
18396 {
18397 item_compare_func = NULL;
18398 item_compare_numbers = TRUE;
18399 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018400#ifdef FEAT_FLOAT
18401 else if (STRCMP(item_compare_func, "f") == 0)
18402 {
18403 item_compare_func = NULL;
18404 item_compare_float = TRUE;
18405 }
18406#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020018407 else if (STRCMP(item_compare_func, "i") == 0)
18408 {
18409 item_compare_func = NULL;
18410 item_compare_ic = TRUE;
18411 }
18412 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018413 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018414
18415 if (argvars[2].v_type != VAR_UNKNOWN)
18416 {
18417 /* optional third argument: {dict} */
18418 if (argvars[2].v_type != VAR_DICT)
18419 {
18420 EMSG(_(e_dictreq));
18421 return;
18422 }
18423 item_compare_selfdict = argvars[2].vval.v_dict;
18424 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018426
Bram Moolenaar0d660222005-01-07 21:51:51 +000018427 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018428 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018429 if (ptrs == NULL)
18430 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018431
Bram Moolenaar327aa022014-03-25 18:24:23 +010018432 i = 0;
18433 if (sort)
18434 {
18435 /* sort(): ptrs will be the list to sort */
18436 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018437 {
18438 ptrs[i].item = li;
18439 ptrs[i].idx = i;
18440 ++i;
18441 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018442
18443 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018444 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018445 /* test the compare function */
18446 if (item_compare_func != NULL
18447 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018448 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018449 EMSG(_("E702: Sort compare function failed"));
18450 else
18451 {
18452 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018453 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018454 item_compare_func == NULL ? item_compare : item_compare2);
18455
18456 if (!item_compare_func_err)
18457 {
18458 /* Clear the List and append the items in sorted order. */
18459 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18460 l->lv_len = 0;
18461 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018462 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018463 }
18464 }
18465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018466 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018467 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018468 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018469
18470 /* f_uniq(): ptrs will be a stack of items to remove */
18471 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018472 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018473 item_compare_func_ptr = item_compare_func
18474 ? item_compare2 : item_compare;
18475
18476 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18477 li = li->li_next)
18478 {
18479 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18480 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018481 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018482 if (item_compare_func_err)
18483 {
18484 EMSG(_("E882: Uniq compare function failed"));
18485 break;
18486 }
18487 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018488
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018489 if (!item_compare_func_err)
18490 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018491 while (--i >= 0)
18492 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018493 li = ptrs[i].item->li_next;
18494 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018495 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018496 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018497 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018498 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018499 list_fix_watch(l, li);
18500 listitem_free(li);
18501 l->lv_len--;
18502 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018503 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018504 }
18505
18506 vim_free(ptrs);
18507 }
18508}
18509
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018510/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018511 * "sort({list})" function
18512 */
18513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018514f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018515{
18516 do_sort_uniq(argvars, rettv, TRUE);
18517}
18518
18519/*
18520 * "uniq({list})" function
18521 */
18522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018523f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018524{
18525 do_sort_uniq(argvars, rettv, FALSE);
18526}
18527
18528/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018529 * "soundfold({word})" function
18530 */
18531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018532f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018533{
18534 char_u *s;
18535
18536 rettv->v_type = VAR_STRING;
18537 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018538#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018539 rettv->vval.v_string = eval_soundfold(s);
18540#else
18541 rettv->vval.v_string = vim_strsave(s);
18542#endif
18543}
18544
18545/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018546 * "spellbadword()" function
18547 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018549f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018550{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018551 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018552 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018553 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018554
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018555 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018556 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018557
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018558#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018559 if (argvars[0].v_type == VAR_UNKNOWN)
18560 {
18561 /* Find the start and length of the badly spelled word. */
18562 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18563 if (len != 0)
18564 word = ml_get_cursor();
18565 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018566 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018567 {
18568 char_u *str = get_tv_string_chk(&argvars[0]);
18569 int capcol = -1;
18570
18571 if (str != NULL)
18572 {
18573 /* Check the argument for spelling. */
18574 while (*str != NUL)
18575 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018576 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018577 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018578 {
18579 word = str;
18580 break;
18581 }
18582 str += len;
18583 }
18584 }
18585 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018586#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018587
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018588 list_append_string(rettv->vval.v_list, word, len);
18589 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018590 attr == HLF_SPB ? "bad" :
18591 attr == HLF_SPR ? "rare" :
18592 attr == HLF_SPL ? "local" :
18593 attr == HLF_SPC ? "caps" :
18594 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018595}
18596
18597/*
18598 * "spellsuggest()" function
18599 */
18600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018601f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018602{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018603#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018604 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018605 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018606 int maxcount;
18607 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018608 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018609 listitem_T *li;
18610 int need_capital = FALSE;
18611#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018612
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018613 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018614 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018615
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018616#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018617 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018618 {
18619 str = get_tv_string(&argvars[0]);
18620 if (argvars[1].v_type != VAR_UNKNOWN)
18621 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018622 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018623 if (maxcount <= 0)
18624 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018625 if (argvars[2].v_type != VAR_UNKNOWN)
18626 {
18627 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18628 if (typeerr)
18629 return;
18630 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018631 }
18632 else
18633 maxcount = 25;
18634
Bram Moolenaar4770d092006-01-12 23:22:24 +000018635 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018636
18637 for (i = 0; i < ga.ga_len; ++i)
18638 {
18639 str = ((char_u **)ga.ga_data)[i];
18640
18641 li = listitem_alloc();
18642 if (li == NULL)
18643 vim_free(str);
18644 else
18645 {
18646 li->li_tv.v_type = VAR_STRING;
18647 li->li_tv.v_lock = 0;
18648 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018649 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018650 }
18651 }
18652 ga_clear(&ga);
18653 }
18654#endif
18655}
18656
Bram Moolenaar0d660222005-01-07 21:51:51 +000018657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018658f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018659{
18660 char_u *str;
18661 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018662 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018663 regmatch_T regmatch;
18664 char_u patbuf[NUMBUFLEN];
18665 char_u *save_cpo;
18666 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018667 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018668 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018669 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018670
18671 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18672 save_cpo = p_cpo;
18673 p_cpo = (char_u *)"";
18674
18675 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018676 if (argvars[1].v_type != VAR_UNKNOWN)
18677 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018678 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18679 if (pat == NULL)
18680 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018681 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018682 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018683 }
18684 if (pat == NULL || *pat == NUL)
18685 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018686
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018687 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018689 if (typeerr)
18690 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018691
Bram Moolenaar0d660222005-01-07 21:51:51 +000018692 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18693 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018694 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018695 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018696 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018697 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018698 if (*str == NUL)
18699 match = FALSE; /* empty item at the end */
18700 else
18701 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018702 if (match)
18703 end = regmatch.startp[0];
18704 else
18705 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018706 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18707 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018708 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018709 if (list_append_string(rettv->vval.v_list, str,
18710 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018711 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018712 }
18713 if (!match)
18714 break;
18715 /* Advance to just after the match. */
18716 if (regmatch.endp[0] > str)
18717 col = 0;
18718 else
18719 {
18720 /* Don't get stuck at the same match. */
18721#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018722 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018723#else
18724 col = 1;
18725#endif
18726 }
18727 str = regmatch.endp[0];
18728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018729
Bram Moolenaar473de612013-06-08 18:19:48 +020018730 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018732
Bram Moolenaar0d660222005-01-07 21:51:51 +000018733 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734}
18735
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018736#ifdef FEAT_FLOAT
18737/*
18738 * "sqrt()" function
18739 */
18740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018741f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018742{
18743 float_T f;
18744
18745 rettv->v_type = VAR_FLOAT;
18746 if (get_float_arg(argvars, &f) == OK)
18747 rettv->vval.v_float = sqrt(f);
18748 else
18749 rettv->vval.v_float = 0.0;
18750}
18751
18752/*
18753 * "str2float()" function
18754 */
18755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018756f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018757{
18758 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18759
18760 if (*p == '+')
18761 p = skipwhite(p + 1);
18762 (void)string2float(p, &rettv->vval.v_float);
18763 rettv->v_type = VAR_FLOAT;
18764}
18765#endif
18766
Bram Moolenaar2c932302006-03-18 21:42:09 +000018767/*
18768 * "str2nr()" function
18769 */
18770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018771f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018772{
18773 int base = 10;
18774 char_u *p;
18775 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018776 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018777
18778 if (argvars[1].v_type != VAR_UNKNOWN)
18779 {
18780 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018781 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018782 {
18783 EMSG(_(e_invarg));
18784 return;
18785 }
18786 }
18787
18788 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018789 if (*p == '+')
18790 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018791 switch (base)
18792 {
18793 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18794 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18795 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18796 default: what = 0;
18797 }
18798 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018799 rettv->vval.v_number = n;
18800}
18801
Bram Moolenaar071d4272004-06-13 20:20:40 +000018802#ifdef HAVE_STRFTIME
18803/*
18804 * "strftime({format}[, {time}])" function
18805 */
18806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018807f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018808{
18809 char_u result_buf[256];
18810 struct tm *curtime;
18811 time_t seconds;
18812 char_u *p;
18813
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018814 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018815
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018816 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018817 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018818 seconds = time(NULL);
18819 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018820 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018821 curtime = localtime(&seconds);
18822 /* MSVC returns NULL for an invalid value of seconds. */
18823 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018824 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018825 else
18826 {
18827# ifdef FEAT_MBYTE
18828 vimconv_T conv;
18829 char_u *enc;
18830
18831 conv.vc_type = CONV_NONE;
18832 enc = enc_locale();
18833 convert_setup(&conv, p_enc, enc);
18834 if (conv.vc_type != CONV_NONE)
18835 p = string_convert(&conv, p, NULL);
18836# endif
18837 if (p != NULL)
18838 (void)strftime((char *)result_buf, sizeof(result_buf),
18839 (char *)p, curtime);
18840 else
18841 result_buf[0] = NUL;
18842
18843# ifdef FEAT_MBYTE
18844 if (conv.vc_type != CONV_NONE)
18845 vim_free(p);
18846 convert_setup(&conv, enc, p_enc);
18847 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018848 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018849 else
18850# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018851 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018852
18853# ifdef FEAT_MBYTE
18854 /* Release conversion descriptors */
18855 convert_setup(&conv, NULL, NULL);
18856 vim_free(enc);
18857# endif
18858 }
18859}
18860#endif
18861
18862/*
18863 * "stridx()" function
18864 */
18865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018866f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018867{
18868 char_u buf[NUMBUFLEN];
18869 char_u *needle;
18870 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018871 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018872 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018873 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018874
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018875 needle = get_tv_string_chk(&argvars[1]);
18876 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018877 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018878 if (needle == NULL || haystack == NULL)
18879 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018880
Bram Moolenaar33570922005-01-25 22:26:29 +000018881 if (argvars[2].v_type != VAR_UNKNOWN)
18882 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018883 int error = FALSE;
18884
18885 start_idx = get_tv_number_chk(&argvars[2], &error);
18886 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018887 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018888 if (start_idx >= 0)
18889 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018890 }
18891
18892 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18893 if (pos != NULL)
18894 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018895}
18896
18897/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018898 * "string()" function
18899 */
18900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018901f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018902{
18903 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018904 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018905
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018906 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018907 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018908 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018909 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018910 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911}
18912
18913/*
18914 * "strlen()" function
18915 */
18916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018917f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018918{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018919 rettv->vval.v_number = (varnumber_T)(STRLEN(
18920 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018921}
18922
18923/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018924 * "strchars()" function
18925 */
18926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018927f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018928{
18929 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018930 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018931#ifdef FEAT_MBYTE
18932 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018933 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018934#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018935
18936 if (argvars[1].v_type != VAR_UNKNOWN)
18937 skipcc = get_tv_number_chk(&argvars[1], NULL);
18938 if (skipcc < 0 || skipcc > 1)
18939 EMSG(_(e_invarg));
18940 else
18941 {
18942#ifdef FEAT_MBYTE
18943 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18944 while (*s != NUL)
18945 {
18946 func_mb_ptr2char_adv(&s);
18947 ++len;
18948 }
18949 rettv->vval.v_number = len;
18950#else
18951 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18952#endif
18953 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018954}
18955
18956/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018957 * "strdisplaywidth()" function
18958 */
18959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018960f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020018961{
18962 char_u *s = get_tv_string(&argvars[0]);
18963 int col = 0;
18964
18965 if (argvars[1].v_type != VAR_UNKNOWN)
18966 col = get_tv_number(&argvars[1]);
18967
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018968 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018969}
18970
18971/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018972 * "strwidth()" function
18973 */
18974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018975f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018976{
18977 char_u *s = get_tv_string(&argvars[0]);
18978
18979 rettv->vval.v_number = (varnumber_T)(
18980#ifdef FEAT_MBYTE
18981 mb_string2cells(s, -1)
18982#else
18983 STRLEN(s)
18984#endif
18985 );
18986}
18987
18988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989 * "strpart()" function
18990 */
18991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018992f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993{
18994 char_u *p;
18995 int n;
18996 int len;
18997 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018998 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018999
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019000 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001 slen = (int)STRLEN(p);
19002
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019003 n = get_tv_number_chk(&argvars[1], &error);
19004 if (error)
19005 len = 0;
19006 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019007 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019008 else
19009 len = slen - n; /* default len: all bytes that are available. */
19010
19011 /*
19012 * Only return the overlap between the specified part and the actual
19013 * string.
19014 */
19015 if (n < 0)
19016 {
19017 len += n;
19018 n = 0;
19019 }
19020 else if (n > slen)
19021 n = slen;
19022 if (len < 0)
19023 len = 0;
19024 else if (n + len > slen)
19025 len = slen - n;
19026
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019027 rettv->v_type = VAR_STRING;
19028 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019029}
19030
19031/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019032 * "strridx()" function
19033 */
19034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019035f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019036{
19037 char_u buf[NUMBUFLEN];
19038 char_u *needle;
19039 char_u *haystack;
19040 char_u *rest;
19041 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019042 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019043
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019044 needle = get_tv_string_chk(&argvars[1]);
19045 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019046
19047 rettv->vval.v_number = -1;
19048 if (needle == NULL || haystack == NULL)
19049 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019050
19051 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019052 if (argvars[2].v_type != VAR_UNKNOWN)
19053 {
19054 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019055 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019056 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019057 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019058 }
19059 else
19060 end_idx = haystack_len;
19061
Bram Moolenaar0d660222005-01-07 21:51:51 +000019062 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019063 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019064 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019065 lastmatch = haystack + end_idx;
19066 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019067 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019068 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019069 for (rest = haystack; *rest != '\0'; ++rest)
19070 {
19071 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019072 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019073 break;
19074 lastmatch = rest;
19075 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019076 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019077
19078 if (lastmatch == NULL)
19079 rettv->vval.v_number = -1;
19080 else
19081 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19082}
19083
19084/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085 * "strtrans()" function
19086 */
19087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019088f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019089{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019090 rettv->v_type = VAR_STRING;
19091 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019092}
19093
19094/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019095 * "submatch()" function
19096 */
19097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019098f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019099{
Bram Moolenaar41571762014-04-02 19:00:58 +020019100 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019101 int no;
19102 int retList = 0;
19103
19104 no = (int)get_tv_number_chk(&argvars[0], &error);
19105 if (error)
19106 return;
19107 error = FALSE;
19108 if (argvars[1].v_type != VAR_UNKNOWN)
19109 retList = get_tv_number_chk(&argvars[1], &error);
19110 if (error)
19111 return;
19112
19113 if (retList == 0)
19114 {
19115 rettv->v_type = VAR_STRING;
19116 rettv->vval.v_string = reg_submatch(no);
19117 }
19118 else
19119 {
19120 rettv->v_type = VAR_LIST;
19121 rettv->vval.v_list = reg_submatch_list(no);
19122 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019123}
19124
19125/*
19126 * "substitute()" function
19127 */
19128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019129f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019130{
19131 char_u patbuf[NUMBUFLEN];
19132 char_u subbuf[NUMBUFLEN];
19133 char_u flagsbuf[NUMBUFLEN];
19134
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019135 char_u *str = get_tv_string_chk(&argvars[0]);
19136 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19137 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19138 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19139
Bram Moolenaar0d660222005-01-07 21:51:51 +000019140 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019141 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19142 rettv->vval.v_string = NULL;
19143 else
19144 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019145}
19146
19147/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019148 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019151f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019152{
19153 int id = 0;
19154#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019155 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019156 long col;
19157 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019158 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019159
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019160 lnum = get_tv_lnum(argvars); /* -1 on type error */
19161 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19162 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019163
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019164 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019165 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019166 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019167#endif
19168
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019169 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019170}
19171
19172/*
19173 * "synIDattr(id, what [, mode])" function
19174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019176f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019177{
19178 char_u *p = NULL;
19179#ifdef FEAT_SYN_HL
19180 int id;
19181 char_u *what;
19182 char_u *mode;
19183 char_u modebuf[NUMBUFLEN];
19184 int modec;
19185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019186 id = get_tv_number(&argvars[0]);
19187 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019188 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019189 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019190 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019191 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019192 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019193 modec = 0; /* replace invalid with current */
19194 }
19195 else
19196 {
19197#ifdef FEAT_GUI
19198 if (gui.in_use)
19199 modec = 'g';
19200 else
19201#endif
19202 if (t_colors > 1)
19203 modec = 'c';
19204 else
19205 modec = 't';
19206 }
19207
19208
19209 switch (TOLOWER_ASC(what[0]))
19210 {
19211 case 'b':
19212 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19213 p = highlight_color(id, what, modec);
19214 else /* bold */
19215 p = highlight_has_attr(id, HL_BOLD, modec);
19216 break;
19217
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019218 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019219 p = highlight_color(id, what, modec);
19220 break;
19221
19222 case 'i':
19223 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19224 p = highlight_has_attr(id, HL_INVERSE, modec);
19225 else /* italic */
19226 p = highlight_has_attr(id, HL_ITALIC, modec);
19227 break;
19228
19229 case 'n': /* name */
19230 p = get_highlight_name(NULL, id - 1);
19231 break;
19232
19233 case 'r': /* reverse */
19234 p = highlight_has_attr(id, HL_INVERSE, modec);
19235 break;
19236
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019237 case 's':
19238 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19239 p = highlight_color(id, what, modec);
19240 else /* standout */
19241 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019242 break;
19243
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019244 case 'u':
19245 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19246 /* underline */
19247 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19248 else
19249 /* undercurl */
19250 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019251 break;
19252 }
19253
19254 if (p != NULL)
19255 p = vim_strsave(p);
19256#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019257 rettv->v_type = VAR_STRING;
19258 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019259}
19260
19261/*
19262 * "synIDtrans(id)" function
19263 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019265f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019266{
19267 int id;
19268
19269#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019270 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019271
19272 if (id > 0)
19273 id = syn_get_final_id(id);
19274 else
19275#endif
19276 id = 0;
19277
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019278 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019279}
19280
19281/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019282 * "synconcealed(lnum, col)" function
19283 */
19284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019285f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019286{
19287#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19288 long lnum;
19289 long col;
19290 int syntax_flags = 0;
19291 int cchar;
19292 int matchid = 0;
19293 char_u str[NUMBUFLEN];
19294#endif
19295
19296 rettv->v_type = VAR_LIST;
19297 rettv->vval.v_list = NULL;
19298
19299#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19300 lnum = get_tv_lnum(argvars); /* -1 on type error */
19301 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19302
19303 vim_memset(str, NUL, sizeof(str));
19304
19305 if (rettv_list_alloc(rettv) != FAIL)
19306 {
19307 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19308 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19309 && curwin->w_p_cole > 0)
19310 {
19311 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19312 syntax_flags = get_syntax_info(&matchid);
19313
19314 /* get the conceal character */
19315 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19316 {
19317 cchar = syn_get_sub_char();
19318 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19319 cchar = lcs_conceal;
19320 if (cchar != NUL)
19321 {
19322# ifdef FEAT_MBYTE
19323 if (has_mbyte)
19324 (*mb_char2bytes)(cchar, str);
19325 else
19326# endif
19327 str[0] = cchar;
19328 }
19329 }
19330 }
19331
19332 list_append_number(rettv->vval.v_list,
19333 (syntax_flags & HL_CONCEAL) != 0);
19334 /* -1 to auto-determine strlen */
19335 list_append_string(rettv->vval.v_list, str, -1);
19336 list_append_number(rettv->vval.v_list, matchid);
19337 }
19338#endif
19339}
19340
19341/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019342 * "synstack(lnum, col)" function
19343 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019345f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019346{
19347#ifdef FEAT_SYN_HL
19348 long lnum;
19349 long col;
19350 int i;
19351 int id;
19352#endif
19353
19354 rettv->v_type = VAR_LIST;
19355 rettv->vval.v_list = NULL;
19356
19357#ifdef FEAT_SYN_HL
19358 lnum = get_tv_lnum(argvars); /* -1 on type error */
19359 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19360
19361 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019362 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019363 && rettv_list_alloc(rettv) != FAIL)
19364 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019365 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019366 for (i = 0; ; ++i)
19367 {
19368 id = syn_get_stack_item(i);
19369 if (id < 0)
19370 break;
19371 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19372 break;
19373 }
19374 }
19375#endif
19376}
19377
Bram Moolenaar071d4272004-06-13 20:20:40 +000019378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019379get_cmd_output_as_rettv(
19380 typval_T *argvars,
19381 typval_T *rettv,
19382 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019383{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019384 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019385 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019386 char_u *infile = NULL;
19387 char_u buf[NUMBUFLEN];
19388 int err = FALSE;
19389 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019390 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019391 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019392
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019393 rettv->v_type = VAR_STRING;
19394 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019395 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019396 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019397
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019398 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019399 {
19400 /*
19401 * Write the string to a temp file, to be used for input of the shell
19402 * command.
19403 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019404 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019405 {
19406 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019407 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019408 }
19409
19410 fd = mch_fopen((char *)infile, WRITEBIN);
19411 if (fd == NULL)
19412 {
19413 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019414 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019415 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019416 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019417 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019418 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19419 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019420 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019421 else
19422 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019423 size_t len;
19424
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019425 p = get_tv_string_buf_chk(&argvars[1], buf);
19426 if (p == NULL)
19427 {
19428 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019429 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019430 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019431 len = STRLEN(p);
19432 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019433 err = TRUE;
19434 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019435 if (fclose(fd) != 0)
19436 err = TRUE;
19437 if (err)
19438 {
19439 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019440 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019441 }
19442 }
19443
Bram Moolenaar52a72462014-08-29 15:53:52 +020019444 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19445 * echoes typeahead, that messes up the display. */
19446 if (!msg_silent)
19447 flags += SHELL_COOKED;
19448
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019449 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019450 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019451 int len;
19452 listitem_T *li;
19453 char_u *s = NULL;
19454 char_u *start;
19455 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019456 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457
Bram Moolenaar52a72462014-08-29 15:53:52 +020019458 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019459 if (res == NULL)
19460 goto errret;
19461
19462 list = list_alloc();
19463 if (list == NULL)
19464 goto errret;
19465
19466 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019467 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019468 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019469 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019470 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019471 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019472
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019473 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019474 if (s == NULL)
19475 goto errret;
19476
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019477 for (p = s; start < end; ++p, ++start)
19478 *p = *start == NUL ? NL : *start;
19479 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019480
19481 li = listitem_alloc();
19482 if (li == NULL)
19483 {
19484 vim_free(s);
19485 goto errret;
19486 }
19487 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019488 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019489 li->li_tv.vval.v_string = s;
19490 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019491 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019492
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019493 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019494 rettv->v_type = VAR_LIST;
19495 rettv->vval.v_list = list;
19496 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019497 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019498 else
19499 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019500 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019501#ifdef USE_CR
19502 /* translate <CR> into <NL> */
19503 if (res != NULL)
19504 {
19505 char_u *s;
19506
19507 for (s = res; *s; ++s)
19508 {
19509 if (*s == CAR)
19510 *s = NL;
19511 }
19512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019513#else
19514# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019515 /* translate <CR><NL> into <NL> */
19516 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019517 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019518 char_u *s, *d;
19519
19520 d = res;
19521 for (s = res; *s; ++s)
19522 {
19523 if (s[0] == CAR && s[1] == NL)
19524 ++s;
19525 *d++ = *s;
19526 }
19527 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529# endif
19530#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019531 rettv->vval.v_string = res;
19532 res = NULL;
19533 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019534
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019535errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019536 if (infile != NULL)
19537 {
19538 mch_remove(infile);
19539 vim_free(infile);
19540 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019541 if (res != NULL)
19542 vim_free(res);
19543 if (list != NULL)
19544 list_free(list, TRUE);
19545}
19546
19547/*
19548 * "system()" function
19549 */
19550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019551f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019552{
19553 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19554}
19555
19556/*
19557 * "systemlist()" function
19558 */
19559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019560f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019561{
19562 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563}
19564
19565/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019566 * "tabpagebuflist()" function
19567 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019569f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019570{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019571#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019572 tabpage_T *tp;
19573 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019574
19575 if (argvars[0].v_type == VAR_UNKNOWN)
19576 wp = firstwin;
19577 else
19578 {
19579 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19580 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019581 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019582 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019583 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019584 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019585 for (; wp != NULL; wp = wp->w_next)
19586 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019587 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019588 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019589 }
19590#endif
19591}
19592
19593
19594/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019595 * "tabpagenr()" function
19596 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019598f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019599{
19600 int nr = 1;
19601#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019602 char_u *arg;
19603
19604 if (argvars[0].v_type != VAR_UNKNOWN)
19605 {
19606 arg = get_tv_string_chk(&argvars[0]);
19607 nr = 0;
19608 if (arg != NULL)
19609 {
19610 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019611 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019612 else
19613 EMSG2(_(e_invexpr2), arg);
19614 }
19615 }
19616 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019617 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019618#endif
19619 rettv->vval.v_number = nr;
19620}
19621
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019622
19623#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019624static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019625
19626/*
19627 * Common code for tabpagewinnr() and winnr().
19628 */
19629 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019630get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019631{
19632 win_T *twin;
19633 int nr = 1;
19634 win_T *wp;
19635 char_u *arg;
19636
19637 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19638 if (argvar->v_type != VAR_UNKNOWN)
19639 {
19640 arg = get_tv_string_chk(argvar);
19641 if (arg == NULL)
19642 nr = 0; /* type error; errmsg already given */
19643 else if (STRCMP(arg, "$") == 0)
19644 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19645 else if (STRCMP(arg, "#") == 0)
19646 {
19647 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19648 if (twin == NULL)
19649 nr = 0;
19650 }
19651 else
19652 {
19653 EMSG2(_(e_invexpr2), arg);
19654 nr = 0;
19655 }
19656 }
19657
19658 if (nr > 0)
19659 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19660 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019661 {
19662 if (wp == NULL)
19663 {
19664 /* didn't find it in this tabpage */
19665 nr = 0;
19666 break;
19667 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019668 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019669 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019670 return nr;
19671}
19672#endif
19673
19674/*
19675 * "tabpagewinnr()" function
19676 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019678f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019679{
19680 int nr = 1;
19681#ifdef FEAT_WINDOWS
19682 tabpage_T *tp;
19683
19684 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19685 if (tp == NULL)
19686 nr = 0;
19687 else
19688 nr = get_winnr(tp, &argvars[1]);
19689#endif
19690 rettv->vval.v_number = nr;
19691}
19692
19693
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019694/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019695 * "tagfiles()" function
19696 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019698f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019699{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019700 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019701 tagname_T tn;
19702 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019703
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019704 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019705 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019706 fname = alloc(MAXPATHL);
19707 if (fname == NULL)
19708 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019709
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019710 for (first = TRUE; ; first = FALSE)
19711 if (get_tagfname(&tn, first, fname) == FAIL
19712 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019713 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019714 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019715 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019716}
19717
19718/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019719 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019720 */
19721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019722f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019723{
19724 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019725
19726 tag_pattern = get_tv_string(&argvars[0]);
19727
19728 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019729 if (*tag_pattern == NUL)
19730 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019731
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019732 if (rettv_list_alloc(rettv) == OK)
19733 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019734}
19735
19736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019737 * "tempname()" function
19738 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019740f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019741{
19742 static int x = 'A';
19743
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019744 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019745 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019746
19747 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19748 * names. Skip 'I' and 'O', they are used for shell redirection. */
19749 do
19750 {
19751 if (x == 'Z')
19752 x = '0';
19753 else if (x == '9')
19754 x = 'A';
19755 else
19756 {
19757#ifdef EBCDIC
19758 if (x == 'I')
19759 x = 'J';
19760 else if (x == 'R')
19761 x = 'S';
19762 else
19763#endif
19764 ++x;
19765 }
19766 } while (x == 'I' || x == 'O');
19767}
19768
19769/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019770 * "test(list)" function: Just checking the walls...
19771 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019773f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019774{
19775 /* Used for unit testing. Change the code below to your liking. */
19776#if 0
19777 listitem_T *li;
19778 list_T *l;
19779 char_u *bad, *good;
19780
19781 if (argvars[0].v_type != VAR_LIST)
19782 return;
19783 l = argvars[0].vval.v_list;
19784 if (l == NULL)
19785 return;
19786 li = l->lv_first;
19787 if (li == NULL)
19788 return;
19789 bad = get_tv_string(&li->li_tv);
19790 li = li->li_next;
19791 if (li == NULL)
19792 return;
19793 good = get_tv_string(&li->li_tv);
19794 rettv->vval.v_number = test_edit_score(bad, good);
19795#endif
19796}
19797
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019798#ifdef FEAT_FLOAT
19799/*
19800 * "tan()" function
19801 */
19802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019803f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019804{
19805 float_T f;
19806
19807 rettv->v_type = VAR_FLOAT;
19808 if (get_float_arg(argvars, &f) == OK)
19809 rettv->vval.v_float = tan(f);
19810 else
19811 rettv->vval.v_float = 0.0;
19812}
19813
19814/*
19815 * "tanh()" function
19816 */
19817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019818f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019819{
19820 float_T f;
19821
19822 rettv->v_type = VAR_FLOAT;
19823 if (get_float_arg(argvars, &f) == OK)
19824 rettv->vval.v_float = tanh(f);
19825 else
19826 rettv->vval.v_float = 0.0;
19827}
19828#endif
19829
Bram Moolenaard52d9742005-08-21 22:20:28 +000019830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019831 * "tolower(string)" function
19832 */
19833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019834f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835{
19836 char_u *p;
19837
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019838 p = vim_strsave(get_tv_string(&argvars[0]));
19839 rettv->v_type = VAR_STRING;
19840 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019841
19842 if (p != NULL)
19843 while (*p != NUL)
19844 {
19845#ifdef FEAT_MBYTE
19846 int l;
19847
19848 if (enc_utf8)
19849 {
19850 int c, lc;
19851
19852 c = utf_ptr2char(p);
19853 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019854 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019855 /* TODO: reallocate string when byte count changes. */
19856 if (utf_char2len(lc) == l)
19857 utf_char2bytes(lc, p);
19858 p += l;
19859 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019860 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019861 p += l; /* skip multi-byte character */
19862 else
19863#endif
19864 {
19865 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19866 ++p;
19867 }
19868 }
19869}
19870
19871/*
19872 * "toupper(string)" function
19873 */
19874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019875f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019876{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019877 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019878 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879}
19880
19881/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019882 * "tr(string, fromstr, tostr)" function
19883 */
19884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019885f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019886{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019887 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019888 char_u *fromstr;
19889 char_u *tostr;
19890 char_u *p;
19891#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019892 int inlen;
19893 int fromlen;
19894 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019895 int idx;
19896 char_u *cpstr;
19897 int cplen;
19898 int first = TRUE;
19899#endif
19900 char_u buf[NUMBUFLEN];
19901 char_u buf2[NUMBUFLEN];
19902 garray_T ga;
19903
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019904 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019905 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19906 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019907
19908 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019909 rettv->v_type = VAR_STRING;
19910 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019911 if (fromstr == NULL || tostr == NULL)
19912 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019913 ga_init2(&ga, (int)sizeof(char), 80);
19914
19915#ifdef FEAT_MBYTE
19916 if (!has_mbyte)
19917#endif
19918 /* not multi-byte: fromstr and tostr must be the same length */
19919 if (STRLEN(fromstr) != STRLEN(tostr))
19920 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019921#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019922error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019923#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019924 EMSG2(_(e_invarg2), fromstr);
19925 ga_clear(&ga);
19926 return;
19927 }
19928
19929 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019930 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019931 {
19932#ifdef FEAT_MBYTE
19933 if (has_mbyte)
19934 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019935 inlen = (*mb_ptr2len)(in_str);
19936 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019937 cplen = inlen;
19938 idx = 0;
19939 for (p = fromstr; *p != NUL; p += fromlen)
19940 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019941 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019942 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019943 {
19944 for (p = tostr; *p != NUL; p += tolen)
19945 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019946 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019947 if (idx-- == 0)
19948 {
19949 cplen = tolen;
19950 cpstr = p;
19951 break;
19952 }
19953 }
19954 if (*p == NUL) /* tostr is shorter than fromstr */
19955 goto error;
19956 break;
19957 }
19958 ++idx;
19959 }
19960
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019961 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019962 {
19963 /* Check that fromstr and tostr have the same number of
19964 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019965 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019966 first = FALSE;
19967 for (p = tostr; *p != NUL; p += tolen)
19968 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019969 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019970 --idx;
19971 }
19972 if (idx != 0)
19973 goto error;
19974 }
19975
Bram Moolenaarcde88542015-08-11 19:14:00 +020019976 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019977 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019978 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019979
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019980 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019981 }
19982 else
19983#endif
19984 {
19985 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019986 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019987 if (p != NULL)
19988 ga_append(&ga, tostr[p - fromstr]);
19989 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019990 ga_append(&ga, *in_str);
19991 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019992 }
19993 }
19994
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019995 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019996 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019997 ga_append(&ga, NUL);
19998
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019999 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020000}
20001
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020002#ifdef FEAT_FLOAT
20003/*
20004 * "trunc({float})" function
20005 */
20006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020007f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020008{
20009 float_T f;
20010
20011 rettv->v_type = VAR_FLOAT;
20012 if (get_float_arg(argvars, &f) == OK)
20013 /* trunc() is not in C90, use floor() or ceil() instead. */
20014 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20015 else
20016 rettv->vval.v_float = 0.0;
20017}
20018#endif
20019
Bram Moolenaar8299df92004-07-10 09:47:34 +000020020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020021 * "type(expr)" function
20022 */
20023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020024f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020025{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020026 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020027
20028 switch (argvars[0].v_type)
20029 {
20030 case VAR_NUMBER: n = 0; break;
20031 case VAR_STRING: n = 1; break;
20032 case VAR_FUNC: n = 2; break;
20033 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020034 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020035 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020036 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020037 if (argvars[0].vval.v_number == VVAL_FALSE
20038 || argvars[0].vval.v_number == VVAL_TRUE)
20039 n = 6;
20040 else
20041 n = 7;
20042 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010020043 case VAR_JOB: n = 8; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020044 case VAR_UNKNOWN:
20045 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20046 n = -1;
20047 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020048 }
20049 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020050}
20051
20052/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020053 * "undofile(name)" function
20054 */
20055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020056f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020057{
20058 rettv->v_type = VAR_STRING;
20059#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020060 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020061 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020062
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020063 if (*fname == NUL)
20064 {
20065 /* If there is no file name there will be no undo file. */
20066 rettv->vval.v_string = NULL;
20067 }
20068 else
20069 {
20070 char_u *ffname = FullName_save(fname, FALSE);
20071
20072 if (ffname != NULL)
20073 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20074 vim_free(ffname);
20075 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020076 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020077#else
20078 rettv->vval.v_string = NULL;
20079#endif
20080}
20081
20082/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020083 * "undotree()" function
20084 */
20085 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020086f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020087{
20088 if (rettv_dict_alloc(rettv) == OK)
20089 {
20090 dict_T *dict = rettv->vval.v_dict;
20091 list_T *list;
20092
Bram Moolenaar730cde92010-06-27 05:18:54 +020020093 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020094 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020095 dict_add_nr_str(dict, "save_last",
20096 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020097 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20098 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020099 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020100
20101 list = list_alloc();
20102 if (list != NULL)
20103 {
20104 u_eval_tree(curbuf->b_u_oldhead, list);
20105 dict_add_list(dict, "entries", list);
20106 }
20107 }
20108}
20109
20110/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020111 * "values(dict)" function
20112 */
20113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020114f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020115{
20116 dict_list(argvars, rettv, 1);
20117}
20118
20119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020120 * "virtcol(string)" function
20121 */
20122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020123f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124{
20125 colnr_T vcol = 0;
20126 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020127 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020128
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020129 fp = var2fpos(&argvars[0], FALSE, &fnum);
20130 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20131 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132 {
20133 getvvcol(curwin, fp, NULL, NULL, &vcol);
20134 ++vcol;
20135 }
20136
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020137 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020138}
20139
20140/*
20141 * "visualmode()" function
20142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020144f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020145{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020146 char_u str[2];
20147
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020148 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020149 str[0] = curbuf->b_visual_mode_eval;
20150 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020151 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152
20153 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020154 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020155 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020156}
20157
20158/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020159 * "wildmenumode()" function
20160 */
20161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020162f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020163{
20164#ifdef FEAT_WILDMENU
20165 if (wild_menu_showing)
20166 rettv->vval.v_number = 1;
20167#endif
20168}
20169
20170/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171 * "winbufnr(nr)" function
20172 */
20173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020174f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020175{
20176 win_T *wp;
20177
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020178 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020179 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020180 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020181 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020182 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020183}
20184
20185/*
20186 * "wincol()" function
20187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020189f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020190{
20191 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020192 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020193}
20194
20195/*
20196 * "winheight(nr)" function
20197 */
20198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020199f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200{
20201 win_T *wp;
20202
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020203 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020204 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020205 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020206 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020207 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020208}
20209
20210/*
20211 * "winline()" function
20212 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020214f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020215{
20216 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020217 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020218}
20219
20220/*
20221 * "winnr()" function
20222 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020224f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225{
20226 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020227
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020229 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020230#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020231 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020232}
20233
20234/*
20235 * "winrestcmd()" function
20236 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020238f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020239{
20240#ifdef FEAT_WINDOWS
20241 win_T *wp;
20242 int winnr = 1;
20243 garray_T ga;
20244 char_u buf[50];
20245
20246 ga_init2(&ga, (int)sizeof(char), 70);
20247 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20248 {
20249 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20250 ga_concat(&ga, buf);
20251# ifdef FEAT_VERTSPLIT
20252 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20253 ga_concat(&ga, buf);
20254# endif
20255 ++winnr;
20256 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020257 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020258
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020259 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020260#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020261 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020262#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020263 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020264}
20265
20266/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020267 * "winrestview()" function
20268 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020270f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020271{
20272 dict_T *dict;
20273
20274 if (argvars[0].v_type != VAR_DICT
20275 || (dict = argvars[0].vval.v_dict) == NULL)
20276 EMSG(_(e_invarg));
20277 else
20278 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020279 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20280 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20281 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20282 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020283#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020284 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20285 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020286#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020287 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20288 {
20289 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20290 curwin->w_set_curswant = FALSE;
20291 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020292
Bram Moolenaar82c25852014-05-28 16:47:16 +020020293 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20294 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020295#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020296 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20297 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020298#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020299 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20300 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20301 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20302 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020303
20304 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020305 win_new_height(curwin, curwin->w_height);
20306# ifdef FEAT_VERTSPLIT
20307 win_new_width(curwin, W_WIDTH(curwin));
20308# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020309 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020310
Bram Moolenaarb851a962014-10-31 15:45:52 +010020311 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020312 curwin->w_topline = 1;
20313 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20314 curwin->w_topline = curbuf->b_ml.ml_line_count;
20315#ifdef FEAT_DIFF
20316 check_topfill(curwin, TRUE);
20317#endif
20318 }
20319}
20320
20321/*
20322 * "winsaveview()" function
20323 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020325f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020326{
20327 dict_T *dict;
20328
Bram Moolenaara800b422010-06-27 01:15:55 +020020329 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020330 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020331 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020332
20333 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20334 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20335#ifdef FEAT_VIRTUALEDIT
20336 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20337#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020338 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020339 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20340
20341 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20342#ifdef FEAT_DIFF
20343 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20344#endif
20345 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20346 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20347}
20348
20349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020350 * "winwidth(nr)" function
20351 */
20352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020353f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020354{
20355 win_T *wp;
20356
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020357 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020359 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360 else
20361#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020362 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020364 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020365#endif
20366}
20367
Bram Moolenaar071d4272004-06-13 20:20:40 +000020368/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020369 * "wordcount()" function
20370 */
20371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020372f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020373{
20374 if (rettv_dict_alloc(rettv) == FAIL)
20375 return;
20376 cursor_pos_info(rettv->vval.v_dict);
20377}
20378
20379/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020380 * Write list of strings to file
20381 */
20382 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020383write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020384{
20385 listitem_T *li;
20386 int c;
20387 int ret = OK;
20388 char_u *s;
20389
20390 for (li = list->lv_first; li != NULL; li = li->li_next)
20391 {
20392 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20393 {
20394 if (*s == '\n')
20395 c = putc(NUL, fd);
20396 else
20397 c = putc(*s, fd);
20398 if (c == EOF)
20399 {
20400 ret = FAIL;
20401 break;
20402 }
20403 }
20404 if (!binary || li->li_next != NULL)
20405 if (putc('\n', fd) == EOF)
20406 {
20407 ret = FAIL;
20408 break;
20409 }
20410 if (ret == FAIL)
20411 {
20412 EMSG(_(e_write));
20413 break;
20414 }
20415 }
20416 return ret;
20417}
20418
20419/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020420 * "writefile()" function
20421 */
20422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020423f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020424{
20425 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020426 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020427 char_u *fname;
20428 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020429 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020430
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020431 if (check_restricted() || check_secure())
20432 return;
20433
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020434 if (argvars[0].v_type != VAR_LIST)
20435 {
20436 EMSG2(_(e_listarg), "writefile()");
20437 return;
20438 }
20439 if (argvars[0].vval.v_list == NULL)
20440 return;
20441
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020442 if (argvars[2].v_type != VAR_UNKNOWN)
20443 {
20444 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20445 binary = TRUE;
20446 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20447 append = TRUE;
20448 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020449
20450 /* Always open the file in binary mode, library functions have a mind of
20451 * their own about CR-LF conversion. */
20452 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020453 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20454 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020455 {
20456 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20457 ret = -1;
20458 }
20459 else
20460 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020461 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20462 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020463 fclose(fd);
20464 }
20465
20466 rettv->vval.v_number = ret;
20467}
20468
20469/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020470 * "xor(expr, expr)" function
20471 */
20472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020473f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020474{
20475 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20476 ^ get_tv_number_chk(&argvars[1], NULL);
20477}
20478
20479
20480/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020481 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020482 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020483 */
20484 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020485var2fpos(
20486 typval_T *varp,
20487 int dollar_lnum, /* TRUE when $ is last line */
20488 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020489{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020490 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020491 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020492 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020493
Bram Moolenaara5525202006-03-02 22:52:09 +000020494 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020495 if (varp->v_type == VAR_LIST)
20496 {
20497 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020498 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020499 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020500 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020501
20502 l = varp->vval.v_list;
20503 if (l == NULL)
20504 return NULL;
20505
20506 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020507 pos.lnum = list_find_nr(l, 0L, &error);
20508 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020509 return NULL; /* invalid line number */
20510
20511 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020512 pos.col = list_find_nr(l, 1L, &error);
20513 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020514 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020515 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020516
20517 /* We accept "$" for the column number: last column. */
20518 li = list_find(l, 1L);
20519 if (li != NULL && li->li_tv.v_type == VAR_STRING
20520 && li->li_tv.vval.v_string != NULL
20521 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20522 pos.col = len + 1;
20523
Bram Moolenaara5525202006-03-02 22:52:09 +000020524 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020525 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020526 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020527 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020528
Bram Moolenaara5525202006-03-02 22:52:09 +000020529#ifdef FEAT_VIRTUALEDIT
20530 /* Get the virtual offset. Defaults to zero. */
20531 pos.coladd = list_find_nr(l, 2L, &error);
20532 if (error)
20533 pos.coladd = 0;
20534#endif
20535
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020536 return &pos;
20537 }
20538
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020539 name = get_tv_string_chk(varp);
20540 if (name == NULL)
20541 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020542 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020543 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020544 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20545 {
20546 if (VIsual_active)
20547 return &VIsual;
20548 return &curwin->w_cursor;
20549 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020550 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020552 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020553 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20554 return NULL;
20555 return pp;
20556 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020557
20558#ifdef FEAT_VIRTUALEDIT
20559 pos.coladd = 0;
20560#endif
20561
Bram Moolenaar477933c2007-07-17 14:32:23 +000020562 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020563 {
20564 pos.col = 0;
20565 if (name[1] == '0') /* "w0": first visible line */
20566 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020567 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020568 pos.lnum = curwin->w_topline;
20569 return &pos;
20570 }
20571 else if (name[1] == '$') /* "w$": last visible line */
20572 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020573 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020574 pos.lnum = curwin->w_botline - 1;
20575 return &pos;
20576 }
20577 }
20578 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020579 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020580 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020581 {
20582 pos.lnum = curbuf->b_ml.ml_line_count;
20583 pos.col = 0;
20584 }
20585 else
20586 {
20587 pos.lnum = curwin->w_cursor.lnum;
20588 pos.col = (colnr_T)STRLEN(ml_get_curline());
20589 }
20590 return &pos;
20591 }
20592 return NULL;
20593}
20594
20595/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020596 * Convert list in "arg" into a position and optional file number.
20597 * When "fnump" is NULL there is no file number, only 3 items.
20598 * Note that the column is passed on as-is, the caller may want to decrement
20599 * it to use 1 for the first column.
20600 * Return FAIL when conversion is not possible, doesn't check the position for
20601 * validity.
20602 */
20603 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020604list2fpos(
20605 typval_T *arg,
20606 pos_T *posp,
20607 int *fnump,
20608 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020609{
20610 list_T *l = arg->vval.v_list;
20611 long i = 0;
20612 long n;
20613
Bram Moolenaar493c1782014-05-28 14:34:46 +020020614 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20615 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020616 if (arg->v_type != VAR_LIST
20617 || l == NULL
20618 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020619 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020620 return FAIL;
20621
20622 if (fnump != NULL)
20623 {
20624 n = list_find_nr(l, i++, NULL); /* fnum */
20625 if (n < 0)
20626 return FAIL;
20627 if (n == 0)
20628 n = curbuf->b_fnum; /* current buffer */
20629 *fnump = n;
20630 }
20631
20632 n = list_find_nr(l, i++, NULL); /* lnum */
20633 if (n < 0)
20634 return FAIL;
20635 posp->lnum = n;
20636
20637 n = list_find_nr(l, i++, NULL); /* col */
20638 if (n < 0)
20639 return FAIL;
20640 posp->col = n;
20641
20642#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020643 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020644 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020645 posp->coladd = 0;
20646 else
20647 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020648#endif
20649
Bram Moolenaar493c1782014-05-28 14:34:46 +020020650 if (curswantp != NULL)
20651 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20652
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020653 return OK;
20654}
20655
20656/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020657 * Get the length of an environment variable name.
20658 * Advance "arg" to the first character after the name.
20659 * Return 0 for error.
20660 */
20661 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020662get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020663{
20664 char_u *p;
20665 int len;
20666
20667 for (p = *arg; vim_isIDc(*p); ++p)
20668 ;
20669 if (p == *arg) /* no name found */
20670 return 0;
20671
20672 len = (int)(p - *arg);
20673 *arg = p;
20674 return len;
20675}
20676
20677/*
20678 * Get the length of the name of a function or internal variable.
20679 * "arg" is advanced to the first non-white character after the name.
20680 * Return 0 if something is wrong.
20681 */
20682 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020683get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020684{
20685 char_u *p;
20686 int len;
20687
20688 /* Find the end of the name. */
20689 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020690 {
20691 if (*p == ':')
20692 {
20693 /* "s:" is start of "s:var", but "n:" is not and can be used in
20694 * slice "[n:]". Also "xx:" is not a namespace. */
20695 len = (int)(p - *arg);
20696 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20697 || len > 1)
20698 break;
20699 }
20700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020701 if (p == *arg) /* no name found */
20702 return 0;
20703
20704 len = (int)(p - *arg);
20705 *arg = skipwhite(p);
20706
20707 return len;
20708}
20709
20710/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020711 * Get the length of the name of a variable or function.
20712 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020713 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020714 * Return -1 if curly braces expansion failed.
20715 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020716 * If the name contains 'magic' {}'s, expand them and return the
20717 * expanded name in an allocated string via 'alias' - caller must free.
20718 */
20719 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020720get_name_len(
20721 char_u **arg,
20722 char_u **alias,
20723 int evaluate,
20724 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020725{
20726 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020727 char_u *p;
20728 char_u *expr_start;
20729 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020730
20731 *alias = NULL; /* default to no alias */
20732
20733 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20734 && (*arg)[2] == (int)KE_SNR)
20735 {
20736 /* hard coded <SNR>, already translated */
20737 *arg += 3;
20738 return get_id_len(arg) + 3;
20739 }
20740 len = eval_fname_script(*arg);
20741 if (len > 0)
20742 {
20743 /* literal "<SID>", "s:" or "<SNR>" */
20744 *arg += len;
20745 }
20746
Bram Moolenaar071d4272004-06-13 20:20:40 +000020747 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020748 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020749 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020750 p = find_name_end(*arg, &expr_start, &expr_end,
20751 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020752 if (expr_start != NULL)
20753 {
20754 char_u *temp_string;
20755
20756 if (!evaluate)
20757 {
20758 len += (int)(p - *arg);
20759 *arg = skipwhite(p);
20760 return len;
20761 }
20762
20763 /*
20764 * Include any <SID> etc in the expanded string:
20765 * Thus the -len here.
20766 */
20767 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20768 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020769 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020770 *alias = temp_string;
20771 *arg = skipwhite(p);
20772 return (int)STRLEN(temp_string);
20773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020774
20775 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020776 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020777 EMSG2(_(e_invexpr2), *arg);
20778
20779 return len;
20780}
20781
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020782/*
20783 * Find the end of a variable or function name, taking care of magic braces.
20784 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20785 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020786 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020787 * Return a pointer to just after the name. Equal to "arg" if there is no
20788 * valid name.
20789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020791find_name_end(
20792 char_u *arg,
20793 char_u **expr_start,
20794 char_u **expr_end,
20795 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020796{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020797 int mb_nest = 0;
20798 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020799 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020800 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020801
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020802 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020803 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020804 *expr_start = NULL;
20805 *expr_end = NULL;
20806 }
20807
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020808 /* Quick check for valid starting character. */
20809 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20810 return arg;
20811
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020812 for (p = arg; *p != NUL
20813 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020814 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020815 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020816 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020817 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020818 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020819 if (*p == '\'')
20820 {
20821 /* skip over 'string' to avoid counting [ and ] inside it. */
20822 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20823 ;
20824 if (*p == NUL)
20825 break;
20826 }
20827 else if (*p == '"')
20828 {
20829 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20830 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20831 if (*p == '\\' && p[1] != NUL)
20832 ++p;
20833 if (*p == NUL)
20834 break;
20835 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020836 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20837 {
20838 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020839 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020840 len = (int)(p - arg);
20841 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020842 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020843 break;
20844 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020845
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020846 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020847 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020848 if (*p == '[')
20849 ++br_nest;
20850 else if (*p == ']')
20851 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020852 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020853
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020854 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020855 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020856 if (*p == '{')
20857 {
20858 mb_nest++;
20859 if (expr_start != NULL && *expr_start == NULL)
20860 *expr_start = p;
20861 }
20862 else if (*p == '}')
20863 {
20864 mb_nest--;
20865 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20866 *expr_end = p;
20867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020869 }
20870
20871 return p;
20872}
20873
20874/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020875 * Expands out the 'magic' {}'s in a variable/function name.
20876 * Note that this can call itself recursively, to deal with
20877 * constructs like foo{bar}{baz}{bam}
20878 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20879 * "in_start" ^
20880 * "expr_start" ^
20881 * "expr_end" ^
20882 * "in_end" ^
20883 *
20884 * Returns a new allocated string, which the caller must free.
20885 * Returns NULL for failure.
20886 */
20887 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020888make_expanded_name(
20889 char_u *in_start,
20890 char_u *expr_start,
20891 char_u *expr_end,
20892 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020893{
20894 char_u c1;
20895 char_u *retval = NULL;
20896 char_u *temp_result;
20897 char_u *nextcmd = NULL;
20898
20899 if (expr_end == NULL || in_end == NULL)
20900 return NULL;
20901 *expr_start = NUL;
20902 *expr_end = NUL;
20903 c1 = *in_end;
20904 *in_end = NUL;
20905
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020906 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020907 if (temp_result != NULL && nextcmd == NULL)
20908 {
20909 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20910 + (in_end - expr_end) + 1));
20911 if (retval != NULL)
20912 {
20913 STRCPY(retval, in_start);
20914 STRCAT(retval, temp_result);
20915 STRCAT(retval, expr_end + 1);
20916 }
20917 }
20918 vim_free(temp_result);
20919
20920 *in_end = c1; /* put char back for error messages */
20921 *expr_start = '{';
20922 *expr_end = '}';
20923
20924 if (retval != NULL)
20925 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020926 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020927 if (expr_start != NULL)
20928 {
20929 /* Further expansion! */
20930 temp_result = make_expanded_name(retval, expr_start,
20931 expr_end, temp_result);
20932 vim_free(retval);
20933 retval = temp_result;
20934 }
20935 }
20936
20937 return retval;
20938}
20939
20940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020941 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020942 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020943 */
20944 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020945eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020946{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020947 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20948}
20949
20950/*
20951 * Return TRUE if character "c" can be used as the first character in a
20952 * variable or function name (excluding '{' and '}').
20953 */
20954 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020955eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020956{
20957 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958}
20959
20960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020961 * Set number v: variable to "val".
20962 */
20963 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020964set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020965{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020966 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020967}
20968
20969/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020970 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020971 */
20972 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010020973get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020974{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020975 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020976}
20977
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020978/*
20979 * Get string v: variable value. Uses a static buffer, can only be used once.
20980 */
20981 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020982get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020983{
20984 return get_tv_string(&vimvars[idx].vv_tv);
20985}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020986
Bram Moolenaar071d4272004-06-13 20:20:40 +000020987/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020988 * Get List v: variable value. Caller must take care of reference count when
20989 * needed.
20990 */
20991 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020992get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000020993{
20994 return vimvars[idx].vv_list;
20995}
20996
20997/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020998 * Set v:char to character "c".
20999 */
21000 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021001set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021002{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021003 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021004
21005#ifdef FEAT_MBYTE
21006 if (has_mbyte)
21007 buf[(*mb_char2bytes)(c, buf)] = NUL;
21008 else
21009#endif
21010 {
21011 buf[0] = c;
21012 buf[1] = NUL;
21013 }
21014 set_vim_var_string(VV_CHAR, buf, -1);
21015}
21016
21017/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021018 * Set v:count to "count" and v:count1 to "count1".
21019 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021020 */
21021 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021022set_vcount(
21023 long count,
21024 long count1,
21025 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021026{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021027 if (set_prevcount)
21028 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021029 vimvars[VV_COUNT].vv_nr = count;
21030 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021031}
21032
21033/*
21034 * Set string v: variable to a copy of "val".
21035 */
21036 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021037set_vim_var_string(
21038 int idx,
21039 char_u *val,
21040 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021041{
Bram Moolenaara542c682016-01-31 16:28:04 +010021042 clear_tv(&vimvars[idx].vv_di.di_tv);
21043 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021044 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021045 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021046 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021047 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021048 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021049 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021050}
21051
21052/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021053 * Set List v: variable to "val".
21054 */
21055 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021056set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021057{
Bram Moolenaara542c682016-01-31 16:28:04 +010021058 clear_tv(&vimvars[idx].vv_di.di_tv);
21059 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021060 vimvars[idx].vv_list = val;
21061 if (val != NULL)
21062 ++val->lv_refcount;
21063}
21064
21065/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021066 * Set Dictionary v: variable to "val".
21067 */
21068 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021069set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021070{
21071 int todo;
21072 hashitem_T *hi;
21073
Bram Moolenaara542c682016-01-31 16:28:04 +010021074 clear_tv(&vimvars[idx].vv_di.di_tv);
21075 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021076 vimvars[idx].vv_dict = val;
21077 if (val != NULL)
21078 {
21079 ++val->dv_refcount;
21080
21081 /* Set readonly */
21082 todo = (int)val->dv_hashtab.ht_used;
21083 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21084 {
21085 if (HASHITEM_EMPTY(hi))
21086 continue;
21087 --todo;
21088 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21089 }
21090 }
21091}
21092
21093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021094 * Set v:register if needed.
21095 */
21096 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021097set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098{
21099 char_u regname;
21100
21101 if (c == 0 || c == ' ')
21102 regname = '"';
21103 else
21104 regname = c;
21105 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021106 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107 set_vim_var_string(VV_REG, &regname, 1);
21108}
21109
21110/*
21111 * Get or set v:exception. If "oldval" == NULL, return the current value.
21112 * Otherwise, restore the value to "oldval" and return NULL.
21113 * Must always be called in pairs to save and restore v:exception! Does not
21114 * take care of memory allocations.
21115 */
21116 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021117v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021118{
21119 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021120 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121
Bram Moolenaare9a41262005-01-15 22:18:47 +000021122 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021123 return NULL;
21124}
21125
21126/*
21127 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21128 * Otherwise, restore the value to "oldval" and return NULL.
21129 * Must always be called in pairs to save and restore v:throwpoint! Does not
21130 * take care of memory allocations.
21131 */
21132 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021133v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021134{
21135 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021136 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021137
Bram Moolenaare9a41262005-01-15 22:18:47 +000021138 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139 return NULL;
21140}
21141
21142#if defined(FEAT_AUTOCMD) || defined(PROTO)
21143/*
21144 * Set v:cmdarg.
21145 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21146 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21147 * Must always be called in pairs!
21148 */
21149 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021150set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021151{
21152 char_u *oldval;
21153 char_u *newval;
21154 unsigned len;
21155
Bram Moolenaare9a41262005-01-15 22:18:47 +000021156 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021157 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021158 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021159 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021160 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021161 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162 }
21163
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021164 if (eap->force_bin == FORCE_BIN)
21165 len = 6;
21166 else if (eap->force_bin == FORCE_NOBIN)
21167 len = 8;
21168 else
21169 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021170
21171 if (eap->read_edit)
21172 len += 7;
21173
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021174 if (eap->force_ff != 0)
21175 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21176# ifdef FEAT_MBYTE
21177 if (eap->force_enc != 0)
21178 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021179 if (eap->bad_char != 0)
21180 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021181# endif
21182
21183 newval = alloc(len + 1);
21184 if (newval == NULL)
21185 return NULL;
21186
21187 if (eap->force_bin == FORCE_BIN)
21188 sprintf((char *)newval, " ++bin");
21189 else if (eap->force_bin == FORCE_NOBIN)
21190 sprintf((char *)newval, " ++nobin");
21191 else
21192 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021193
21194 if (eap->read_edit)
21195 STRCAT(newval, " ++edit");
21196
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021197 if (eap->force_ff != 0)
21198 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21199 eap->cmd + eap->force_ff);
21200# ifdef FEAT_MBYTE
21201 if (eap->force_enc != 0)
21202 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21203 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021204 if (eap->bad_char == BAD_KEEP)
21205 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21206 else if (eap->bad_char == BAD_DROP)
21207 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21208 else if (eap->bad_char != 0)
21209 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021210# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021211 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021212 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021213}
21214#endif
21215
21216/*
21217 * Get the value of internal variable "name".
21218 * Return OK or FAIL.
21219 */
21220 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021221get_var_tv(
21222 char_u *name,
21223 int len, /* length of "name" */
21224 typval_T *rettv, /* NULL when only checking existence */
21225 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21226 int verbose, /* may give error message */
21227 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021228{
21229 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021230 typval_T *tv = NULL;
21231 typval_T atv;
21232 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021233 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021234
21235 /* truncate the name, so that we can use strcmp() */
21236 cc = name[len];
21237 name[len] = NUL;
21238
21239 /*
21240 * Check for "b:changedtick".
21241 */
21242 if (STRCMP(name, "b:changedtick") == 0)
21243 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021244 atv.v_type = VAR_NUMBER;
21245 atv.vval.v_number = curbuf->b_changedtick;
21246 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021247 }
21248
21249 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021250 * Check for user-defined variables.
21251 */
21252 else
21253 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021254 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021255 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021256 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021257 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021258 if (dip != NULL)
21259 *dip = v;
21260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021261 }
21262
Bram Moolenaare9a41262005-01-15 22:18:47 +000021263 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021264 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021265 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021266 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267 ret = FAIL;
21268 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021269 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021270 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271
21272 name[len] = cc;
21273
21274 return ret;
21275}
21276
21277/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021278 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21279 * Also handle function call with Funcref variable: func(expr)
21280 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21281 */
21282 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021283handle_subscript(
21284 char_u **arg,
21285 typval_T *rettv,
21286 int evaluate, /* do more than finding the end */
21287 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021288{
21289 int ret = OK;
21290 dict_T *selfdict = NULL;
21291 char_u *s;
21292 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021293 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021294
21295 while (ret == OK
21296 && (**arg == '['
21297 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021298 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021299 && !vim_iswhite(*(*arg - 1)))
21300 {
21301 if (**arg == '(')
21302 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021303 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021304 if (evaluate)
21305 {
21306 functv = *rettv;
21307 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021308
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021309 /* Invoke the function. Recursive! */
21310 s = functv.vval.v_string;
21311 }
21312 else
21313 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021314 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021315 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21316 &len, evaluate, selfdict);
21317
21318 /* Clear the funcref afterwards, so that deleting it while
21319 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021320 if (evaluate)
21321 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021322
21323 /* Stop the expression evaluation when immediately aborting on
21324 * error, or when an interrupt occurred or an exception was thrown
21325 * but not caught. */
21326 if (aborting())
21327 {
21328 if (ret == OK)
21329 clear_tv(rettv);
21330 ret = FAIL;
21331 }
21332 dict_unref(selfdict);
21333 selfdict = NULL;
21334 }
21335 else /* **arg == '[' || **arg == '.' */
21336 {
21337 dict_unref(selfdict);
21338 if (rettv->v_type == VAR_DICT)
21339 {
21340 selfdict = rettv->vval.v_dict;
21341 if (selfdict != NULL)
21342 ++selfdict->dv_refcount;
21343 }
21344 else
21345 selfdict = NULL;
21346 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21347 {
21348 clear_tv(rettv);
21349 ret = FAIL;
21350 }
21351 }
21352 }
21353 dict_unref(selfdict);
21354 return ret;
21355}
21356
21357/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021358 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021359 * value).
21360 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021361 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021362alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021363{
Bram Moolenaar33570922005-01-25 22:26:29 +000021364 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021365}
21366
21367/*
21368 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021369 * The string "s" must have been allocated, it is consumed.
21370 * Return NULL for out of memory, the variable otherwise.
21371 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021372 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021373alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021374{
Bram Moolenaar33570922005-01-25 22:26:29 +000021375 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021376
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021377 rettv = alloc_tv();
21378 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021379 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021380 rettv->v_type = VAR_STRING;
21381 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021382 }
21383 else
21384 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021385 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386}
21387
21388/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021389 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021390 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021391 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021392free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021393{
21394 if (varp != NULL)
21395 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021396 switch (varp->v_type)
21397 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021398 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021399 func_unref(varp->vval.v_string);
21400 /*FALLTHROUGH*/
21401 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021402 vim_free(varp->vval.v_string);
21403 break;
21404 case VAR_LIST:
21405 list_unref(varp->vval.v_list);
21406 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021407 case VAR_DICT:
21408 dict_unref(varp->vval.v_dict);
21409 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021410 case VAR_JOB:
21411#ifdef FEAT_JOB
21412 job_unref(varp->vval.v_job);
21413 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021414#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021415 case VAR_NUMBER:
21416 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021417 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021418 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021419 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021421 vim_free(varp);
21422 }
21423}
21424
21425/*
21426 * Free the memory for a variable value and set the value to NULL or 0.
21427 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021428 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021429clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021430{
21431 if (varp != NULL)
21432 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021433 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021434 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021435 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021436 func_unref(varp->vval.v_string);
21437 /*FALLTHROUGH*/
21438 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021439 vim_free(varp->vval.v_string);
21440 varp->vval.v_string = NULL;
21441 break;
21442 case VAR_LIST:
21443 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021444 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021445 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021446 case VAR_DICT:
21447 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021448 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021449 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021450 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021451 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021452 varp->vval.v_number = 0;
21453 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021454 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021455#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021456 varp->vval.v_float = 0.0;
21457 break;
21458#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021459 case VAR_JOB:
21460#ifdef FEAT_JOB
21461 job_unref(varp->vval.v_job);
21462 varp->vval.v_job = NULL;
21463#endif
21464 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021465 case VAR_UNKNOWN:
21466 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021467 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021468 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021469 }
21470}
21471
21472/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021473 * Set the value of a variable to NULL without freeing items.
21474 */
21475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021476init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021477{
21478 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021479 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021480}
21481
21482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021483 * Get the number value of a variable.
21484 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021485 * For incompatible types, return 0.
21486 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21487 * caller of incompatible types: it sets *denote to TRUE if "denote"
21488 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021489 */
21490 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021491get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021492{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021493 int error = FALSE;
21494
21495 return get_tv_number_chk(varp, &error); /* return 0L on error */
21496}
21497
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021498 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021499get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021500{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021501 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021502
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021503 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021504 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021505 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021506 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021507 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021508#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021509 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021510 break;
21511#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021512 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021513 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021514 break;
21515 case VAR_STRING:
21516 if (varp->vval.v_string != NULL)
21517 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021518 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021519 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021520 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021521 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021522 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021523 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021524 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021525 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021526 case VAR_SPECIAL:
21527 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21528 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021529 case VAR_JOB:
21530#ifdef FEAT_JOB
21531 EMSG(_("E910: Using a Job as a Number"));
21532 break;
21533#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021534 case VAR_UNKNOWN:
21535 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021536 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021537 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021538 if (denote == NULL) /* useful for values that must be unsigned */
21539 n = -1;
21540 else
21541 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021542 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021543}
21544
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021545#ifdef FEAT_FLOAT
21546 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021547get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021548{
21549 switch (varp->v_type)
21550 {
21551 case VAR_NUMBER:
21552 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021553 case VAR_FLOAT:
21554 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021555 case VAR_FUNC:
21556 EMSG(_("E891: Using a Funcref as a Float"));
21557 break;
21558 case VAR_STRING:
21559 EMSG(_("E892: Using a String as a Float"));
21560 break;
21561 case VAR_LIST:
21562 EMSG(_("E893: Using a List as a Float"));
21563 break;
21564 case VAR_DICT:
21565 EMSG(_("E894: Using a Dictionary as a Float"));
21566 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021567 case VAR_SPECIAL:
21568 EMSG(_("E907: Using a special value as a Float"));
21569 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021570 case VAR_JOB:
21571# ifdef FEAT_JOB
21572 EMSG(_("E911: Using a Job as a Float"));
21573 break;
21574# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021575 case VAR_UNKNOWN:
21576 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021577 break;
21578 }
21579 return 0;
21580}
21581#endif
21582
Bram Moolenaar071d4272004-06-13 20:20:40 +000021583/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021584 * Get the lnum from the first argument.
21585 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021586 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021587 */
21588 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021589get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021590{
Bram Moolenaar33570922005-01-25 22:26:29 +000021591 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021592 linenr_T lnum;
21593
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021594 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021595 if (lnum == 0) /* no valid number, try using line() */
21596 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021597 rettv.v_type = VAR_NUMBER;
21598 f_line(argvars, &rettv);
21599 lnum = rettv.vval.v_number;
21600 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021601 }
21602 return lnum;
21603}
21604
21605/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021606 * Get the lnum from the first argument.
21607 * Also accepts "$", then "buf" is used.
21608 * Returns 0 on error.
21609 */
21610 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021611get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021612{
21613 if (argvars[0].v_type == VAR_STRING
21614 && argvars[0].vval.v_string != NULL
21615 && argvars[0].vval.v_string[0] == '$'
21616 && buf != NULL)
21617 return buf->b_ml.ml_line_count;
21618 return get_tv_number_chk(&argvars[0], NULL);
21619}
21620
21621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021622 * Get the string value of a variable.
21623 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021624 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21625 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021626 * If the String variable has never been set, return an empty string.
21627 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021628 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21629 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630 */
21631 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021632get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021633{
21634 static char_u mybuf[NUMBUFLEN];
21635
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021636 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021637}
21638
21639 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021640get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021641{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021642 char_u *res = get_tv_string_buf_chk(varp, buf);
21643
21644 return res != NULL ? res : (char_u *)"";
21645}
21646
Bram Moolenaar7d647822014-04-05 21:28:56 +020021647/*
21648 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21649 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021650 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021651get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021652{
21653 static char_u mybuf[NUMBUFLEN];
21654
21655 return get_tv_string_buf_chk(varp, mybuf);
21656}
21657
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021658 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021659get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021660{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021661 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021662 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021663 case VAR_NUMBER:
21664 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21665 return buf;
21666 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021667 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021668 break;
21669 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021670 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021671 break;
21672 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021673 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021674 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021675 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021676#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021677 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021678 break;
21679#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021680 case VAR_STRING:
21681 if (varp->vval.v_string != NULL)
21682 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021683 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021684 case VAR_SPECIAL:
21685 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21686 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021687 case VAR_JOB:
21688#ifdef FEAT_JOB
21689 {
21690 job_T *job = varp->vval.v_job;
21691 char *status = job->jv_status == JOB_FAILED ? "fail"
21692 : job->jv_status == JOB_ENDED ? "dead"
21693 : "run";
21694# ifdef UNIX
21695 vim_snprintf((char *)buf, NUMBUFLEN,
21696 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021697# elif defined(WIN32)
21698 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010021699 "process %ld %s",
21700 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021701 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010021702# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021703 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010021704 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
21705# endif
21706 return buf;
21707 }
21708#endif
21709 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021710 case VAR_UNKNOWN:
21711 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021712 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021713 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021714 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021715}
21716
21717/*
21718 * Find variable "name" in the list of variables.
21719 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021720 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021721 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021722 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021723 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021724 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021725find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021726{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021727 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021728 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021729
Bram Moolenaara7043832005-01-21 11:56:39 +000021730 ht = find_var_ht(name, &varname);
21731 if (htp != NULL)
21732 *htp = ht;
21733 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021735 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021736}
21737
21738/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021739 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021740 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021741 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021742 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021743find_var_in_ht(
21744 hashtab_T *ht,
21745 int htname,
21746 char_u *varname,
21747 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021748{
Bram Moolenaar33570922005-01-25 22:26:29 +000021749 hashitem_T *hi;
21750
21751 if (*varname == NUL)
21752 {
21753 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021754 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021755 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021756 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021757 case 'g': return &globvars_var;
21758 case 'v': return &vimvars_var;
21759 case 'b': return &curbuf->b_bufvar;
21760 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021761#ifdef FEAT_WINDOWS
21762 case 't': return &curtab->tp_winvar;
21763#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021764 case 'l': return current_funccal == NULL
21765 ? NULL : &current_funccal->l_vars_var;
21766 case 'a': return current_funccal == NULL
21767 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021768 }
21769 return NULL;
21770 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021771
21772 hi = hash_find(ht, varname);
21773 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021774 {
21775 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021776 * worked find the variable again. Don't auto-load a script if it was
21777 * loaded already, otherwise it would be loaded every time when
21778 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021779 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021780 {
21781 /* Note: script_autoload() may make "hi" invalid. It must either
21782 * be obtained again or not used. */
21783 if (!script_autoload(varname, FALSE) || aborting())
21784 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021785 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021786 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021787 if (HASHITEM_EMPTY(hi))
21788 return NULL;
21789 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021790 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021791}
21792
21793/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021794 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021795 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021796 * Set "varname" to the start of name without ':'.
21797 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021798 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021799find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021800{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021801 hashitem_T *hi;
21802
Bram Moolenaar73627d02015-08-11 15:46:09 +020021803 if (name[0] == NUL)
21804 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021805 if (name[1] != ':')
21806 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021807 /* The name must not start with a colon or #. */
21808 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021809 return NULL;
21810 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021811
21812 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021813 hi = hash_find(&compat_hashtab, name);
21814 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021815 return &compat_hashtab;
21816
Bram Moolenaar071d4272004-06-13 20:20:40 +000021817 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021818 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021819 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021820 }
21821 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021822 if (*name == 'g') /* global variable */
21823 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021824 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21825 */
21826 if (vim_strchr(name + 2, ':') != NULL
21827 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021828 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021829 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021830 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021831 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021832 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021833#ifdef FEAT_WINDOWS
21834 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021835 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021836#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021837 if (*name == 'v') /* v: variable */
21838 return &vimvarht;
21839 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021840 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021841 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021842 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021843 if (*name == 's' /* script variable */
21844 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21845 return &SCRIPT_VARS(current_SID);
21846 return NULL;
21847}
21848
21849/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021850 * Get function call environment based on bactrace debug level
21851 */
21852 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021853get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021854{
21855 int i;
21856 funccall_T *funccal;
21857 funccall_T *temp_funccal;
21858
21859 funccal = current_funccal;
21860 if (debug_backtrace_level > 0)
21861 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021862 for (i = 0; i < debug_backtrace_level; i++)
21863 {
21864 temp_funccal = funccal->caller;
21865 if (temp_funccal)
21866 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021867 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021868 /* backtrace level overflow. reset to max */
21869 debug_backtrace_level = i;
21870 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021871 }
21872 return funccal;
21873}
21874
21875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021876 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021877 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021878 * Returns NULL when it doesn't exist.
21879 */
21880 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021881get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021882{
Bram Moolenaar33570922005-01-25 22:26:29 +000021883 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021884
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021885 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021886 if (v == NULL)
21887 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021888 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021889}
21890
21891/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021892 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893 * sourcing this script and when executing functions defined in the script.
21894 */
21895 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021896new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021897{
Bram Moolenaara7043832005-01-21 11:56:39 +000021898 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021899 hashtab_T *ht;
21900 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021901
Bram Moolenaar071d4272004-06-13 20:20:40 +000021902 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21903 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021904 /* Re-allocating ga_data means that an ht_array pointing to
21905 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021906 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021907 for (i = 1; i <= ga_scripts.ga_len; ++i)
21908 {
21909 ht = &SCRIPT_VARS(i);
21910 if (ht->ht_mask == HT_INIT_SIZE - 1)
21911 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021912 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021913 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021914 }
21915
Bram Moolenaar071d4272004-06-13 20:20:40 +000021916 while (ga_scripts.ga_len < id)
21917 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021918 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021919 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021920 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021922 }
21923 }
21924}
21925
21926/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021927 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21928 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021929 */
21930 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021931init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021932{
Bram Moolenaar33570922005-01-25 22:26:29 +000021933 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021934 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021935 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021936 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021937 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021938 dict_var->di_tv.vval.v_dict = dict;
21939 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021940 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021941 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21942 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021943}
21944
21945/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021946 * Unreference a dictionary initialized by init_var_dict().
21947 */
21948 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021949unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020021950{
21951 /* Now the dict needs to be freed if no one else is using it, go back to
21952 * normal reference counting. */
21953 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21954 dict_unref(dict);
21955}
21956
21957/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021958 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021959 * Frees all allocated variables and the value they contain.
21960 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021961 */
21962 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021963vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021964{
21965 vars_clear_ext(ht, TRUE);
21966}
21967
21968/*
21969 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21970 */
21971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021972vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021973{
Bram Moolenaara7043832005-01-21 11:56:39 +000021974 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021975 hashitem_T *hi;
21976 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021977
Bram Moolenaar33570922005-01-25 22:26:29 +000021978 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021979 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021980 for (hi = ht->ht_array; todo > 0; ++hi)
21981 {
21982 if (!HASHITEM_EMPTY(hi))
21983 {
21984 --todo;
21985
Bram Moolenaar33570922005-01-25 22:26:29 +000021986 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021987 * ht_array might change then. hash_clear() takes care of it
21988 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021989 v = HI2DI(hi);
21990 if (free_val)
21991 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021992 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021993 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021994 }
21995 }
21996 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021997 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021998}
21999
Bram Moolenaara7043832005-01-21 11:56:39 +000022000/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022001 * Delete a variable from hashtab "ht" at item "hi".
22002 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022005delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022006{
Bram Moolenaar33570922005-01-25 22:26:29 +000022007 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022008
22009 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022010 clear_tv(&di->di_tv);
22011 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022012}
22013
22014/*
22015 * List the value of one internal variable.
22016 */
22017 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022018list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022019{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022020 char_u *tofree;
22021 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022022 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022023
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022024 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022025 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022026 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022027 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028}
22029
Bram Moolenaar071d4272004-06-13 20:20:40 +000022030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022031list_one_var_a(
22032 char_u *prefix,
22033 char_u *name,
22034 int type,
22035 char_u *string,
22036 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022037{
Bram Moolenaar31859182007-08-14 20:41:13 +000022038 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22039 msg_start();
22040 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022041 if (name != NULL) /* "a:" vars don't have a name stored */
22042 msg_puts(name);
22043 msg_putchar(' ');
22044 msg_advance(22);
22045 if (type == VAR_NUMBER)
22046 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022047 else if (type == VAR_FUNC)
22048 msg_putchar('*');
22049 else if (type == VAR_LIST)
22050 {
22051 msg_putchar('[');
22052 if (*string == '[')
22053 ++string;
22054 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022055 else if (type == VAR_DICT)
22056 {
22057 msg_putchar('{');
22058 if (*string == '{')
22059 ++string;
22060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022061 else
22062 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022063
Bram Moolenaar071d4272004-06-13 20:20:40 +000022064 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022065
22066 if (type == VAR_FUNC)
22067 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022068 if (*first)
22069 {
22070 msg_clr_eos();
22071 *first = FALSE;
22072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022073}
22074
22075/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022076 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022077 * If the variable already exists, the value is updated.
22078 * Otherwise the variable is created.
22079 */
22080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022081set_var(
22082 char_u *name,
22083 typval_T *tv,
22084 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022085{
Bram Moolenaar33570922005-01-25 22:26:29 +000022086 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022087 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022088 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022089
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022090 ht = find_var_ht(name, &varname);
22091 if (ht == NULL || *varname == NUL)
22092 {
22093 EMSG2(_(e_illvar), name);
22094 return;
22095 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022096 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022097
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022098 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22099 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022100
Bram Moolenaar33570922005-01-25 22:26:29 +000022101 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022102 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022103 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022104 if (var_check_ro(v->di_flags, name, FALSE)
22105 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022106 return;
22107 if (v->di_tv.v_type != tv->v_type
22108 && !((v->di_tv.v_type == VAR_STRING
22109 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022110 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022111 || tv->v_type == VAR_NUMBER))
22112#ifdef FEAT_FLOAT
22113 && !((v->di_tv.v_type == VAR_NUMBER
22114 || v->di_tv.v_type == VAR_FLOAT)
22115 && (tv->v_type == VAR_NUMBER
22116 || tv->v_type == VAR_FLOAT))
22117#endif
22118 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022119 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022120 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022121 return;
22122 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022123
22124 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022125 * Handle setting internal v: variables separately where needed to
22126 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022127 */
22128 if (ht == &vimvarht)
22129 {
22130 if (v->di_tv.v_type == VAR_STRING)
22131 {
22132 vim_free(v->di_tv.vval.v_string);
22133 if (copy || tv->v_type != VAR_STRING)
22134 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22135 else
22136 {
22137 /* Take over the string to avoid an extra alloc/free. */
22138 v->di_tv.vval.v_string = tv->vval.v_string;
22139 tv->vval.v_string = NULL;
22140 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022141 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022142 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022143 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022144 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022145 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022146 if (STRCMP(varname, "searchforward") == 0)
22147 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022148#ifdef FEAT_SEARCH_EXTRA
22149 else if (STRCMP(varname, "hlsearch") == 0)
22150 {
22151 no_hlsearch = !v->di_tv.vval.v_number;
22152 redraw_all_later(SOME_VALID);
22153 }
22154#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022155 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022156 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022157 else if (v->di_tv.v_type != tv->v_type)
22158 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022159 }
22160
22161 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022162 }
22163 else /* add a new variable */
22164 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022165 /* Can't add "v:" variable. */
22166 if (ht == &vimvarht)
22167 {
22168 EMSG2(_(e_illvar), name);
22169 return;
22170 }
22171
Bram Moolenaar92124a32005-06-17 22:03:40 +000022172 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022173 if (!valid_varname(varname))
22174 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022175
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022176 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22177 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022178 if (v == NULL)
22179 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022180 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022181 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022182 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022183 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022184 return;
22185 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022186 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022187 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022188
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022189 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022190 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022191 else
22192 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022193 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022194 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022195 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022197}
22198
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022199/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022200 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022201 * Also give an error message.
22202 */
22203 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022204var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022205{
22206 if (flags & DI_FLAGS_RO)
22207 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022208 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022209 return TRUE;
22210 }
22211 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22212 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022213 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022214 return TRUE;
22215 }
22216 return FALSE;
22217}
22218
22219/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022220 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22221 * Also give an error message.
22222 */
22223 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022224var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022225{
22226 if (flags & DI_FLAGS_FIX)
22227 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022228 EMSG2(_("E795: Cannot delete variable %s"),
22229 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022230 return TRUE;
22231 }
22232 return FALSE;
22233}
22234
22235/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022236 * Check if a funcref is assigned to a valid variable name.
22237 * Return TRUE and give an error if not.
22238 */
22239 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022240var_check_func_name(
22241 char_u *name, /* points to start of variable name */
22242 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022243{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022244 /* Allow for w: b: s: and t:. */
22245 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022246 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22247 ? name[2] : name[0]))
22248 {
22249 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22250 name);
22251 return TRUE;
22252 }
22253 /* Don't allow hiding a function. When "v" is not NULL we might be
22254 * assigning another function to the same var, the type is checked
22255 * below. */
22256 if (new_var && function_exists(name))
22257 {
22258 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22259 name);
22260 return TRUE;
22261 }
22262 return FALSE;
22263}
22264
22265/*
22266 * Check if a variable name is valid.
22267 * Return FALSE and give an error if not.
22268 */
22269 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022270valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022271{
22272 char_u *p;
22273
22274 for (p = varname; *p != NUL; ++p)
22275 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22276 && *p != AUTOLOAD_CHAR)
22277 {
22278 EMSG2(_(e_illvar), varname);
22279 return FALSE;
22280 }
22281 return TRUE;
22282}
22283
22284/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022285 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022286 * Also give an error message, using "name" or _("name") when use_gettext is
22287 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022288 */
22289 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022290tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022291{
22292 if (lock & VAR_LOCKED)
22293 {
22294 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022295 name == NULL ? (char_u *)_("Unknown")
22296 : use_gettext ? (char_u *)_(name)
22297 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022298 return TRUE;
22299 }
22300 if (lock & VAR_FIXED)
22301 {
22302 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022303 name == NULL ? (char_u *)_("Unknown")
22304 : use_gettext ? (char_u *)_(name)
22305 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022306 return TRUE;
22307 }
22308 return FALSE;
22309}
22310
22311/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022312 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022313 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022314 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022315 * It is OK for "from" and "to" to point to the same item. This is used to
22316 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022317 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022318 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022319copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022320{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022321 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022322 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022323 switch (from->v_type)
22324 {
22325 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022326 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022327 to->vval.v_number = from->vval.v_number;
22328 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022329 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022330#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022331 to->vval.v_float = from->vval.v_float;
22332 break;
22333#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022334 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010022335#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010022336 to->vval.v_job = from->vval.v_job;
22337 ++to->vval.v_job->jv_refcount;
22338 break;
22339#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022340 case VAR_STRING:
22341 case VAR_FUNC:
22342 if (from->vval.v_string == NULL)
22343 to->vval.v_string = NULL;
22344 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022345 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022346 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022347 if (from->v_type == VAR_FUNC)
22348 func_ref(to->vval.v_string);
22349 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022350 break;
22351 case VAR_LIST:
22352 if (from->vval.v_list == NULL)
22353 to->vval.v_list = NULL;
22354 else
22355 {
22356 to->vval.v_list = from->vval.v_list;
22357 ++to->vval.v_list->lv_refcount;
22358 }
22359 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022360 case VAR_DICT:
22361 if (from->vval.v_dict == NULL)
22362 to->vval.v_dict = NULL;
22363 else
22364 {
22365 to->vval.v_dict = from->vval.v_dict;
22366 ++to->vval.v_dict->dv_refcount;
22367 }
22368 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022369 case VAR_UNKNOWN:
22370 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022371 break;
22372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022373}
22374
22375/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022376 * Make a copy of an item.
22377 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022378 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22379 * reference to an already copied list/dict can be used.
22380 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022381 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022382 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022383item_copy(
22384 typval_T *from,
22385 typval_T *to,
22386 int deep,
22387 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022388{
22389 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022390 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022391
Bram Moolenaar33570922005-01-25 22:26:29 +000022392 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022393 {
22394 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022395 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022396 }
22397 ++recurse;
22398
22399 switch (from->v_type)
22400 {
22401 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022402 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022403 case VAR_STRING:
22404 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010022405 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022406 case VAR_JOB:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022407 copy_tv(from, to);
22408 break;
22409 case VAR_LIST:
22410 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022411 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022412 if (from->vval.v_list == NULL)
22413 to->vval.v_list = NULL;
22414 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22415 {
22416 /* use the copy made earlier */
22417 to->vval.v_list = from->vval.v_list->lv_copylist;
22418 ++to->vval.v_list->lv_refcount;
22419 }
22420 else
22421 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22422 if (to->vval.v_list == NULL)
22423 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022424 break;
22425 case VAR_DICT:
22426 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022427 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022428 if (from->vval.v_dict == NULL)
22429 to->vval.v_dict = NULL;
22430 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22431 {
22432 /* use the copy made earlier */
22433 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22434 ++to->vval.v_dict->dv_refcount;
22435 }
22436 else
22437 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22438 if (to->vval.v_dict == NULL)
22439 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022440 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022441 case VAR_UNKNOWN:
22442 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022443 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022444 }
22445 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022446 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022447}
22448
22449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022450 * ":echo expr1 ..." print each argument separated with a space, add a
22451 * newline at the end.
22452 * ":echon expr1 ..." print each argument plain.
22453 */
22454 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022455ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022456{
22457 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022458 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022459 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022460 char_u *p;
22461 int needclr = TRUE;
22462 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022463 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022464
22465 if (eap->skip)
22466 ++emsg_skip;
22467 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22468 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022469 /* If eval1() causes an error message the text from the command may
22470 * still need to be cleared. E.g., "echo 22,44". */
22471 need_clr_eos = needclr;
22472
Bram Moolenaar071d4272004-06-13 20:20:40 +000022473 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022474 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022475 {
22476 /*
22477 * Report the invalid expression unless the expression evaluation
22478 * has been cancelled due to an aborting error, an interrupt, or an
22479 * exception.
22480 */
22481 if (!aborting())
22482 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022483 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022484 break;
22485 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022486 need_clr_eos = FALSE;
22487
Bram Moolenaar071d4272004-06-13 20:20:40 +000022488 if (!eap->skip)
22489 {
22490 if (atstart)
22491 {
22492 atstart = FALSE;
22493 /* Call msg_start() after eval1(), evaluating the expression
22494 * may cause a message to appear. */
22495 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022496 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022497 /* Mark the saved text as finishing the line, so that what
22498 * follows is displayed on a new line when scrolling back
22499 * at the more prompt. */
22500 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022501 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022503 }
22504 else if (eap->cmdidx == CMD_echo)
22505 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022506 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022507 if (p != NULL)
22508 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022509 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022510 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022511 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022512 if (*p != TAB && needclr)
22513 {
22514 /* remove any text still there from the command */
22515 msg_clr_eos();
22516 needclr = FALSE;
22517 }
22518 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022519 }
22520 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022521 {
22522#ifdef FEAT_MBYTE
22523 if (has_mbyte)
22524 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022525 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022526
22527 (void)msg_outtrans_len_attr(p, i, echo_attr);
22528 p += i - 1;
22529 }
22530 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022531#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022532 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022534 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022535 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022536 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022537 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022538 arg = skipwhite(arg);
22539 }
22540 eap->nextcmd = check_nextcmd(arg);
22541
22542 if (eap->skip)
22543 --emsg_skip;
22544 else
22545 {
22546 /* remove text that may still be there from the command */
22547 if (needclr)
22548 msg_clr_eos();
22549 if (eap->cmdidx == CMD_echo)
22550 msg_end();
22551 }
22552}
22553
22554/*
22555 * ":echohl {name}".
22556 */
22557 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022558ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022559{
22560 int id;
22561
22562 id = syn_name2id(eap->arg);
22563 if (id == 0)
22564 echo_attr = 0;
22565 else
22566 echo_attr = syn_id2attr(id);
22567}
22568
22569/*
22570 * ":execute expr1 ..." execute the result of an expression.
22571 * ":echomsg expr1 ..." Print a message
22572 * ":echoerr expr1 ..." Print an error
22573 * Each gets spaces around each argument and a newline at the end for
22574 * echo commands
22575 */
22576 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022577ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022578{
22579 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022580 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022581 int ret = OK;
22582 char_u *p;
22583 garray_T ga;
22584 int len;
22585 int save_did_emsg;
22586
22587 ga_init2(&ga, 1, 80);
22588
22589 if (eap->skip)
22590 ++emsg_skip;
22591 while (*arg != NUL && *arg != '|' && *arg != '\n')
22592 {
22593 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022594 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022595 {
22596 /*
22597 * Report the invalid expression unless the expression evaluation
22598 * has been cancelled due to an aborting error, an interrupt, or an
22599 * exception.
22600 */
22601 if (!aborting())
22602 EMSG2(_(e_invexpr2), p);
22603 ret = FAIL;
22604 break;
22605 }
22606
22607 if (!eap->skip)
22608 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022609 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022610 len = (int)STRLEN(p);
22611 if (ga_grow(&ga, len + 2) == FAIL)
22612 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022613 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022614 ret = FAIL;
22615 break;
22616 }
22617 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022618 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022619 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022620 ga.ga_len += len;
22621 }
22622
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022623 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022624 arg = skipwhite(arg);
22625 }
22626
22627 if (ret != FAIL && ga.ga_data != NULL)
22628 {
22629 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022630 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022631 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022632 out_flush();
22633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022634 else if (eap->cmdidx == CMD_echoerr)
22635 {
22636 /* We don't want to abort following commands, restore did_emsg. */
22637 save_did_emsg = did_emsg;
22638 EMSG((char_u *)ga.ga_data);
22639 if (!force_abort)
22640 did_emsg = save_did_emsg;
22641 }
22642 else if (eap->cmdidx == CMD_execute)
22643 do_cmdline((char_u *)ga.ga_data,
22644 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22645 }
22646
22647 ga_clear(&ga);
22648
22649 if (eap->skip)
22650 --emsg_skip;
22651
22652 eap->nextcmd = check_nextcmd(arg);
22653}
22654
22655/*
22656 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22657 * "arg" points to the "&" or '+' when called, to "option" when returning.
22658 * Returns NULL when no option name found. Otherwise pointer to the char
22659 * after the option name.
22660 */
22661 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022662find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022663{
22664 char_u *p = *arg;
22665
22666 ++p;
22667 if (*p == 'g' && p[1] == ':')
22668 {
22669 *opt_flags = OPT_GLOBAL;
22670 p += 2;
22671 }
22672 else if (*p == 'l' && p[1] == ':')
22673 {
22674 *opt_flags = OPT_LOCAL;
22675 p += 2;
22676 }
22677 else
22678 *opt_flags = 0;
22679
22680 if (!ASCII_ISALPHA(*p))
22681 return NULL;
22682 *arg = p;
22683
22684 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22685 p += 4; /* termcap option */
22686 else
22687 while (ASCII_ISALPHA(*p))
22688 ++p;
22689 return p;
22690}
22691
22692/*
22693 * ":function"
22694 */
22695 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022696ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022697{
22698 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022699 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022700 int j;
22701 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022702 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022703 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704 char_u *name = NULL;
22705 char_u *p;
22706 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022707 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022708 garray_T newargs;
22709 garray_T newlines;
22710 int varargs = FALSE;
22711 int mustend = FALSE;
22712 int flags = 0;
22713 ufunc_T *fp;
22714 int indent;
22715 int nesting;
22716 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022717 dictitem_T *v;
22718 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022719 static int func_nr = 0; /* number for nameless function */
22720 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022721 hashtab_T *ht;
22722 int todo;
22723 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022724 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022725
22726 /*
22727 * ":function" without argument: list functions.
22728 */
22729 if (ends_excmd(*eap->arg))
22730 {
22731 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022732 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022733 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022734 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022735 {
22736 if (!HASHITEM_EMPTY(hi))
22737 {
22738 --todo;
22739 fp = HI2UF(hi);
22740 if (!isdigit(*fp->uf_name))
22741 list_func_head(fp, FALSE);
22742 }
22743 }
22744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022745 eap->nextcmd = check_nextcmd(eap->arg);
22746 return;
22747 }
22748
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022749 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022750 * ":function /pat": list functions matching pattern.
22751 */
22752 if (*eap->arg == '/')
22753 {
22754 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22755 if (!eap->skip)
22756 {
22757 regmatch_T regmatch;
22758
22759 c = *p;
22760 *p = NUL;
22761 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22762 *p = c;
22763 if (regmatch.regprog != NULL)
22764 {
22765 regmatch.rm_ic = p_ic;
22766
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022767 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022768 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22769 {
22770 if (!HASHITEM_EMPTY(hi))
22771 {
22772 --todo;
22773 fp = HI2UF(hi);
22774 if (!isdigit(*fp->uf_name)
22775 && vim_regexec(&regmatch, fp->uf_name, 0))
22776 list_func_head(fp, FALSE);
22777 }
22778 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022779 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022780 }
22781 }
22782 if (*p == '/')
22783 ++p;
22784 eap->nextcmd = check_nextcmd(p);
22785 return;
22786 }
22787
22788 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022789 * Get the function name. There are these situations:
22790 * func normal function name
22791 * "name" == func, "fudi.fd_dict" == NULL
22792 * dict.func new dictionary entry
22793 * "name" == NULL, "fudi.fd_dict" set,
22794 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22795 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022796 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022797 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22798 * dict.func existing dict entry that's not a Funcref
22799 * "name" == NULL, "fudi.fd_dict" set,
22800 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022801 * s:func script-local function name
22802 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022803 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022804 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022805 name = trans_function_name(&p, eap->skip, 0, &fudi);
22806 paren = (vim_strchr(p, '(') != NULL);
22807 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022808 {
22809 /*
22810 * Return on an invalid expression in braces, unless the expression
22811 * evaluation has been cancelled due to an aborting error, an
22812 * interrupt, or an exception.
22813 */
22814 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022815 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022816 if (!eap->skip && fudi.fd_newkey != NULL)
22817 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022818 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022819 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022821 else
22822 eap->skip = TRUE;
22823 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022824
Bram Moolenaar071d4272004-06-13 20:20:40 +000022825 /* An error in a function call during evaluation of an expression in magic
22826 * braces should not cause the function not to be defined. */
22827 saved_did_emsg = did_emsg;
22828 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022829
22830 /*
22831 * ":function func" with only function name: list function.
22832 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022833 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022834 {
22835 if (!ends_excmd(*skipwhite(p)))
22836 {
22837 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022838 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022839 }
22840 eap->nextcmd = check_nextcmd(p);
22841 if (eap->nextcmd != NULL)
22842 *p = NUL;
22843 if (!eap->skip && !got_int)
22844 {
22845 fp = find_func(name);
22846 if (fp != NULL)
22847 {
22848 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022849 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022850 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022851 if (FUNCLINE(fp, j) == NULL)
22852 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022853 msg_putchar('\n');
22854 msg_outnum((long)(j + 1));
22855 if (j < 9)
22856 msg_putchar(' ');
22857 if (j < 99)
22858 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022859 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022860 out_flush(); /* show a line at a time */
22861 ui_breakcheck();
22862 }
22863 if (!got_int)
22864 {
22865 msg_putchar('\n');
22866 msg_puts((char_u *)" endfunction");
22867 }
22868 }
22869 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022870 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022871 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022872 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022873 }
22874
22875 /*
22876 * ":function name(arg1, arg2)" Define function.
22877 */
22878 p = skipwhite(p);
22879 if (*p != '(')
22880 {
22881 if (!eap->skip)
22882 {
22883 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022884 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022885 }
22886 /* attempt to continue by skipping some text */
22887 if (vim_strchr(p, '(') != NULL)
22888 p = vim_strchr(p, '(');
22889 }
22890 p = skipwhite(p + 1);
22891
22892 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22893 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22894
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022895 if (!eap->skip)
22896 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022897 /* Check the name of the function. Unless it's a dictionary function
22898 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022899 if (name != NULL)
22900 arg = name;
22901 else
22902 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022903 if (arg != NULL && (fudi.fd_di == NULL
22904 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022905 {
22906 if (*arg == K_SPECIAL)
22907 j = 3;
22908 else
22909 j = 0;
22910 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22911 : eval_isnamec(arg[j])))
22912 ++j;
22913 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022914 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022915 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022916 /* Disallow using the g: dict. */
22917 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22918 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022919 }
22920
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921 /*
22922 * Isolate the arguments: "arg1, arg2, ...)"
22923 */
22924 while (*p != ')')
22925 {
22926 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22927 {
22928 varargs = TRUE;
22929 p += 3;
22930 mustend = TRUE;
22931 }
22932 else
22933 {
22934 arg = p;
22935 while (ASCII_ISALNUM(*p) || *p == '_')
22936 ++p;
22937 if (arg == p || isdigit(*arg)
22938 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22939 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22940 {
22941 if (!eap->skip)
22942 EMSG2(_("E125: Illegal argument: %s"), arg);
22943 break;
22944 }
22945 if (ga_grow(&newargs, 1) == FAIL)
22946 goto erret;
22947 c = *p;
22948 *p = NUL;
22949 arg = vim_strsave(arg);
22950 if (arg == NULL)
22951 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022952
22953 /* Check for duplicate argument name. */
22954 for (i = 0; i < newargs.ga_len; ++i)
22955 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22956 {
22957 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022958 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022959 goto erret;
22960 }
22961
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22963 *p = c;
22964 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022965 if (*p == ',')
22966 ++p;
22967 else
22968 mustend = TRUE;
22969 }
22970 p = skipwhite(p);
22971 if (mustend && *p != ')')
22972 {
22973 if (!eap->skip)
22974 EMSG2(_(e_invarg2), eap->arg);
22975 break;
22976 }
22977 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022978 if (*p != ')')
22979 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022980 ++p; /* skip the ')' */
22981
Bram Moolenaare9a41262005-01-15 22:18:47 +000022982 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022983 for (;;)
22984 {
22985 p = skipwhite(p);
22986 if (STRNCMP(p, "range", 5) == 0)
22987 {
22988 flags |= FC_RANGE;
22989 p += 5;
22990 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022991 else if (STRNCMP(p, "dict", 4) == 0)
22992 {
22993 flags |= FC_DICT;
22994 p += 4;
22995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022996 else if (STRNCMP(p, "abort", 5) == 0)
22997 {
22998 flags |= FC_ABORT;
22999 p += 5;
23000 }
23001 else
23002 break;
23003 }
23004
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023005 /* When there is a line break use what follows for the function body.
23006 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23007 if (*p == '\n')
23008 line_arg = p + 1;
23009 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023010 EMSG(_(e_trailing));
23011
23012 /*
23013 * Read the body of the function, until ":endfunction" is found.
23014 */
23015 if (KeyTyped)
23016 {
23017 /* Check if the function already exists, don't let the user type the
23018 * whole function before telling him it doesn't work! For a script we
23019 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023020 if (!eap->skip && !eap->forceit)
23021 {
23022 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23023 EMSG(_(e_funcdict));
23024 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023025 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023027
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023028 if (!eap->skip && did_emsg)
23029 goto erret;
23030
Bram Moolenaar071d4272004-06-13 20:20:40 +000023031 msg_putchar('\n'); /* don't overwrite the function name */
23032 cmdline_row = msg_row;
23033 }
23034
23035 indent = 2;
23036 nesting = 0;
23037 for (;;)
23038 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023039 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023040 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023041 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023042 saved_wait_return = FALSE;
23043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023044 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023045 sourcing_lnum_off = sourcing_lnum;
23046
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023047 if (line_arg != NULL)
23048 {
23049 /* Use eap->arg, split up in parts by line breaks. */
23050 theline = line_arg;
23051 p = vim_strchr(theline, '\n');
23052 if (p == NULL)
23053 line_arg += STRLEN(line_arg);
23054 else
23055 {
23056 *p = NUL;
23057 line_arg = p + 1;
23058 }
23059 }
23060 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023061 theline = getcmdline(':', 0L, indent);
23062 else
23063 theline = eap->getline(':', eap->cookie, indent);
23064 if (KeyTyped)
23065 lines_left = Rows - 1;
23066 if (theline == NULL)
23067 {
23068 EMSG(_("E126: Missing :endfunction"));
23069 goto erret;
23070 }
23071
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023072 /* Detect line continuation: sourcing_lnum increased more than one. */
23073 if (sourcing_lnum > sourcing_lnum_off + 1)
23074 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23075 else
23076 sourcing_lnum_off = 0;
23077
Bram Moolenaar071d4272004-06-13 20:20:40 +000023078 if (skip_until != NULL)
23079 {
23080 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23081 * don't check for ":endfunc". */
23082 if (STRCMP(theline, skip_until) == 0)
23083 {
23084 vim_free(skip_until);
23085 skip_until = NULL;
23086 }
23087 }
23088 else
23089 {
23090 /* skip ':' and blanks*/
23091 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23092 ;
23093
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023094 /* Check for "endfunction". */
23095 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023096 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023097 if (line_arg == NULL)
23098 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023099 break;
23100 }
23101
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023102 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023103 * at "end". */
23104 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23105 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023106 else if (STRNCMP(p, "if", 2) == 0
23107 || STRNCMP(p, "wh", 2) == 0
23108 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023109 || STRNCMP(p, "try", 3) == 0)
23110 indent += 2;
23111
23112 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023113 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023114 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023115 if (*p == '!')
23116 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023117 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023118 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23119 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023120 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023121 ++nesting;
23122 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023123 }
23124 }
23125
23126 /* Check for ":append" or ":insert". */
23127 p = skip_range(p, NULL);
23128 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23129 || (p[0] == 'i'
23130 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23131 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23132 skip_until = vim_strsave((char_u *)".");
23133
23134 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23135 arg = skipwhite(skiptowhite(p));
23136 if (arg[0] == '<' && arg[1] =='<'
23137 && ((p[0] == 'p' && p[1] == 'y'
23138 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23139 || (p[0] == 'p' && p[1] == 'e'
23140 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23141 || (p[0] == 't' && p[1] == 'c'
23142 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023143 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23144 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23146 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023147 || (p[0] == 'm' && p[1] == 'z'
23148 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023149 ))
23150 {
23151 /* ":python <<" continues until a dot, like ":append" */
23152 p = skipwhite(arg + 2);
23153 if (*p == NUL)
23154 skip_until = vim_strsave((char_u *)".");
23155 else
23156 skip_until = vim_strsave(p);
23157 }
23158 }
23159
23160 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023161 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023162 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023163 if (line_arg == NULL)
23164 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023165 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023166 }
23167
23168 /* Copy the line to newly allocated memory. get_one_sourceline()
23169 * allocates 250 bytes per line, this saves 80% on average. The cost
23170 * is an extra alloc/free. */
23171 p = vim_strsave(theline);
23172 if (p != NULL)
23173 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023174 if (line_arg == NULL)
23175 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023176 theline = p;
23177 }
23178
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023179 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23180
23181 /* Add NULL lines for continuation lines, so that the line count is
23182 * equal to the index in the growarray. */
23183 while (sourcing_lnum_off-- > 0)
23184 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023185
23186 /* Check for end of eap->arg. */
23187 if (line_arg != NULL && *line_arg == NUL)
23188 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189 }
23190
23191 /* Don't define the function when skipping commands or when an error was
23192 * detected. */
23193 if (eap->skip || did_emsg)
23194 goto erret;
23195
23196 /*
23197 * If there are no errors, add the function
23198 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023199 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023200 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023201 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023202 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023203 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023204 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023205 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023206 goto erret;
23207 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023208
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023209 fp = find_func(name);
23210 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023211 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023212 if (!eap->forceit)
23213 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023214 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023215 goto erret;
23216 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023217 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023218 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023219 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023220 name);
23221 goto erret;
23222 }
23223 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023224 ga_clear_strings(&(fp->uf_args));
23225 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023226 vim_free(name);
23227 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023229 }
23230 else
23231 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023232 char numbuf[20];
23233
23234 fp = NULL;
23235 if (fudi.fd_newkey == NULL && !eap->forceit)
23236 {
23237 EMSG(_(e_funcdict));
23238 goto erret;
23239 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023240 if (fudi.fd_di == NULL)
23241 {
23242 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023243 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023244 goto erret;
23245 }
23246 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023247 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023248 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023249
23250 /* Give the function a sequential number. Can only be used with a
23251 * Funcref! */
23252 vim_free(name);
23253 sprintf(numbuf, "%d", ++func_nr);
23254 name = vim_strsave((char_u *)numbuf);
23255 if (name == NULL)
23256 goto erret;
23257 }
23258
23259 if (fp == NULL)
23260 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023261 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023262 {
23263 int slen, plen;
23264 char_u *scriptname;
23265
23266 /* Check that the autoload name matches the script name. */
23267 j = FAIL;
23268 if (sourcing_name != NULL)
23269 {
23270 scriptname = autoload_name(name);
23271 if (scriptname != NULL)
23272 {
23273 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023274 plen = (int)STRLEN(p);
23275 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023276 if (slen > plen && fnamecmp(p,
23277 sourcing_name + slen - plen) == 0)
23278 j = OK;
23279 vim_free(scriptname);
23280 }
23281 }
23282 if (j == FAIL)
23283 {
23284 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23285 goto erret;
23286 }
23287 }
23288
23289 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023290 if (fp == NULL)
23291 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023292
23293 if (fudi.fd_dict != NULL)
23294 {
23295 if (fudi.fd_di == NULL)
23296 {
23297 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023298 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023299 if (fudi.fd_di == NULL)
23300 {
23301 vim_free(fp);
23302 goto erret;
23303 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023304 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23305 {
23306 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023307 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023308 goto erret;
23309 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023310 }
23311 else
23312 /* overwrite existing dict entry */
23313 clear_tv(&fudi.fd_di->di_tv);
23314 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023315 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023316 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023317 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023318
23319 /* behave like "dict" was used */
23320 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023321 }
23322
Bram Moolenaar071d4272004-06-13 20:20:40 +000023323 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023324 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023325 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23326 {
23327 vim_free(fp);
23328 goto erret;
23329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023330 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023331 fp->uf_args = newargs;
23332 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023333#ifdef FEAT_PROFILE
23334 fp->uf_tml_count = NULL;
23335 fp->uf_tml_total = NULL;
23336 fp->uf_tml_self = NULL;
23337 fp->uf_profiling = FALSE;
23338 if (prof_def_func())
23339 func_do_profile(fp);
23340#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023341 fp->uf_varargs = varargs;
23342 fp->uf_flags = flags;
23343 fp->uf_calls = 0;
23344 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023345 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023346
23347erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023348 ga_clear_strings(&newargs);
23349 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023350ret_free:
23351 vim_free(skip_until);
23352 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023353 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023354 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023355 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023356}
23357
23358/*
23359 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023360 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023361 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023362 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023363 * TFN_INT: internal function name OK
23364 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023365 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023366 * Advances "pp" to just after the function name (if no error).
23367 */
23368 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023369trans_function_name(
23370 char_u **pp,
23371 int skip, /* only find the end, don't evaluate */
23372 int flags,
23373 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023374{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023375 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023376 char_u *start;
23377 char_u *end;
23378 int lead;
23379 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023380 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023381 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023382
23383 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023384 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023385 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023386
23387 /* Check for hard coded <SNR>: already translated function ID (from a user
23388 * command). */
23389 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23390 && (*pp)[2] == (int)KE_SNR)
23391 {
23392 *pp += 3;
23393 len = get_id_len(pp) + 3;
23394 return vim_strnsave(start, len);
23395 }
23396
23397 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23398 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023399 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023400 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023401 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023402
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023403 /* Note that TFN_ flags use the same values as GLV_ flags. */
23404 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023405 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023406 if (end == start)
23407 {
23408 if (!skip)
23409 EMSG(_("E129: Function name required"));
23410 goto theend;
23411 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023412 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023413 {
23414 /*
23415 * Report an invalid expression in braces, unless the expression
23416 * evaluation has been cancelled due to an aborting error, an
23417 * interrupt, or an exception.
23418 */
23419 if (!aborting())
23420 {
23421 if (end != NULL)
23422 EMSG2(_(e_invarg2), start);
23423 }
23424 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023425 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023426 goto theend;
23427 }
23428
23429 if (lv.ll_tv != NULL)
23430 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023431 if (fdp != NULL)
23432 {
23433 fdp->fd_dict = lv.ll_dict;
23434 fdp->fd_newkey = lv.ll_newkey;
23435 lv.ll_newkey = NULL;
23436 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023437 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023438 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23439 {
23440 name = vim_strsave(lv.ll_tv->vval.v_string);
23441 *pp = end;
23442 }
23443 else
23444 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023445 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23446 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023447 EMSG(_(e_funcref));
23448 else
23449 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023450 name = NULL;
23451 }
23452 goto theend;
23453 }
23454
23455 if (lv.ll_name == NULL)
23456 {
23457 /* Error found, but continue after the function name. */
23458 *pp = end;
23459 goto theend;
23460 }
23461
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023462 /* Check if the name is a Funcref. If so, use the value. */
23463 if (lv.ll_exp_name != NULL)
23464 {
23465 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023466 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023467 if (name == lv.ll_exp_name)
23468 name = NULL;
23469 }
23470 else
23471 {
23472 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023473 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023474 if (name == *pp)
23475 name = NULL;
23476 }
23477 if (name != NULL)
23478 {
23479 name = vim_strsave(name);
23480 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023481 if (STRNCMP(name, "<SNR>", 5) == 0)
23482 {
23483 /* Change "<SNR>" to the byte sequence. */
23484 name[0] = K_SPECIAL;
23485 name[1] = KS_EXTRA;
23486 name[2] = (int)KE_SNR;
23487 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23488 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023489 goto theend;
23490 }
23491
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023492 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023493 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023494 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023495 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23496 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23497 {
23498 /* When there was "s:" already or the name expanded to get a
23499 * leading "s:" then remove it. */
23500 lv.ll_name += 2;
23501 len -= 2;
23502 lead = 2;
23503 }
23504 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023505 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023506 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023507 /* skip over "s:" and "g:" */
23508 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023509 lv.ll_name += 2;
23510 len = (int)(end - lv.ll_name);
23511 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023512
23513 /*
23514 * Copy the function name to allocated memory.
23515 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23516 * Accept <SNR>123_name() outside a script.
23517 */
23518 if (skip)
23519 lead = 0; /* do nothing */
23520 else if (lead > 0)
23521 {
23522 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023523 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23524 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023525 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023526 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023527 if (current_SID <= 0)
23528 {
23529 EMSG(_(e_usingsid));
23530 goto theend;
23531 }
23532 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23533 lead += (int)STRLEN(sid_buf);
23534 }
23535 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023536 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023537 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023538 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023539 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023540 goto theend;
23541 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023542 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023543 {
23544 char_u *cp = vim_strchr(lv.ll_name, ':');
23545
23546 if (cp != NULL && cp < end)
23547 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023548 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023549 goto theend;
23550 }
23551 }
23552
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023553 name = alloc((unsigned)(len + lead + 1));
23554 if (name != NULL)
23555 {
23556 if (lead > 0)
23557 {
23558 name[0] = K_SPECIAL;
23559 name[1] = KS_EXTRA;
23560 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023561 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023562 STRCPY(name + 3, sid_buf);
23563 }
23564 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023565 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023566 }
23567 *pp = end;
23568
23569theend:
23570 clear_lval(&lv);
23571 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023572}
23573
23574/*
23575 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23576 * Return 2 if "p" starts with "s:".
23577 * Return 0 otherwise.
23578 */
23579 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023580eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023581{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023582 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23583 * the standard library function. */
23584 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23585 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023586 return 5;
23587 if (p[0] == 's' && p[1] == ':')
23588 return 2;
23589 return 0;
23590}
23591
23592/*
23593 * Return TRUE if "p" starts with "<SID>" or "s:".
23594 * Only works if eval_fname_script() returned non-zero for "p"!
23595 */
23596 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023597eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023598{
23599 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23600}
23601
23602/*
23603 * List the head of the function: "name(arg1, arg2)".
23604 */
23605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023606list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023607{
23608 int j;
23609
23610 msg_start();
23611 if (indent)
23612 MSG_PUTS(" ");
23613 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023614 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023615 {
23616 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023617 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023618 }
23619 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023620 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023621 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023622 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023623 {
23624 if (j)
23625 MSG_PUTS(", ");
23626 msg_puts(FUNCARG(fp, j));
23627 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023628 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023629 {
23630 if (j)
23631 MSG_PUTS(", ");
23632 MSG_PUTS("...");
23633 }
23634 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023635 if (fp->uf_flags & FC_ABORT)
23636 MSG_PUTS(" abort");
23637 if (fp->uf_flags & FC_RANGE)
23638 MSG_PUTS(" range");
23639 if (fp->uf_flags & FC_DICT)
23640 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023641 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023642 if (p_verbose > 0)
23643 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023644}
23645
23646/*
23647 * Find a function by name, return pointer to it in ufuncs.
23648 * Return NULL for unknown function.
23649 */
23650 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023651find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023652{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023653 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023654
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023655 hi = hash_find(&func_hashtab, name);
23656 if (!HASHITEM_EMPTY(hi))
23657 return HI2UF(hi);
23658 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023659}
23660
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023661#if defined(EXITFREE) || defined(PROTO)
23662 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023663free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023664{
23665 hashitem_T *hi;
23666
23667 /* Need to start all over every time, because func_free() may change the
23668 * hash table. */
23669 while (func_hashtab.ht_used > 0)
23670 for (hi = func_hashtab.ht_array; ; ++hi)
23671 if (!HASHITEM_EMPTY(hi))
23672 {
23673 func_free(HI2UF(hi));
23674 break;
23675 }
23676}
23677#endif
23678
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023679 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023680translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023681{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023682 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023683 return find_internal_func(name) >= 0;
23684 return find_func(name) != NULL;
23685}
23686
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023687/*
23688 * Return TRUE if a function "name" exists.
23689 */
23690 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023691function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023692{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023693 char_u *nm = name;
23694 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023695 int n = FALSE;
23696
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023697 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23698 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023699 nm = skipwhite(nm);
23700
23701 /* Only accept "funcname", "funcname ", "funcname (..." and
23702 * "funcname(...", not "funcname!...". */
23703 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023704 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023705 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023706 return n;
23707}
23708
Bram Moolenaara1544c02013-05-30 12:35:52 +020023709 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023710get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023711{
23712 char_u *nm = name;
23713 char_u *p;
23714
23715 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23716
23717 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023718 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023719 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023720
Bram Moolenaara1544c02013-05-30 12:35:52 +020023721 vim_free(p);
23722 return NULL;
23723}
23724
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023725/*
23726 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023727 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23728 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023729 */
23730 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023731builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023732{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023733 char_u *p;
23734
23735 if (!ASCII_ISLOWER(name[0]))
23736 return FALSE;
23737 p = vim_strchr(name, AUTOLOAD_CHAR);
23738 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023739}
23740
Bram Moolenaar05159a02005-02-26 23:04:13 +000023741#if defined(FEAT_PROFILE) || defined(PROTO)
23742/*
23743 * Start profiling function "fp".
23744 */
23745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023746func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023747{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023748 int len = fp->uf_lines.ga_len;
23749
23750 if (len == 0)
23751 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023752 fp->uf_tm_count = 0;
23753 profile_zero(&fp->uf_tm_self);
23754 profile_zero(&fp->uf_tm_total);
23755 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023756 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023757 if (fp->uf_tml_total == NULL)
23758 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023759 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023760 if (fp->uf_tml_self == NULL)
23761 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023762 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023763 fp->uf_tml_idx = -1;
23764 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23765 || fp->uf_tml_self == NULL)
23766 return; /* out of memory */
23767
23768 fp->uf_profiling = TRUE;
23769}
23770
23771/*
23772 * Dump the profiling results for all functions in file "fd".
23773 */
23774 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023775func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023776{
23777 hashitem_T *hi;
23778 int todo;
23779 ufunc_T *fp;
23780 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023781 ufunc_T **sorttab;
23782 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023783
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023784 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023785 if (todo == 0)
23786 return; /* nothing to dump */
23787
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023788 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023789
Bram Moolenaar05159a02005-02-26 23:04:13 +000023790 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23791 {
23792 if (!HASHITEM_EMPTY(hi))
23793 {
23794 --todo;
23795 fp = HI2UF(hi);
23796 if (fp->uf_profiling)
23797 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023798 if (sorttab != NULL)
23799 sorttab[st_len++] = fp;
23800
Bram Moolenaar05159a02005-02-26 23:04:13 +000023801 if (fp->uf_name[0] == K_SPECIAL)
23802 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23803 else
23804 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23805 if (fp->uf_tm_count == 1)
23806 fprintf(fd, "Called 1 time\n");
23807 else
23808 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23809 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23810 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23811 fprintf(fd, "\n");
23812 fprintf(fd, "count total (s) self (s)\n");
23813
23814 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23815 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023816 if (FUNCLINE(fp, i) == NULL)
23817 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023818 prof_func_line(fd, fp->uf_tml_count[i],
23819 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023820 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23821 }
23822 fprintf(fd, "\n");
23823 }
23824 }
23825 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023826
23827 if (sorttab != NULL && st_len > 0)
23828 {
23829 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23830 prof_total_cmp);
23831 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23832 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23833 prof_self_cmp);
23834 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23835 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023836
23837 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023838}
Bram Moolenaar73830342005-02-28 22:48:19 +000023839
23840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023841prof_sort_list(
23842 FILE *fd,
23843 ufunc_T **sorttab,
23844 int st_len,
23845 char *title,
23846 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023847{
23848 int i;
23849 ufunc_T *fp;
23850
23851 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23852 fprintf(fd, "count total (s) self (s) function\n");
23853 for (i = 0; i < 20 && i < st_len; ++i)
23854 {
23855 fp = sorttab[i];
23856 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23857 prefer_self);
23858 if (fp->uf_name[0] == K_SPECIAL)
23859 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23860 else
23861 fprintf(fd, " %s()\n", fp->uf_name);
23862 }
23863 fprintf(fd, "\n");
23864}
23865
23866/*
23867 * Print the count and times for one function or function line.
23868 */
23869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023870prof_func_line(
23871 FILE *fd,
23872 int count,
23873 proftime_T *total,
23874 proftime_T *self,
23875 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023876{
23877 if (count > 0)
23878 {
23879 fprintf(fd, "%5d ", count);
23880 if (prefer_self && profile_equal(total, self))
23881 fprintf(fd, " ");
23882 else
23883 fprintf(fd, "%s ", profile_msg(total));
23884 if (!prefer_self && profile_equal(total, self))
23885 fprintf(fd, " ");
23886 else
23887 fprintf(fd, "%s ", profile_msg(self));
23888 }
23889 else
23890 fprintf(fd, " ");
23891}
23892
23893/*
23894 * Compare function for total time sorting.
23895 */
23896 static int
23897#ifdef __BORLANDC__
23898_RTLENTRYF
23899#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023900prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023901{
23902 ufunc_T *p1, *p2;
23903
23904 p1 = *(ufunc_T **)s1;
23905 p2 = *(ufunc_T **)s2;
23906 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23907}
23908
23909/*
23910 * Compare function for self time sorting.
23911 */
23912 static int
23913#ifdef __BORLANDC__
23914_RTLENTRYF
23915#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023916prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023917{
23918 ufunc_T *p1, *p2;
23919
23920 p1 = *(ufunc_T **)s1;
23921 p2 = *(ufunc_T **)s2;
23922 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23923}
23924
Bram Moolenaar05159a02005-02-26 23:04:13 +000023925#endif
23926
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023927/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023928 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023929 * Return TRUE if a package was loaded.
23930 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023931 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023932script_autoload(
23933 char_u *name,
23934 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023935{
23936 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023937 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023938 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023939 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023940
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023941 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023942 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023943 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023944 return FALSE;
23945
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023946 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023947
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023948 /* Find the name in the list of previously loaded package names. Skip
23949 * "autoload/", it's always the same. */
23950 for (i = 0; i < ga_loaded.ga_len; ++i)
23951 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23952 break;
23953 if (!reload && i < ga_loaded.ga_len)
23954 ret = FALSE; /* was loaded already */
23955 else
23956 {
23957 /* Remember the name if it wasn't loaded already. */
23958 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23959 {
23960 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23961 tofree = NULL;
23962 }
23963
23964 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023965 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023966 ret = TRUE;
23967 }
23968
23969 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023970 return ret;
23971}
23972
23973/*
23974 * Return the autoload script name for a function or variable name.
23975 * Returns NULL when out of memory.
23976 */
23977 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023978autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023979{
23980 char_u *p;
23981 char_u *scriptname;
23982
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023983 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023984 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23985 if (scriptname == NULL)
23986 return FALSE;
23987 STRCPY(scriptname, "autoload/");
23988 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023989 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023990 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023991 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023992 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023993 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023994}
23995
Bram Moolenaar071d4272004-06-13 20:20:40 +000023996#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23997
23998/*
23999 * Function given to ExpandGeneric() to obtain the list of user defined
24000 * function names.
24001 */
24002 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024003get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024004{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024005 static long_u done;
24006 static hashitem_T *hi;
24007 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008
24009 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024010 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024011 done = 0;
24012 hi = func_hashtab.ht_array;
24013 }
24014 if (done < func_hashtab.ht_used)
24015 {
24016 if (done++ > 0)
24017 ++hi;
24018 while (HASHITEM_EMPTY(hi))
24019 ++hi;
24020 fp = HI2UF(hi);
24021
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024022 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024023 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024024
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024025 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24026 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024027
24028 cat_func_name(IObuff, fp);
24029 if (xp->xp_context != EXPAND_USER_FUNC)
24030 {
24031 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024032 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024033 STRCAT(IObuff, ")");
24034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024035 return IObuff;
24036 }
24037 return NULL;
24038}
24039
24040#endif /* FEAT_CMDL_COMPL */
24041
24042/*
24043 * Copy the function name of "fp" to buffer "buf".
24044 * "buf" must be able to hold the function name plus three bytes.
24045 * Takes care of script-local function names.
24046 */
24047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024048cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024049{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024050 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024051 {
24052 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024053 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024054 }
24055 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024056 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024057}
24058
24059/*
24060 * ":delfunction {name}"
24061 */
24062 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024063ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024064{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024065 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024066 char_u *p;
24067 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024068 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024069
24070 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024071 name = trans_function_name(&p, eap->skip, 0, &fudi);
24072 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024073 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024074 {
24075 if (fudi.fd_dict != NULL && !eap->skip)
24076 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024077 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024079 if (!ends_excmd(*skipwhite(p)))
24080 {
24081 vim_free(name);
24082 EMSG(_(e_trailing));
24083 return;
24084 }
24085 eap->nextcmd = check_nextcmd(p);
24086 if (eap->nextcmd != NULL)
24087 *p = NUL;
24088
24089 if (!eap->skip)
24090 fp = find_func(name);
24091 vim_free(name);
24092
24093 if (!eap->skip)
24094 {
24095 if (fp == NULL)
24096 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024097 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024098 return;
24099 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024100 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024101 {
24102 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24103 return;
24104 }
24105
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024106 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024107 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024108 /* Delete the dict item that refers to the function, it will
24109 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024110 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024111 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024112 else
24113 func_free(fp);
24114 }
24115}
24116
24117/*
24118 * Free a function and remove it from the list of functions.
24119 */
24120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024121func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024122{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024123 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024124
24125 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024126 ga_clear_strings(&(fp->uf_args));
24127 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024128#ifdef FEAT_PROFILE
24129 vim_free(fp->uf_tml_count);
24130 vim_free(fp->uf_tml_total);
24131 vim_free(fp->uf_tml_self);
24132#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024133
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024134 /* remove the function from the function hashtable */
24135 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24136 if (HASHITEM_EMPTY(hi))
24137 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024138 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024139 hash_remove(&func_hashtab, hi);
24140
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024141 vim_free(fp);
24142}
24143
24144/*
24145 * Unreference a Function: decrement the reference count and free it when it
24146 * becomes zero. Only for numbered functions.
24147 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024148 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024149func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024150{
24151 ufunc_T *fp;
24152
24153 if (name != NULL && isdigit(*name))
24154 {
24155 fp = find_func(name);
24156 if (fp == NULL)
24157 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024158 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024159 {
24160 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024161 * when "uf_calls" becomes zero. */
24162 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024163 func_free(fp);
24164 }
24165 }
24166}
24167
24168/*
24169 * Count a reference to a Function.
24170 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024171 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024172func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024173{
24174 ufunc_T *fp;
24175
24176 if (name != NULL && isdigit(*name))
24177 {
24178 fp = find_func(name);
24179 if (fp == NULL)
24180 EMSG2(_(e_intern2), "func_ref()");
24181 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024182 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024183 }
24184}
24185
24186/*
24187 * Call a user function.
24188 */
24189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024190call_user_func(
24191 ufunc_T *fp, /* pointer to function */
24192 int argcount, /* nr of args */
24193 typval_T *argvars, /* arguments */
24194 typval_T *rettv, /* return value */
24195 linenr_T firstline, /* first line of range */
24196 linenr_T lastline, /* last line of range */
24197 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024198{
Bram Moolenaar33570922005-01-25 22:26:29 +000024199 char_u *save_sourcing_name;
24200 linenr_T save_sourcing_lnum;
24201 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024202 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024203 int save_did_emsg;
24204 static int depth = 0;
24205 dictitem_T *v;
24206 int fixvar_idx = 0; /* index in fixvar[] */
24207 int i;
24208 int ai;
24209 char_u numbuf[NUMBUFLEN];
24210 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024211 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024212#ifdef FEAT_PROFILE
24213 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024214 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024216
24217 /* If depth of calling is getting too high, don't execute the function */
24218 if (depth >= p_mfd)
24219 {
24220 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024221 rettv->v_type = VAR_NUMBER;
24222 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024223 return;
24224 }
24225 ++depth;
24226
24227 line_breakcheck(); /* check for CTRL-C hit */
24228
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024229 fc = (funccall_T *)alloc(sizeof(funccall_T));
24230 fc->caller = current_funccal;
24231 current_funccal = fc;
24232 fc->func = fp;
24233 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024234 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024235 fc->linenr = 0;
24236 fc->returned = FALSE;
24237 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024238 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024239 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24240 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024241
Bram Moolenaar33570922005-01-25 22:26:29 +000024242 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024243 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024244 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24245 * each argument variable and saves a lot of time.
24246 */
24247 /*
24248 * Init l: variables.
24249 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024250 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024251 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024252 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024253 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24254 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024255 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024256 name = v->di_key;
24257 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024258 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024259 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024260 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024261 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024262 v->di_tv.vval.v_dict = selfdict;
24263 ++selfdict->dv_refcount;
24264 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024265
Bram Moolenaar33570922005-01-25 22:26:29 +000024266 /*
24267 * Init a: variables.
24268 * Set a:0 to "argcount".
24269 * Set a:000 to a list with room for the "..." arguments.
24270 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024271 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024272 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024273 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024274 /* Use "name" to avoid a warning from some compiler that checks the
24275 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024276 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024277 name = v->di_key;
24278 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024279 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024280 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024281 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024282 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024283 v->di_tv.vval.v_list = &fc->l_varlist;
24284 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24285 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24286 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024287
24288 /*
24289 * Set a:firstline to "firstline" and a:lastline to "lastline".
24290 * Set a:name to named arguments.
24291 * Set a:N to the "..." arguments.
24292 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024293 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024294 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024295 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024296 (varnumber_T)lastline);
24297 for (i = 0; i < argcount; ++i)
24298 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024299 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024300 if (ai < 0)
24301 /* named argument a:name */
24302 name = FUNCARG(fp, i);
24303 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024304 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024305 /* "..." argument a:1, a:2, etc. */
24306 sprintf((char *)numbuf, "%d", ai + 1);
24307 name = numbuf;
24308 }
24309 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24310 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024311 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024312 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24313 }
24314 else
24315 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024316 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24317 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024318 if (v == NULL)
24319 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024320 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024321 }
24322 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024323 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024324
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024325 /* Note: the values are copied directly to avoid alloc/free.
24326 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024327 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024328 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024329
24330 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24331 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024332 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24333 fc->l_listitems[ai].li_tv = argvars[i];
24334 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024335 }
24336 }
24337
Bram Moolenaar071d4272004-06-13 20:20:40 +000024338 /* Don't redraw while executing the function. */
24339 ++RedrawingDisabled;
24340 save_sourcing_name = sourcing_name;
24341 save_sourcing_lnum = sourcing_lnum;
24342 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024343 /* need space for function name + ("function " + 3) or "[number]" */
24344 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24345 + STRLEN(fp->uf_name) + 20;
24346 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024347 if (sourcing_name != NULL)
24348 {
24349 if (save_sourcing_name != NULL
24350 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024351 sprintf((char *)sourcing_name, "%s[%d]..",
24352 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024353 else
24354 STRCPY(sourcing_name, "function ");
24355 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24356
24357 if (p_verbose >= 12)
24358 {
24359 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024360 verbose_enter_scroll();
24361
Bram Moolenaar555b2802005-05-19 21:08:39 +000024362 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024363 if (p_verbose >= 14)
24364 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024365 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024366 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024367 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024368 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024369
24370 msg_puts((char_u *)"(");
24371 for (i = 0; i < argcount; ++i)
24372 {
24373 if (i > 0)
24374 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024375 if (argvars[i].v_type == VAR_NUMBER)
24376 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024377 else
24378 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024379 /* Do not want errors such as E724 here. */
24380 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024381 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024382 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024383 if (s != NULL)
24384 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024385 if (vim_strsize(s) > MSG_BUF_CLEN)
24386 {
24387 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24388 s = buf;
24389 }
24390 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024391 vim_free(tofree);
24392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024393 }
24394 }
24395 msg_puts((char_u *)")");
24396 }
24397 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024398
24399 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024400 --no_wait_return;
24401 }
24402 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024403#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024404 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024405 {
24406 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24407 func_do_profile(fp);
24408 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024409 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024410 {
24411 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024412 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024413 profile_zero(&fp->uf_tm_children);
24414 }
24415 script_prof_save(&wait_start);
24416 }
24417#endif
24418
Bram Moolenaar071d4272004-06-13 20:20:40 +000024419 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024420 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024421 save_did_emsg = did_emsg;
24422 did_emsg = FALSE;
24423
24424 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024425 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024426 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24427
24428 --RedrawingDisabled;
24429
24430 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024431 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024432 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024433 clear_tv(rettv);
24434 rettv->v_type = VAR_NUMBER;
24435 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024436 }
24437
Bram Moolenaar05159a02005-02-26 23:04:13 +000024438#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024439 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024440 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024441 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024442 profile_end(&call_start);
24443 profile_sub_wait(&wait_start, &call_start);
24444 profile_add(&fp->uf_tm_total, &call_start);
24445 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024446 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024447 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024448 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24449 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024450 }
24451 }
24452#endif
24453
Bram Moolenaar071d4272004-06-13 20:20:40 +000024454 /* when being verbose, mention the return value */
24455 if (p_verbose >= 12)
24456 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024457 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024458 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024459
Bram Moolenaar071d4272004-06-13 20:20:40 +000024460 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024461 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024462 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024463 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024464 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024465 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024466 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024467 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024468 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024469 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024470 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024471
Bram Moolenaar555b2802005-05-19 21:08:39 +000024472 /* The value may be very long. Skip the middle part, so that we
24473 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024474 * truncate it at the end. Don't want errors such as E724 here. */
24475 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024476 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024477 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024478 if (s != NULL)
24479 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024480 if (vim_strsize(s) > MSG_BUF_CLEN)
24481 {
24482 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24483 s = buf;
24484 }
24485 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024486 vim_free(tofree);
24487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024488 }
24489 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024490
24491 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024492 --no_wait_return;
24493 }
24494
24495 vim_free(sourcing_name);
24496 sourcing_name = save_sourcing_name;
24497 sourcing_lnum = save_sourcing_lnum;
24498 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024499#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024500 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024501 script_prof_restore(&wait_start);
24502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024503
24504 if (p_verbose >= 12 && sourcing_name != NULL)
24505 {
24506 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024507 verbose_enter_scroll();
24508
Bram Moolenaar555b2802005-05-19 21:08:39 +000024509 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024510 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024511
24512 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024513 --no_wait_return;
24514 }
24515
24516 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024517 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024518 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024519
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024520 /* If the a:000 list and the l: and a: dicts are not referenced we can
24521 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024522 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24523 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24524 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24525 {
24526 free_funccal(fc, FALSE);
24527 }
24528 else
24529 {
24530 hashitem_T *hi;
24531 listitem_T *li;
24532 int todo;
24533
24534 /* "fc" is still in use. This can happen when returning "a:000" or
24535 * assigning "l:" to a global variable.
24536 * Link "fc" in the list for garbage collection later. */
24537 fc->caller = previous_funccal;
24538 previous_funccal = fc;
24539
24540 /* Make a copy of the a: variables, since we didn't do that above. */
24541 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24542 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24543 {
24544 if (!HASHITEM_EMPTY(hi))
24545 {
24546 --todo;
24547 v = HI2DI(hi);
24548 copy_tv(&v->di_tv, &v->di_tv);
24549 }
24550 }
24551
24552 /* Make a copy of the a:000 items, since we didn't do that above. */
24553 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24554 copy_tv(&li->li_tv, &li->li_tv);
24555 }
24556}
24557
24558/*
24559 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024560 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024561 */
24562 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024563can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024564{
24565 return (fc->l_varlist.lv_copyID != copyID
24566 && fc->l_vars.dv_copyID != copyID
24567 && fc->l_avars.dv_copyID != copyID);
24568}
24569
24570/*
24571 * Free "fc" and what it contains.
24572 */
24573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024574free_funccal(
24575 funccall_T *fc,
24576 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024577{
24578 listitem_T *li;
24579
24580 /* The a: variables typevals may not have been allocated, only free the
24581 * allocated variables. */
24582 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24583
24584 /* free all l: variables */
24585 vars_clear(&fc->l_vars.dv_hashtab);
24586
24587 /* Free the a:000 variables if they were allocated. */
24588 if (free_val)
24589 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24590 clear_tv(&li->li_tv);
24591
24592 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024593}
24594
24595/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024596 * Add a number variable "name" to dict "dp" with value "nr".
24597 */
24598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024599add_nr_var(
24600 dict_T *dp,
24601 dictitem_T *v,
24602 char *name,
24603 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024604{
24605 STRCPY(v->di_key, name);
24606 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24607 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24608 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024609 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024610 v->di_tv.vval.v_number = nr;
24611}
24612
24613/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024614 * ":return [expr]"
24615 */
24616 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024617ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024618{
24619 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024620 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024621 int returning = FALSE;
24622
24623 if (current_funccal == NULL)
24624 {
24625 EMSG(_("E133: :return not inside a function"));
24626 return;
24627 }
24628
24629 if (eap->skip)
24630 ++emsg_skip;
24631
24632 eap->nextcmd = NULL;
24633 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024634 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024635 {
24636 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024637 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024638 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024639 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024640 }
24641 /* It's safer to return also on error. */
24642 else if (!eap->skip)
24643 {
24644 /*
24645 * Return unless the expression evaluation has been cancelled due to an
24646 * aborting error, an interrupt, or an exception.
24647 */
24648 if (!aborting())
24649 returning = do_return(eap, FALSE, TRUE, NULL);
24650 }
24651
24652 /* When skipping or the return gets pending, advance to the next command
24653 * in this line (!returning). Otherwise, ignore the rest of the line.
24654 * Following lines will be ignored by get_func_line(). */
24655 if (returning)
24656 eap->nextcmd = NULL;
24657 else if (eap->nextcmd == NULL) /* no argument */
24658 eap->nextcmd = check_nextcmd(arg);
24659
24660 if (eap->skip)
24661 --emsg_skip;
24662}
24663
24664/*
24665 * Return from a function. Possibly makes the return pending. Also called
24666 * for a pending return at the ":endtry" or after returning from an extra
24667 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024668 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024669 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024670 * FALSE when the return gets pending.
24671 */
24672 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024673do_return(
24674 exarg_T *eap,
24675 int reanimate,
24676 int is_cmd,
24677 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024678{
24679 int idx;
24680 struct condstack *cstack = eap->cstack;
24681
24682 if (reanimate)
24683 /* Undo the return. */
24684 current_funccal->returned = FALSE;
24685
24686 /*
24687 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24688 * not in its finally clause (which then is to be executed next) is found.
24689 * In this case, make the ":return" pending for execution at the ":endtry".
24690 * Otherwise, return normally.
24691 */
24692 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24693 if (idx >= 0)
24694 {
24695 cstack->cs_pending[idx] = CSTP_RETURN;
24696
24697 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024698 /* A pending return again gets pending. "rettv" points to an
24699 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024700 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024701 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024702 else
24703 {
24704 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024705 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024706 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024707 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024708
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024709 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024710 {
24711 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024712 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024713 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024714 else
24715 EMSG(_(e_outofmem));
24716 }
24717 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024718 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024719
24720 if (reanimate)
24721 {
24722 /* The pending return value could be overwritten by a ":return"
24723 * without argument in a finally clause; reset the default
24724 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024725 current_funccal->rettv->v_type = VAR_NUMBER;
24726 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024727 }
24728 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024729 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024730 }
24731 else
24732 {
24733 current_funccal->returned = TRUE;
24734
24735 /* If the return is carried out now, store the return value. For
24736 * a return immediately after reanimation, the value is already
24737 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024738 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024739 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024740 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024741 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024742 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024743 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024744 }
24745 }
24746
24747 return idx < 0;
24748}
24749
24750/*
24751 * Free the variable with a pending return value.
24752 */
24753 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024754discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024755{
Bram Moolenaar33570922005-01-25 22:26:29 +000024756 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024757}
24758
24759/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024760 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024761 * is an allocated string. Used by report_pending() for verbose messages.
24762 */
24763 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024764get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024765{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024766 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024767 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024768 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024769
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024770 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024771 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024772 if (s == NULL)
24773 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024774
24775 STRCPY(IObuff, ":return ");
24776 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24777 if (STRLEN(s) + 8 >= IOSIZE)
24778 STRCPY(IObuff + IOSIZE - 4, "...");
24779 vim_free(tofree);
24780 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024781}
24782
24783/*
24784 * Get next function line.
24785 * Called by do_cmdline() to get the next line.
24786 * Returns allocated string, or NULL for end of function.
24787 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024788 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024789get_func_line(
24790 int c UNUSED,
24791 void *cookie,
24792 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024793{
Bram Moolenaar33570922005-01-25 22:26:29 +000024794 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024795 ufunc_T *fp = fcp->func;
24796 char_u *retval;
24797 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024798
24799 /* If breakpoints have been added/deleted need to check for it. */
24800 if (fcp->dbg_tick != debug_tick)
24801 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024802 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024803 sourcing_lnum);
24804 fcp->dbg_tick = debug_tick;
24805 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024806#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024807 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024808 func_line_end(cookie);
24809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024810
Bram Moolenaar05159a02005-02-26 23:04:13 +000024811 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024812 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24813 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024814 retval = NULL;
24815 else
24816 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024817 /* Skip NULL lines (continuation lines). */
24818 while (fcp->linenr < gap->ga_len
24819 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24820 ++fcp->linenr;
24821 if (fcp->linenr >= gap->ga_len)
24822 retval = NULL;
24823 else
24824 {
24825 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24826 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024827#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024828 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024829 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024830#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024832 }
24833
24834 /* Did we encounter a breakpoint? */
24835 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24836 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024837 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024838 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024839 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024840 sourcing_lnum);
24841 fcp->dbg_tick = debug_tick;
24842 }
24843
24844 return retval;
24845}
24846
Bram Moolenaar05159a02005-02-26 23:04:13 +000024847#if defined(FEAT_PROFILE) || defined(PROTO)
24848/*
24849 * Called when starting to read a function line.
24850 * "sourcing_lnum" must be correct!
24851 * When skipping lines it may not actually be executed, but we won't find out
24852 * until later and we need to store the time now.
24853 */
24854 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024855func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024856{
24857 funccall_T *fcp = (funccall_T *)cookie;
24858 ufunc_T *fp = fcp->func;
24859
24860 if (fp->uf_profiling && sourcing_lnum >= 1
24861 && sourcing_lnum <= fp->uf_lines.ga_len)
24862 {
24863 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024864 /* Skip continuation lines. */
24865 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24866 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024867 fp->uf_tml_execed = FALSE;
24868 profile_start(&fp->uf_tml_start);
24869 profile_zero(&fp->uf_tml_children);
24870 profile_get_wait(&fp->uf_tml_wait);
24871 }
24872}
24873
24874/*
24875 * Called when actually executing a function line.
24876 */
24877 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024878func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024879{
24880 funccall_T *fcp = (funccall_T *)cookie;
24881 ufunc_T *fp = fcp->func;
24882
24883 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24884 fp->uf_tml_execed = TRUE;
24885}
24886
24887/*
24888 * Called when done with a function line.
24889 */
24890 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024891func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024892{
24893 funccall_T *fcp = (funccall_T *)cookie;
24894 ufunc_T *fp = fcp->func;
24895
24896 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24897 {
24898 if (fp->uf_tml_execed)
24899 {
24900 ++fp->uf_tml_count[fp->uf_tml_idx];
24901 profile_end(&fp->uf_tml_start);
24902 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024903 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024904 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24905 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024906 }
24907 fp->uf_tml_idx = -1;
24908 }
24909}
24910#endif
24911
Bram Moolenaar071d4272004-06-13 20:20:40 +000024912/*
24913 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024914 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024915 */
24916 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024917func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024918{
Bram Moolenaar33570922005-01-25 22:26:29 +000024919 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024920
24921 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24922 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024923 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024924 || fcp->returned);
24925}
24926
24927/*
24928 * return TRUE if cookie indicates a function which "abort"s on errors.
24929 */
24930 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024931func_has_abort(
24932 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024933{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024934 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024935}
24936
24937#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24938typedef enum
24939{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024940 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24941 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24942 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024943} var_flavour_T;
24944
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024945static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024946
24947 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010024948var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024949{
24950 char_u *p = varname;
24951
24952 if (ASCII_ISUPPER(*p))
24953 {
24954 while (*(++p))
24955 if (ASCII_ISLOWER(*p))
24956 return VAR_FLAVOUR_SESSION;
24957 return VAR_FLAVOUR_VIMINFO;
24958 }
24959 else
24960 return VAR_FLAVOUR_DEFAULT;
24961}
24962#endif
24963
24964#if defined(FEAT_VIMINFO) || defined(PROTO)
24965/*
24966 * Restore global vars that start with a capital from the viminfo file
24967 */
24968 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024969read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024970{
24971 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024972 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024973 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024974 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024975
24976 if (!writing && (find_viminfo_parameter('!') != NULL))
24977 {
24978 tab = vim_strchr(virp->vir_line + 1, '\t');
24979 if (tab != NULL)
24980 {
24981 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024982 switch (*tab)
24983 {
24984 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024985#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024986 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024987#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024988 case 'D': type = VAR_DICT; break;
24989 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010024990 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024992
24993 tab = vim_strchr(tab, '\t');
24994 if (tab != NULL)
24995 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024996 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024997 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024998 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024999 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025000#ifdef FEAT_FLOAT
25001 else if (type == VAR_FLOAT)
25002 (void)string2float(tab + 1, &tv.vval.v_float);
25003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025004 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025005 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025006 if (type == VAR_DICT || type == VAR_LIST)
25007 {
25008 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25009
25010 if (etv == NULL)
25011 /* Failed to parse back the dict or list, use it as a
25012 * string. */
25013 tv.v_type = VAR_STRING;
25014 else
25015 {
25016 vim_free(tv.vval.v_string);
25017 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025018 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025019 }
25020 }
25021
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025022 /* when in a function use global variables */
25023 save_funccal = current_funccal;
25024 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025025 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025026 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025027
25028 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025029 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025030 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25031 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025032 }
25033 }
25034 }
25035
25036 return viminfo_readline(virp);
25037}
25038
25039/*
25040 * Write global vars that start with a capital to the viminfo file
25041 */
25042 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025043write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025044{
Bram Moolenaar33570922005-01-25 22:26:29 +000025045 hashitem_T *hi;
25046 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025047 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025048 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025049 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025050 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025051 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025052
25053 if (find_viminfo_parameter('!') == NULL)
25054 return;
25055
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025056 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025057
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025058 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025059 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025060 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025061 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025062 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025063 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025064 this_var = HI2DI(hi);
25065 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025066 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025067 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025068 {
25069 case VAR_STRING: s = "STR"; break;
25070 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025071 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025072 case VAR_DICT: s = "DIC"; break;
25073 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025074 case VAR_SPECIAL: s = "XPL"; break;
25075
25076 case VAR_UNKNOWN:
25077 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025078 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025079 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025080 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025081 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025082 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025083 if (p != NULL)
25084 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025085 vim_free(tofree);
25086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025087 }
25088 }
25089}
25090#endif
25091
25092#if defined(FEAT_SESSION) || defined(PROTO)
25093 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025094store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025095{
Bram Moolenaar33570922005-01-25 22:26:29 +000025096 hashitem_T *hi;
25097 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025098 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025099 char_u *p, *t;
25100
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025101 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025102 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025103 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025104 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025105 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025106 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025107 this_var = HI2DI(hi);
25108 if ((this_var->di_tv.v_type == VAR_NUMBER
25109 || this_var->di_tv.v_type == VAR_STRING)
25110 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025111 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025112 /* Escape special characters with a backslash. Turn a LF and
25113 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025114 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025115 (char_u *)"\\\"\n\r");
25116 if (p == NULL) /* out of memory */
25117 break;
25118 for (t = p; *t != NUL; ++t)
25119 if (*t == '\n')
25120 *t = 'n';
25121 else if (*t == '\r')
25122 *t = 'r';
25123 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025124 this_var->di_key,
25125 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25126 : ' ',
25127 p,
25128 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25129 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025130 || put_eol(fd) == FAIL)
25131 {
25132 vim_free(p);
25133 return FAIL;
25134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025135 vim_free(p);
25136 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025137#ifdef FEAT_FLOAT
25138 else if (this_var->di_tv.v_type == VAR_FLOAT
25139 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25140 {
25141 float_T f = this_var->di_tv.vval.v_float;
25142 int sign = ' ';
25143
25144 if (f < 0)
25145 {
25146 f = -f;
25147 sign = '-';
25148 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025149 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025150 this_var->di_key, sign, f) < 0)
25151 || put_eol(fd) == FAIL)
25152 return FAIL;
25153 }
25154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025155 }
25156 }
25157 return OK;
25158}
25159#endif
25160
Bram Moolenaar661b1822005-07-28 22:36:45 +000025161/*
25162 * Display script name where an item was last set.
25163 * Should only be invoked when 'verbose' is non-zero.
25164 */
25165 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025166last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025167{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025168 char_u *p;
25169
Bram Moolenaar661b1822005-07-28 22:36:45 +000025170 if (scriptID != 0)
25171 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025172 p = home_replace_save(NULL, get_scriptname(scriptID));
25173 if (p != NULL)
25174 {
25175 verbose_enter();
25176 MSG_PUTS(_("\n\tLast set from "));
25177 MSG_PUTS(p);
25178 vim_free(p);
25179 verbose_leave();
25180 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025181 }
25182}
25183
Bram Moolenaard812df62008-11-09 12:46:09 +000025184/*
25185 * List v:oldfiles in a nice way.
25186 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025187 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025188ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025189{
25190 list_T *l = vimvars[VV_OLDFILES].vv_list;
25191 listitem_T *li;
25192 int nr = 0;
25193
25194 if (l == NULL)
25195 msg((char_u *)_("No old files"));
25196 else
25197 {
25198 msg_start();
25199 msg_scroll = TRUE;
25200 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25201 {
25202 msg_outnum((long)++nr);
25203 MSG_PUTS(": ");
25204 msg_outtrans(get_tv_string(&li->li_tv));
25205 msg_putchar('\n');
25206 out_flush(); /* output one line at a time */
25207 ui_breakcheck();
25208 }
25209 /* Assume "got_int" was set to truncate the listing. */
25210 got_int = FALSE;
25211
25212#ifdef FEAT_BROWSE_CMD
25213 if (cmdmod.browse)
25214 {
25215 quit_more = FALSE;
25216 nr = prompt_for_number(FALSE);
25217 msg_starthere();
25218 if (nr > 0)
25219 {
25220 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25221 (long)nr);
25222
25223 if (p != NULL)
25224 {
25225 p = expand_env_save(p);
25226 eap->arg = p;
25227 eap->cmdidx = CMD_edit;
25228 cmdmod.browse = FALSE;
25229 do_exedit(eap, NULL);
25230 vim_free(p);
25231 }
25232 }
25233 }
25234#endif
25235 }
25236}
25237
Bram Moolenaar53744302015-07-17 17:38:22 +020025238/* reset v:option_new, v:option_old and v:option_type */
25239 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025240reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025241{
25242 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25243 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25244 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25245}
25246
25247
Bram Moolenaar071d4272004-06-13 20:20:40 +000025248#endif /* FEAT_EVAL */
25249
Bram Moolenaar071d4272004-06-13 20:20:40 +000025250
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025251#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025252
25253#ifdef WIN3264
25254/*
25255 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25256 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025257static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25258static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25259static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025260
25261/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025262 * Get the short path (8.3) for the filename in "fnamep".
25263 * Only works for a valid file name.
25264 * When the path gets longer "fnamep" is changed and the allocated buffer
25265 * is put in "bufp".
25266 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25267 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025268 */
25269 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025270get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025271{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025272 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025273 char_u *newbuf;
25274
25275 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025276 l = GetShortPathName(*fnamep, *fnamep, len);
25277 if (l > len - 1)
25278 {
25279 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025280 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025281 newbuf = vim_strnsave(*fnamep, l);
25282 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025283 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025284
25285 vim_free(*bufp);
25286 *fnamep = *bufp = newbuf;
25287
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025288 /* Really should always succeed, as the buffer is big enough. */
25289 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025290 }
25291
25292 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025293 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025294}
25295
25296/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025297 * Get the short path (8.3) for the filename in "fname". The converted
25298 * path is returned in "bufp".
25299 *
25300 * Some of the directories specified in "fname" may not exist. This function
25301 * will shorten the existing directories at the beginning of the path and then
25302 * append the remaining non-existing path.
25303 *
25304 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025305 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025306 * bufp - Pointer to an allocated buffer for the filename.
25307 * fnamelen - Length of the filename pointed to by fname
25308 *
25309 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025310 */
25311 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025312shortpath_for_invalid_fname(
25313 char_u **fname,
25314 char_u **bufp,
25315 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025316{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025317 char_u *short_fname, *save_fname, *pbuf_unused;
25318 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025319 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025320 int old_len, len;
25321 int new_len, sfx_len;
25322 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025323
25324 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025325 old_len = *fnamelen;
25326 save_fname = vim_strnsave(*fname, old_len);
25327 pbuf_unused = NULL;
25328 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025329
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025330 endp = save_fname + old_len - 1; /* Find the end of the copy */
25331 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025332
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025333 /*
25334 * Try shortening the supplied path till it succeeds by removing one
25335 * directory at a time from the tail of the path.
25336 */
25337 len = 0;
25338 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025339 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025340 /* go back one path-separator */
25341 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25342 --endp;
25343 if (endp <= save_fname)
25344 break; /* processed the complete path */
25345
25346 /*
25347 * Replace the path separator with a NUL and try to shorten the
25348 * resulting path.
25349 */
25350 ch = *endp;
25351 *endp = 0;
25352 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025353 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025354 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25355 {
25356 retval = FAIL;
25357 goto theend;
25358 }
25359 *endp = ch; /* preserve the string */
25360
25361 if (len > 0)
25362 break; /* successfully shortened the path */
25363
25364 /* failed to shorten the path. Skip the path separator */
25365 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025366 }
25367
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025368 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025369 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025370 /*
25371 * Succeeded in shortening the path. Now concatenate the shortened
25372 * path with the remaining path at the tail.
25373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025374
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025375 /* Compute the length of the new path. */
25376 sfx_len = (int)(save_endp - endp) + 1;
25377 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025378
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025379 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025380 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025381 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025382 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025383 /* There is not enough space in the currently allocated string,
25384 * copy it to a buffer big enough. */
25385 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025386 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025387 {
25388 retval = FAIL;
25389 goto theend;
25390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025391 }
25392 else
25393 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025394 /* Transfer short_fname to the main buffer (it's big enough),
25395 * unless get_short_pathname() did its work in-place. */
25396 *fname = *bufp = save_fname;
25397 if (short_fname != save_fname)
25398 vim_strncpy(save_fname, short_fname, len);
25399 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025400 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025401
25402 /* concat the not-shortened part of the path */
25403 vim_strncpy(*fname + len, endp, sfx_len);
25404 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025405 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025406
25407theend:
25408 vim_free(pbuf_unused);
25409 vim_free(save_fname);
25410
25411 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025412}
25413
25414/*
25415 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025416 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025417 */
25418 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025419shortpath_for_partial(
25420 char_u **fnamep,
25421 char_u **bufp,
25422 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025423{
25424 int sepcount, len, tflen;
25425 char_u *p;
25426 char_u *pbuf, *tfname;
25427 int hasTilde;
25428
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025429 /* Count up the path separators from the RHS.. so we know which part
25430 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025431 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025432 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025433 if (vim_ispathsep(*p))
25434 ++sepcount;
25435
25436 /* Need full path first (use expand_env() to remove a "~/") */
25437 hasTilde = (**fnamep == '~');
25438 if (hasTilde)
25439 pbuf = tfname = expand_env_save(*fnamep);
25440 else
25441 pbuf = tfname = FullName_save(*fnamep, FALSE);
25442
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025443 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025444
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025445 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25446 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025447
25448 if (len == 0)
25449 {
25450 /* Don't have a valid filename, so shorten the rest of the
25451 * path if we can. This CAN give us invalid 8.3 filenames, but
25452 * there's not a lot of point in guessing what it might be.
25453 */
25454 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025455 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25456 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025457 }
25458
25459 /* Count the paths backward to find the beginning of the desired string. */
25460 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025461 {
25462#ifdef FEAT_MBYTE
25463 if (has_mbyte)
25464 p -= mb_head_off(tfname, p);
25465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025466 if (vim_ispathsep(*p))
25467 {
25468 if (sepcount == 0 || (hasTilde && sepcount == 1))
25469 break;
25470 else
25471 sepcount --;
25472 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025474 if (hasTilde)
25475 {
25476 --p;
25477 if (p >= tfname)
25478 *p = '~';
25479 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025480 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025481 }
25482 else
25483 ++p;
25484
25485 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25486 vim_free(*bufp);
25487 *fnamelen = (int)STRLEN(p);
25488 *bufp = pbuf;
25489 *fnamep = p;
25490
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025491 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025492}
25493#endif /* WIN3264 */
25494
25495/*
25496 * Adjust a filename, according to a string of modifiers.
25497 * *fnamep must be NUL terminated when called. When returning, the length is
25498 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025499 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025500 * When there is an error, *fnamep is set to NULL.
25501 */
25502 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025503modify_fname(
25504 char_u *src, /* string with modifiers */
25505 int *usedlen, /* characters after src that are used */
25506 char_u **fnamep, /* file name so far */
25507 char_u **bufp, /* buffer for allocated file name or NULL */
25508 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025509{
25510 int valid = 0;
25511 char_u *tail;
25512 char_u *s, *p, *pbuf;
25513 char_u dirname[MAXPATHL];
25514 int c;
25515 int has_fullname = 0;
25516#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025517 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025518 int has_shortname = 0;
25519#endif
25520
25521repeat:
25522 /* ":p" - full path/file_name */
25523 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25524 {
25525 has_fullname = 1;
25526
25527 valid |= VALID_PATH;
25528 *usedlen += 2;
25529
25530 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25531 if ((*fnamep)[0] == '~'
25532#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25533 && ((*fnamep)[1] == '/'
25534# ifdef BACKSLASH_IN_FILENAME
25535 || (*fnamep)[1] == '\\'
25536# endif
25537 || (*fnamep)[1] == NUL)
25538
25539#endif
25540 )
25541 {
25542 *fnamep = expand_env_save(*fnamep);
25543 vim_free(*bufp); /* free any allocated file name */
25544 *bufp = *fnamep;
25545 if (*fnamep == NULL)
25546 return -1;
25547 }
25548
25549 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025550 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025551 {
25552 if (vim_ispathsep(*p)
25553 && p[1] == '.'
25554 && (p[2] == NUL
25555 || vim_ispathsep(p[2])
25556 || (p[2] == '.'
25557 && (p[3] == NUL || vim_ispathsep(p[3])))))
25558 break;
25559 }
25560
25561 /* FullName_save() is slow, don't use it when not needed. */
25562 if (*p != NUL || !vim_isAbsName(*fnamep))
25563 {
25564 *fnamep = FullName_save(*fnamep, *p != NUL);
25565 vim_free(*bufp); /* free any allocated file name */
25566 *bufp = *fnamep;
25567 if (*fnamep == NULL)
25568 return -1;
25569 }
25570
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025571#ifdef WIN3264
25572# if _WIN32_WINNT >= 0x0500
25573 if (vim_strchr(*fnamep, '~') != NULL)
25574 {
25575 /* Expand 8.3 filename to full path. Needed to make sure the same
25576 * file does not have two different names.
25577 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25578 p = alloc(_MAX_PATH + 1);
25579 if (p != NULL)
25580 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025581 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025582 {
25583 vim_free(*bufp);
25584 *bufp = *fnamep = p;
25585 }
25586 else
25587 vim_free(p);
25588 }
25589 }
25590# endif
25591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025592 /* Append a path separator to a directory. */
25593 if (mch_isdir(*fnamep))
25594 {
25595 /* Make room for one or two extra characters. */
25596 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25597 vim_free(*bufp); /* free any allocated file name */
25598 *bufp = *fnamep;
25599 if (*fnamep == NULL)
25600 return -1;
25601 add_pathsep(*fnamep);
25602 }
25603 }
25604
25605 /* ":." - path relative to the current directory */
25606 /* ":~" - path relative to the home directory */
25607 /* ":8" - shortname path - postponed till after */
25608 while (src[*usedlen] == ':'
25609 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25610 {
25611 *usedlen += 2;
25612 if (c == '8')
25613 {
25614#ifdef WIN3264
25615 has_shortname = 1; /* Postpone this. */
25616#endif
25617 continue;
25618 }
25619 pbuf = NULL;
25620 /* Need full path first (use expand_env() to remove a "~/") */
25621 if (!has_fullname)
25622 {
25623 if (c == '.' && **fnamep == '~')
25624 p = pbuf = expand_env_save(*fnamep);
25625 else
25626 p = pbuf = FullName_save(*fnamep, FALSE);
25627 }
25628 else
25629 p = *fnamep;
25630
25631 has_fullname = 0;
25632
25633 if (p != NULL)
25634 {
25635 if (c == '.')
25636 {
25637 mch_dirname(dirname, MAXPATHL);
25638 s = shorten_fname(p, dirname);
25639 if (s != NULL)
25640 {
25641 *fnamep = s;
25642 if (pbuf != NULL)
25643 {
25644 vim_free(*bufp); /* free any allocated file name */
25645 *bufp = pbuf;
25646 pbuf = NULL;
25647 }
25648 }
25649 }
25650 else
25651 {
25652 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25653 /* Only replace it when it starts with '~' */
25654 if (*dirname == '~')
25655 {
25656 s = vim_strsave(dirname);
25657 if (s != NULL)
25658 {
25659 *fnamep = s;
25660 vim_free(*bufp);
25661 *bufp = s;
25662 }
25663 }
25664 }
25665 vim_free(pbuf);
25666 }
25667 }
25668
25669 tail = gettail(*fnamep);
25670 *fnamelen = (int)STRLEN(*fnamep);
25671
25672 /* ":h" - head, remove "/file_name", can be repeated */
25673 /* Don't remove the first "/" or "c:\" */
25674 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25675 {
25676 valid |= VALID_HEAD;
25677 *usedlen += 2;
25678 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025679 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025680 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025681 *fnamelen = (int)(tail - *fnamep);
25682#ifdef VMS
25683 if (*fnamelen > 0)
25684 *fnamelen += 1; /* the path separator is part of the path */
25685#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025686 if (*fnamelen == 0)
25687 {
25688 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25689 p = vim_strsave((char_u *)".");
25690 if (p == NULL)
25691 return -1;
25692 vim_free(*bufp);
25693 *bufp = *fnamep = tail = p;
25694 *fnamelen = 1;
25695 }
25696 else
25697 {
25698 while (tail > s && !after_pathsep(s, tail))
25699 mb_ptr_back(*fnamep, tail);
25700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025701 }
25702
25703 /* ":8" - shortname */
25704 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25705 {
25706 *usedlen += 2;
25707#ifdef WIN3264
25708 has_shortname = 1;
25709#endif
25710 }
25711
25712#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025713 /*
25714 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025715 */
25716 if (has_shortname)
25717 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025718 /* Copy the string if it is shortened by :h and when it wasn't copied
25719 * yet, because we are going to change it in place. Avoids changing
25720 * the buffer name for "%:8". */
25721 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025722 {
25723 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025724 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025725 return -1;
25726 vim_free(*bufp);
25727 *bufp = *fnamep = p;
25728 }
25729
25730 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025731 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025732 if (!has_fullname && !vim_isAbsName(*fnamep))
25733 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025734 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025735 return -1;
25736 }
25737 else
25738 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025739 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025740
Bram Moolenaardc935552011-08-17 15:23:23 +020025741 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025742 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025743 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025744 return -1;
25745
25746 if (l == 0)
25747 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025748 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025749 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025750 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025751 return -1;
25752 }
25753 *fnamelen = l;
25754 }
25755 }
25756#endif /* WIN3264 */
25757
25758 /* ":t" - tail, just the basename */
25759 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25760 {
25761 *usedlen += 2;
25762 *fnamelen -= (int)(tail - *fnamep);
25763 *fnamep = tail;
25764 }
25765
25766 /* ":e" - extension, can be repeated */
25767 /* ":r" - root, without extension, can be repeated */
25768 while (src[*usedlen] == ':'
25769 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25770 {
25771 /* find a '.' in the tail:
25772 * - for second :e: before the current fname
25773 * - otherwise: The last '.'
25774 */
25775 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25776 s = *fnamep - 2;
25777 else
25778 s = *fnamep + *fnamelen - 1;
25779 for ( ; s > tail; --s)
25780 if (s[0] == '.')
25781 break;
25782 if (src[*usedlen + 1] == 'e') /* :e */
25783 {
25784 if (s > tail)
25785 {
25786 *fnamelen += (int)(*fnamep - (s + 1));
25787 *fnamep = s + 1;
25788#ifdef VMS
25789 /* cut version from the extension */
25790 s = *fnamep + *fnamelen - 1;
25791 for ( ; s > *fnamep; --s)
25792 if (s[0] == ';')
25793 break;
25794 if (s > *fnamep)
25795 *fnamelen = s - *fnamep;
25796#endif
25797 }
25798 else if (*fnamep <= tail)
25799 *fnamelen = 0;
25800 }
25801 else /* :r */
25802 {
25803 if (s > tail) /* remove one extension */
25804 *fnamelen = (int)(s - *fnamep);
25805 }
25806 *usedlen += 2;
25807 }
25808
25809 /* ":s?pat?foo?" - substitute */
25810 /* ":gs?pat?foo?" - global substitute */
25811 if (src[*usedlen] == ':'
25812 && (src[*usedlen + 1] == 's'
25813 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25814 {
25815 char_u *str;
25816 char_u *pat;
25817 char_u *sub;
25818 int sep;
25819 char_u *flags;
25820 int didit = FALSE;
25821
25822 flags = (char_u *)"";
25823 s = src + *usedlen + 2;
25824 if (src[*usedlen + 1] == 'g')
25825 {
25826 flags = (char_u *)"g";
25827 ++s;
25828 }
25829
25830 sep = *s++;
25831 if (sep)
25832 {
25833 /* find end of pattern */
25834 p = vim_strchr(s, sep);
25835 if (p != NULL)
25836 {
25837 pat = vim_strnsave(s, (int)(p - s));
25838 if (pat != NULL)
25839 {
25840 s = p + 1;
25841 /* find end of substitution */
25842 p = vim_strchr(s, sep);
25843 if (p != NULL)
25844 {
25845 sub = vim_strnsave(s, (int)(p - s));
25846 str = vim_strnsave(*fnamep, *fnamelen);
25847 if (sub != NULL && str != NULL)
25848 {
25849 *usedlen = (int)(p + 1 - src);
25850 s = do_string_sub(str, pat, sub, flags);
25851 if (s != NULL)
25852 {
25853 *fnamep = s;
25854 *fnamelen = (int)STRLEN(s);
25855 vim_free(*bufp);
25856 *bufp = s;
25857 didit = TRUE;
25858 }
25859 }
25860 vim_free(sub);
25861 vim_free(str);
25862 }
25863 vim_free(pat);
25864 }
25865 }
25866 /* after using ":s", repeat all the modifiers */
25867 if (didit)
25868 goto repeat;
25869 }
25870 }
25871
Bram Moolenaar26df0922014-02-23 23:39:13 +010025872 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25873 {
25874 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25875 if (p == NULL)
25876 return -1;
25877 vim_free(*bufp);
25878 *bufp = *fnamep = p;
25879 *fnamelen = (int)STRLEN(p);
25880 *usedlen += 2;
25881 }
25882
Bram Moolenaar071d4272004-06-13 20:20:40 +000025883 return valid;
25884}
25885
25886/*
25887 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25888 * "flags" can be "g" to do a global substitute.
25889 * Returns an allocated string, NULL for error.
25890 */
25891 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025892do_string_sub(
25893 char_u *str,
25894 char_u *pat,
25895 char_u *sub,
25896 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025897{
25898 int sublen;
25899 regmatch_T regmatch;
25900 int i;
25901 int do_all;
25902 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025903 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025904 garray_T ga;
25905 char_u *ret;
25906 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025907 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025908
25909 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25910 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025911 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025912
25913 ga_init2(&ga, 1, 200);
25914
25915 do_all = (flags[0] == 'g');
25916
25917 regmatch.rm_ic = p_ic;
25918 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25919 if (regmatch.regprog != NULL)
25920 {
25921 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025922 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025923 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25924 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025925 /* Skip empty match except for first match. */
25926 if (regmatch.startp[0] == regmatch.endp[0])
25927 {
25928 if (zero_width == regmatch.startp[0])
25929 {
25930 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025931 i = MB_PTR2LEN(tail);
25932 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25933 (size_t)i);
25934 ga.ga_len += i;
25935 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025936 continue;
25937 }
25938 zero_width = regmatch.startp[0];
25939 }
25940
Bram Moolenaar071d4272004-06-13 20:20:40 +000025941 /*
25942 * Get some space for a temporary buffer to do the substitution
25943 * into. It will contain:
25944 * - The text up to where the match is.
25945 * - The substituted text.
25946 * - The text after the match.
25947 */
25948 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025949 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025950 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25951 {
25952 ga_clear(&ga);
25953 break;
25954 }
25955
25956 /* copy the text up to where the match is */
25957 i = (int)(regmatch.startp[0] - tail);
25958 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25959 /* add the substituted text */
25960 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25961 + ga.ga_len + i, TRUE, TRUE, FALSE);
25962 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025963 tail = regmatch.endp[0];
25964 if (*tail == NUL)
25965 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025966 if (!do_all)
25967 break;
25968 }
25969
25970 if (ga.ga_data != NULL)
25971 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25972
Bram Moolenaar473de612013-06-08 18:19:48 +020025973 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025974 }
25975
25976 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25977 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025978 if (p_cpo == empty_option)
25979 p_cpo = save_cpo;
25980 else
25981 /* Darn, evaluating {sub} expression changed the value. */
25982 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025983
25984 return ret;
25985}
25986
25987#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */