blob: 503512667f84be5963d6d2fb459338b777d62b60 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000102static char *e_listreq = N_("E714: List required");
103static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000104static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000105static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
106static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
107static char *e_funcdict = N_("E717: Dictionary entry already exists");
108static char *e_funcref = N_("E718: Funcref required");
109static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
110static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000111static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000112static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200113#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200114static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000116
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100117#define NAMESPACE_CHAR (char_u *)"abglstvw"
118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
Bram Moolenaar8502c702014-06-17 12:51:16 +0200137/* Abort conversion to string after a recursion error. */
138static int did_echo_string_emsg = FALSE;
139
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100159#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
160
161/* Values for get_lval() flags argument: */
162#define GLV_QUIET TFN_QUIET /* no error messages */
163#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000215/* list heads for garbage collection */
216static dict_T *first_dict = NULL; /* list of all dicts */
217static list_T *first_list = NULL; /* list of all lists */
218
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000219/* From user function to hashitem and back. */
220static ufunc_T dumuf;
221#define UF2HIKEY(fp) ((fp)->uf_name)
222#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
223#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
224
225#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
226#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
Bram Moolenaar33570922005-01-25 22:26:29 +0000228#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
229#define VAR_SHORT_LEN 20 /* short variable name length */
230#define FIXVAR_CNT 12 /* number of fixed variables */
231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000233typedef struct funccall_S funccall_T;
234
235struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
237 ufunc_T *func; /* function being called */
238 int linenr; /* next line to be executed */
239 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000240 struct /* fixed variables for arguments */
241 {
242 dictitem_T var; /* variable (without room for name) */
243 char_u room[VAR_SHORT_LEN]; /* room for the name */
244 } fixvar[FIXVAR_CNT];
245 dict_T l_vars; /* l: local function variables */
246 dictitem_T l_vars_var; /* variable for l: scope */
247 dict_T l_avars; /* a: argument variables */
248 dictitem_T l_avars_var; /* variable for a: scope */
249 list_T l_varlist; /* list for a:000 */
250 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
251 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 linenr_T breakpoint; /* next line with breakpoint or zero */
253 int dbg_tick; /* debug_tick when breakpoint was set */
254 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000255#ifdef FEAT_PROFILE
256 proftime_T prof_child; /* time spent in a child */
257#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000258 funccall_T *caller; /* calling function or NULL */
259};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
261/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262 * Info used by a ":for" loop.
263 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000264typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000265{
266 int fi_semicolon; /* TRUE if ending in '; var]' */
267 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000268 listwatch_T fi_lw; /* keep an eye on the item used. */
269 list_T *fi_list; /* list being used */
270} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000272/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000273 * Struct used by trans_function_name()
274 */
275typedef struct
276{
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000278 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dictitem_T *fd_di; /* Dictionary item used */
280} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000281
Bram Moolenaara7043832005-01-21 11:56:39 +0000282
283/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000284 * Array to hold the value of v: variables.
285 * The value is in a dictitem, so that it can also be used in the v: scope.
286 * The reason to use this table anyway is for very quick access to the
287 * variables with the VV_ defines.
288 */
289#include "version.h"
290
291/* values for vv_flags: */
292#define VV_COMPAT 1 /* compatible, also used without "v:" */
293#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000294#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000295
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000296#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
298static struct vimvar
299{
300 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301 dictitem_T vv_di; /* value and name for key */
302 char vv_filler[16]; /* space for LONGEST name below!!! */
303 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
304} vimvars[VV_LEN] =
305{
306 /*
307 * The order here must match the VV_ defines in vim.h!
308 * Initializing a union does not work, leave tv.vval empty to get zero's.
309 */
310 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
311 {VV_NAME("count1", VAR_NUMBER), VV_RO},
312 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
313 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
314 {VV_NAME("warningmsg", VAR_STRING), 0},
315 {VV_NAME("statusmsg", VAR_STRING), 0},
316 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
318 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
319 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("termresponse", VAR_STRING), VV_RO},
321 {VV_NAME("fname", VAR_STRING), VV_RO},
322 {VV_NAME("lang", VAR_STRING), VV_RO},
323 {VV_NAME("lc_time", VAR_STRING), VV_RO},
324 {VV_NAME("ctype", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
327 {VV_NAME("fname_in", VAR_STRING), VV_RO},
328 {VV_NAME("fname_out", VAR_STRING), VV_RO},
329 {VV_NAME("fname_new", VAR_STRING), VV_RO},
330 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
331 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
332 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
335 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
336 {VV_NAME("progname", VAR_STRING), VV_RO},
337 {VV_NAME("servername", VAR_STRING), VV_RO},
338 {VV_NAME("dying", VAR_NUMBER), VV_RO},
339 {VV_NAME("exception", VAR_STRING), VV_RO},
340 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
341 {VV_NAME("register", VAR_STRING), VV_RO},
342 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
343 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000344 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
345 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000346 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000347 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
348 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000349 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000354 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000355 {VV_NAME("swapname", VAR_STRING), VV_RO},
356 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000357 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200358 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000359 {VV_NAME("mouse_win", VAR_NUMBER), 0},
360 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
361 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000362 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000363 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100364 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000365 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200366 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200367 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200368 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200369 {VV_NAME("option_new", VAR_STRING), VV_RO},
370 {VV_NAME("option_old", VAR_STRING), VV_RO},
371 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100372 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100373 {VV_NAME("false", VAR_SPECIAL), VV_RO},
374 {VV_NAME("true", VAR_SPECIAL), VV_RO},
375 {VV_NAME("null", VAR_SPECIAL), VV_RO},
376 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000377};
378
379/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000380#define vv_type vv_di.di_tv.v_type
381#define vv_nr vv_di.di_tv.vval.v_number
382#define vv_float vv_di.di_tv.vval.v_float
383#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000384#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200385#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000386#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000387
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200388static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000389#define vimvarht vimvardict.dv_hashtab
390
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100391static void prepare_vimvar(int idx, typval_T *save_tv);
392static void restore_vimvar(int idx, typval_T *save_tv);
393static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
394static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
395static char_u *skip_var_one(char_u *arg);
396static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
397static void list_glob_vars(int *first);
398static void list_buf_vars(int *first);
399static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000400#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100401static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000402#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100403static void list_vim_vars(int *first);
404static void list_script_vars(int *first);
405static void list_func_vars(int *first);
406static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
407static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
408static int check_changedtick(char_u *arg);
409static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
410static void clear_lval(lval_T *lp);
411static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
412static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
413static void list_fix_watch(list_T *l, listitem_T *item);
414static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
415static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
416static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
417static void item_lock(typval_T *tv, int deep, int lock);
418static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000419
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100420static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
421static int eval1(char_u **arg, typval_T *rettv, int evaluate);
422static int eval2(char_u **arg, typval_T *rettv, int evaluate);
423static int eval3(char_u **arg, typval_T *rettv, int evaluate);
424static int eval4(char_u **arg, typval_T *rettv, int evaluate);
425static int eval5(char_u **arg, typval_T *rettv, int evaluate);
426static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
427static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000428
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100429static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
430static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
431static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
432static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
433static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
434static long list_len(list_T *l);
435static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
436static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
437static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
438static long list_find_nr(list_T *l, long idx, int *errorp);
439static long list_idx_of_item(list_T *l, listitem_T *item);
440static int list_append_number(list_T *l, varnumber_T n);
441static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
442static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
443static list_T *list_copy(list_T *orig, int deep, int copyID);
444static char_u *list2string(typval_T *tv, int copyID);
445static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
446static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
447static int free_unref_items(int copyID);
448static dictitem_T *dictitem_copy(dictitem_T *org);
449static void dictitem_remove(dict_T *dict, dictitem_T *item);
450static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
451static long dict_len(dict_T *d);
452static char_u *dict2string(typval_T *tv, int copyID);
453static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
454static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
455static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
456static char_u *string_quote(char_u *str, int function);
457static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
458static int find_internal_func(char_u *name);
459static char_u *deref_func_name(char_u *name, int *lenp, int no_autoload);
460static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100461static void emsg_funcname(char *ermsg, char_u *name);
462static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000463
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100465static void f_abs(typval_T *argvars, typval_T *rettv);
466static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100468static void f_add(typval_T *argvars, typval_T *rettv);
469static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
470static void f_and(typval_T *argvars, typval_T *rettv);
471static void f_append(typval_T *argvars, typval_T *rettv);
472static void f_argc(typval_T *argvars, typval_T *rettv);
473static void f_argidx(typval_T *argvars, typval_T *rettv);
474static void f_arglistid(typval_T *argvars, typval_T *rettv);
475static void f_argv(typval_T *argvars, typval_T *rettv);
476static void f_assert_equal(typval_T *argvars, typval_T *rettv);
477static void f_assert_exception(typval_T *argvars, typval_T *rettv);
478static void f_assert_fails(typval_T *argvars, typval_T *rettv);
479static void f_assert_false(typval_T *argvars, typval_T *rettv);
480static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000481#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100482static void f_asin(typval_T *argvars, typval_T *rettv);
483static void f_atan(typval_T *argvars, typval_T *rettv);
484static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100486static void f_browse(typval_T *argvars, typval_T *rettv);
487static void f_browsedir(typval_T *argvars, typval_T *rettv);
488static void f_bufexists(typval_T *argvars, typval_T *rettv);
489static void f_buflisted(typval_T *argvars, typval_T *rettv);
490static void f_bufloaded(typval_T *argvars, typval_T *rettv);
491static void f_bufname(typval_T *argvars, typval_T *rettv);
492static void f_bufnr(typval_T *argvars, typval_T *rettv);
493static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
494static void f_byte2line(typval_T *argvars, typval_T *rettv);
495static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
496static void f_byteidx(typval_T *argvars, typval_T *rettv);
497static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
498static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000499#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100500static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000501#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100502static void f_changenr(typval_T *argvars, typval_T *rettv);
503static void f_char2nr(typval_T *argvars, typval_T *rettv);
504static void f_cindent(typval_T *argvars, typval_T *rettv);
505static void f_clearmatches(typval_T *argvars, typval_T *rettv);
506static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000507#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100508static void f_complete(typval_T *argvars, typval_T *rettv);
509static void f_complete_add(typval_T *argvars, typval_T *rettv);
510static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000511#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100512static void f_confirm(typval_T *argvars, typval_T *rettv);
513static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000514#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100515static void f_cos(typval_T *argvars, typval_T *rettv);
516static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000517#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100518#ifdef FEAT_CHANNEL
519static void f_connect(typval_T *argvars, typval_T *rettv);
520#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100521static void f_count(typval_T *argvars, typval_T *rettv);
522static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
523static void f_cursor(typval_T *argsvars, typval_T *rettv);
524static void f_deepcopy(typval_T *argvars, typval_T *rettv);
525static void f_delete(typval_T *argvars, typval_T *rettv);
526static void f_did_filetype(typval_T *argvars, typval_T *rettv);
527static void f_diff_filler(typval_T *argvars, typval_T *rettv);
528static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100529#ifdef FEAT_CHANNEL
530static void f_disconnect(typval_T *argvars, typval_T *rettv);
531#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100532static void f_empty(typval_T *argvars, typval_T *rettv);
533static void f_escape(typval_T *argvars, typval_T *rettv);
534static void f_eval(typval_T *argvars, typval_T *rettv);
535static void f_eventhandler(typval_T *argvars, typval_T *rettv);
536static void f_executable(typval_T *argvars, typval_T *rettv);
537static void f_exepath(typval_T *argvars, typval_T *rettv);
538static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200539#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100540static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200541#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100542static void f_expand(typval_T *argvars, typval_T *rettv);
543static void f_extend(typval_T *argvars, typval_T *rettv);
544static void f_feedkeys(typval_T *argvars, typval_T *rettv);
545static void f_filereadable(typval_T *argvars, typval_T *rettv);
546static void f_filewritable(typval_T *argvars, typval_T *rettv);
547static void f_filter(typval_T *argvars, typval_T *rettv);
548static void f_finddir(typval_T *argvars, typval_T *rettv);
549static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000550#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100551static void f_float2nr(typval_T *argvars, typval_T *rettv);
552static void f_floor(typval_T *argvars, typval_T *rettv);
553static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000554#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100555static void f_fnameescape(typval_T *argvars, typval_T *rettv);
556static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
557static void f_foldclosed(typval_T *argvars, typval_T *rettv);
558static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
559static void f_foldlevel(typval_T *argvars, typval_T *rettv);
560static void f_foldtext(typval_T *argvars, typval_T *rettv);
561static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
562static void f_foreground(typval_T *argvars, typval_T *rettv);
563static void f_function(typval_T *argvars, typval_T *rettv);
564static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
565static void f_get(typval_T *argvars, typval_T *rettv);
566static void f_getbufline(typval_T *argvars, typval_T *rettv);
567static void f_getbufvar(typval_T *argvars, typval_T *rettv);
568static void f_getchar(typval_T *argvars, typval_T *rettv);
569static void f_getcharmod(typval_T *argvars, typval_T *rettv);
570static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
571static void f_getcmdline(typval_T *argvars, typval_T *rettv);
572static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
573static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
574static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
575static void f_getcwd(typval_T *argvars, typval_T *rettv);
576static void f_getfontname(typval_T *argvars, typval_T *rettv);
577static void f_getfperm(typval_T *argvars, typval_T *rettv);
578static void f_getfsize(typval_T *argvars, typval_T *rettv);
579static void f_getftime(typval_T *argvars, typval_T *rettv);
580static void f_getftype(typval_T *argvars, typval_T *rettv);
581static void f_getline(typval_T *argvars, typval_T *rettv);
582static void f_getmatches(typval_T *argvars, typval_T *rettv);
583static void f_getpid(typval_T *argvars, typval_T *rettv);
584static void f_getcurpos(typval_T *argvars, typval_T *rettv);
585static void f_getpos(typval_T *argvars, typval_T *rettv);
586static void f_getqflist(typval_T *argvars, typval_T *rettv);
587static void f_getreg(typval_T *argvars, typval_T *rettv);
588static void f_getregtype(typval_T *argvars, typval_T *rettv);
589static void f_gettabvar(typval_T *argvars, typval_T *rettv);
590static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
591static void f_getwinposx(typval_T *argvars, typval_T *rettv);
592static void f_getwinposy(typval_T *argvars, typval_T *rettv);
593static void f_getwinvar(typval_T *argvars, typval_T *rettv);
594static void f_glob(typval_T *argvars, typval_T *rettv);
595static void f_globpath(typval_T *argvars, typval_T *rettv);
596static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
597static void f_has(typval_T *argvars, typval_T *rettv);
598static void f_has_key(typval_T *argvars, typval_T *rettv);
599static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
600static void f_hasmapto(typval_T *argvars, typval_T *rettv);
601static void f_histadd(typval_T *argvars, typval_T *rettv);
602static void f_histdel(typval_T *argvars, typval_T *rettv);
603static void f_histget(typval_T *argvars, typval_T *rettv);
604static void f_histnr(typval_T *argvars, typval_T *rettv);
605static void f_hlID(typval_T *argvars, typval_T *rettv);
606static void f_hlexists(typval_T *argvars, typval_T *rettv);
607static void f_hostname(typval_T *argvars, typval_T *rettv);
608static void f_iconv(typval_T *argvars, typval_T *rettv);
609static void f_indent(typval_T *argvars, typval_T *rettv);
610static void f_index(typval_T *argvars, typval_T *rettv);
611static void f_input(typval_T *argvars, typval_T *rettv);
612static void f_inputdialog(typval_T *argvars, typval_T *rettv);
613static void f_inputlist(typval_T *argvars, typval_T *rettv);
614static void f_inputrestore(typval_T *argvars, typval_T *rettv);
615static void f_inputsave(typval_T *argvars, typval_T *rettv);
616static void f_inputsecret(typval_T *argvars, typval_T *rettv);
617static void f_insert(typval_T *argvars, typval_T *rettv);
618static void f_invert(typval_T *argvars, typval_T *rettv);
619static void f_isdirectory(typval_T *argvars, typval_T *rettv);
620static void f_islocked(typval_T *argvars, typval_T *rettv);
621static void f_items(typval_T *argvars, typval_T *rettv);
622static void f_join(typval_T *argvars, typval_T *rettv);
623static void f_jsondecode(typval_T *argvars, typval_T *rettv);
624static void f_jsonencode(typval_T *argvars, typval_T *rettv);
625static void f_keys(typval_T *argvars, typval_T *rettv);
626static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
627static void f_len(typval_T *argvars, typval_T *rettv);
628static void f_libcall(typval_T *argvars, typval_T *rettv);
629static void f_libcallnr(typval_T *argvars, typval_T *rettv);
630static void f_line(typval_T *argvars, typval_T *rettv);
631static void f_line2byte(typval_T *argvars, typval_T *rettv);
632static void f_lispindent(typval_T *argvars, typval_T *rettv);
633static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000634#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100635static void f_log(typval_T *argvars, typval_T *rettv);
636static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000637#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200638#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100639static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200640#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100641static void f_map(typval_T *argvars, typval_T *rettv);
642static void f_maparg(typval_T *argvars, typval_T *rettv);
643static void f_mapcheck(typval_T *argvars, typval_T *rettv);
644static void f_match(typval_T *argvars, typval_T *rettv);
645static void f_matchadd(typval_T *argvars, typval_T *rettv);
646static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
647static void f_matcharg(typval_T *argvars, typval_T *rettv);
648static void f_matchdelete(typval_T *argvars, typval_T *rettv);
649static void f_matchend(typval_T *argvars, typval_T *rettv);
650static void f_matchlist(typval_T *argvars, typval_T *rettv);
651static void f_matchstr(typval_T *argvars, typval_T *rettv);
652static void f_max(typval_T *argvars, typval_T *rettv);
653static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000654#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100655static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000656#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100657static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100658#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100659static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100660#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100661static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
662static void f_nr2char(typval_T *argvars, typval_T *rettv);
663static void f_or(typval_T *argvars, typval_T *rettv);
664static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100665#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100666static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100667#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000668#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100669static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000670#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100671static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
672static void f_printf(typval_T *argvars, typval_T *rettv);
673static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200674#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100675static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200676#endif
677#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100678static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200679#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100680static void f_range(typval_T *argvars, typval_T *rettv);
681static void f_readfile(typval_T *argvars, typval_T *rettv);
682static void f_reltime(typval_T *argvars, typval_T *rettv);
683static void f_reltimestr(typval_T *argvars, typval_T *rettv);
684static void f_remote_expr(typval_T *argvars, typval_T *rettv);
685static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
686static void f_remote_peek(typval_T *argvars, typval_T *rettv);
687static void f_remote_read(typval_T *argvars, typval_T *rettv);
688static void f_remote_send(typval_T *argvars, typval_T *rettv);
689static void f_remove(typval_T *argvars, typval_T *rettv);
690static void f_rename(typval_T *argvars, typval_T *rettv);
691static void f_repeat(typval_T *argvars, typval_T *rettv);
692static void f_resolve(typval_T *argvars, typval_T *rettv);
693static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000696#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100697static void f_screenattr(typval_T *argvars, typval_T *rettv);
698static void f_screenchar(typval_T *argvars, typval_T *rettv);
699static void f_screencol(typval_T *argvars, typval_T *rettv);
700static void f_screenrow(typval_T *argvars, typval_T *rettv);
701static void f_search(typval_T *argvars, typval_T *rettv);
702static void f_searchdecl(typval_T *argvars, typval_T *rettv);
703static void f_searchpair(typval_T *argvars, typval_T *rettv);
704static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
705static void f_searchpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100706#ifdef FEAT_CHANNEL
707static void f_sendexpr(typval_T *argvars, typval_T *rettv);
708static void f_sendraw(typval_T *argvars, typval_T *rettv);
709#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100710static void f_server2client(typval_T *argvars, typval_T *rettv);
711static void f_serverlist(typval_T *argvars, typval_T *rettv);
712static void f_setbufvar(typval_T *argvars, typval_T *rettv);
713static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
714static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
715static void f_setline(typval_T *argvars, typval_T *rettv);
716static void f_setloclist(typval_T *argvars, typval_T *rettv);
717static void f_setmatches(typval_T *argvars, typval_T *rettv);
718static void f_setpos(typval_T *argvars, typval_T *rettv);
719static void f_setqflist(typval_T *argvars, typval_T *rettv);
720static void f_setreg(typval_T *argvars, typval_T *rettv);
721static void f_settabvar(typval_T *argvars, typval_T *rettv);
722static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
723static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100724#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100725static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100726#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100727static void f_shellescape(typval_T *argvars, typval_T *rettv);
728static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
729static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000730#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100731static void f_sin(typval_T *argvars, typval_T *rettv);
732static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000733#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100734static void f_sort(typval_T *argvars, typval_T *rettv);
735static void f_soundfold(typval_T *argvars, typval_T *rettv);
736static void f_spellbadword(typval_T *argvars, typval_T *rettv);
737static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
738static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000739#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100740static void f_sqrt(typval_T *argvars, typval_T *rettv);
741static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000742#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100743static void f_str2nr(typval_T *argvars, typval_T *rettv);
744static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000745#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100746static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000747#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100748static void f_stridx(typval_T *argvars, typval_T *rettv);
749static void f_string(typval_T *argvars, typval_T *rettv);
750static void f_strlen(typval_T *argvars, typval_T *rettv);
751static void f_strpart(typval_T *argvars, typval_T *rettv);
752static void f_strridx(typval_T *argvars, typval_T *rettv);
753static void f_strtrans(typval_T *argvars, typval_T *rettv);
754static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
755static void f_strwidth(typval_T *argvars, typval_T *rettv);
756static void f_submatch(typval_T *argvars, typval_T *rettv);
757static void f_substitute(typval_T *argvars, typval_T *rettv);
758static void f_synID(typval_T *argvars, typval_T *rettv);
759static void f_synIDattr(typval_T *argvars, typval_T *rettv);
760static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
761static void f_synstack(typval_T *argvars, typval_T *rettv);
762static void f_synconcealed(typval_T *argvars, typval_T *rettv);
763static void f_system(typval_T *argvars, typval_T *rettv);
764static void f_systemlist(typval_T *argvars, typval_T *rettv);
765static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
766static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
767static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
768static void f_taglist(typval_T *argvars, typval_T *rettv);
769static void f_tagfiles(typval_T *argvars, typval_T *rettv);
770static void f_tempname(typval_T *argvars, typval_T *rettv);
771static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200772#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100773static void f_tan(typval_T *argvars, typval_T *rettv);
774static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200775#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100776static void f_tolower(typval_T *argvars, typval_T *rettv);
777static void f_toupper(typval_T *argvars, typval_T *rettv);
778static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000779#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100780static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000781#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100782static void f_type(typval_T *argvars, typval_T *rettv);
783static void f_undofile(typval_T *argvars, typval_T *rettv);
784static void f_undotree(typval_T *argvars, typval_T *rettv);
785static void f_uniq(typval_T *argvars, typval_T *rettv);
786static void f_values(typval_T *argvars, typval_T *rettv);
787static void f_virtcol(typval_T *argvars, typval_T *rettv);
788static void f_visualmode(typval_T *argvars, typval_T *rettv);
789static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
790static void f_winbufnr(typval_T *argvars, typval_T *rettv);
791static void f_wincol(typval_T *argvars, typval_T *rettv);
792static void f_winheight(typval_T *argvars, typval_T *rettv);
793static void f_winline(typval_T *argvars, typval_T *rettv);
794static void f_winnr(typval_T *argvars, typval_T *rettv);
795static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
796static void f_winrestview(typval_T *argvars, typval_T *rettv);
797static void f_winsaveview(typval_T *argvars, typval_T *rettv);
798static void f_winwidth(typval_T *argvars, typval_T *rettv);
799static void f_writefile(typval_T *argvars, typval_T *rettv);
800static void f_wordcount(typval_T *argvars, typval_T *rettv);
801static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000802
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100803static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
804static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
805static int get_env_len(char_u **arg);
806static int get_id_len(char_u **arg);
807static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
808static char_u *find_name_end(char_u *arg, char_u **expr_start, char_u **expr_end, int flags);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000809#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
810#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
811 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100812static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
813static int eval_isnamec(int c);
814static int eval_isnamec1(int c);
815static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
816static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
817static typval_T *alloc_tv(void);
818static typval_T *alloc_string_tv(char_u *string);
819static void init_tv(typval_T *varp);
820static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100821#ifdef FEAT_FLOAT
822static float_T get_tv_float(typval_T *varp);
823#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100824static linenr_T get_tv_lnum(typval_T *argvars);
825static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
826static char_u *get_tv_string(typval_T *varp);
827static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
828static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
829static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
830static hashtab_T *find_var_ht(char_u *name, char_u **varname);
831static funccall_T *get_funccal(void);
832static void vars_clear_ext(hashtab_T *ht, int free_val);
833static void delete_var(hashtab_T *ht, hashitem_T *hi);
834static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
835static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
836static void set_var(char_u *name, typval_T *varp, int copy);
837static int var_check_ro(int flags, char_u *name, int use_gettext);
838static int var_check_fixed(int flags, char_u *name, int use_gettext);
839static int var_check_func_name(char_u *name, int new_var);
840static int valid_varname(char_u *varname);
841static int tv_check_lock(int lock, char_u *name, int use_gettext);
842static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
843static char_u *find_option_end(char_u **arg, int *opt_flags);
844static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
845static int eval_fname_script(char_u *p);
846static int eval_fname_sid(char_u *p);
847static void list_func_head(ufunc_T *fp, int indent);
848static ufunc_T *find_func(char_u *name);
849static int function_exists(char_u *name);
850static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000851#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100852static void func_do_profile(ufunc_T *fp);
853static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
854static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000855static int
856# ifdef __BORLANDC__
857 _RTLENTRYF
858# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100859 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000860static int
861# ifdef __BORLANDC__
862 _RTLENTRYF
863# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100864 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000865#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100866static int script_autoload(char_u *name, int reload);
867static char_u *autoload_name(char_u *name);
868static void cat_func_name(char_u *buf, ufunc_T *fp);
869static void func_free(ufunc_T *fp);
870static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
871static int can_free_funccal(funccall_T *fc, int copyID) ;
872static void free_funccal(funccall_T *fc, int free_val);
873static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
874static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
875static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
876static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
877static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
878static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
879static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
880static int write_list(FILE *fd, list_T *list, int binary);
881static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000882
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200883
884#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100885static int compare_func_name(const void *s1, const void *s2);
886static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200887#endif
888
Bram Moolenaar33570922005-01-25 22:26:29 +0000889/*
890 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000891 */
892 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100893eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000894{
Bram Moolenaar33570922005-01-25 22:26:29 +0000895 int i;
896 struct vimvar *p;
897
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200898 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
899 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200900 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000901 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000902 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000903
904 for (i = 0; i < VV_LEN; ++i)
905 {
906 p = &vimvars[i];
907 STRCPY(p->vv_di.di_key, p->vv_name);
908 if (p->vv_flags & VV_RO)
909 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
910 else if (p->vv_flags & VV_RO_SBX)
911 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
912 else
913 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000914
915 /* add to v: scope dict, unless the value is not always available */
916 if (p->vv_type != VAR_UNKNOWN)
917 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000918 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000919 /* add to compat scope dict */
920 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000921 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100922 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
923
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000924 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100925 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200926 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100927 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100928
929 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
930 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
931 set_vim_var_nr(VV_NONE, VVAL_NONE);
932 set_vim_var_nr(VV_NULL, VVAL_NULL);
933
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200934 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200935
936#ifdef EBCDIC
937 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100938 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200939 */
940 sortFunctions();
941#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000942}
943
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000944#if defined(EXITFREE) || defined(PROTO)
945 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100946eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000947{
948 int i;
949 struct vimvar *p;
950
951 for (i = 0; i < VV_LEN; ++i)
952 {
953 p = &vimvars[i];
954 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000955 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000956 vim_free(p->vv_str);
957 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000958 }
959 else if (p->vv_di.di_tv.v_type == VAR_LIST)
960 {
961 list_unref(p->vv_list);
962 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000963 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000964 }
965 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000966 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000967 hash_clear(&compat_hashtab);
968
Bram Moolenaard9fba312005-06-26 22:34:35 +0000969 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100970# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200971 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100972# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000973
974 /* global variables */
975 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000976
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000977 /* autoloaded script names */
978 ga_clear_strings(&ga_loaded);
979
Bram Moolenaarcca74132013-09-25 21:00:28 +0200980 /* Script-local variables. First clear all the variables and in a second
981 * loop free the scriptvar_T, because a variable in one script might hold
982 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200983 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200984 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200985 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200986 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200987 ga_clear(&ga_scripts);
988
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000989 /* unreferenced lists and dicts */
990 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000991
992 /* functions */
993 free_all_functions();
994 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000995}
996#endif
997
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 * Return the name of the executed function.
1000 */
1001 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001002func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001004 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005}
1006
1007/*
1008 * Return the address holding the next breakpoint line for a funccall cookie.
1009 */
1010 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001011func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012{
Bram Moolenaar33570922005-01-25 22:26:29 +00001013 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014}
1015
1016/*
1017 * Return the address holding the debug tick for a funccall cookie.
1018 */
1019 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001020func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021{
Bram Moolenaar33570922005-01-25 22:26:29 +00001022 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023}
1024
1025/*
1026 * Return the nesting level for a funccall cookie.
1027 */
1028 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001029func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030{
Bram Moolenaar33570922005-01-25 22:26:29 +00001031 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032}
1033
1034/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001035funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001037/* pointer to list of previously used funccal, still around because some
1038 * item in it is still being used. */
1039funccall_T *previous_funccal = NULL;
1040
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041/*
1042 * Return TRUE when a function was ended by a ":return" command.
1043 */
1044 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001045current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046{
1047 return current_funccal->returned;
1048}
1049
1050
1051/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 * Set an internal variable to a string value. Creates the variable if it does
1053 * not already exist.
1054 */
1055 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001056set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001058 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001059 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060
1061 val = vim_strsave(value);
1062 if (val != NULL)
1063 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001064 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001065 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001067 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001068 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 }
1070 }
1071}
1072
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001073static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001074static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075static char_u *redir_endp = NULL;
1076static char_u *redir_varname = NULL;
1077
1078/*
1079 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001080 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001081 * Returns OK if successfully completed the setup. FAIL otherwise.
1082 */
1083 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001084var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001085{
1086 int save_emsg;
1087 int err;
1088 typval_T tv;
1089
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001090 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001091 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092 {
1093 EMSG(_(e_invarg));
1094 return FAIL;
1095 }
1096
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001097 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 redir_varname = vim_strsave(name);
1099 if (redir_varname == NULL)
1100 return FAIL;
1101
1102 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1103 if (redir_lval == NULL)
1104 {
1105 var_redir_stop();
1106 return FAIL;
1107 }
1108
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001109 /* The output is stored in growarray "redir_ga" until redirection ends. */
1110 ga_init2(&redir_ga, (int)sizeof(char), 500);
1111
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001112 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001113 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001114 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001115 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1116 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001117 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118 if (redir_endp != NULL && *redir_endp != NUL)
1119 /* Trailing characters are present after the variable name */
1120 EMSG(_(e_trailing));
1121 else
1122 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001123 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001124 var_redir_stop();
1125 return FAIL;
1126 }
1127
1128 /* check if we can write to the variable: set it to or append an empty
1129 * string */
1130 save_emsg = did_emsg;
1131 did_emsg = FALSE;
1132 tv.v_type = VAR_STRING;
1133 tv.vval.v_string = (char_u *)"";
1134 if (append)
1135 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1136 else
1137 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001138 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001140 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141 if (err)
1142 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001143 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 var_redir_stop();
1145 return FAIL;
1146 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001147
1148 return OK;
1149}
1150
1151/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001152 * Append "value[value_len]" to the variable set by var_redir_start().
1153 * The actual appending is postponed until redirection ends, because the value
1154 * appended may in fact be the string we write to, changing it may cause freed
1155 * memory to be used:
1156 * :redir => foo
1157 * :let foo
1158 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159 */
1160 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001161var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001163 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001164
1165 if (redir_lval == NULL)
1166 return;
1167
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001168 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001169 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001170 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001171 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001172
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001173 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001174 {
1175 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001176 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001177 }
1178 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001179 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001180}
1181
1182/*
1183 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001184 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001185 */
1186 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001187var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001188{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001189 typval_T tv;
1190
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001191 if (redir_lval != NULL)
1192 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001193 /* If there was no error: assign the text to the variable. */
1194 if (redir_endp != NULL)
1195 {
1196 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1197 tv.v_type = VAR_STRING;
1198 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001199 /* Call get_lval() again, if it's inside a Dict or List it may
1200 * have changed. */
1201 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001202 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001203 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1204 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1205 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001206 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001207
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001208 /* free the collected output */
1209 vim_free(redir_ga.ga_data);
1210 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001211
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001212 vim_free(redir_lval);
1213 redir_lval = NULL;
1214 }
1215 vim_free(redir_varname);
1216 redir_varname = NULL;
1217}
1218
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219# if defined(FEAT_MBYTE) || defined(PROTO)
1220 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001221eval_charconvert(
1222 char_u *enc_from,
1223 char_u *enc_to,
1224 char_u *fname_from,
1225 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226{
1227 int err = FALSE;
1228
1229 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1230 set_vim_var_string(VV_CC_TO, enc_to, -1);
1231 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1232 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1233 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1234 err = TRUE;
1235 set_vim_var_string(VV_CC_FROM, NULL, -1);
1236 set_vim_var_string(VV_CC_TO, NULL, -1);
1237 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1238 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1239
1240 if (err)
1241 return FAIL;
1242 return OK;
1243}
1244# endif
1245
1246# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1247 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001248eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249{
1250 int err = FALSE;
1251
1252 set_vim_var_string(VV_FNAME_IN, fname, -1);
1253 set_vim_var_string(VV_CMDARG, args, -1);
1254 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1255 err = TRUE;
1256 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1257 set_vim_var_string(VV_CMDARG, NULL, -1);
1258
1259 if (err)
1260 {
1261 mch_remove(fname);
1262 return FAIL;
1263 }
1264 return OK;
1265}
1266# endif
1267
1268# if defined(FEAT_DIFF) || defined(PROTO)
1269 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001270eval_diff(
1271 char_u *origfile,
1272 char_u *newfile,
1273 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274{
1275 int err = FALSE;
1276
1277 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1278 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1279 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1280 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1281 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1282 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1283 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1284}
1285
1286 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001287eval_patch(
1288 char_u *origfile,
1289 char_u *difffile,
1290 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291{
1292 int err;
1293
1294 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1295 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1296 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1297 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1298 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1299 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1300 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1301}
1302# endif
1303
1304/*
1305 * Top level evaluation function, returning a boolean.
1306 * Sets "error" to TRUE if there was an error.
1307 * Return TRUE or FALSE.
1308 */
1309 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001310eval_to_bool(
1311 char_u *arg,
1312 int *error,
1313 char_u **nextcmd,
1314 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315{
Bram Moolenaar33570922005-01-25 22:26:29 +00001316 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 int retval = FALSE;
1318
1319 if (skip)
1320 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001321 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 else
1324 {
1325 *error = FALSE;
1326 if (!skip)
1327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001328 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001329 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 }
1331 }
1332 if (skip)
1333 --emsg_skip;
1334
1335 return retval;
1336}
1337
1338/*
1339 * Top level evaluation function, returning a string. If "skip" is TRUE,
1340 * only parsing to "nextcmd" is done, without reporting errors. Return
1341 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1342 */
1343 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001344eval_to_string_skip(
1345 char_u *arg,
1346 char_u **nextcmd,
1347 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
Bram Moolenaar33570922005-01-25 22:26:29 +00001349 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 char_u *retval;
1351
1352 if (skip)
1353 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001354 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 retval = NULL;
1356 else
1357 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001358 retval = vim_strsave(get_tv_string(&tv));
1359 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 }
1361 if (skip)
1362 --emsg_skip;
1363
1364 return retval;
1365}
1366
1367/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001368 * Skip over an expression at "*pp".
1369 * Return FAIL for an error, OK otherwise.
1370 */
1371 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001372skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001373{
Bram Moolenaar33570922005-01-25 22:26:29 +00001374 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001375
1376 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001377 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001378}
1379
1380/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001382 * When "convert" is TRUE convert a List into a sequence of lines and convert
1383 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 * Return pointer to allocated memory, or NULL for failure.
1385 */
1386 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001387eval_to_string(
1388 char_u *arg,
1389 char_u **nextcmd,
1390 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391{
Bram Moolenaar33570922005-01-25 22:26:29 +00001392 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001394 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001395#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001396 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001399 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 retval = NULL;
1401 else
1402 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001403 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001404 {
1405 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001406 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001407 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001408 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001409 if (tv.vval.v_list->lv_len > 0)
1410 ga_append(&ga, NL);
1411 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001412 ga_append(&ga, NUL);
1413 retval = (char_u *)ga.ga_data;
1414 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001415#ifdef FEAT_FLOAT
1416 else if (convert && tv.v_type == VAR_FLOAT)
1417 {
1418 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1419 retval = vim_strsave(numbuf);
1420 }
1421#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001422 else
1423 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001424 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 }
1426
1427 return retval;
1428}
1429
1430/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001431 * Call eval_to_string() without using current local variables and using
1432 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 */
1434 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001435eval_to_string_safe(
1436 char_u *arg,
1437 char_u **nextcmd,
1438 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439{
1440 char_u *retval;
1441 void *save_funccalp;
1442
1443 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001444 if (use_sandbox)
1445 ++sandbox;
1446 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001447 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001448 if (use_sandbox)
1449 --sandbox;
1450 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 restore_funccal(save_funccalp);
1452 return retval;
1453}
1454
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455/*
1456 * Top level evaluation function, returning a number.
1457 * Evaluates "expr" silently.
1458 * Returns -1 for an error.
1459 */
1460 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001461eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462{
Bram Moolenaar33570922005-01-25 22:26:29 +00001463 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001465 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466
1467 ++emsg_off;
1468
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001469 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 retval = -1;
1471 else
1472 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001473 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001474 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 }
1476 --emsg_off;
1477
1478 return retval;
1479}
1480
Bram Moolenaara40058a2005-07-11 22:42:07 +00001481/*
1482 * Prepare v: variable "idx" to be used.
1483 * Save the current typeval in "save_tv".
1484 * When not used yet add the variable to the v: hashtable.
1485 */
1486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001487prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001488{
1489 *save_tv = vimvars[idx].vv_tv;
1490 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1491 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1492}
1493
1494/*
1495 * Restore v: variable "idx" to typeval "save_tv".
1496 * When no longer defined, remove the variable from the v: hashtable.
1497 */
1498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001499restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001500{
1501 hashitem_T *hi;
1502
Bram Moolenaara40058a2005-07-11 22:42:07 +00001503 vimvars[idx].vv_tv = *save_tv;
1504 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1505 {
1506 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1507 if (HASHITEM_EMPTY(hi))
1508 EMSG2(_(e_intern2), "restore_vimvar()");
1509 else
1510 hash_remove(&vimvarht, hi);
1511 }
1512}
1513
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001514#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001515/*
1516 * Evaluate an expression to a list with suggestions.
1517 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001518 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001519 */
1520 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001521eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001522{
1523 typval_T save_val;
1524 typval_T rettv;
1525 list_T *list = NULL;
1526 char_u *p = skipwhite(expr);
1527
1528 /* Set "v:val" to the bad word. */
1529 prepare_vimvar(VV_VAL, &save_val);
1530 vimvars[VV_VAL].vv_type = VAR_STRING;
1531 vimvars[VV_VAL].vv_str = badword;
1532 if (p_verbose == 0)
1533 ++emsg_off;
1534
1535 if (eval1(&p, &rettv, TRUE) == OK)
1536 {
1537 if (rettv.v_type != VAR_LIST)
1538 clear_tv(&rettv);
1539 else
1540 list = rettv.vval.v_list;
1541 }
1542
1543 if (p_verbose == 0)
1544 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001545 restore_vimvar(VV_VAL, &save_val);
1546
1547 return list;
1548}
1549
1550/*
1551 * "list" is supposed to contain two items: a word and a number. Return the
1552 * word in "pp" and the number as the return value.
1553 * Return -1 if anything isn't right.
1554 * Used to get the good word and score from the eval_spell_expr() result.
1555 */
1556 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001557get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001558{
1559 listitem_T *li;
1560
1561 li = list->lv_first;
1562 if (li == NULL)
1563 return -1;
1564 *pp = get_tv_string(&li->li_tv);
1565
1566 li = li->li_next;
1567 if (li == NULL)
1568 return -1;
1569 return get_tv_number(&li->li_tv);
1570}
1571#endif
1572
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001573/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001574 * Top level evaluation function.
1575 * Returns an allocated typval_T with the result.
1576 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001577 */
1578 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001579eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001580{
1581 typval_T *tv;
1582
1583 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001584 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001585 {
1586 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001587 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001588 }
1589
1590 return tv;
1591}
1592
1593
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001595 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001596 * Uses argv[argc] for the function arguments. Only Number and String
1597 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001598 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001600 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001601call_vim_function(
1602 char_u *func,
1603 int argc,
1604 char_u **argv,
1605 int safe, /* use the sandbox */
1606 int str_arg_only, /* all arguments are strings */
1607 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608{
Bram Moolenaar33570922005-01-25 22:26:29 +00001609 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 long n;
1611 int len;
1612 int i;
1613 int doesrange;
1614 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001615 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001617 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001619 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620
1621 for (i = 0; i < argc; i++)
1622 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001623 /* Pass a NULL or empty argument as an empty string */
1624 if (argv[i] == NULL || *argv[i] == NUL)
1625 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001626 argvars[i].v_type = VAR_STRING;
1627 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001628 continue;
1629 }
1630
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001631 if (str_arg_only)
1632 len = 0;
1633 else
1634 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001635 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 if (len != 0 && len == (int)STRLEN(argv[i]))
1637 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001638 argvars[i].v_type = VAR_NUMBER;
1639 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 }
1641 else
1642 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001643 argvars[i].v_type = VAR_STRING;
1644 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 }
1646 }
1647
1648 if (safe)
1649 {
1650 save_funccalp = save_funccal();
1651 ++sandbox;
1652 }
1653
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001654 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1655 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001657 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 if (safe)
1659 {
1660 --sandbox;
1661 restore_funccal(save_funccalp);
1662 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001663 vim_free(argvars);
1664
1665 if (ret == FAIL)
1666 clear_tv(rettv);
1667
1668 return ret;
1669}
1670
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001671/*
1672 * Call vimL function "func" and return the result as a number.
1673 * Returns -1 when calling the function fails.
1674 * Uses argv[argc] for the function arguments.
1675 */
1676 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001677call_func_retnr(
1678 char_u *func,
1679 int argc,
1680 char_u **argv,
1681 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001682{
1683 typval_T rettv;
1684 long retval;
1685
1686 /* All arguments are passed as strings, no conversion to number. */
1687 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1688 return -1;
1689
1690 retval = get_tv_number_chk(&rettv, NULL);
1691 clear_tv(&rettv);
1692 return retval;
1693}
1694
1695#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1696 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1697
Bram Moolenaar4f688582007-07-24 12:34:30 +00001698# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001699/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001700 * Call vimL function "func" and return the result as a string.
1701 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001702 * Uses argv[argc] for the function arguments.
1703 */
1704 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001705call_func_retstr(
1706 char_u *func,
1707 int argc,
1708 char_u **argv,
1709 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001710{
1711 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001712 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001713
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001714 /* All arguments are passed as strings, no conversion to number. */
1715 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001716 return NULL;
1717
1718 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001719 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 return retval;
1721}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001722# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001723
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001724/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001725 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001726 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001727 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001728 */
1729 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001730call_func_retlist(
1731 char_u *func,
1732 int argc,
1733 char_u **argv,
1734 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001735{
1736 typval_T rettv;
1737
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001738 /* All arguments are passed as strings, no conversion to number. */
1739 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001740 return NULL;
1741
1742 if (rettv.v_type != VAR_LIST)
1743 {
1744 clear_tv(&rettv);
1745 return NULL;
1746 }
1747
1748 return rettv.vval.v_list;
1749}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750#endif
1751
1752/*
1753 * Save the current function call pointer, and set it to NULL.
1754 * Used when executing autocommands and for ":source".
1755 */
1756 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001757save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001759 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 current_funccal = NULL;
1762 return (void *)fc;
1763}
1764
1765 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001766restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001768 funccall_T *fc = (funccall_T *)vfc;
1769
1770 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771}
1772
Bram Moolenaar05159a02005-02-26 23:04:13 +00001773#if defined(FEAT_PROFILE) || defined(PROTO)
1774/*
1775 * Prepare profiling for entering a child or something else that is not
1776 * counted for the script/function itself.
1777 * Should always be called in pair with prof_child_exit().
1778 */
1779 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001780prof_child_enter(
1781 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001782{
1783 funccall_T *fc = current_funccal;
1784
1785 if (fc != NULL && fc->func->uf_profiling)
1786 profile_start(&fc->prof_child);
1787 script_prof_save(tm);
1788}
1789
1790/*
1791 * Take care of time spent in a child.
1792 * Should always be called after prof_child_enter().
1793 */
1794 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001795prof_child_exit(
1796 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001797{
1798 funccall_T *fc = current_funccal;
1799
1800 if (fc != NULL && fc->func->uf_profiling)
1801 {
1802 profile_end(&fc->prof_child);
1803 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1804 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1805 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1806 }
1807 script_prof_restore(tm);
1808}
1809#endif
1810
1811
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812#ifdef FEAT_FOLDING
1813/*
1814 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1815 * it in "*cp". Doesn't give error messages.
1816 */
1817 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001818eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819{
Bram Moolenaar33570922005-01-25 22:26:29 +00001820 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 int retval;
1822 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001823 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1824 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825
1826 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001827 if (use_sandbox)
1828 ++sandbox;
1829 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 retval = 0;
1833 else
1834 {
1835 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001836 if (tv.v_type == VAR_NUMBER)
1837 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001838 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 retval = 0;
1840 else
1841 {
1842 /* If the result is a string, check if there is a non-digit before
1843 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 if (!VIM_ISDIGIT(*s) && *s != '-')
1846 *cp = *s++;
1847 retval = atol((char *)s);
1848 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001849 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 }
1851 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001852 if (use_sandbox)
1853 --sandbox;
1854 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855
1856 return retval;
1857}
1858#endif
1859
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 * ":let" list all variable values
1862 * ":let var1 var2" list variable values
1863 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001864 * ":let var += expr" assignment command.
1865 * ":let var -= expr" assignment command.
1866 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 */
1869 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001870ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871{
1872 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001874 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 int var_count = 0;
1877 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001878 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001879 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001880 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881
Bram Moolenaardb552d602006-03-23 22:59:57 +00001882 argend = skip_var_list(arg, &var_count, &semicolon);
1883 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001884 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001885 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1886 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001887 expr = skipwhite(argend);
1888 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1889 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001891 /*
1892 * ":let" without "=": list variables
1893 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001894 if (*arg == '[')
1895 EMSG(_(e_invarg));
1896 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001898 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001900 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001902 list_glob_vars(&first);
1903 list_buf_vars(&first);
1904 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001905#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001906 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001907#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001908 list_script_vars(&first);
1909 list_func_vars(&first);
1910 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 eap->nextcmd = check_nextcmd(arg);
1913 }
1914 else
1915 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001916 op[0] = '=';
1917 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001918 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001919 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001920 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1921 op[0] = *expr; /* +=, -= or .= */
1922 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001923 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001924 else
1925 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001926
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 if (eap->skip)
1928 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 if (eap->skip)
1931 {
1932 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 --emsg_skip;
1935 }
1936 else if (i != FAIL)
1937 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001939 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001940 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 }
1942 }
1943}
1944
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945/*
1946 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1947 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001948 * When "nextchars" is not NULL it points to a string with characters that
1949 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1950 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 * Returns OK or FAIL;
1952 */
1953 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001954ex_let_vars(
1955 char_u *arg_start,
1956 typval_T *tv,
1957 int copy, /* copy values from "tv", don't move */
1958 int semicolon, /* from skip_var_list() */
1959 int var_count, /* from skip_var_list() */
1960 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001961{
1962 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001963 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001965 listitem_T *item;
1966 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001967
1968 if (*arg != '[')
1969 {
1970 /*
1971 * ":let var = expr" or ":for var in list"
1972 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001973 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974 return FAIL;
1975 return OK;
1976 }
1977
1978 /*
1979 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1980 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001981 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001982 {
1983 EMSG(_(e_listreq));
1984 return FAIL;
1985 }
1986
1987 i = list_len(l);
1988 if (semicolon == 0 && var_count < i)
1989 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001990 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 return FAIL;
1992 }
1993 if (var_count - semicolon > i)
1994 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001995 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 return FAIL;
1997 }
1998
1999 item = l->lv_first;
2000 while (*arg != ']')
2001 {
2002 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002003 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002004 item = item->li_next;
2005 if (arg == NULL)
2006 return FAIL;
2007
2008 arg = skipwhite(arg);
2009 if (*arg == ';')
2010 {
2011 /* Put the rest of the list (may be empty) in the var after ';'.
2012 * Create a new list for this. */
2013 l = list_alloc();
2014 if (l == NULL)
2015 return FAIL;
2016 while (item != NULL)
2017 {
2018 list_append_tv(l, &item->li_tv);
2019 item = item->li_next;
2020 }
2021
2022 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002023 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024 ltv.vval.v_list = l;
2025 l->lv_refcount = 1;
2026
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002027 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2028 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002029 clear_tv(&ltv);
2030 if (arg == NULL)
2031 return FAIL;
2032 break;
2033 }
2034 else if (*arg != ',' && *arg != ']')
2035 {
2036 EMSG2(_(e_intern2), "ex_let_vars()");
2037 return FAIL;
2038 }
2039 }
2040
2041 return OK;
2042}
2043
2044/*
2045 * Skip over assignable variable "var" or list of variables "[var, var]".
2046 * Used for ":let varvar = expr" and ":for varvar in expr".
2047 * For "[var, var]" increment "*var_count" for each variable.
2048 * for "[var, var; var]" set "semicolon".
2049 * Return NULL for an error.
2050 */
2051 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002052skip_var_list(
2053 char_u *arg,
2054 int *var_count,
2055 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002056{
2057 char_u *p, *s;
2058
2059 if (*arg == '[')
2060 {
2061 /* "[var, var]": find the matching ']'. */
2062 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002063 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002064 {
2065 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2066 s = skip_var_one(p);
2067 if (s == p)
2068 {
2069 EMSG2(_(e_invarg2), p);
2070 return NULL;
2071 }
2072 ++*var_count;
2073
2074 p = skipwhite(s);
2075 if (*p == ']')
2076 break;
2077 else if (*p == ';')
2078 {
2079 if (*semicolon == 1)
2080 {
2081 EMSG(_("Double ; in list of variables"));
2082 return NULL;
2083 }
2084 *semicolon = 1;
2085 }
2086 else if (*p != ',')
2087 {
2088 EMSG2(_(e_invarg2), p);
2089 return NULL;
2090 }
2091 }
2092 return p + 1;
2093 }
2094 else
2095 return skip_var_one(arg);
2096}
2097
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002098/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002099 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002100 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002101 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002102 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002103skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002104{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002105 if (*arg == '@' && arg[1] != NUL)
2106 return arg + 2;
2107 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2108 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002109}
2110
Bram Moolenaara7043832005-01-21 11:56:39 +00002111/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002112 * List variables for hashtab "ht" with prefix "prefix".
2113 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002114 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002116list_hashtable_vars(
2117 hashtab_T *ht,
2118 char_u *prefix,
2119 int empty,
2120 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002121{
Bram Moolenaar33570922005-01-25 22:26:29 +00002122 hashitem_T *hi;
2123 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002124 int todo;
2125
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002126 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002127 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2128 {
2129 if (!HASHITEM_EMPTY(hi))
2130 {
2131 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002132 di = HI2DI(hi);
2133 if (empty || di->di_tv.v_type != VAR_STRING
2134 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002135 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002136 }
2137 }
2138}
2139
2140/*
2141 * List global variables.
2142 */
2143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002144list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002145{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002146 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002147}
2148
2149/*
2150 * List buffer variables.
2151 */
2152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002153list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002154{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002155 char_u numbuf[NUMBUFLEN];
2156
Bram Moolenaar429fa852013-04-15 12:27:36 +02002157 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002159
2160 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2162 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002163}
2164
2165/*
2166 * List window variables.
2167 */
2168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002169list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002170{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002171 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002173}
2174
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002175#ifdef FEAT_WINDOWS
2176/*
2177 * List tab page variables.
2178 */
2179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002180list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002181{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002182 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002184}
2185#endif
2186
Bram Moolenaara7043832005-01-21 11:56:39 +00002187/*
2188 * List Vim variables.
2189 */
2190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002191list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002193 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194}
2195
2196/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002197 * List script-local variables, if there is a script.
2198 */
2199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002200list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002201{
2202 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002203 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2204 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002205}
2206
2207/*
2208 * List function variables, if there is a function.
2209 */
2210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002211list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002212{
2213 if (current_funccal != NULL)
2214 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002215 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002216}
2217
2218/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 * List variables in "arg".
2220 */
2221 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002222list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223{
2224 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002227 char_u *name_start;
2228 char_u *arg_subsc;
2229 char_u *tofree;
2230 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231
2232 while (!ends_excmd(*arg) && !got_int)
2233 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002234 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002236 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002237 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2238 {
2239 emsg_severe = TRUE;
2240 EMSG(_(e_trailing));
2241 break;
2242 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 /* get_name_len() takes care of expanding curly braces */
2247 name_start = name = arg;
2248 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2249 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002251 /* This is mainly to keep test 49 working: when expanding
2252 * curly braces fails overrule the exception error message. */
2253 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002255 emsg_severe = TRUE;
2256 EMSG2(_(e_invarg2), arg);
2257 break;
2258 }
2259 error = TRUE;
2260 }
2261 else
2262 {
2263 if (tofree != NULL)
2264 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002265 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267 else
2268 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002269 /* handle d.key, l[idx], f(expr) */
2270 arg_subsc = arg;
2271 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002272 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002274 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002275 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002276 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002277 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002278 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002279 case 'g': list_glob_vars(first); break;
2280 case 'b': list_buf_vars(first); break;
2281 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002282#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002283 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002284#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002285 case 'v': list_vim_vars(first); break;
2286 case 's': list_script_vars(first); break;
2287 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 default:
2289 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002290 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002291 }
2292 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002293 {
2294 char_u numbuf[NUMBUFLEN];
2295 char_u *tf;
2296 int c;
2297 char_u *s;
2298
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002299 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002300 c = *arg;
2301 *arg = NUL;
2302 list_one_var_a((char_u *)"",
2303 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002304 tv.v_type,
2305 s == NULL ? (char_u *)"" : s,
2306 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002307 *arg = c;
2308 vim_free(tf);
2309 }
2310 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002311 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312 }
2313 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002314
2315 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002317
2318 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 }
2320
2321 return arg;
2322}
2323
2324/*
2325 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2326 * Returns a pointer to the char just after the var name.
2327 * Returns NULL if there is an error.
2328 */
2329 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002330ex_let_one(
2331 char_u *arg, /* points to variable name */
2332 typval_T *tv, /* value to assign to variable */
2333 int copy, /* copy value from "tv" */
2334 char_u *endchars, /* valid chars after variable name or NULL */
2335 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002336{
2337 int c1;
2338 char_u *name;
2339 char_u *p;
2340 char_u *arg_end = NULL;
2341 int len;
2342 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002343 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344
2345 /*
2346 * ":let $VAR = expr": Set environment variable.
2347 */
2348 if (*arg == '$')
2349 {
2350 /* Find the end of the name. */
2351 ++arg;
2352 name = arg;
2353 len = get_env_len(&arg);
2354 if (len == 0)
2355 EMSG2(_(e_invarg2), name - 1);
2356 else
2357 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002358 if (op != NULL && (*op == '+' || *op == '-'))
2359 EMSG2(_(e_letwrong), op);
2360 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002361 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002362 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002363 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364 {
2365 c1 = name[len];
2366 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002367 p = get_tv_string_chk(tv);
2368 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002369 {
2370 int mustfree = FALSE;
2371 char_u *s = vim_getenv(name, &mustfree);
2372
2373 if (s != NULL)
2374 {
2375 p = tofree = concat_str(s, p);
2376 if (mustfree)
2377 vim_free(s);
2378 }
2379 }
2380 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002381 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002382 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002383 if (STRICMP(name, "HOME") == 0)
2384 init_homedir();
2385 else if (didset_vim && STRICMP(name, "VIM") == 0)
2386 didset_vim = FALSE;
2387 else if (didset_vimruntime
2388 && STRICMP(name, "VIMRUNTIME") == 0)
2389 didset_vimruntime = FALSE;
2390 arg_end = arg;
2391 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002392 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002393 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002394 }
2395 }
2396 }
2397
2398 /*
2399 * ":let &option = expr": Set option value.
2400 * ":let &l:option = expr": Set local option value.
2401 * ":let &g:option = expr": Set global option value.
2402 */
2403 else if (*arg == '&')
2404 {
2405 /* Find the end of the name. */
2406 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002407 if (p == NULL || (endchars != NULL
2408 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002409 EMSG(_(e_letunexp));
2410 else
2411 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002412 long n;
2413 int opt_type;
2414 long numval;
2415 char_u *stringval = NULL;
2416 char_u *s;
2417
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002418 c1 = *p;
2419 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002420
2421 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002422 s = get_tv_string_chk(tv); /* != NULL if number or string */
2423 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002424 {
2425 opt_type = get_option_value(arg, &numval,
2426 &stringval, opt_flags);
2427 if ((opt_type == 1 && *op == '.')
2428 || (opt_type == 0 && *op != '.'))
2429 EMSG2(_(e_letwrong), op);
2430 else
2431 {
2432 if (opt_type == 1) /* number */
2433 {
2434 if (*op == '+')
2435 n = numval + n;
2436 else
2437 n = numval - n;
2438 }
2439 else if (opt_type == 0 && stringval != NULL) /* string */
2440 {
2441 s = concat_str(stringval, s);
2442 vim_free(stringval);
2443 stringval = s;
2444 }
2445 }
2446 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002447 if (s != NULL)
2448 {
2449 set_option_value(arg, n, s, opt_flags);
2450 arg_end = p;
2451 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002452 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002453 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002454 }
2455 }
2456
2457 /*
2458 * ":let @r = expr": Set register contents.
2459 */
2460 else if (*arg == '@')
2461 {
2462 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002463 if (op != NULL && (*op == '+' || *op == '-'))
2464 EMSG2(_(e_letwrong), op);
2465 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002466 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 EMSG(_(e_letunexp));
2468 else
2469 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002470 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002471 char_u *s;
2472
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002473 p = get_tv_string_chk(tv);
2474 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002475 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002476 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002477 if (s != NULL)
2478 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002479 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 vim_free(s);
2481 }
2482 }
2483 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002484 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002485 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002486 arg_end = arg + 1;
2487 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002488 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002489 }
2490 }
2491
2492 /*
2493 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002494 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002495 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002496 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002498 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002499
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002500 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002502 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2504 EMSG(_(e_letunexp));
2505 else
2506 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002507 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508 arg_end = p;
2509 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002512 }
2513
2514 else
2515 EMSG2(_(e_invarg2), arg);
2516
2517 return arg_end;
2518}
2519
2520/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002521 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2522 */
2523 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002524check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002525{
2526 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2527 {
2528 EMSG2(_(e_readonlyvar), arg);
2529 return TRUE;
2530 }
2531 return FALSE;
2532}
2533
2534/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 * Get an lval: variable, Dict item or List item that can be assigned a value
2536 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2537 * "name.key", "name.key[expr]" etc.
2538 * Indexing only works if "name" is an existing List or Dictionary.
2539 * "name" points to the start of the name.
2540 * If "rettv" is not NULL it points to the value to be assigned.
2541 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2542 * wrong; must end in space or cmd separator.
2543 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002544 * flags:
2545 * GLV_QUIET: do not give error messages
2546 * GLV_NO_AUTOLOAD: do not use script autoloading
2547 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002549 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002551 */
2552 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002553get_lval(
2554 char_u *name,
2555 typval_T *rettv,
2556 lval_T *lp,
2557 int unlet,
2558 int skip,
2559 int flags, /* GLV_ values */
2560 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002561{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002562 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 char_u *expr_start, *expr_end;
2564 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002565 dictitem_T *v;
2566 typval_T var1;
2567 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002569 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002570 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002571 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002572 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002573 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002576 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577
2578 if (skip)
2579 {
2580 /* When skipping just find the end of the name. */
2581 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002582 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 }
2584
2585 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002586 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 if (expr_start != NULL)
2588 {
2589 /* Don't expand the name when we already know there is an error. */
2590 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2591 && *p != '[' && *p != '.')
2592 {
2593 EMSG(_(e_trailing));
2594 return NULL;
2595 }
2596
2597 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2598 if (lp->ll_exp_name == NULL)
2599 {
2600 /* Report an invalid expression in braces, unless the
2601 * expression evaluation has been cancelled due to an
2602 * aborting error, an interrupt, or an exception. */
2603 if (!aborting() && !quiet)
2604 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002605 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 EMSG2(_(e_invarg2), name);
2607 return NULL;
2608 }
2609 }
2610 lp->ll_name = lp->ll_exp_name;
2611 }
2612 else
2613 lp->ll_name = name;
2614
2615 /* Without [idx] or .key we are done. */
2616 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2617 return p;
2618
2619 cc = *p;
2620 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002621 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 if (v == NULL && !quiet)
2623 EMSG2(_(e_undefvar), lp->ll_name);
2624 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002625 if (v == NULL)
2626 return NULL;
2627
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 /*
2629 * Loop until no more [idx] or .key is following.
2630 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002631 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2635 && !(lp->ll_tv->v_type == VAR_DICT
2636 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002637 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 if (!quiet)
2639 EMSG(_("E689: Can only index a List or Dictionary"));
2640 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002641 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002643 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 if (!quiet)
2645 EMSG(_("E708: [:] must come last"));
2646 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002647 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002648
Bram Moolenaar8c711452005-01-14 21:53:12 +00002649 len = -1;
2650 if (*p == '.')
2651 {
2652 key = p + 1;
2653 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2654 ;
2655 if (len == 0)
2656 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 if (!quiet)
2658 EMSG(_(e_emptykey));
2659 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 }
2661 p = key + len;
2662 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002663 else
2664 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002665 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002666 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 if (*p == ':')
2668 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002669 else
2670 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 empty1 = FALSE;
2672 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002674 if (get_tv_string_chk(&var1) == NULL)
2675 {
2676 /* not a number or string */
2677 clear_tv(&var1);
2678 return NULL;
2679 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 }
2681
2682 /* Optionally get the second index [ :expr]. */
2683 if (*p == ':')
2684 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002688 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002689 if (!empty1)
2690 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 if (rettv != NULL && (rettv->v_type != VAR_LIST
2694 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (!quiet)
2697 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 if (!empty1)
2699 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 }
2702 p = skipwhite(p + 1);
2703 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 else
2706 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2709 {
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 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002714 if (get_tv_string_chk(&var2) == NULL)
2715 {
2716 /* not a number or string */
2717 if (!empty1)
2718 clear_tv(&var1);
2719 clear_tv(&var2);
2720 return NULL;
2721 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002724 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002727
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002729 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 if (!quiet)
2731 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 if (!empty1)
2733 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 }
2738
2739 /* Skip to past ']'. */
2740 ++p;
2741 }
2742
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 {
2745 if (len == -1)
2746 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002748 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 if (*key == NUL)
2750 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 if (!quiet)
2752 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 }
2756 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002757 lp->ll_list = NULL;
2758 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002759 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002760
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002761 /* When assigning to a scope dictionary check that a function and
2762 * variable name is valid (only variable name unless it is l: or
2763 * g: dictionary). Disallow overwriting a builtin function. */
2764 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002765 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002766 int prevval;
2767 int wrong;
2768
2769 if (len != -1)
2770 {
2771 prevval = key[len];
2772 key[len] = NUL;
2773 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002774 else
2775 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002776 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2777 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002778 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002779 || !valid_varname(key);
2780 if (len != -1)
2781 key[len] = prevval;
2782 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002783 return NULL;
2784 }
2785
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002788 /* Can't add "v:" variable. */
2789 if (lp->ll_dict == &vimvardict)
2790 {
2791 EMSG2(_(e_illvar), name);
2792 return NULL;
2793 }
2794
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002795 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002799 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002800 if (len == -1)
2801 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002802 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002803 }
2804 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002807 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 if (len == -1)
2809 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 p = NULL;
2812 break;
2813 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002814 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002815 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002816 return NULL;
2817
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 if (len == -1)
2819 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 }
2822 else
2823 {
2824 /*
2825 * Get the number and item for the only or first index of the List.
2826 */
2827 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002829 else
2830 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002831 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 clear_tv(&var1);
2833 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002834 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835 lp->ll_list = lp->ll_tv->vval.v_list;
2836 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2837 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002839 if (lp->ll_n1 < 0)
2840 {
2841 lp->ll_n1 = 0;
2842 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2843 }
2844 }
2845 if (lp->ll_li == NULL)
2846 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002849 if (!quiet)
2850 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 }
2853
2854 /*
2855 * May need to find the item or absolute index for the second
2856 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 * When no index given: "lp->ll_empty2" is TRUE.
2858 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002862 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002865 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002868 {
2869 if (!quiet)
2870 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002872 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 }
2875
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2877 if (lp->ll_n1 < 0)
2878 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2879 if (lp->ll_n2 < lp->ll_n1)
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 Moolenaar6cc16192005-01-08 21:49:45 +00002885 }
2886
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002888 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002889 }
2890
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 return p;
2892}
2893
2894/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002895 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 */
2897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002898clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899{
2900 vim_free(lp->ll_exp_name);
2901 vim_free(lp->ll_newkey);
2902}
2903
2904/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002905 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002907 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 */
2909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002910set_var_lval(
2911 lval_T *lp,
2912 char_u *endp,
2913 typval_T *rettv,
2914 int copy,
2915 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916{
2917 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002918 listitem_T *ri;
2919 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920
2921 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002922 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002924 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 cc = *endp;
2926 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002927 if (op != NULL && *op != '=')
2928 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002929 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002930
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002931 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002932 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002933 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002934 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002935 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002936 if ((di == NULL
2937 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2938 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2939 FALSE)))
2940 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002941 set_var(lp->ll_name, &tv, FALSE);
2942 clear_tv(&tv);
2943 }
2944 }
2945 else
2946 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002948 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002950 else if (tv_check_lock(lp->ll_newkey == NULL
2951 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002952 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002953 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954 else if (lp->ll_range)
2955 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002956 listitem_T *ll_li = lp->ll_li;
2957 int ll_n1 = lp->ll_n1;
2958
2959 /*
2960 * Check whether any of the list items is locked
2961 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002962 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002963 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002964 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002965 return;
2966 ri = ri->li_next;
2967 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2968 break;
2969 ll_li = ll_li->li_next;
2970 ++ll_n1;
2971 }
2972
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002973 /*
2974 * Assign the List values to the list items.
2975 */
2976 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002977 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002978 if (op != NULL && *op != '=')
2979 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2980 else
2981 {
2982 clear_tv(&lp->ll_li->li_tv);
2983 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2984 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002985 ri = ri->li_next;
2986 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2987 break;
2988 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002989 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002991 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002992 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002993 ri = NULL;
2994 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002995 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002996 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002997 lp->ll_li = lp->ll_li->li_next;
2998 ++lp->ll_n1;
2999 }
3000 if (ri != NULL)
3001 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003002 else if (lp->ll_empty2
3003 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003004 : lp->ll_n1 != lp->ll_n2)
3005 EMSG(_("E711: List value has not enough items"));
3006 }
3007 else
3008 {
3009 /*
3010 * Assign to a List or Dictionary item.
3011 */
3012 if (lp->ll_newkey != NULL)
3013 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003014 if (op != NULL && *op != '=')
3015 {
3016 EMSG2(_(e_letwrong), op);
3017 return;
3018 }
3019
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003020 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003021 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003022 if (di == NULL)
3023 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003024 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3025 {
3026 vim_free(di);
3027 return;
3028 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003029 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003030 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003031 else if (op != NULL && *op != '=')
3032 {
3033 tv_op(lp->ll_tv, rettv, op);
3034 return;
3035 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003036 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003037 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003038
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003039 /*
3040 * Assign the value to the variable or list item.
3041 */
3042 if (copy)
3043 copy_tv(rettv, lp->ll_tv);
3044 else
3045 {
3046 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003047 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003048 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003049 }
3050 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003051}
3052
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003053/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003054 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3055 * Returns OK or FAIL.
3056 */
3057 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003058tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003059{
3060 long n;
3061 char_u numbuf[NUMBUFLEN];
3062 char_u *s;
3063
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003064 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3065 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3066 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003067 {
3068 switch (tv1->v_type)
3069 {
3070 case VAR_DICT:
3071 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003072 case VAR_SPECIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003073 break;
3074
3075 case VAR_LIST:
3076 if (*op != '+' || tv2->v_type != VAR_LIST)
3077 break;
3078 /* List += List */
3079 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3080 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3081 return OK;
3082
3083 case VAR_NUMBER:
3084 case VAR_STRING:
3085 if (tv2->v_type == VAR_LIST)
3086 break;
3087 if (*op == '+' || *op == '-')
3088 {
3089 /* nr += nr or nr -= nr*/
3090 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003091#ifdef FEAT_FLOAT
3092 if (tv2->v_type == VAR_FLOAT)
3093 {
3094 float_T f = n;
3095
3096 if (*op == '+')
3097 f += tv2->vval.v_float;
3098 else
3099 f -= tv2->vval.v_float;
3100 clear_tv(tv1);
3101 tv1->v_type = VAR_FLOAT;
3102 tv1->vval.v_float = f;
3103 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003104 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003105#endif
3106 {
3107 if (*op == '+')
3108 n += get_tv_number(tv2);
3109 else
3110 n -= get_tv_number(tv2);
3111 clear_tv(tv1);
3112 tv1->v_type = VAR_NUMBER;
3113 tv1->vval.v_number = n;
3114 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003115 }
3116 else
3117 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003118 if (tv2->v_type == VAR_FLOAT)
3119 break;
3120
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003121 /* str .= str */
3122 s = get_tv_string(tv1);
3123 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3124 clear_tv(tv1);
3125 tv1->v_type = VAR_STRING;
3126 tv1->vval.v_string = s;
3127 }
3128 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003129
3130#ifdef FEAT_FLOAT
3131 case VAR_FLOAT:
3132 {
3133 float_T f;
3134
3135 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3136 && tv2->v_type != VAR_NUMBER
3137 && tv2->v_type != VAR_STRING))
3138 break;
3139 if (tv2->v_type == VAR_FLOAT)
3140 f = tv2->vval.v_float;
3141 else
3142 f = get_tv_number(tv2);
3143 if (*op == '+')
3144 tv1->vval.v_float += f;
3145 else
3146 tv1->vval.v_float -= f;
3147 }
3148 return OK;
3149#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003150 }
3151 }
3152
3153 EMSG2(_(e_letwrong), op);
3154 return FAIL;
3155}
3156
3157/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003158 * Add a watcher to a list.
3159 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003160 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003161list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003162{
3163 lw->lw_next = l->lv_watch;
3164 l->lv_watch = lw;
3165}
3166
3167/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003168 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003169 * No warning when it isn't found...
3170 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003171 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003172list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003173{
Bram Moolenaar33570922005-01-25 22:26:29 +00003174 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003175
3176 lwp = &l->lv_watch;
3177 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3178 {
3179 if (lw == lwrem)
3180 {
3181 *lwp = lw->lw_next;
3182 break;
3183 }
3184 lwp = &lw->lw_next;
3185 }
3186}
3187
3188/*
3189 * Just before removing an item from a list: advance watchers to the next
3190 * item.
3191 */
3192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003193list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194{
Bram Moolenaar33570922005-01-25 22:26:29 +00003195 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003196
3197 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3198 if (lw->lw_item == item)
3199 lw->lw_item = item->li_next;
3200}
3201
3202/*
3203 * Evaluate the expression used in a ":for var in expr" command.
3204 * "arg" points to "var".
3205 * Set "*errp" to TRUE for an error, FALSE otherwise;
3206 * Return a pointer that holds the info. Null when there is an error.
3207 */
3208 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003209eval_for_line(
3210 char_u *arg,
3211 int *errp,
3212 char_u **nextcmdp,
3213 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214{
Bram Moolenaar33570922005-01-25 22:26:29 +00003215 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003216 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003217 typval_T tv;
3218 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219
3220 *errp = TRUE; /* default: there is an error */
3221
Bram Moolenaar33570922005-01-25 22:26:29 +00003222 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003223 if (fi == NULL)
3224 return NULL;
3225
3226 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3227 if (expr == NULL)
3228 return fi;
3229
3230 expr = skipwhite(expr);
3231 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3232 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003233 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003234 return fi;
3235 }
3236
3237 if (skip)
3238 ++emsg_skip;
3239 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3240 {
3241 *errp = FALSE;
3242 if (!skip)
3243 {
3244 l = tv.vval.v_list;
3245 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003246 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003247 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003248 clear_tv(&tv);
3249 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003250 else
3251 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003252 /* No need to increment the refcount, it's already set for the
3253 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003254 fi->fi_list = l;
3255 list_add_watch(l, &fi->fi_lw);
3256 fi->fi_lw.lw_item = l->lv_first;
3257 }
3258 }
3259 }
3260 if (skip)
3261 --emsg_skip;
3262
3263 return fi;
3264}
3265
3266/*
3267 * Use the first item in a ":for" list. Advance to the next.
3268 * Assign the values to the variable (list). "arg" points to the first one.
3269 * Return TRUE when a valid item was found, FALSE when at end of list or
3270 * something wrong.
3271 */
3272 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003273next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003274{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003275 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003277 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003278
3279 item = fi->fi_lw.lw_item;
3280 if (item == NULL)
3281 result = FALSE;
3282 else
3283 {
3284 fi->fi_lw.lw_item = item->li_next;
3285 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3286 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3287 }
3288 return result;
3289}
3290
3291/*
3292 * Free the structure used to store info used by ":for".
3293 */
3294 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003295free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003296{
Bram Moolenaar33570922005-01-25 22:26:29 +00003297 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003298
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003299 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003300 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003301 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003302 list_unref(fi->fi_list);
3303 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003304 vim_free(fi);
3305}
3306
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3308
3309 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003310set_context_for_expression(
3311 expand_T *xp,
3312 char_u *arg,
3313 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314{
3315 int got_eq = FALSE;
3316 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003317 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003319 if (cmdidx == CMD_let)
3320 {
3321 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003322 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003323 {
3324 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003325 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003326 {
3327 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003328 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003329 if (vim_iswhite(*p))
3330 break;
3331 }
3332 return;
3333 }
3334 }
3335 else
3336 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3337 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 while ((xp->xp_pattern = vim_strpbrk(arg,
3339 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3340 {
3341 c = *xp->xp_pattern;
3342 if (c == '&')
3343 {
3344 c = xp->xp_pattern[1];
3345 if (c == '&')
3346 {
3347 ++xp->xp_pattern;
3348 xp->xp_context = cmdidx != CMD_let || got_eq
3349 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3350 }
3351 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003352 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003354 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3355 xp->xp_pattern += 2;
3356
3357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 }
3359 else if (c == '$')
3360 {
3361 /* environment variable */
3362 xp->xp_context = EXPAND_ENV_VARS;
3363 }
3364 else if (c == '=')
3365 {
3366 got_eq = TRUE;
3367 xp->xp_context = EXPAND_EXPRESSION;
3368 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003369 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 && xp->xp_context == EXPAND_FUNCTIONS
3371 && vim_strchr(xp->xp_pattern, '(') == NULL)
3372 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003373 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 break;
3375 }
3376 else if (cmdidx != CMD_let || got_eq)
3377 {
3378 if (c == '"') /* string */
3379 {
3380 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3381 if (c == '\\' && xp->xp_pattern[1] != NUL)
3382 ++xp->xp_pattern;
3383 xp->xp_context = EXPAND_NOTHING;
3384 }
3385 else if (c == '\'') /* literal string */
3386 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003387 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3389 /* skip */ ;
3390 xp->xp_context = EXPAND_NOTHING;
3391 }
3392 else if (c == '|')
3393 {
3394 if (xp->xp_pattern[1] == '|')
3395 {
3396 ++xp->xp_pattern;
3397 xp->xp_context = EXPAND_EXPRESSION;
3398 }
3399 else
3400 xp->xp_context = EXPAND_COMMANDS;
3401 }
3402 else
3403 xp->xp_context = EXPAND_EXPRESSION;
3404 }
3405 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003406 /* Doesn't look like something valid, expand as an expression
3407 * anyway. */
3408 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 arg = xp->xp_pattern;
3410 if (*arg != NUL)
3411 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3412 /* skip */ ;
3413 }
3414 xp->xp_pattern = arg;
3415}
3416
3417#endif /* FEAT_CMDL_COMPL */
3418
3419/*
3420 * ":1,25call func(arg1, arg2)" function call.
3421 */
3422 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003423ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424{
3425 char_u *arg = eap->arg;
3426 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003428 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003430 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 linenr_T lnum;
3432 int doesrange;
3433 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003434 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003436 if (eap->skip)
3437 {
3438 /* trans_function_name() doesn't work well when skipping, use eval0()
3439 * instead to skip to any following command, e.g. for:
3440 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003441 ++emsg_skip;
3442 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3443 clear_tv(&rettv);
3444 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003445 return;
3446 }
3447
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003448 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003449 if (fudi.fd_newkey != NULL)
3450 {
3451 /* Still need to give an error message for missing key. */
3452 EMSG2(_(e_dictkey), fudi.fd_newkey);
3453 vim_free(fudi.fd_newkey);
3454 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003455 if (tofree == NULL)
3456 return;
3457
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003458 /* Increase refcount on dictionary, it could get deleted when evaluating
3459 * the arguments. */
3460 if (fudi.fd_dict != NULL)
3461 ++fudi.fd_dict->dv_refcount;
3462
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003463 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003464 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003465 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
Bram Moolenaar532c7802005-01-27 14:44:31 +00003467 /* Skip white space to allow ":call func ()". Not good, but required for
3468 * backward compatibility. */
3469 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003470 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471
3472 if (*startarg != '(')
3473 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003474 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 goto end;
3476 }
3477
3478 /*
3479 * When skipping, evaluate the function once, to find the end of the
3480 * arguments.
3481 * When the function takes a range, this is discovered after the first
3482 * call, and the loop is broken.
3483 */
3484 if (eap->skip)
3485 {
3486 ++emsg_skip;
3487 lnum = eap->line2; /* do it once, also with an invalid range */
3488 }
3489 else
3490 lnum = eap->line1;
3491 for ( ; lnum <= eap->line2; ++lnum)
3492 {
3493 if (!eap->skip && eap->addr_count > 0)
3494 {
3495 curwin->w_cursor.lnum = lnum;
3496 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003497#ifdef FEAT_VIRTUALEDIT
3498 curwin->w_cursor.coladd = 0;
3499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 }
3501 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003502 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003503 eap->line1, eap->line2, &doesrange,
3504 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
3506 failed = TRUE;
3507 break;
3508 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003509
3510 /* Handle a function returning a Funcref, Dictionary or List. */
3511 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3512 {
3513 failed = TRUE;
3514 break;
3515 }
3516
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003517 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 if (doesrange || eap->skip)
3519 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003520
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003522 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003523 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003524 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 if (aborting())
3526 break;
3527 }
3528 if (eap->skip)
3529 --emsg_skip;
3530
3531 if (!failed)
3532 {
3533 /* Check for trailing illegal characters and a following command. */
3534 if (!ends_excmd(*arg))
3535 {
3536 emsg_severe = TRUE;
3537 EMSG(_(e_trailing));
3538 }
3539 else
3540 eap->nextcmd = check_nextcmd(arg);
3541 }
3542
3543end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003544 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003545 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546}
3547
3548/*
3549 * ":unlet[!] var1 ... " command.
3550 */
3551 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003552ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003554 ex_unletlock(eap, eap->arg, 0);
3555}
3556
3557/*
3558 * ":lockvar" and ":unlockvar" commands
3559 */
3560 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003561ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003562{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003564 int deep = 2;
3565
3566 if (eap->forceit)
3567 deep = -1;
3568 else if (vim_isdigit(*arg))
3569 {
3570 deep = getdigits(&arg);
3571 arg = skipwhite(arg);
3572 }
3573
3574 ex_unletlock(eap, arg, deep);
3575}
3576
3577/*
3578 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3579 */
3580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003581ex_unletlock(
3582 exarg_T *eap,
3583 char_u *argstart,
3584 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003585{
3586 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003589 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590
3591 do
3592 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003593 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003594 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003595 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003596 if (lv.ll_name == NULL)
3597 error = TRUE; /* error but continue parsing */
3598 if (name_end == NULL || (!vim_iswhite(*name_end)
3599 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003601 if (name_end != NULL)
3602 {
3603 emsg_severe = TRUE;
3604 EMSG(_(e_trailing));
3605 }
3606 if (!(eap->skip || error))
3607 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 break;
3609 }
3610
3611 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003612 {
3613 if (eap->cmdidx == CMD_unlet)
3614 {
3615 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3616 error = TRUE;
3617 }
3618 else
3619 {
3620 if (do_lock_var(&lv, name_end, deep,
3621 eap->cmdidx == CMD_lockvar) == FAIL)
3622 error = TRUE;
3623 }
3624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003626 if (!eap->skip)
3627 clear_lval(&lv);
3628
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 arg = skipwhite(name_end);
3630 } while (!ends_excmd(*arg));
3631
3632 eap->nextcmd = check_nextcmd(arg);
3633}
3634
Bram Moolenaar8c711452005-01-14 21:53:12 +00003635 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003636do_unlet_var(
3637 lval_T *lp,
3638 char_u *name_end,
3639 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003640{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003641 int ret = OK;
3642 int cc;
3643
3644 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003645 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003646 cc = *name_end;
3647 *name_end = NUL;
3648
3649 /* Normal name or expanded name. */
3650 if (check_changedtick(lp->ll_name))
3651 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003652 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003653 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003654 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003655 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003656 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003657 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003658 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003659 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003660 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003661 else if (lp->ll_range)
3662 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003663 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003664 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003665 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003666
3667 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3668 {
3669 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003670 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003671 return FAIL;
3672 ll_li = li;
3673 ++ll_n1;
3674 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003675
3676 /* Delete a range of List items. */
3677 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3678 {
3679 li = lp->ll_li->li_next;
3680 listitem_remove(lp->ll_list, lp->ll_li);
3681 lp->ll_li = li;
3682 ++lp->ll_n1;
3683 }
3684 }
3685 else
3686 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003687 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003688 /* unlet a List item. */
3689 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003690 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003691 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003692 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003693 }
3694
3695 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003696}
3697
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698/*
3699 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003700 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 */
3702 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003703do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704{
Bram Moolenaar33570922005-01-25 22:26:29 +00003705 hashtab_T *ht;
3706 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003707 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003708 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003709 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710
Bram Moolenaar33570922005-01-25 22:26:29 +00003711 ht = find_var_ht(name, &varname);
3712 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003714 if (ht == &globvarht)
3715 d = &globvardict;
3716 else if (current_funccal != NULL
3717 && ht == &current_funccal->l_vars.dv_hashtab)
3718 d = &current_funccal->l_vars;
3719 else if (ht == &compat_hashtab)
3720 d = &vimvardict;
3721 else
3722 {
3723 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3724 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3725 }
3726 if (d == NULL)
3727 {
3728 EMSG2(_(e_intern2), "do_unlet()");
3729 return FAIL;
3730 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003731 hi = hash_find(ht, varname);
3732 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003733 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003734 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003735 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003736 || var_check_ro(di->di_flags, name, FALSE)
3737 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003738 return FAIL;
3739
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003740 delete_var(ht, hi);
3741 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003744 if (forceit)
3745 return OK;
3746 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 return FAIL;
3748}
3749
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003750/*
3751 * Lock or unlock variable indicated by "lp".
3752 * "deep" is the levels to go (-1 for unlimited);
3753 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3754 */
3755 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003756do_lock_var(
3757 lval_T *lp,
3758 char_u *name_end,
3759 int deep,
3760 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003761{
3762 int ret = OK;
3763 int cc;
3764 dictitem_T *di;
3765
3766 if (deep == 0) /* nothing to do */
3767 return OK;
3768
3769 if (lp->ll_tv == NULL)
3770 {
3771 cc = *name_end;
3772 *name_end = NUL;
3773
3774 /* Normal name or expanded name. */
3775 if (check_changedtick(lp->ll_name))
3776 ret = FAIL;
3777 else
3778 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003779 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003780 if (di == NULL)
3781 ret = FAIL;
3782 else
3783 {
3784 if (lock)
3785 di->di_flags |= DI_FLAGS_LOCK;
3786 else
3787 di->di_flags &= ~DI_FLAGS_LOCK;
3788 item_lock(&di->di_tv, deep, lock);
3789 }
3790 }
3791 *name_end = cc;
3792 }
3793 else if (lp->ll_range)
3794 {
3795 listitem_T *li = lp->ll_li;
3796
3797 /* (un)lock a range of List items. */
3798 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3799 {
3800 item_lock(&li->li_tv, deep, lock);
3801 li = li->li_next;
3802 ++lp->ll_n1;
3803 }
3804 }
3805 else if (lp->ll_list != NULL)
3806 /* (un)lock a List item. */
3807 item_lock(&lp->ll_li->li_tv, deep, lock);
3808 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003809 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003810 item_lock(&lp->ll_di->di_tv, deep, lock);
3811
3812 return ret;
3813}
3814
3815/*
3816 * Lock or unlock an item. "deep" is nr of levels to go.
3817 */
3818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003819item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003820{
3821 static int recurse = 0;
3822 list_T *l;
3823 listitem_T *li;
3824 dict_T *d;
3825 hashitem_T *hi;
3826 int todo;
3827
3828 if (recurse >= DICT_MAXNEST)
3829 {
3830 EMSG(_("E743: variable nested too deep for (un)lock"));
3831 return;
3832 }
3833 if (deep == 0)
3834 return;
3835 ++recurse;
3836
3837 /* lock/unlock the item itself */
3838 if (lock)
3839 tv->v_lock |= VAR_LOCKED;
3840 else
3841 tv->v_lock &= ~VAR_LOCKED;
3842
3843 switch (tv->v_type)
3844 {
3845 case VAR_LIST:
3846 if ((l = tv->vval.v_list) != NULL)
3847 {
3848 if (lock)
3849 l->lv_lock |= VAR_LOCKED;
3850 else
3851 l->lv_lock &= ~VAR_LOCKED;
3852 if (deep < 0 || deep > 1)
3853 /* recursive: lock/unlock the items the List contains */
3854 for (li = l->lv_first; li != NULL; li = li->li_next)
3855 item_lock(&li->li_tv, deep - 1, lock);
3856 }
3857 break;
3858 case VAR_DICT:
3859 if ((d = tv->vval.v_dict) != NULL)
3860 {
3861 if (lock)
3862 d->dv_lock |= VAR_LOCKED;
3863 else
3864 d->dv_lock &= ~VAR_LOCKED;
3865 if (deep < 0 || deep > 1)
3866 {
3867 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003868 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003869 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3870 {
3871 if (!HASHITEM_EMPTY(hi))
3872 {
3873 --todo;
3874 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3875 }
3876 }
3877 }
3878 }
3879 }
3880 --recurse;
3881}
3882
Bram Moolenaara40058a2005-07-11 22:42:07 +00003883/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003884 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3885 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003886 */
3887 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003888tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003889{
3890 return (tv->v_lock & VAR_LOCKED)
3891 || (tv->v_type == VAR_LIST
3892 && tv->vval.v_list != NULL
3893 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3894 || (tv->v_type == VAR_DICT
3895 && tv->vval.v_dict != NULL
3896 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3897}
3898
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3900/*
3901 * Delete all "menutrans_" variables.
3902 */
3903 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003904del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905{
Bram Moolenaar33570922005-01-25 22:26:29 +00003906 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003907 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908
Bram Moolenaar33570922005-01-25 22:26:29 +00003909 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003910 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003911 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003912 {
3913 if (!HASHITEM_EMPTY(hi))
3914 {
3915 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003916 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3917 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003918 }
3919 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003920 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921}
3922#endif
3923
3924#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3925
3926/*
3927 * Local string buffer for the next two functions to store a variable name
3928 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3929 * get_user_var_name().
3930 */
3931
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003932static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933
3934static char_u *varnamebuf = NULL;
3935static int varnamebuflen = 0;
3936
3937/*
3938 * Function to concatenate a prefix and a variable name.
3939 */
3940 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003941cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942{
3943 int len;
3944
3945 len = (int)STRLEN(name) + 3;
3946 if (len > varnamebuflen)
3947 {
3948 vim_free(varnamebuf);
3949 len += 10; /* some additional space */
3950 varnamebuf = alloc(len);
3951 if (varnamebuf == NULL)
3952 {
3953 varnamebuflen = 0;
3954 return NULL;
3955 }
3956 varnamebuflen = len;
3957 }
3958 *varnamebuf = prefix;
3959 varnamebuf[1] = ':';
3960 STRCPY(varnamebuf + 2, name);
3961 return varnamebuf;
3962}
3963
3964/*
3965 * Function given to ExpandGeneric() to obtain the list of user defined
3966 * (global/buffer/window/built-in) variable names.
3967 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003969get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003971 static long_u gdone;
3972 static long_u bdone;
3973 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003974#ifdef FEAT_WINDOWS
3975 static long_u tdone;
3976#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003977 static int vidx;
3978 static hashitem_T *hi;
3979 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980
3981 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003982 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003983 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003984#ifdef FEAT_WINDOWS
3985 tdone = 0;
3986#endif
3987 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003988
3989 /* Global variables */
3990 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003992 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003993 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003994 else
3995 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003996 while (HASHITEM_EMPTY(hi))
3997 ++hi;
3998 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3999 return cat_prefix_varname('g', hi->hi_key);
4000 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004002
4003 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004004 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004005 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004007 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004008 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004009 else
4010 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004011 while (HASHITEM_EMPTY(hi))
4012 ++hi;
4013 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004015 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004017 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 return (char_u *)"b:changedtick";
4019 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004020
4021 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004022 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004023 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004025 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004026 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004027 else
4028 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004029 while (HASHITEM_EMPTY(hi))
4030 ++hi;
4031 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004033
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004034#ifdef FEAT_WINDOWS
4035 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004036 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004037 if (tdone < ht->ht_used)
4038 {
4039 if (tdone++ == 0)
4040 hi = ht->ht_array;
4041 else
4042 ++hi;
4043 while (HASHITEM_EMPTY(hi))
4044 ++hi;
4045 return cat_prefix_varname('t', hi->hi_key);
4046 }
4047#endif
4048
Bram Moolenaar33570922005-01-25 22:26:29 +00004049 /* v: variables */
4050 if (vidx < VV_LEN)
4051 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052
4053 vim_free(varnamebuf);
4054 varnamebuf = NULL;
4055 varnamebuflen = 0;
4056 return NULL;
4057}
4058
4059#endif /* FEAT_CMDL_COMPL */
4060
4061/*
4062 * types for expressions.
4063 */
4064typedef enum
4065{
4066 TYPE_UNKNOWN = 0
4067 , TYPE_EQUAL /* == */
4068 , TYPE_NEQUAL /* != */
4069 , TYPE_GREATER /* > */
4070 , TYPE_GEQUAL /* >= */
4071 , TYPE_SMALLER /* < */
4072 , TYPE_SEQUAL /* <= */
4073 , TYPE_MATCH /* =~ */
4074 , TYPE_NOMATCH /* !~ */
4075} exptype_T;
4076
4077/*
4078 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004079 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4081 */
4082
4083/*
4084 * Handle zero level expression.
4085 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004086 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004087 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 * Return OK or FAIL.
4089 */
4090 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004091eval0(
4092 char_u *arg,
4093 typval_T *rettv,
4094 char_u **nextcmd,
4095 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096{
4097 int ret;
4098 char_u *p;
4099
4100 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004101 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 if (ret == FAIL || !ends_excmd(*p))
4103 {
4104 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004105 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 /*
4107 * Report the invalid expression unless the expression evaluation has
4108 * been cancelled due to an aborting error, an interrupt, or an
4109 * exception.
4110 */
4111 if (!aborting())
4112 EMSG2(_(e_invexpr2), arg);
4113 ret = FAIL;
4114 }
4115 if (nextcmd != NULL)
4116 *nextcmd = check_nextcmd(p);
4117
4118 return ret;
4119}
4120
4121/*
4122 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004123 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 *
4125 * "arg" must point to the first non-white of the expression.
4126 * "arg" is advanced to the next non-white after the recognized expression.
4127 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004128 * Note: "rettv.v_lock" is not set.
4129 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 * Return OK or FAIL.
4131 */
4132 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004133eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134{
4135 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004136 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137
4138 /*
4139 * Get the first variable.
4140 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004141 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 return FAIL;
4143
4144 if ((*arg)[0] == '?')
4145 {
4146 result = FALSE;
4147 if (evaluate)
4148 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004149 int error = FALSE;
4150
4151 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004153 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004154 if (error)
4155 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 }
4157
4158 /*
4159 * Get the second variable.
4160 */
4161 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004162 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 return FAIL;
4164
4165 /*
4166 * Check for the ":".
4167 */
4168 if ((*arg)[0] != ':')
4169 {
4170 EMSG(_("E109: Missing ':' after '?'"));
4171 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004172 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 return FAIL;
4174 }
4175
4176 /*
4177 * Get the third variable.
4178 */
4179 *arg = skipwhite(*arg + 1);
4180 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4181 {
4182 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004183 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 return FAIL;
4185 }
4186 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004187 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 }
4189
4190 return OK;
4191}
4192
4193/*
4194 * Handle first level expression:
4195 * expr2 || expr2 || expr2 logical OR
4196 *
4197 * "arg" must point to the first non-white of the expression.
4198 * "arg" is advanced to the next non-white after the recognized expression.
4199 *
4200 * Return OK or FAIL.
4201 */
4202 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004203eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204{
Bram Moolenaar33570922005-01-25 22:26:29 +00004205 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 long result;
4207 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004208 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209
4210 /*
4211 * Get the first variable.
4212 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 return FAIL;
4215
4216 /*
4217 * Repeat until there is no following "||".
4218 */
4219 first = TRUE;
4220 result = FALSE;
4221 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4222 {
4223 if (evaluate && first)
4224 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004225 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004227 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004228 if (error)
4229 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 first = FALSE;
4231 }
4232
4233 /*
4234 * Get the second variable.
4235 */
4236 *arg = skipwhite(*arg + 2);
4237 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4238 return FAIL;
4239
4240 /*
4241 * Compute the result.
4242 */
4243 if (evaluate && !result)
4244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004245 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004247 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004248 if (error)
4249 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 }
4251 if (evaluate)
4252 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004253 rettv->v_type = VAR_NUMBER;
4254 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 }
4256 }
4257
4258 return OK;
4259}
4260
4261/*
4262 * Handle second level expression:
4263 * expr3 && expr3 && expr3 logical AND
4264 *
4265 * "arg" must point to the first non-white of the expression.
4266 * "arg" is advanced to the next non-white after the recognized expression.
4267 *
4268 * Return OK or FAIL.
4269 */
4270 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004271eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272{
Bram Moolenaar33570922005-01-25 22:26:29 +00004273 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 long result;
4275 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004276 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277
4278 /*
4279 * Get the first variable.
4280 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004281 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 return FAIL;
4283
4284 /*
4285 * Repeat until there is no following "&&".
4286 */
4287 first = TRUE;
4288 result = TRUE;
4289 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4290 {
4291 if (evaluate && first)
4292 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004293 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004295 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004296 if (error)
4297 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 first = FALSE;
4299 }
4300
4301 /*
4302 * Get the second variable.
4303 */
4304 *arg = skipwhite(*arg + 2);
4305 if (eval4(arg, &var2, evaluate && result) == FAIL)
4306 return FAIL;
4307
4308 /*
4309 * Compute the result.
4310 */
4311 if (evaluate && result)
4312 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004313 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004315 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004316 if (error)
4317 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 }
4319 if (evaluate)
4320 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004321 rettv->v_type = VAR_NUMBER;
4322 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 }
4324 }
4325
4326 return OK;
4327}
4328
4329/*
4330 * Handle third level expression:
4331 * var1 == var2
4332 * var1 =~ var2
4333 * var1 != var2
4334 * var1 !~ var2
4335 * var1 > var2
4336 * var1 >= var2
4337 * var1 < var2
4338 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004339 * var1 is var2
4340 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 *
4342 * "arg" must point to the first non-white of the expression.
4343 * "arg" is advanced to the next non-white after the recognized expression.
4344 *
4345 * Return OK or FAIL.
4346 */
4347 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004348eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349{
Bram Moolenaar33570922005-01-25 22:26:29 +00004350 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 char_u *p;
4352 int i;
4353 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004354 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 int len = 2;
4356 long n1, n2;
4357 char_u *s1, *s2;
4358 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4359 regmatch_T regmatch;
4360 int ic;
4361 char_u *save_cpo;
4362
4363 /*
4364 * Get the first variable.
4365 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004366 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 return FAIL;
4368
4369 p = *arg;
4370 switch (p[0])
4371 {
4372 case '=': if (p[1] == '=')
4373 type = TYPE_EQUAL;
4374 else if (p[1] == '~')
4375 type = TYPE_MATCH;
4376 break;
4377 case '!': if (p[1] == '=')
4378 type = TYPE_NEQUAL;
4379 else if (p[1] == '~')
4380 type = TYPE_NOMATCH;
4381 break;
4382 case '>': if (p[1] != '=')
4383 {
4384 type = TYPE_GREATER;
4385 len = 1;
4386 }
4387 else
4388 type = TYPE_GEQUAL;
4389 break;
4390 case '<': if (p[1] != '=')
4391 {
4392 type = TYPE_SMALLER;
4393 len = 1;
4394 }
4395 else
4396 type = TYPE_SEQUAL;
4397 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004398 case 'i': if (p[1] == 's')
4399 {
4400 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4401 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004402 i = p[len];
4403 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004404 {
4405 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4406 type_is = TRUE;
4407 }
4408 }
4409 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 }
4411
4412 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004413 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 */
4415 if (type != TYPE_UNKNOWN)
4416 {
4417 /* extra question mark appended: ignore case */
4418 if (p[len] == '?')
4419 {
4420 ic = TRUE;
4421 ++len;
4422 }
4423 /* extra '#' appended: match case */
4424 else if (p[len] == '#')
4425 {
4426 ic = FALSE;
4427 ++len;
4428 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004429 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 else
4431 ic = p_ic;
4432
4433 /*
4434 * Get the second variable.
4435 */
4436 *arg = skipwhite(p + len);
4437 if (eval5(arg, &var2, evaluate) == FAIL)
4438 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004439 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 return FAIL;
4441 }
4442
4443 if (evaluate)
4444 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004445 if (type_is && rettv->v_type != var2.v_type)
4446 {
4447 /* For "is" a different type always means FALSE, for "notis"
4448 * it means TRUE. */
4449 n1 = (type == TYPE_NEQUAL);
4450 }
4451 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4452 {
4453 if (type_is)
4454 {
4455 n1 = (rettv->v_type == var2.v_type
4456 && rettv->vval.v_list == var2.vval.v_list);
4457 if (type == TYPE_NEQUAL)
4458 n1 = !n1;
4459 }
4460 else if (rettv->v_type != var2.v_type
4461 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4462 {
4463 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004464 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004465 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004466 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004467 clear_tv(rettv);
4468 clear_tv(&var2);
4469 return FAIL;
4470 }
4471 else
4472 {
4473 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004474 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4475 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004476 if (type == TYPE_NEQUAL)
4477 n1 = !n1;
4478 }
4479 }
4480
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004481 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4482 {
4483 if (type_is)
4484 {
4485 n1 = (rettv->v_type == var2.v_type
4486 && rettv->vval.v_dict == var2.vval.v_dict);
4487 if (type == TYPE_NEQUAL)
4488 n1 = !n1;
4489 }
4490 else if (rettv->v_type != var2.v_type
4491 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4492 {
4493 if (rettv->v_type != var2.v_type)
4494 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4495 else
4496 EMSG(_("E736: Invalid operation for Dictionary"));
4497 clear_tv(rettv);
4498 clear_tv(&var2);
4499 return FAIL;
4500 }
4501 else
4502 {
4503 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004504 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4505 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004506 if (type == TYPE_NEQUAL)
4507 n1 = !n1;
4508 }
4509 }
4510
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004511 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4512 {
4513 if (rettv->v_type != var2.v_type
4514 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4515 {
4516 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004517 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004518 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004519 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004520 clear_tv(rettv);
4521 clear_tv(&var2);
4522 return FAIL;
4523 }
4524 else
4525 {
4526 /* Compare two Funcrefs for being equal or unequal. */
4527 if (rettv->vval.v_string == NULL
4528 || var2.vval.v_string == NULL)
4529 n1 = FALSE;
4530 else
4531 n1 = STRCMP(rettv->vval.v_string,
4532 var2.vval.v_string) == 0;
4533 if (type == TYPE_NEQUAL)
4534 n1 = !n1;
4535 }
4536 }
4537
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004538#ifdef FEAT_FLOAT
4539 /*
4540 * If one of the two variables is a float, compare as a float.
4541 * When using "=~" or "!~", always compare as string.
4542 */
4543 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4544 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4545 {
4546 float_T f1, f2;
4547
4548 if (rettv->v_type == VAR_FLOAT)
4549 f1 = rettv->vval.v_float;
4550 else
4551 f1 = get_tv_number(rettv);
4552 if (var2.v_type == VAR_FLOAT)
4553 f2 = var2.vval.v_float;
4554 else
4555 f2 = get_tv_number(&var2);
4556 n1 = FALSE;
4557 switch (type)
4558 {
4559 case TYPE_EQUAL: n1 = (f1 == f2); break;
4560 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4561 case TYPE_GREATER: n1 = (f1 > f2); break;
4562 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4563 case TYPE_SMALLER: n1 = (f1 < f2); break;
4564 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4565 case TYPE_UNKNOWN:
4566 case TYPE_MATCH:
4567 case TYPE_NOMATCH: break; /* avoid gcc warning */
4568 }
4569 }
4570#endif
4571
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 /*
4573 * If one of the two variables is a number, compare as a number.
4574 * When using "=~" or "!~", always compare as string.
4575 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004576 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004579 n1 = get_tv_number(rettv);
4580 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 switch (type)
4582 {
4583 case TYPE_EQUAL: n1 = (n1 == n2); break;
4584 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4585 case TYPE_GREATER: n1 = (n1 > n2); break;
4586 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4587 case TYPE_SMALLER: n1 = (n1 < n2); break;
4588 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4589 case TYPE_UNKNOWN:
4590 case TYPE_MATCH:
4591 case TYPE_NOMATCH: break; /* avoid gcc warning */
4592 }
4593 }
4594 else
4595 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004596 s1 = get_tv_string_buf(rettv, buf1);
4597 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4599 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4600 else
4601 i = 0;
4602 n1 = FALSE;
4603 switch (type)
4604 {
4605 case TYPE_EQUAL: n1 = (i == 0); break;
4606 case TYPE_NEQUAL: n1 = (i != 0); break;
4607 case TYPE_GREATER: n1 = (i > 0); break;
4608 case TYPE_GEQUAL: n1 = (i >= 0); break;
4609 case TYPE_SMALLER: n1 = (i < 0); break;
4610 case TYPE_SEQUAL: n1 = (i <= 0); break;
4611
4612 case TYPE_MATCH:
4613 case TYPE_NOMATCH:
4614 /* avoid 'l' flag in 'cpoptions' */
4615 save_cpo = p_cpo;
4616 p_cpo = (char_u *)"";
4617 regmatch.regprog = vim_regcomp(s2,
4618 RE_MAGIC + RE_STRING);
4619 regmatch.rm_ic = ic;
4620 if (regmatch.regprog != NULL)
4621 {
4622 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004623 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 if (type == TYPE_NOMATCH)
4625 n1 = !n1;
4626 }
4627 p_cpo = save_cpo;
4628 break;
4629
4630 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4631 }
4632 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004633 clear_tv(rettv);
4634 clear_tv(&var2);
4635 rettv->v_type = VAR_NUMBER;
4636 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 }
4638 }
4639
4640 return OK;
4641}
4642
4643/*
4644 * Handle fourth level expression:
4645 * + number addition
4646 * - number subtraction
4647 * . string concatenation
4648 *
4649 * "arg" must point to the first non-white of the expression.
4650 * "arg" is advanced to the next non-white after the recognized expression.
4651 *
4652 * Return OK or FAIL.
4653 */
4654 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004655eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656{
Bram Moolenaar33570922005-01-25 22:26:29 +00004657 typval_T var2;
4658 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 int op;
4660 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004661#ifdef FEAT_FLOAT
4662 float_T f1 = 0, f2 = 0;
4663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 char_u *s1, *s2;
4665 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4666 char_u *p;
4667
4668 /*
4669 * Get the first variable.
4670 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004671 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 return FAIL;
4673
4674 /*
4675 * Repeat computing, until no '+', '-' or '.' is following.
4676 */
4677 for (;;)
4678 {
4679 op = **arg;
4680 if (op != '+' && op != '-' && op != '.')
4681 break;
4682
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004683 if ((op != '+' || rettv->v_type != VAR_LIST)
4684#ifdef FEAT_FLOAT
4685 && (op == '.' || rettv->v_type != VAR_FLOAT)
4686#endif
4687 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004688 {
4689 /* For "list + ...", an illegal use of the first operand as
4690 * a number cannot be determined before evaluating the 2nd
4691 * operand: if this is also a list, all is ok.
4692 * For "something . ...", "something - ..." or "non-list + ...",
4693 * we know that the first operand needs to be a string or number
4694 * without evaluating the 2nd operand. So check before to avoid
4695 * side effects after an error. */
4696 if (evaluate && get_tv_string_chk(rettv) == NULL)
4697 {
4698 clear_tv(rettv);
4699 return FAIL;
4700 }
4701 }
4702
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 /*
4704 * Get the second variable.
4705 */
4706 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004707 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004709 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 return FAIL;
4711 }
4712
4713 if (evaluate)
4714 {
4715 /*
4716 * Compute the result.
4717 */
4718 if (op == '.')
4719 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004720 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4721 s2 = get_tv_string_buf_chk(&var2, buf2);
4722 if (s2 == NULL) /* type error ? */
4723 {
4724 clear_tv(rettv);
4725 clear_tv(&var2);
4726 return FAIL;
4727 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004728 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004729 clear_tv(rettv);
4730 rettv->v_type = VAR_STRING;
4731 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004733 else if (op == '+' && rettv->v_type == VAR_LIST
4734 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004735 {
4736 /* concatenate Lists */
4737 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4738 &var3) == FAIL)
4739 {
4740 clear_tv(rettv);
4741 clear_tv(&var2);
4742 return FAIL;
4743 }
4744 clear_tv(rettv);
4745 *rettv = var3;
4746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 else
4748 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004749 int error = FALSE;
4750
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004751#ifdef FEAT_FLOAT
4752 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004753 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004754 f1 = rettv->vval.v_float;
4755 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004756 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004757 else
4758#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004759 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004760 n1 = get_tv_number_chk(rettv, &error);
4761 if (error)
4762 {
4763 /* This can only happen for "list + non-list". For
4764 * "non-list + ..." or "something - ...", we returned
4765 * before evaluating the 2nd operand. */
4766 clear_tv(rettv);
4767 return FAIL;
4768 }
4769#ifdef FEAT_FLOAT
4770 if (var2.v_type == VAR_FLOAT)
4771 f1 = n1;
4772#endif
4773 }
4774#ifdef FEAT_FLOAT
4775 if (var2.v_type == VAR_FLOAT)
4776 {
4777 f2 = var2.vval.v_float;
4778 n2 = 0;
4779 }
4780 else
4781#endif
4782 {
4783 n2 = get_tv_number_chk(&var2, &error);
4784 if (error)
4785 {
4786 clear_tv(rettv);
4787 clear_tv(&var2);
4788 return FAIL;
4789 }
4790#ifdef FEAT_FLOAT
4791 if (rettv->v_type == VAR_FLOAT)
4792 f2 = n2;
4793#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004794 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004795 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004796
4797#ifdef FEAT_FLOAT
4798 /* If there is a float on either side the result is a float. */
4799 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4800 {
4801 if (op == '+')
4802 f1 = f1 + f2;
4803 else
4804 f1 = f1 - f2;
4805 rettv->v_type = VAR_FLOAT;
4806 rettv->vval.v_float = f1;
4807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004809#endif
4810 {
4811 if (op == '+')
4812 n1 = n1 + n2;
4813 else
4814 n1 = n1 - n2;
4815 rettv->v_type = VAR_NUMBER;
4816 rettv->vval.v_number = n1;
4817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004819 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 }
4821 }
4822 return OK;
4823}
4824
4825/*
4826 * Handle fifth level expression:
4827 * * number multiplication
4828 * / number division
4829 * % number modulo
4830 *
4831 * "arg" must point to the first non-white of the expression.
4832 * "arg" is advanced to the next non-white after the recognized expression.
4833 *
4834 * Return OK or FAIL.
4835 */
4836 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004837eval6(
4838 char_u **arg,
4839 typval_T *rettv,
4840 int evaluate,
4841 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842{
Bram Moolenaar33570922005-01-25 22:26:29 +00004843 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 int op;
4845 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004846#ifdef FEAT_FLOAT
4847 int use_float = FALSE;
4848 float_T f1 = 0, f2;
4849#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004850 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851
4852 /*
4853 * Get the first variable.
4854 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004855 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 return FAIL;
4857
4858 /*
4859 * Repeat computing, until no '*', '/' or '%' is following.
4860 */
4861 for (;;)
4862 {
4863 op = **arg;
4864 if (op != '*' && op != '/' && op != '%')
4865 break;
4866
4867 if (evaluate)
4868 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004869#ifdef FEAT_FLOAT
4870 if (rettv->v_type == VAR_FLOAT)
4871 {
4872 f1 = rettv->vval.v_float;
4873 use_float = TRUE;
4874 n1 = 0;
4875 }
4876 else
4877#endif
4878 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004879 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004880 if (error)
4881 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 }
4883 else
4884 n1 = 0;
4885
4886 /*
4887 * Get the second variable.
4888 */
4889 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004890 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 return FAIL;
4892
4893 if (evaluate)
4894 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004895#ifdef FEAT_FLOAT
4896 if (var2.v_type == VAR_FLOAT)
4897 {
4898 if (!use_float)
4899 {
4900 f1 = n1;
4901 use_float = TRUE;
4902 }
4903 f2 = var2.vval.v_float;
4904 n2 = 0;
4905 }
4906 else
4907#endif
4908 {
4909 n2 = get_tv_number_chk(&var2, &error);
4910 clear_tv(&var2);
4911 if (error)
4912 return FAIL;
4913#ifdef FEAT_FLOAT
4914 if (use_float)
4915 f2 = n2;
4916#endif
4917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918
4919 /*
4920 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004921 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004923#ifdef FEAT_FLOAT
4924 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004926 if (op == '*')
4927 f1 = f1 * f2;
4928 else if (op == '/')
4929 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004930# ifdef VMS
4931 /* VMS crashes on divide by zero, work around it */
4932 if (f2 == 0.0)
4933 {
4934 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004935 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004936 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004937 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004938 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004939 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004940 }
4941 else
4942 f1 = f1 / f2;
4943# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004944 /* We rely on the floating point library to handle divide
4945 * by zero to result in "inf" and not a crash. */
4946 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004947# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004950 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004951 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004952 return FAIL;
4953 }
4954 rettv->v_type = VAR_FLOAT;
4955 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 }
4957 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004960 if (op == '*')
4961 n1 = n1 * n2;
4962 else if (op == '/')
4963 {
4964 if (n2 == 0) /* give an error message? */
4965 {
4966 if (n1 == 0)
4967 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4968 else if (n1 < 0)
4969 n1 = -0x7fffffffL;
4970 else
4971 n1 = 0x7fffffffL;
4972 }
4973 else
4974 n1 = n1 / n2;
4975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004977 {
4978 if (n2 == 0) /* give an error message? */
4979 n1 = 0;
4980 else
4981 n1 = n1 % n2;
4982 }
4983 rettv->v_type = VAR_NUMBER;
4984 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 }
4987 }
4988
4989 return OK;
4990}
4991
4992/*
4993 * Handle sixth level expression:
4994 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004995 * "string" string constant
4996 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 * &option-name option value
4998 * @r register contents
4999 * identifier variable value
5000 * function() function call
5001 * $VAR environment variable
5002 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005003 * [expr, expr] List
5004 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 *
5006 * Also handle:
5007 * ! in front logical NOT
5008 * - in front unary minus
5009 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005010 * trailing [] subscript in String or List
5011 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 *
5013 * "arg" must point to the first non-white of the expression.
5014 * "arg" is advanced to the next non-white after the recognized expression.
5015 *
5016 * Return OK or FAIL.
5017 */
5018 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005019eval7(
5020 char_u **arg,
5021 typval_T *rettv,
5022 int evaluate,
5023 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 long n;
5026 int len;
5027 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 char_u *start_leader, *end_leader;
5029 int ret = OK;
5030 char_u *alias;
5031
5032 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005033 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005034 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005036 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037
5038 /*
5039 * Skip '!' and '-' characters. They are handled later.
5040 */
5041 start_leader = *arg;
5042 while (**arg == '!' || **arg == '-' || **arg == '+')
5043 *arg = skipwhite(*arg + 1);
5044 end_leader = *arg;
5045
5046 switch (**arg)
5047 {
5048 /*
5049 * Number constant.
5050 */
5051 case '0':
5052 case '1':
5053 case '2':
5054 case '3':
5055 case '4':
5056 case '5':
5057 case '6':
5058 case '7':
5059 case '8':
5060 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005061 {
5062#ifdef FEAT_FLOAT
5063 char_u *p = skipdigits(*arg + 1);
5064 int get_float = FALSE;
5065
5066 /* We accept a float when the format matches
5067 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005068 * strict to avoid backwards compatibility problems.
5069 * Don't look for a float after the "." operator, so that
5070 * ":let vers = 1.2.3" doesn't fail. */
5071 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005073 get_float = TRUE;
5074 p = skipdigits(p + 2);
5075 if (*p == 'e' || *p == 'E')
5076 {
5077 ++p;
5078 if (*p == '-' || *p == '+')
5079 ++p;
5080 if (!vim_isdigit(*p))
5081 get_float = FALSE;
5082 else
5083 p = skipdigits(p + 1);
5084 }
5085 if (ASCII_ISALPHA(*p) || *p == '.')
5086 get_float = FALSE;
5087 }
5088 if (get_float)
5089 {
5090 float_T f;
5091
5092 *arg += string2float(*arg, &f);
5093 if (evaluate)
5094 {
5095 rettv->v_type = VAR_FLOAT;
5096 rettv->vval.v_float = f;
5097 }
5098 }
5099 else
5100#endif
5101 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005102 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005103 *arg += len;
5104 if (evaluate)
5105 {
5106 rettv->v_type = VAR_NUMBER;
5107 rettv->vval.v_number = n;
5108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 }
5110 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112
5113 /*
5114 * String constant: "string".
5115 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005116 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 break;
5118
5119 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005120 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005122 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005123 break;
5124
5125 /*
5126 * List: [expr, expr]
5127 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005128 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 break;
5130
5131 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005132 * Dictionary: {key: val, key: val}
5133 */
5134 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5135 break;
5136
5137 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005138 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005140 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 break;
5142
5143 /*
5144 * Environment variable: $VAR.
5145 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005146 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 break;
5148
5149 /*
5150 * Register contents: @r.
5151 */
5152 case '@': ++*arg;
5153 if (evaluate)
5154 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005155 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005156 rettv->vval.v_string = get_reg_contents(**arg,
5157 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 }
5159 if (**arg != NUL)
5160 ++*arg;
5161 break;
5162
5163 /*
5164 * nested expression: (expression).
5165 */
5166 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005167 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 if (**arg == ')')
5169 ++*arg;
5170 else if (ret == OK)
5171 {
5172 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005173 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 ret = FAIL;
5175 }
5176 break;
5177
Bram Moolenaar8c711452005-01-14 21:53:12 +00005178 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 break;
5180 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005181
5182 if (ret == NOTDONE)
5183 {
5184 /*
5185 * Must be a variable or function name.
5186 * Can also be a curly-braces kind of name: {expr}.
5187 */
5188 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005189 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005190 if (alias != NULL)
5191 s = alias;
5192
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005193 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005194 ret = FAIL;
5195 else
5196 {
5197 if (**arg == '(') /* recursive! */
5198 {
5199 /* If "s" is the name of a variable of type VAR_FUNC
5200 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005201 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005202
5203 /* Invoke the function. */
5204 ret = get_func_tv(s, len, rettv, arg,
5205 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005206 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005207
5208 /* If evaluate is FALSE rettv->v_type was not set in
5209 * get_func_tv, but it's needed in handle_subscript() to parse
5210 * what follows. So set it here. */
5211 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5212 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005213 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005214 rettv->v_type = VAR_FUNC;
5215 }
5216
Bram Moolenaar8c711452005-01-14 21:53:12 +00005217 /* Stop the expression evaluation when immediately
5218 * aborting on error, or when an interrupt occurred or
5219 * an exception was thrown but not caught. */
5220 if (aborting())
5221 {
5222 if (ret == OK)
5223 clear_tv(rettv);
5224 ret = FAIL;
5225 }
5226 }
5227 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005228 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005229 else
5230 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005231 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005232 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005233 }
5234
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 *arg = skipwhite(*arg);
5236
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005237 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5238 * expr(expr). */
5239 if (ret == OK)
5240 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241
5242 /*
5243 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5244 */
5245 if (ret == OK && evaluate && end_leader > start_leader)
5246 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005247 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005248 int val = 0;
5249#ifdef FEAT_FLOAT
5250 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005251
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005252 if (rettv->v_type == VAR_FLOAT)
5253 f = rettv->vval.v_float;
5254 else
5255#endif
5256 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005257 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005259 clear_tv(rettv);
5260 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005262 else
5263 {
5264 while (end_leader > start_leader)
5265 {
5266 --end_leader;
5267 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005268 {
5269#ifdef FEAT_FLOAT
5270 if (rettv->v_type == VAR_FLOAT)
5271 f = !f;
5272 else
5273#endif
5274 val = !val;
5275 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005276 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005277 {
5278#ifdef FEAT_FLOAT
5279 if (rettv->v_type == VAR_FLOAT)
5280 f = -f;
5281 else
5282#endif
5283 val = -val;
5284 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005285 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005286#ifdef FEAT_FLOAT
5287 if (rettv->v_type == VAR_FLOAT)
5288 {
5289 clear_tv(rettv);
5290 rettv->vval.v_float = f;
5291 }
5292 else
5293#endif
5294 {
5295 clear_tv(rettv);
5296 rettv->v_type = VAR_NUMBER;
5297 rettv->vval.v_number = val;
5298 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 }
5301
5302 return ret;
5303}
5304
5305/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005306 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5307 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005308 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5309 */
5310 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005311eval_index(
5312 char_u **arg,
5313 typval_T *rettv,
5314 int evaluate,
5315 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005316{
5317 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005318 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005319 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005320 long len = -1;
5321 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005322 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005323 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005324
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005325 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005326 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005327 if (verbose)
5328 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005329 return FAIL;
5330 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005331#ifdef FEAT_FLOAT
5332 else if (rettv->v_type == VAR_FLOAT)
5333 {
5334 if (verbose)
5335 EMSG(_(e_float_as_string));
5336 return FAIL;
5337 }
5338#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005339 else if (rettv->v_type == VAR_SPECIAL)
5340 {
5341 return FAIL;
5342 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005343
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005344 init_tv(&var1);
5345 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005346 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005347 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005348 /*
5349 * dict.name
5350 */
5351 key = *arg + 1;
5352 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5353 ;
5354 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005356 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357 }
5358 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005360 /*
5361 * something[idx]
5362 *
5363 * Get the (first) variable from inside the [].
5364 */
5365 *arg = skipwhite(*arg + 1);
5366 if (**arg == ':')
5367 empty1 = TRUE;
5368 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5369 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005370 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5371 {
5372 /* not a number or string */
5373 clear_tv(&var1);
5374 return FAIL;
5375 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005376
5377 /*
5378 * Get the second variable from inside the [:].
5379 */
5380 if (**arg == ':')
5381 {
5382 range = TRUE;
5383 *arg = skipwhite(*arg + 1);
5384 if (**arg == ']')
5385 empty2 = TRUE;
5386 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5387 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005388 if (!empty1)
5389 clear_tv(&var1);
5390 return FAIL;
5391 }
5392 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5393 {
5394 /* not a number or string */
5395 if (!empty1)
5396 clear_tv(&var1);
5397 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005398 return FAIL;
5399 }
5400 }
5401
5402 /* Check for the ']'. */
5403 if (**arg != ']')
5404 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005405 if (verbose)
5406 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407 clear_tv(&var1);
5408 if (range)
5409 clear_tv(&var2);
5410 return FAIL;
5411 }
5412 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005413 }
5414
5415 if (evaluate)
5416 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005417 n1 = 0;
5418 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005419 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005420 n1 = get_tv_number(&var1);
5421 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005422 }
5423 if (range)
5424 {
5425 if (empty2)
5426 n2 = -1;
5427 else
5428 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005429 n2 = get_tv_number(&var2);
5430 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005431 }
5432 }
5433
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005434 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435 {
5436 case VAR_NUMBER:
5437 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005438 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 len = (long)STRLEN(s);
5440 if (range)
5441 {
5442 /* The resulting variable is a substring. If the indexes
5443 * are out of range the result is empty. */
5444 if (n1 < 0)
5445 {
5446 n1 = len + n1;
5447 if (n1 < 0)
5448 n1 = 0;
5449 }
5450 if (n2 < 0)
5451 n2 = len + n2;
5452 else if (n2 >= len)
5453 n2 = len;
5454 if (n1 >= len || n2 < 0 || n1 > n2)
5455 s = NULL;
5456 else
5457 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5458 }
5459 else
5460 {
5461 /* The resulting variable is a string of a single
5462 * character. If the index is too big or negative the
5463 * result is empty. */
5464 if (n1 >= len || n1 < 0)
5465 s = NULL;
5466 else
5467 s = vim_strnsave(s + n1, 1);
5468 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005469 clear_tv(rettv);
5470 rettv->v_type = VAR_STRING;
5471 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005472 break;
5473
5474 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005475 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 if (n1 < 0)
5477 n1 = len + n1;
5478 if (!empty1 && (n1 < 0 || n1 >= len))
5479 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005480 /* For a range we allow invalid values and return an empty
5481 * list. A list index out of range is an error. */
5482 if (!range)
5483 {
5484 if (verbose)
5485 EMSGN(_(e_listidx), n1);
5486 return FAIL;
5487 }
5488 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 }
5490 if (range)
5491 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005492 list_T *l;
5493 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494
5495 if (n2 < 0)
5496 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005497 else if (n2 >= len)
5498 n2 = len - 1;
5499 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005500 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501 l = list_alloc();
5502 if (l == NULL)
5503 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005504 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005505 n1 <= n2; ++n1)
5506 {
5507 if (list_append_tv(l, &item->li_tv) == FAIL)
5508 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005509 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005510 return FAIL;
5511 }
5512 item = item->li_next;
5513 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005514 clear_tv(rettv);
5515 rettv->v_type = VAR_LIST;
5516 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005517 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005518 }
5519 else
5520 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005521 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005522 clear_tv(rettv);
5523 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005524 }
5525 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005526
5527 case VAR_DICT:
5528 if (range)
5529 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005530 if (verbose)
5531 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005532 if (len == -1)
5533 clear_tv(&var1);
5534 return FAIL;
5535 }
5536 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005537 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005538
5539 if (len == -1)
5540 {
5541 key = get_tv_string(&var1);
5542 if (*key == NUL)
5543 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005544 if (verbose)
5545 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005546 clear_tv(&var1);
5547 return FAIL;
5548 }
5549 }
5550
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005551 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005552
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005553 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005554 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005555 if (len == -1)
5556 clear_tv(&var1);
5557 if (item == NULL)
5558 return FAIL;
5559
5560 copy_tv(&item->di_tv, &var1);
5561 clear_tv(rettv);
5562 *rettv = var1;
5563 }
5564 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005565 }
5566 }
5567
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005568 return OK;
5569}
5570
5571/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 * Get an option value.
5573 * "arg" points to the '&' or '+' before the option name.
5574 * "arg" is advanced to character after the option name.
5575 * Return OK or FAIL.
5576 */
5577 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005578get_option_tv(
5579 char_u **arg,
5580 typval_T *rettv, /* when NULL, only check if option exists */
5581 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582{
5583 char_u *option_end;
5584 long numval;
5585 char_u *stringval;
5586 int opt_type;
5587 int c;
5588 int working = (**arg == '+'); /* has("+option") */
5589 int ret = OK;
5590 int opt_flags;
5591
5592 /*
5593 * Isolate the option name and find its value.
5594 */
5595 option_end = find_option_end(arg, &opt_flags);
5596 if (option_end == NULL)
5597 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005598 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 EMSG2(_("E112: Option name missing: %s"), *arg);
5600 return FAIL;
5601 }
5602
5603 if (!evaluate)
5604 {
5605 *arg = option_end;
5606 return OK;
5607 }
5608
5609 c = *option_end;
5610 *option_end = NUL;
5611 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005612 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613
5614 if (opt_type == -3) /* invalid name */
5615 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005616 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 EMSG2(_("E113: Unknown option: %s"), *arg);
5618 ret = FAIL;
5619 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005620 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 {
5622 if (opt_type == -2) /* hidden string option */
5623 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005624 rettv->v_type = VAR_STRING;
5625 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 }
5627 else if (opt_type == -1) /* hidden number option */
5628 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005629 rettv->v_type = VAR_NUMBER;
5630 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 }
5632 else if (opt_type == 1) /* number option */
5633 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005634 rettv->v_type = VAR_NUMBER;
5635 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 }
5637 else /* string option */
5638 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005639 rettv->v_type = VAR_STRING;
5640 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 }
5642 }
5643 else if (working && (opt_type == -2 || opt_type == -1))
5644 ret = FAIL;
5645
5646 *option_end = c; /* put back for error messages */
5647 *arg = option_end;
5648
5649 return ret;
5650}
5651
5652/*
5653 * Allocate a variable for a string constant.
5654 * Return OK or FAIL.
5655 */
5656 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005657get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658{
5659 char_u *p;
5660 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 int extra = 0;
5662
5663 /*
5664 * Find the end of the string, skipping backslashed characters.
5665 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005666 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 {
5668 if (*p == '\\' && p[1] != NUL)
5669 {
5670 ++p;
5671 /* A "\<x>" form occupies at least 4 characters, and produces up
5672 * to 6 characters: reserve space for 2 extra */
5673 if (*p == '<')
5674 extra += 2;
5675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 }
5677
5678 if (*p != '"')
5679 {
5680 EMSG2(_("E114: Missing quote: %s"), *arg);
5681 return FAIL;
5682 }
5683
5684 /* If only parsing, set *arg and return here */
5685 if (!evaluate)
5686 {
5687 *arg = p + 1;
5688 return OK;
5689 }
5690
5691 /*
5692 * Copy the string into allocated memory, handling backslashed
5693 * characters.
5694 */
5695 name = alloc((unsigned)(p - *arg + extra));
5696 if (name == NULL)
5697 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005698 rettv->v_type = VAR_STRING;
5699 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700
Bram Moolenaar8c711452005-01-14 21:53:12 +00005701 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 {
5703 if (*p == '\\')
5704 {
5705 switch (*++p)
5706 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005707 case 'b': *name++ = BS; ++p; break;
5708 case 'e': *name++ = ESC; ++p; break;
5709 case 'f': *name++ = FF; ++p; break;
5710 case 'n': *name++ = NL; ++p; break;
5711 case 'r': *name++ = CAR; ++p; break;
5712 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713
5714 case 'X': /* hex: "\x1", "\x12" */
5715 case 'x':
5716 case 'u': /* Unicode: "\u0023" */
5717 case 'U':
5718 if (vim_isxdigit(p[1]))
5719 {
5720 int n, nr;
5721 int c = toupper(*p);
5722
5723 if (c == 'X')
5724 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005725 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005727 else
5728 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 nr = 0;
5730 while (--n >= 0 && vim_isxdigit(p[1]))
5731 {
5732 ++p;
5733 nr = (nr << 4) + hex2nr(*p);
5734 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736#ifdef FEAT_MBYTE
5737 /* For "\u" store the number according to
5738 * 'encoding'. */
5739 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005740 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 else
5742#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005743 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 break;
5746
5747 /* octal: "\1", "\12", "\123" */
5748 case '0':
5749 case '1':
5750 case '2':
5751 case '3':
5752 case '4':
5753 case '5':
5754 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005755 case '7': *name = *p++ - '0';
5756 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005758 *name = (*name << 3) + *p++ - '0';
5759 if (*p >= '0' && *p <= '7')
5760 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005762 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 break;
5764
5765 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005766 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 if (extra != 0)
5768 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 break;
5771 }
5772 /* FALLTHROUGH */
5773
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 break;
5776 }
5777 }
5778 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 *arg = p + 1;
5784
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 return OK;
5786}
5787
5788/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 * Return OK or FAIL.
5791 */
5792 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005793get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794{
5795 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005796 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005797 int reduce = 0;
5798
5799 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005800 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005801 */
5802 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5803 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005805 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005806 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005807 break;
5808 ++reduce;
5809 ++p;
5810 }
5811 }
5812
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005814 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005815 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005816 return FAIL;
5817 }
5818
Bram Moolenaar8c711452005-01-14 21:53:12 +00005819 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005820 if (!evaluate)
5821 {
5822 *arg = p + 1;
5823 return OK;
5824 }
5825
5826 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005827 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005828 */
5829 str = alloc((unsigned)((p - *arg) - reduce));
5830 if (str == NULL)
5831 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 rettv->v_type = VAR_STRING;
5833 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005834
Bram Moolenaar8c711452005-01-14 21:53:12 +00005835 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005836 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005837 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005838 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005839 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005840 break;
5841 ++p;
5842 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005844 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005845 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005846 *arg = p + 1;
5847
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005848 return OK;
5849}
5850
5851/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852 * Allocate a variable for a List and fill it from "*arg".
5853 * Return OK or FAIL.
5854 */
5855 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005856get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005857{
Bram Moolenaar33570922005-01-25 22:26:29 +00005858 list_T *l = NULL;
5859 typval_T tv;
5860 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005861
5862 if (evaluate)
5863 {
5864 l = list_alloc();
5865 if (l == NULL)
5866 return FAIL;
5867 }
5868
5869 *arg = skipwhite(*arg + 1);
5870 while (**arg != ']' && **arg != NUL)
5871 {
5872 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5873 goto failret;
5874 if (evaluate)
5875 {
5876 item = listitem_alloc();
5877 if (item != NULL)
5878 {
5879 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005880 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005881 list_append(l, item);
5882 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005883 else
5884 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005885 }
5886
5887 if (**arg == ']')
5888 break;
5889 if (**arg != ',')
5890 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005891 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005892 goto failret;
5893 }
5894 *arg = skipwhite(*arg + 1);
5895 }
5896
5897 if (**arg != ']')
5898 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005899 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005900failret:
5901 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005902 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005903 return FAIL;
5904 }
5905
5906 *arg = skipwhite(*arg + 1);
5907 if (evaluate)
5908 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005909 rettv->v_type = VAR_LIST;
5910 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005911 ++l->lv_refcount;
5912 }
5913
5914 return OK;
5915}
5916
5917/*
5918 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005919 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005920 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005921 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005922list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005923{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005924 list_T *l;
5925
5926 l = (list_T *)alloc_clear(sizeof(list_T));
5927 if (l != NULL)
5928 {
5929 /* Prepend the list to the list of lists for garbage collection. */
5930 if (first_list != NULL)
5931 first_list->lv_used_prev = l;
5932 l->lv_used_prev = NULL;
5933 l->lv_used_next = first_list;
5934 first_list = l;
5935 }
5936 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937}
5938
5939/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005940 * Allocate an empty list for a return value.
5941 * Returns OK or FAIL.
5942 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005943 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005944rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005945{
5946 list_T *l = list_alloc();
5947
5948 if (l == NULL)
5949 return FAIL;
5950
5951 rettv->vval.v_list = l;
5952 rettv->v_type = VAR_LIST;
5953 ++l->lv_refcount;
5954 return OK;
5955}
5956
5957/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958 * Unreference a list: decrement the reference count and free it when it
5959 * becomes zero.
5960 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005961 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005962list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005963{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005964 if (l != NULL && --l->lv_refcount <= 0)
5965 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966}
5967
5968/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01005969 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970 * Ignores the reference count.
5971 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005972 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005973list_free(
5974 list_T *l,
5975 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005976{
Bram Moolenaar33570922005-01-25 22:26:29 +00005977 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005979 /* Remove the list from the list of lists for garbage collection. */
5980 if (l->lv_used_prev == NULL)
5981 first_list = l->lv_used_next;
5982 else
5983 l->lv_used_prev->lv_used_next = l->lv_used_next;
5984 if (l->lv_used_next != NULL)
5985 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5986
Bram Moolenaard9fba312005-06-26 22:34:35 +00005987 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005989 /* Remove the item before deleting it. */
5990 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005991 if (recurse || (item->li_tv.v_type != VAR_LIST
5992 && item->li_tv.v_type != VAR_DICT))
5993 clear_tv(&item->li_tv);
5994 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005995 }
5996 vim_free(l);
5997}
5998
5999/*
6000 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006001 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006002 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006003 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006004listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006005{
Bram Moolenaar33570922005-01-25 22:26:29 +00006006 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007}
6008
6009/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006010 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006012 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006013listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006014{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006015 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006016 vim_free(item);
6017}
6018
6019/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006020 * Remove a list item from a List and free it. Also clears the value.
6021 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006022 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006023listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006024{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006025 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006026 listitem_free(item);
6027}
6028
6029/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030 * Get the number of items in a list.
6031 */
6032 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006033list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035 if (l == NULL)
6036 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006037 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038}
6039
6040/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006041 * Return TRUE when two lists have exactly the same values.
6042 */
6043 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006044list_equal(
6045 list_T *l1,
6046 list_T *l2,
6047 int ic, /* ignore case for strings */
6048 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006049{
Bram Moolenaar33570922005-01-25 22:26:29 +00006050 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006051
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006052 if (l1 == NULL || l2 == NULL)
6053 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006054 if (l1 == l2)
6055 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006056 if (list_len(l1) != list_len(l2))
6057 return FALSE;
6058
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006059 for (item1 = l1->lv_first, item2 = l2->lv_first;
6060 item1 != NULL && item2 != NULL;
6061 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006062 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006063 return FALSE;
6064 return item1 == NULL && item2 == NULL;
6065}
6066
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006067/*
6068 * Return the dictitem that an entry in a hashtable points to.
6069 */
6070 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006071dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006072{
6073 return HI2DI(hi);
6074}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006075
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006076/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006077 * Return TRUE when two dictionaries have exactly the same key/values.
6078 */
6079 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006080dict_equal(
6081 dict_T *d1,
6082 dict_T *d2,
6083 int ic, /* ignore case for strings */
6084 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006085{
Bram Moolenaar33570922005-01-25 22:26:29 +00006086 hashitem_T *hi;
6087 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006088 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006089
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006090 if (d1 == NULL || d2 == NULL)
6091 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006092 if (d1 == d2)
6093 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006094 if (dict_len(d1) != dict_len(d2))
6095 return FALSE;
6096
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006097 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006098 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006099 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006100 if (!HASHITEM_EMPTY(hi))
6101 {
6102 item2 = dict_find(d2, hi->hi_key, -1);
6103 if (item2 == NULL)
6104 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006105 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006106 return FALSE;
6107 --todo;
6108 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006109 }
6110 return TRUE;
6111}
6112
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006113static int tv_equal_recurse_limit;
6114
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006115/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006116 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006117 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006118 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006119 */
6120 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006121tv_equal(
6122 typval_T *tv1,
6123 typval_T *tv2,
6124 int ic, /* ignore case */
6125 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006126{
6127 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006128 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006129 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006130 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006131
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006132 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006133 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006134
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006135 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006136 * recursiveness to a limit. We guess they are equal then.
6137 * A fixed limit has the problem of still taking an awful long time.
6138 * Reduce the limit every time running into it. That should work fine for
6139 * deeply linked structures that are not recursively linked and catch
6140 * recursiveness quickly. */
6141 if (!recursive)
6142 tv_equal_recurse_limit = 1000;
6143 if (recursive_cnt >= tv_equal_recurse_limit)
6144 {
6145 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006146 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006147 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006148
6149 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006150 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006151 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006152 ++recursive_cnt;
6153 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6154 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006155 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006156
6157 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006158 ++recursive_cnt;
6159 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6160 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006161 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006162
6163 case VAR_FUNC:
6164 return (tv1->vval.v_string != NULL
6165 && tv2->vval.v_string != NULL
6166 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6167
6168 case VAR_NUMBER:
6169 return tv1->vval.v_number == tv2->vval.v_number;
6170
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006171#ifdef FEAT_FLOAT
6172 case VAR_FLOAT:
6173 return tv1->vval.v_float == tv2->vval.v_float;
6174#endif
6175
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006176 case VAR_STRING:
6177 s1 = get_tv_string_buf(tv1, buf1);
6178 s2 = get_tv_string_buf(tv2, buf2);
6179 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006180
6181 case VAR_SPECIAL:
6182 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006183 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006184
6185 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006186 return TRUE;
6187}
6188
6189/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006190 * Locate item with index "n" in list "l" and return it.
6191 * A negative index is counted from the end; -1 is the last item.
6192 * Returns NULL when "n" is out of range.
6193 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006194 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006195list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006196{
Bram Moolenaar33570922005-01-25 22:26:29 +00006197 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006198 long idx;
6199
6200 if (l == NULL)
6201 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006202
6203 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006204 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006205 n = l->lv_len + n;
6206
6207 /* Check for index out of range. */
6208 if (n < 0 || n >= l->lv_len)
6209 return NULL;
6210
6211 /* When there is a cached index may start search from there. */
6212 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006213 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006214 if (n < l->lv_idx / 2)
6215 {
6216 /* closest to the start of the list */
6217 item = l->lv_first;
6218 idx = 0;
6219 }
6220 else if (n > (l->lv_idx + l->lv_len) / 2)
6221 {
6222 /* closest to the end of the list */
6223 item = l->lv_last;
6224 idx = l->lv_len - 1;
6225 }
6226 else
6227 {
6228 /* closest to the cached index */
6229 item = l->lv_idx_item;
6230 idx = l->lv_idx;
6231 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006232 }
6233 else
6234 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006235 if (n < l->lv_len / 2)
6236 {
6237 /* closest to the start of the list */
6238 item = l->lv_first;
6239 idx = 0;
6240 }
6241 else
6242 {
6243 /* closest to the end of the list */
6244 item = l->lv_last;
6245 idx = l->lv_len - 1;
6246 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006247 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006248
6249 while (n > idx)
6250 {
6251 /* search forward */
6252 item = item->li_next;
6253 ++idx;
6254 }
6255 while (n < idx)
6256 {
6257 /* search backward */
6258 item = item->li_prev;
6259 --idx;
6260 }
6261
6262 /* cache the used index */
6263 l->lv_idx = idx;
6264 l->lv_idx_item = item;
6265
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006266 return item;
6267}
6268
6269/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006270 * Get list item "l[idx]" as a number.
6271 */
6272 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006273list_find_nr(
6274 list_T *l,
6275 long idx,
6276 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006277{
6278 listitem_T *li;
6279
6280 li = list_find(l, idx);
6281 if (li == NULL)
6282 {
6283 if (errorp != NULL)
6284 *errorp = TRUE;
6285 return -1L;
6286 }
6287 return get_tv_number_chk(&li->li_tv, errorp);
6288}
6289
6290/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006291 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6292 */
6293 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006294list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006295{
6296 listitem_T *li;
6297
6298 li = list_find(l, idx - 1);
6299 if (li == NULL)
6300 {
6301 EMSGN(_(e_listidx), idx);
6302 return NULL;
6303 }
6304 return get_tv_string(&li->li_tv);
6305}
6306
6307/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006308 * Locate "item" list "l" and return its index.
6309 * Returns -1 when "item" is not in the list.
6310 */
6311 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006312list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006313{
6314 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006315 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006316
6317 if (l == NULL)
6318 return -1;
6319 idx = 0;
6320 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6321 ++idx;
6322 if (li == NULL)
6323 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006324 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006325}
6326
6327/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006328 * Append item "item" to the end of list "l".
6329 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006330 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006331list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006332{
6333 if (l->lv_last == NULL)
6334 {
6335 /* empty list */
6336 l->lv_first = item;
6337 l->lv_last = item;
6338 item->li_prev = NULL;
6339 }
6340 else
6341 {
6342 l->lv_last->li_next = item;
6343 item->li_prev = l->lv_last;
6344 l->lv_last = item;
6345 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006346 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006347 item->li_next = NULL;
6348}
6349
6350/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006351 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006352 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006353 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006354 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006355list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006356{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006357 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006358
Bram Moolenaar05159a02005-02-26 23:04:13 +00006359 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006360 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006361 copy_tv(tv, &li->li_tv);
6362 list_append(l, li);
6363 return OK;
6364}
6365
6366/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006367 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006368 * Return FAIL when out of memory.
6369 */
6370 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006371list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006372{
6373 listitem_T *li = listitem_alloc();
6374
6375 if (li == NULL)
6376 return FAIL;
6377 li->li_tv.v_type = VAR_DICT;
6378 li->li_tv.v_lock = 0;
6379 li->li_tv.vval.v_dict = dict;
6380 list_append(list, li);
6381 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006382 return OK;
6383}
6384
6385/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006386 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006387 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006388 * Returns FAIL when out of memory.
6389 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006390 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006391list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006392{
6393 listitem_T *li = listitem_alloc();
6394
6395 if (li == NULL)
6396 return FAIL;
6397 list_append(l, li);
6398 li->li_tv.v_type = VAR_STRING;
6399 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006400 if (str == NULL)
6401 li->li_tv.vval.v_string = NULL;
6402 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006403 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006404 return FAIL;
6405 return OK;
6406}
6407
6408/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006409 * Append "n" to list "l".
6410 * Returns FAIL when out of memory.
6411 */
6412 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006413list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006414{
6415 listitem_T *li;
6416
6417 li = listitem_alloc();
6418 if (li == NULL)
6419 return FAIL;
6420 li->li_tv.v_type = VAR_NUMBER;
6421 li->li_tv.v_lock = 0;
6422 li->li_tv.vval.v_number = n;
6423 list_append(l, li);
6424 return OK;
6425}
6426
6427/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006428 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429 * If "item" is NULL append at the end.
6430 * Return FAIL when out of memory.
6431 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006432 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006433list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006434{
Bram Moolenaar33570922005-01-25 22:26:29 +00006435 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006436
6437 if (ni == NULL)
6438 return FAIL;
6439 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006440 list_insert(l, ni, item);
6441 return OK;
6442}
6443
6444 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006445list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006446{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006447 if (item == NULL)
6448 /* Append new item at end of list. */
6449 list_append(l, ni);
6450 else
6451 {
6452 /* Insert new item before existing item. */
6453 ni->li_prev = item->li_prev;
6454 ni->li_next = item;
6455 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006456 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006457 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006458 ++l->lv_idx;
6459 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006460 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006461 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006462 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006463 l->lv_idx_item = NULL;
6464 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006465 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006466 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006467 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006468}
6469
6470/*
6471 * Extend "l1" with "l2".
6472 * If "bef" is NULL append at the end, otherwise insert before this item.
6473 * Returns FAIL when out of memory.
6474 */
6475 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006476list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006477{
Bram Moolenaar33570922005-01-25 22:26:29 +00006478 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006479 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006480
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006481 /* We also quit the loop when we have inserted the original item count of
6482 * the list, avoid a hang when we extend a list with itself. */
6483 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006484 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6485 return FAIL;
6486 return OK;
6487}
6488
6489/*
6490 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6491 * Return FAIL when out of memory.
6492 */
6493 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006494list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006495{
Bram Moolenaar33570922005-01-25 22:26:29 +00006496 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006497
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006498 if (l1 == NULL || l2 == NULL)
6499 return FAIL;
6500
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006501 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006502 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006503 if (l == NULL)
6504 return FAIL;
6505 tv->v_type = VAR_LIST;
6506 tv->vval.v_list = l;
6507
6508 /* append all items from the second list */
6509 return list_extend(l, l2, NULL);
6510}
6511
6512/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006513 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006514 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006515 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006516 * Returns NULL when out of memory.
6517 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006518 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006519list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006520{
Bram Moolenaar33570922005-01-25 22:26:29 +00006521 list_T *copy;
6522 listitem_T *item;
6523 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524
6525 if (orig == NULL)
6526 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006527
6528 copy = list_alloc();
6529 if (copy != NULL)
6530 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006531 if (copyID != 0)
6532 {
6533 /* Do this before adding the items, because one of the items may
6534 * refer back to this list. */
6535 orig->lv_copyID = copyID;
6536 orig->lv_copylist = copy;
6537 }
6538 for (item = orig->lv_first; item != NULL && !got_int;
6539 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006540 {
6541 ni = listitem_alloc();
6542 if (ni == NULL)
6543 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006544 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006545 {
6546 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6547 {
6548 vim_free(ni);
6549 break;
6550 }
6551 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006552 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006553 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006554 list_append(copy, ni);
6555 }
6556 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006557 if (item != NULL)
6558 {
6559 list_unref(copy);
6560 copy = NULL;
6561 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006562 }
6563
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006564 return copy;
6565}
6566
6567/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006568 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006569 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006570 * This used to be called list_remove, but that conflicts with a Sun header
6571 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006573 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006574vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006575{
Bram Moolenaar33570922005-01-25 22:26:29 +00006576 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006577
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006578 /* notify watchers */
6579 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006580 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006581 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006582 list_fix_watch(l, ip);
6583 if (ip == item2)
6584 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006586
6587 if (item2->li_next == NULL)
6588 l->lv_last = item->li_prev;
6589 else
6590 item2->li_next->li_prev = item->li_prev;
6591 if (item->li_prev == NULL)
6592 l->lv_first = item2->li_next;
6593 else
6594 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006595 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006596}
6597
6598/*
6599 * Return an allocated string with the string representation of a list.
6600 * May return NULL.
6601 */
6602 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006603list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006604{
6605 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006606
6607 if (tv->vval.v_list == NULL)
6608 return NULL;
6609 ga_init2(&ga, (int)sizeof(char), 80);
6610 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006611 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006612 {
6613 vim_free(ga.ga_data);
6614 return NULL;
6615 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006616 ga_append(&ga, ']');
6617 ga_append(&ga, NUL);
6618 return (char_u *)ga.ga_data;
6619}
6620
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006621typedef struct join_S {
6622 char_u *s;
6623 char_u *tofree;
6624} join_T;
6625
6626 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006627list_join_inner(
6628 garray_T *gap, /* to store the result in */
6629 list_T *l,
6630 char_u *sep,
6631 int echo_style,
6632 int copyID,
6633 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006634{
6635 int i;
6636 join_T *p;
6637 int len;
6638 int sumlen = 0;
6639 int first = TRUE;
6640 char_u *tofree;
6641 char_u numbuf[NUMBUFLEN];
6642 listitem_T *item;
6643 char_u *s;
6644
6645 /* Stringify each item in the list. */
6646 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6647 {
6648 if (echo_style)
6649 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6650 else
6651 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6652 if (s == NULL)
6653 return FAIL;
6654
6655 len = (int)STRLEN(s);
6656 sumlen += len;
6657
Bram Moolenaarcde88542015-08-11 19:14:00 +02006658 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006659 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6660 if (tofree != NULL || s != numbuf)
6661 {
6662 p->s = s;
6663 p->tofree = tofree;
6664 }
6665 else
6666 {
6667 p->s = vim_strnsave(s, len);
6668 p->tofree = p->s;
6669 }
6670
6671 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006672 if (did_echo_string_emsg) /* recursion error, bail out */
6673 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006674 }
6675
6676 /* Allocate result buffer with its total size, avoid re-allocation and
6677 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6678 if (join_gap->ga_len >= 2)
6679 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6680 if (ga_grow(gap, sumlen + 2) == FAIL)
6681 return FAIL;
6682
6683 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6684 {
6685 if (first)
6686 first = FALSE;
6687 else
6688 ga_concat(gap, sep);
6689 p = ((join_T *)join_gap->ga_data) + i;
6690
6691 if (p->s != NULL)
6692 ga_concat(gap, p->s);
6693 line_breakcheck();
6694 }
6695
6696 return OK;
6697}
6698
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006699/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006700 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006701 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006702 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006703 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006704 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006705list_join(
6706 garray_T *gap,
6707 list_T *l,
6708 char_u *sep,
6709 int echo_style,
6710 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006711{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006712 garray_T join_ga;
6713 int retval;
6714 join_T *p;
6715 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006716
Bram Moolenaard39a7512015-04-16 22:51:22 +02006717 if (l->lv_len < 1)
6718 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006719 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6720 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6721
6722 /* Dispose each item in join_ga. */
6723 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006724 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006725 p = (join_T *)join_ga.ga_data;
6726 for (i = 0; i < join_ga.ga_len; ++i)
6727 {
6728 vim_free(p->tofree);
6729 ++p;
6730 }
6731 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006732 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006733
6734 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006735}
6736
6737/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006738 * Return the next (unique) copy ID.
6739 * Used for serializing nested structures.
6740 */
6741 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006742get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006743{
6744 current_copyID += COPYID_INC;
6745 return current_copyID;
6746}
6747
6748/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006749 * Garbage collection for lists and dictionaries.
6750 *
6751 * We use reference counts to be able to free most items right away when they
6752 * are no longer used. But for composite items it's possible that it becomes
6753 * unused while the reference count is > 0: When there is a recursive
6754 * reference. Example:
6755 * :let l = [1, 2, 3]
6756 * :let d = {9: l}
6757 * :let l[1] = d
6758 *
6759 * Since this is quite unusual we handle this with garbage collection: every
6760 * once in a while find out which lists and dicts are not referenced from any
6761 * variable.
6762 *
6763 * Here is a good reference text about garbage collection (refers to Python
6764 * but it applies to all reference-counting mechanisms):
6765 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006766 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006767
6768/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006769 * Do garbage collection for lists and dicts.
6770 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006771 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006772 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006773garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006774{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006775 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006776 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006777 buf_T *buf;
6778 win_T *wp;
6779 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006780 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006781 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006782 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006783#ifdef FEAT_WINDOWS
6784 tabpage_T *tp;
6785#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006786
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006787 /* Only do this once. */
6788 want_garbage_collect = FALSE;
6789 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006790 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006791
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006792 /* We advance by two because we add one for items referenced through
6793 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006794 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006795
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006796 /*
6797 * 1. Go through all accessible variables and mark all lists and dicts
6798 * with copyID.
6799 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006800
6801 /* Don't free variables in the previous_funccal list unless they are only
6802 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006803 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006804 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6805 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006806 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6807 NULL);
6808 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6809 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006810 }
6811
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006812 /* script-local variables */
6813 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006814 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006815
6816 /* buffer-local variables */
6817 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006818 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6819 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006820
6821 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006822 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006823 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6824 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006825#ifdef FEAT_AUTOCMD
6826 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006827 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6828 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006829#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006830
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006831#ifdef FEAT_WINDOWS
6832 /* tabpage-local variables */
6833 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006834 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6835 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006836#endif
6837
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006838 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006839 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006840
6841 /* function-local variables */
6842 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6843 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006844 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6845 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006846 }
6847
Bram Moolenaard812df62008-11-09 12:46:09 +00006848 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006849 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006850
Bram Moolenaar1dced572012-04-05 16:54:08 +02006851#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006852 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006853#endif
6854
Bram Moolenaardb913952012-06-29 12:54:53 +02006855#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006856 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006857#endif
6858
6859#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006860 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006861#endif
6862
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006863 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006864 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006865 /*
6866 * 2. Free lists and dictionaries that are not referenced.
6867 */
6868 did_free = free_unref_items(copyID);
6869
6870 /*
6871 * 3. Check if any funccal can be freed now.
6872 */
6873 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006874 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006875 if (can_free_funccal(*pfc, copyID))
6876 {
6877 fc = *pfc;
6878 *pfc = fc->caller;
6879 free_funccal(fc, TRUE);
6880 did_free = TRUE;
6881 did_free_funccal = TRUE;
6882 }
6883 else
6884 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006885 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006886 if (did_free_funccal)
6887 /* When a funccal was freed some more items might be garbage
6888 * collected, so run again. */
6889 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006890 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006891 else if (p_verbose > 0)
6892 {
6893 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6894 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006895
6896 return did_free;
6897}
6898
6899/*
6900 * Free lists and dictionaries that are no longer referenced.
6901 */
6902 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006903free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006904{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006905 dict_T *dd, *dd_next;
6906 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006907 int did_free = FALSE;
6908
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006909 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006910 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006911 */
6912 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006913 {
6914 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006915 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006916 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006917 /* Free the Dictionary and ordinary items it contains, but don't
6918 * recurse into Lists and Dictionaries, they will be in the list
6919 * of dicts or list of lists. */
6920 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006921 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006922 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006923 dd = dd_next;
6924 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006925
6926 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006927 * Go through the list of lists and free items without the copyID.
6928 * But don't free a list that has a watcher (used in a for loop), these
6929 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006930 */
6931 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006932 {
6933 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006934 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6935 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006936 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006937 /* Free the List and ordinary items it contains, but don't recurse
6938 * into Lists and Dictionaries, they will be in the list of dicts
6939 * or list of lists. */
6940 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006941 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006942 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006943 ll = ll_next;
6944 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006945 return did_free;
6946}
6947
6948/*
6949 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006950 * "list_stack" is used to add lists to be marked. Can be NULL.
6951 *
6952 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006953 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006954 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006955set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006956{
6957 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006958 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006959 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006960 hashtab_T *cur_ht;
6961 ht_stack_T *ht_stack = NULL;
6962 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006963
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006964 cur_ht = ht;
6965 for (;;)
6966 {
6967 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006968 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006969 /* Mark each item in the hashtab. If the item contains a hashtab
6970 * it is added to ht_stack, if it contains a list it is added to
6971 * list_stack. */
6972 todo = (int)cur_ht->ht_used;
6973 for (hi = cur_ht->ht_array; todo > 0; ++hi)
6974 if (!HASHITEM_EMPTY(hi))
6975 {
6976 --todo;
6977 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
6978 &ht_stack, list_stack);
6979 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006980 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006981
6982 if (ht_stack == NULL)
6983 break;
6984
6985 /* take an item from the stack */
6986 cur_ht = ht_stack->ht;
6987 tempitem = ht_stack;
6988 ht_stack = ht_stack->prev;
6989 free(tempitem);
6990 }
6991
6992 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993}
6994
6995/*
6996 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006997 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6998 *
6999 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007001 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007002set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007003{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007004 listitem_T *li;
7005 int abort = FALSE;
7006 list_T *cur_l;
7007 list_stack_T *list_stack = NULL;
7008 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007009
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007010 cur_l = l;
7011 for (;;)
7012 {
7013 if (!abort)
7014 /* Mark each item in the list. If the item contains a hashtab
7015 * it is added to ht_stack, if it contains a list it is added to
7016 * list_stack. */
7017 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7018 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7019 ht_stack, &list_stack);
7020 if (list_stack == NULL)
7021 break;
7022
7023 /* take an item from the stack */
7024 cur_l = list_stack->list;
7025 tempitem = list_stack;
7026 list_stack = list_stack->prev;
7027 free(tempitem);
7028 }
7029
7030 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007031}
7032
7033/*
7034 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007035 * "list_stack" is used to add lists to be marked. Can be NULL.
7036 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7037 *
7038 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007039 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007040 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007041set_ref_in_item(
7042 typval_T *tv,
7043 int copyID,
7044 ht_stack_T **ht_stack,
7045 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007046{
7047 dict_T *dd;
7048 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007049 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007050
7051 switch (tv->v_type)
7052 {
7053 case VAR_DICT:
7054 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007055 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007056 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007057 /* Didn't see this dict yet. */
7058 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007059 if (ht_stack == NULL)
7060 {
7061 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7062 }
7063 else
7064 {
7065 ht_stack_T *newitem = (ht_stack_T*)malloc(
7066 sizeof(ht_stack_T));
7067 if (newitem == NULL)
7068 abort = TRUE;
7069 else
7070 {
7071 newitem->ht = &dd->dv_hashtab;
7072 newitem->prev = *ht_stack;
7073 *ht_stack = newitem;
7074 }
7075 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007076 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007077 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007078
7079 case VAR_LIST:
7080 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007081 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007082 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007083 /* Didn't see this list yet. */
7084 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007085 if (list_stack == NULL)
7086 {
7087 abort = set_ref_in_list(ll, copyID, ht_stack);
7088 }
7089 else
7090 {
7091 list_stack_T *newitem = (list_stack_T*)malloc(
7092 sizeof(list_stack_T));
7093 if (newitem == NULL)
7094 abort = TRUE;
7095 else
7096 {
7097 newitem->list = ll;
7098 newitem->prev = *list_stack;
7099 *list_stack = newitem;
7100 }
7101 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007102 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007103 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007104 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007105 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007106}
7107
7108/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007109 * Allocate an empty header for a dictionary.
7110 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007111 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007112dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007113{
Bram Moolenaar33570922005-01-25 22:26:29 +00007114 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007115
Bram Moolenaar33570922005-01-25 22:26:29 +00007116 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007117 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007118 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007119 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007120 if (first_dict != NULL)
7121 first_dict->dv_used_prev = d;
7122 d->dv_used_next = first_dict;
7123 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007124 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007125
Bram Moolenaar33570922005-01-25 22:26:29 +00007126 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007127 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007128 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007129 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007130 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007131 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007132 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007133}
7134
7135/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007136 * Allocate an empty dict for a return value.
7137 * Returns OK or FAIL.
7138 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007139 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007140rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007141{
7142 dict_T *d = dict_alloc();
7143
7144 if (d == NULL)
7145 return FAIL;
7146
7147 rettv->vval.v_dict = d;
7148 rettv->v_type = VAR_DICT;
7149 ++d->dv_refcount;
7150 return OK;
7151}
7152
7153
7154/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007155 * Unreference a Dictionary: decrement the reference count and free it when it
7156 * becomes zero.
7157 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007158 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007159dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007160{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007161 if (d != NULL && --d->dv_refcount <= 0)
7162 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007163}
7164
7165/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007166 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007167 * Ignores the reference count.
7168 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007169 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007170dict_free(
7171 dict_T *d,
7172 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007173{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007174 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007175 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007176 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007177
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007178 /* Remove the dict from the list of dicts for garbage collection. */
7179 if (d->dv_used_prev == NULL)
7180 first_dict = d->dv_used_next;
7181 else
7182 d->dv_used_prev->dv_used_next = d->dv_used_next;
7183 if (d->dv_used_next != NULL)
7184 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7185
7186 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007187 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007188 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007189 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007190 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007191 if (!HASHITEM_EMPTY(hi))
7192 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007193 /* Remove the item before deleting it, just in case there is
7194 * something recursive causing trouble. */
7195 di = HI2DI(hi);
7196 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007197 if (recurse || (di->di_tv.v_type != VAR_LIST
7198 && di->di_tv.v_type != VAR_DICT))
7199 clear_tv(&di->di_tv);
7200 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007201 --todo;
7202 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007203 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007204 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007205 vim_free(d);
7206}
7207
7208/*
7209 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007210 * The "key" is copied to the new item.
7211 * Note that the value of the item "di_tv" still needs to be initialized!
7212 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007213 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007214 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007215dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007216{
Bram Moolenaar33570922005-01-25 22:26:29 +00007217 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007218
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007219 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007220 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007221 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007222 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007223 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007224 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007225 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007226}
7227
7228/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007229 * Make a copy of a Dictionary item.
7230 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007231 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007232dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007233{
Bram Moolenaar33570922005-01-25 22:26:29 +00007234 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007235
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007236 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7237 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007238 if (di != NULL)
7239 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007240 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007241 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007242 copy_tv(&org->di_tv, &di->di_tv);
7243 }
7244 return di;
7245}
7246
7247/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007248 * Remove item "item" from Dictionary "dict" and free it.
7249 */
7250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007251dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007252{
Bram Moolenaar33570922005-01-25 22:26:29 +00007253 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007254
Bram Moolenaar33570922005-01-25 22:26:29 +00007255 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007256 if (HASHITEM_EMPTY(hi))
7257 EMSG2(_(e_intern2), "dictitem_remove()");
7258 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007259 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007260 dictitem_free(item);
7261}
7262
7263/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007264 * Free a dict item. Also clears the value.
7265 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007266 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007267dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007269 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007270 if (item->di_flags & DI_FLAGS_ALLOC)
7271 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007272}
7273
7274/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007275 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7276 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007277 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007278 * Returns NULL when out of memory.
7279 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007280 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007281dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007282{
Bram Moolenaar33570922005-01-25 22:26:29 +00007283 dict_T *copy;
7284 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007285 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007286 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007287
7288 if (orig == NULL)
7289 return NULL;
7290
7291 copy = dict_alloc();
7292 if (copy != NULL)
7293 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007294 if (copyID != 0)
7295 {
7296 orig->dv_copyID = copyID;
7297 orig->dv_copydict = copy;
7298 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007299 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007300 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007301 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007302 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007303 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007304 --todo;
7305
7306 di = dictitem_alloc(hi->hi_key);
7307 if (di == NULL)
7308 break;
7309 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007310 {
7311 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7312 copyID) == FAIL)
7313 {
7314 vim_free(di);
7315 break;
7316 }
7317 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007318 else
7319 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7320 if (dict_add(copy, di) == FAIL)
7321 {
7322 dictitem_free(di);
7323 break;
7324 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007325 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007326 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007327
Bram Moolenaare9a41262005-01-15 22:18:47 +00007328 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007329 if (todo > 0)
7330 {
7331 dict_unref(copy);
7332 copy = NULL;
7333 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007334 }
7335
7336 return copy;
7337}
7338
7339/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007340 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007341 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007342 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007343 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007344dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007345{
Bram Moolenaar33570922005-01-25 22:26:29 +00007346 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007347}
7348
Bram Moolenaar8c711452005-01-14 21:53:12 +00007349/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007350 * Add a number or string entry to dictionary "d".
7351 * When "str" is NULL use number "nr", otherwise use "str".
7352 * Returns FAIL when out of memory and when key already exists.
7353 */
7354 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007355dict_add_nr_str(
7356 dict_T *d,
7357 char *key,
7358 long nr,
7359 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007360{
7361 dictitem_T *item;
7362
7363 item = dictitem_alloc((char_u *)key);
7364 if (item == NULL)
7365 return FAIL;
7366 item->di_tv.v_lock = 0;
7367 if (str == NULL)
7368 {
7369 item->di_tv.v_type = VAR_NUMBER;
7370 item->di_tv.vval.v_number = nr;
7371 }
7372 else
7373 {
7374 item->di_tv.v_type = VAR_STRING;
7375 item->di_tv.vval.v_string = vim_strsave(str);
7376 }
7377 if (dict_add(d, item) == FAIL)
7378 {
7379 dictitem_free(item);
7380 return FAIL;
7381 }
7382 return OK;
7383}
7384
7385/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007386 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007387 * Returns FAIL when out of memory and when key already exists.
7388 */
7389 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007390dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007391{
7392 dictitem_T *item;
7393
7394 item = dictitem_alloc((char_u *)key);
7395 if (item == NULL)
7396 return FAIL;
7397 item->di_tv.v_lock = 0;
7398 item->di_tv.v_type = VAR_LIST;
7399 item->di_tv.vval.v_list = list;
7400 if (dict_add(d, item) == FAIL)
7401 {
7402 dictitem_free(item);
7403 return FAIL;
7404 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007405 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007406 return OK;
7407}
7408
7409/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007410 * Get the number of items in a Dictionary.
7411 */
7412 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007413dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007414{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007415 if (d == NULL)
7416 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007417 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007418}
7419
7420/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007421 * Find item "key[len]" in Dictionary "d".
7422 * If "len" is negative use strlen(key).
7423 * Returns NULL when not found.
7424 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007425 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007426dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007427{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007428#define AKEYLEN 200
7429 char_u buf[AKEYLEN];
7430 char_u *akey;
7431 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007432 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007433
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007434 if (len < 0)
7435 akey = key;
7436 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007437 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007438 tofree = akey = vim_strnsave(key, len);
7439 if (akey == NULL)
7440 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007441 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007442 else
7443 {
7444 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007445 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007446 akey = buf;
7447 }
7448
Bram Moolenaar33570922005-01-25 22:26:29 +00007449 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007450 vim_free(tofree);
7451 if (HASHITEM_EMPTY(hi))
7452 return NULL;
7453 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007454}
7455
7456/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007457 * Get a string item from a dictionary.
7458 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007459 * Returns NULL if the entry doesn't exist or out of memory.
7460 */
7461 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007462get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007463{
7464 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007465 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007466
7467 di = dict_find(d, key, -1);
7468 if (di == NULL)
7469 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007470 s = get_tv_string(&di->di_tv);
7471 if (save && s != NULL)
7472 s = vim_strsave(s);
7473 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007474}
7475
7476/*
7477 * Get a number item from a dictionary.
7478 * Returns 0 if the entry doesn't exist or out of memory.
7479 */
7480 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007481get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007482{
7483 dictitem_T *di;
7484
7485 di = dict_find(d, key, -1);
7486 if (di == NULL)
7487 return 0;
7488 return get_tv_number(&di->di_tv);
7489}
7490
7491/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007492 * Return an allocated string with the string representation of a Dictionary.
7493 * May return NULL.
7494 */
7495 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007496dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007497{
7498 garray_T ga;
7499 int first = TRUE;
7500 char_u *tofree;
7501 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007502 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007503 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007504 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007505 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007506
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007507 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007508 return NULL;
7509 ga_init2(&ga, (int)sizeof(char), 80);
7510 ga_append(&ga, '{');
7511
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007512 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007513 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007514 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007515 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007516 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007517 --todo;
7518
7519 if (first)
7520 first = FALSE;
7521 else
7522 ga_concat(&ga, (char_u *)", ");
7523
7524 tofree = string_quote(hi->hi_key, FALSE);
7525 if (tofree != NULL)
7526 {
7527 ga_concat(&ga, tofree);
7528 vim_free(tofree);
7529 }
7530 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007531 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007532 if (s != NULL)
7533 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007534 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007535 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007536 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007537 line_breakcheck();
7538
Bram Moolenaar8c711452005-01-14 21:53:12 +00007539 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007540 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007541 if (todo > 0)
7542 {
7543 vim_free(ga.ga_data);
7544 return NULL;
7545 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007546
7547 ga_append(&ga, '}');
7548 ga_append(&ga, NUL);
7549 return (char_u *)ga.ga_data;
7550}
7551
7552/*
7553 * Allocate a variable for a Dictionary and fill it from "*arg".
7554 * Return OK or FAIL. Returns NOTDONE for {expr}.
7555 */
7556 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007557get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007558{
Bram Moolenaar33570922005-01-25 22:26:29 +00007559 dict_T *d = NULL;
7560 typval_T tvkey;
7561 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007562 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007563 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007564 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007565 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007566
7567 /*
7568 * First check if it's not a curly-braces thing: {expr}.
7569 * Must do this without evaluating, otherwise a function may be called
7570 * twice. Unfortunately this means we need to call eval1() twice for the
7571 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007572 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007573 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007574 if (*start != '}')
7575 {
7576 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7577 return FAIL;
7578 if (*start == '}')
7579 return NOTDONE;
7580 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581
7582 if (evaluate)
7583 {
7584 d = dict_alloc();
7585 if (d == NULL)
7586 return FAIL;
7587 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007588 tvkey.v_type = VAR_UNKNOWN;
7589 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007590
7591 *arg = skipwhite(*arg + 1);
7592 while (**arg != '}' && **arg != NUL)
7593 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007594 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007595 goto failret;
7596 if (**arg != ':')
7597 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007598 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007599 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600 goto failret;
7601 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007602 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007603 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007604 key = get_tv_string_buf_chk(&tvkey, buf);
7605 if (key == NULL || *key == NUL)
7606 {
7607 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7608 if (key != NULL)
7609 EMSG(_(e_emptykey));
7610 clear_tv(&tvkey);
7611 goto failret;
7612 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007614
7615 *arg = skipwhite(*arg + 1);
7616 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7617 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007618 if (evaluate)
7619 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007620 goto failret;
7621 }
7622 if (evaluate)
7623 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007624 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007625 if (item != NULL)
7626 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007627 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007628 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007629 clear_tv(&tv);
7630 goto failret;
7631 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007632 item = dictitem_alloc(key);
7633 clear_tv(&tvkey);
7634 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007635 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007636 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007637 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007638 if (dict_add(d, item) == FAIL)
7639 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007640 }
7641 }
7642
7643 if (**arg == '}')
7644 break;
7645 if (**arg != ',')
7646 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007647 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007648 goto failret;
7649 }
7650 *arg = skipwhite(*arg + 1);
7651 }
7652
7653 if (**arg != '}')
7654 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007655 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007656failret:
7657 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007658 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007659 return FAIL;
7660 }
7661
7662 *arg = skipwhite(*arg + 1);
7663 if (evaluate)
7664 {
7665 rettv->v_type = VAR_DICT;
7666 rettv->vval.v_dict = d;
7667 ++d->dv_refcount;
7668 }
7669
7670 return OK;
7671}
7672
Bram Moolenaar17a13432016-01-24 14:22:10 +01007673 static char *
7674get_var_special_name(int nr)
7675{
7676 switch (nr)
7677 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007678 case VVAL_FALSE: return "v:false";
7679 case VVAL_TRUE: return "v:true";
7680 case VVAL_NONE: return "v:none";
7681 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007682 }
7683 EMSG2(_(e_intern2), "get_var_special_name()");
7684 return "42";
7685}
7686
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007688 * Return a string with the string representation of a variable.
7689 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007690 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007691 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007692 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007693 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007694 */
7695 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007696echo_string(
7697 typval_T *tv,
7698 char_u **tofree,
7699 char_u *numbuf,
7700 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007701{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007702 static int recurse = 0;
7703 char_u *r = NULL;
7704
Bram Moolenaar33570922005-01-25 22:26:29 +00007705 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007706 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007707 if (!did_echo_string_emsg)
7708 {
7709 /* Only give this message once for a recursive call to avoid
7710 * flooding the user with errors. And stop iterating over lists
7711 * and dicts. */
7712 did_echo_string_emsg = TRUE;
7713 EMSG(_("E724: variable nested too deep for displaying"));
7714 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007715 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007716 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007717 }
7718 ++recurse;
7719
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007720 switch (tv->v_type)
7721 {
7722 case VAR_FUNC:
7723 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007724 r = tv->vval.v_string;
7725 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007726
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007727 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007728 if (tv->vval.v_list == NULL)
7729 {
7730 *tofree = NULL;
7731 r = NULL;
7732 }
7733 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7734 {
7735 *tofree = NULL;
7736 r = (char_u *)"[...]";
7737 }
7738 else
7739 {
7740 tv->vval.v_list->lv_copyID = copyID;
7741 *tofree = list2string(tv, copyID);
7742 r = *tofree;
7743 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007744 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007745
Bram Moolenaar8c711452005-01-14 21:53:12 +00007746 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007747 if (tv->vval.v_dict == NULL)
7748 {
7749 *tofree = NULL;
7750 r = NULL;
7751 }
7752 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7753 {
7754 *tofree = NULL;
7755 r = (char_u *)"{...}";
7756 }
7757 else
7758 {
7759 tv->vval.v_dict->dv_copyID = copyID;
7760 *tofree = dict2string(tv, copyID);
7761 r = *tofree;
7762 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007763 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007764
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007765 case VAR_STRING:
7766 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007767 *tofree = NULL;
7768 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007769 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007770
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007771#ifdef FEAT_FLOAT
7772 case VAR_FLOAT:
7773 *tofree = NULL;
7774 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7775 r = numbuf;
7776 break;
7777#endif
7778
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007779 case VAR_SPECIAL:
7780 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007781 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007782 break;
7783
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007784 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007785 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007786 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007787 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007788
Bram Moolenaar8502c702014-06-17 12:51:16 +02007789 if (--recurse == 0)
7790 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007791 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007792}
7793
7794/*
7795 * Return a string with the string representation of a variable.
7796 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7797 * "numbuf" is used for a number.
7798 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007799 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007800 */
7801 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007802tv2string(
7803 typval_T *tv,
7804 char_u **tofree,
7805 char_u *numbuf,
7806 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007807{
7808 switch (tv->v_type)
7809 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007810 case VAR_FUNC:
7811 *tofree = string_quote(tv->vval.v_string, TRUE);
7812 return *tofree;
7813 case VAR_STRING:
7814 *tofree = string_quote(tv->vval.v_string, FALSE);
7815 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007816#ifdef FEAT_FLOAT
7817 case VAR_FLOAT:
7818 *tofree = NULL;
7819 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7820 return numbuf;
7821#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007822 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007823 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007824 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007825 case VAR_SPECIAL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007826 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007827 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007828 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007829 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007830 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007831}
7832
7833/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007834 * Return string "str" in ' quotes, doubling ' characters.
7835 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007836 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007837 */
7838 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007839string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007840{
Bram Moolenaar33570922005-01-25 22:26:29 +00007841 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007842 char_u *p, *r, *s;
7843
Bram Moolenaar33570922005-01-25 22:26:29 +00007844 len = (function ? 13 : 3);
7845 if (str != NULL)
7846 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007847 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007848 for (p = str; *p != NUL; mb_ptr_adv(p))
7849 if (*p == '\'')
7850 ++len;
7851 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007852 s = r = alloc(len);
7853 if (r != NULL)
7854 {
7855 if (function)
7856 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007857 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007858 r += 10;
7859 }
7860 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007861 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007862 if (str != NULL)
7863 for (p = str; *p != NUL; )
7864 {
7865 if (*p == '\'')
7866 *r++ = '\'';
7867 MB_COPY_CHAR(p, r);
7868 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007869 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007870 if (function)
7871 *r++ = ')';
7872 *r++ = NUL;
7873 }
7874 return s;
7875}
7876
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007877#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007878/*
7879 * Convert the string "text" to a floating point number.
7880 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7881 * this always uses a decimal point.
7882 * Returns the length of the text that was consumed.
7883 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007884 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007885string2float(
7886 char_u *text,
7887 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007888{
7889 char *s = (char *)text;
7890 float_T f;
7891
7892 f = strtod(s, &s);
7893 *value = f;
7894 return (int)((char_u *)s - text);
7895}
7896#endif
7897
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 * Get the value of an environment variable.
7900 * "arg" is pointing to the '$'. It is advanced to after the name.
7901 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007902 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 */
7904 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007905get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906{
7907 char_u *string = NULL;
7908 int len;
7909 int cc;
7910 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007911 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912
7913 ++*arg;
7914 name = *arg;
7915 len = get_env_len(arg);
7916 if (evaluate)
7917 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007918 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01007919 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007920
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007921 cc = name[len];
7922 name[len] = NUL;
7923 /* first try vim_getenv(), fast for normal environment vars */
7924 string = vim_getenv(name, &mustfree);
7925 if (string != NULL && *string != NUL)
7926 {
7927 if (!mustfree)
7928 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007930 else
7931 {
7932 if (mustfree)
7933 vim_free(string);
7934
7935 /* next try expanding things like $VIM and ${HOME} */
7936 string = expand_env_save(name - 1);
7937 if (string != NULL && *string == '$')
7938 {
7939 vim_free(string);
7940 string = NULL;
7941 }
7942 }
7943 name[len] = cc;
7944
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007945 rettv->v_type = VAR_STRING;
7946 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 }
7948
7949 return OK;
7950}
7951
7952/*
7953 * Array with names and number of arguments of all internal functions
7954 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7955 */
7956static struct fst
7957{
7958 char *f_name; /* function name */
7959 char f_min_argc; /* minimal number of arguments */
7960 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01007961 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007962 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963} functions[] =
7964{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007965#ifdef FEAT_FLOAT
7966 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007967 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007968#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007969 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01007970 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007971 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 {"append", 2, 2, f_append},
7973 {"argc", 0, 0, f_argc},
7974 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007975 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007976 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01007977#ifdef FEAT_FLOAT
7978 {"asin", 1, 1, f_asin}, /* WJMc */
7979#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01007980 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01007981 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01007982 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01007983 {"assert_false", 1, 2, f_assert_false},
7984 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007985#ifdef FEAT_FLOAT
7986 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007987 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007990 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 {"bufexists", 1, 1, f_bufexists},
7992 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7993 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7994 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7995 {"buflisted", 1, 1, f_buflisted},
7996 {"bufloaded", 1, 1, f_bufloaded},
7997 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007998 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 {"bufwinnr", 1, 1, f_bufwinnr},
8000 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008001 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008002 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008003 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008004#ifdef FEAT_FLOAT
8005 {"ceil", 1, 1, f_ceil},
8006#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008007 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008008 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008010 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008012#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008013 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008014 {"complete_add", 1, 1, f_complete_add},
8015 {"complete_check", 0, 0, f_complete_check},
8016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 {"confirm", 1, 4, f_confirm},
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008018#ifdef FEAT_CHANNEL
8019 {"connect", 2, 3, f_connect},
8020#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008021 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008022#ifdef FEAT_FLOAT
8023 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008024 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008025#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008026 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008028 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008029 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008030 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008032 {"diff_filler", 1, 1, f_diff_filler},
8033 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008034#ifdef FEAT_CHANNEL
8035 {"disconnect", 1, 1, f_disconnect},
8036#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008037 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008039 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 {"eventhandler", 0, 0, f_eventhandler},
8041 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008042 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008044#ifdef FEAT_FLOAT
8045 {"exp", 1, 1, f_exp},
8046#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008047 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008048 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008049 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8051 {"filereadable", 1, 1, f_filereadable},
8052 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008053 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008054 {"finddir", 1, 3, f_finddir},
8055 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008056#ifdef FEAT_FLOAT
8057 {"float2nr", 1, 1, f_float2nr},
8058 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008059 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008060#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008061 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 {"fnamemodify", 2, 2, f_fnamemodify},
8063 {"foldclosed", 1, 1, f_foldclosed},
8064 {"foldclosedend", 1, 1, f_foldclosedend},
8065 {"foldlevel", 1, 1, f_foldlevel},
8066 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008067 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008069 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008070 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008071 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008072 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008073 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 {"getchar", 0, 1, f_getchar},
8075 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008076 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 {"getcmdline", 0, 0, f_getcmdline},
8078 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008079 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008080 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008081 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008082 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008083 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008084 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 {"getfsize", 1, 1, f_getfsize},
8086 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008087 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008088 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008089 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008090 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008091 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008092 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008093 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008094 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008096 {"gettabvar", 2, 3, f_gettabvar},
8097 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 {"getwinposx", 0, 0, f_getwinposx},
8099 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008100 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008101 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008102 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008103 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008105 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008106 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008107 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8109 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8110 {"histadd", 2, 2, f_histadd},
8111 {"histdel", 1, 2, f_histdel},
8112 {"histget", 1, 2, f_histget},
8113 {"histnr", 1, 1, f_histnr},
8114 {"hlID", 1, 1, f_hlID},
8115 {"hlexists", 1, 1, f_hlexists},
8116 {"hostname", 0, 0, f_hostname},
8117 {"iconv", 3, 3, f_iconv},
8118 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008119 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008120 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008122 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {"inputrestore", 0, 0, f_inputrestore},
8124 {"inputsave", 0, 0, f_inputsave},
8125 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008126 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008127 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008129 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008130 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008131 {"join", 1, 2, f_join},
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008132 {"jsondecode", 1, 1, f_jsondecode},
8133 {"jsonencode", 1, 1, f_jsonencode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008134 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008136 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 {"libcall", 3, 3, f_libcall},
8138 {"libcallnr", 3, 3, f_libcallnr},
8139 {"line", 1, 1, f_line},
8140 {"line2byte", 1, 1, f_line2byte},
8141 {"lispindent", 1, 1, f_lispindent},
8142 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008143#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008144 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008145 {"log10", 1, 1, f_log10},
8146#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008147#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008148 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008149#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008150 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008151 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008152 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008153 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008154 {"matchadd", 2, 5, f_matchadd},
8155 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008156 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008157 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008158 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008159 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008160 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008161 {"max", 1, 1, f_max},
8162 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008163#ifdef vim_mkdir
8164 {"mkdir", 1, 3, f_mkdir},
8165#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008166 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008167#ifdef FEAT_MZSCHEME
8168 {"mzeval", 1, 1, f_mzeval},
8169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008171 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008172 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008173 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008174#ifdef FEAT_PERL
8175 {"perleval", 1, 1, f_perleval},
8176#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008177#ifdef FEAT_FLOAT
8178 {"pow", 2, 2, f_pow},
8179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008181 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008182 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008183#ifdef FEAT_PYTHON3
8184 {"py3eval", 1, 1, f_py3eval},
8185#endif
8186#ifdef FEAT_PYTHON
8187 {"pyeval", 1, 1, f_pyeval},
8188#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008189 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008190 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008191 {"reltime", 0, 2, f_reltime},
8192 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 {"remote_expr", 2, 3, f_remote_expr},
8194 {"remote_foreground", 1, 1, f_remote_foreground},
8195 {"remote_peek", 1, 2, f_remote_peek},
8196 {"remote_read", 1, 1, f_remote_read},
8197 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008198 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008200 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008202 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008203#ifdef FEAT_FLOAT
8204 {"round", 1, 1, f_round},
8205#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008206 {"screenattr", 2, 2, f_screenattr},
8207 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008208 {"screencol", 0, 0, f_screencol},
8209 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008210 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008211 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008212 {"searchpair", 3, 7, f_searchpair},
8213 {"searchpairpos", 3, 7, f_searchpairpos},
8214 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008215#ifdef FEAT_CHANNEL
8216 {"sendexpr", 2, 3, f_sendexpr},
8217 {"sendraw", 2, 3, f_sendraw},
8218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 {"server2client", 2, 2, f_server2client},
8220 {"serverlist", 0, 0, f_serverlist},
8221 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008222 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 {"setcmdpos", 1, 1, f_setcmdpos},
8224 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008225 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008226 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008227 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008228 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008230 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008231 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008233#ifdef FEAT_CRYPT
8234 {"sha256", 1, 1, f_sha256},
8235#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008236 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008237 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008239#ifdef FEAT_FLOAT
8240 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008241 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008242#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008243 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008244 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008245 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008246 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008247 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008248#ifdef FEAT_FLOAT
8249 {"sqrt", 1, 1, f_sqrt},
8250 {"str2float", 1, 1, f_str2float},
8251#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008252 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008253 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008254 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255#ifdef HAVE_STRFTIME
8256 {"strftime", 1, 2, f_strftime},
8257#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008258 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008259 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 {"strlen", 1, 1, f_strlen},
8261 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008262 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008264 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008265 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 {"substitute", 4, 4, f_substitute},
8267 {"synID", 3, 3, f_synID},
8268 {"synIDattr", 2, 3, f_synIDattr},
8269 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008270 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008271 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008272 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008273 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008274 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008275 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008276 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008277 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008278 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008279#ifdef FEAT_FLOAT
8280 {"tan", 1, 1, f_tan},
8281 {"tanh", 1, 1, f_tanh},
8282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008284 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 {"tolower", 1, 1, f_tolower},
8286 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008287 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008288#ifdef FEAT_FLOAT
8289 {"trunc", 1, 1, f_trunc},
8290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008292 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008293 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008294 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008295 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 {"virtcol", 1, 1, f_virtcol},
8297 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008298 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 {"winbufnr", 1, 1, f_winbufnr},
8300 {"wincol", 0, 0, f_wincol},
8301 {"winheight", 1, 1, f_winheight},
8302 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008303 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008305 {"winrestview", 1, 1, f_winrestview},
8306 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008308 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008309 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008310 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311};
8312
8313#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8314
8315/*
8316 * Function given to ExpandGeneric() to obtain the list of internal
8317 * or user defined function names.
8318 */
8319 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008320get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321{
8322 static int intidx = -1;
8323 char_u *name;
8324
8325 if (idx == 0)
8326 intidx = -1;
8327 if (intidx < 0)
8328 {
8329 name = get_user_func_name(xp, idx);
8330 if (name != NULL)
8331 return name;
8332 }
8333 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8334 {
8335 STRCPY(IObuff, functions[intidx].f_name);
8336 STRCAT(IObuff, "(");
8337 if (functions[intidx].f_max_argc == 0)
8338 STRCAT(IObuff, ")");
8339 return IObuff;
8340 }
8341
8342 return NULL;
8343}
8344
8345/*
8346 * Function given to ExpandGeneric() to obtain the list of internal or
8347 * user defined variable or function names.
8348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008350get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351{
8352 static int intidx = -1;
8353 char_u *name;
8354
8355 if (idx == 0)
8356 intidx = -1;
8357 if (intidx < 0)
8358 {
8359 name = get_function_name(xp, idx);
8360 if (name != NULL)
8361 return name;
8362 }
8363 return get_user_var_name(xp, ++intidx);
8364}
8365
8366#endif /* FEAT_CMDL_COMPL */
8367
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008368#if defined(EBCDIC) || defined(PROTO)
8369/*
8370 * Compare struct fst by function name.
8371 */
8372 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008373compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008374{
8375 struct fst *p1 = (struct fst *)s1;
8376 struct fst *p2 = (struct fst *)s2;
8377
8378 return STRCMP(p1->f_name, p2->f_name);
8379}
8380
8381/*
8382 * Sort the function table by function name.
8383 * The sorting of the table above is ASCII dependant.
8384 * On machines using EBCDIC we have to sort it.
8385 */
8386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008387sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008388{
8389 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8390
8391 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8392}
8393#endif
8394
8395
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396/*
8397 * Find internal function in table above.
8398 * Return index, or -1 if not found
8399 */
8400 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008401find_internal_func(
8402 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403{
8404 int first = 0;
8405 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8406 int cmp;
8407 int x;
8408
8409 /*
8410 * Find the function name in the table. Binary search.
8411 */
8412 while (first <= last)
8413 {
8414 x = first + ((unsigned)(last - first) >> 1);
8415 cmp = STRCMP(name, functions[x].f_name);
8416 if (cmp < 0)
8417 last = x - 1;
8418 else if (cmp > 0)
8419 first = x + 1;
8420 else
8421 return x;
8422 }
8423 return -1;
8424}
8425
8426/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008427 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8428 * name it contains, otherwise return "name".
8429 */
8430 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008431deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008432{
Bram Moolenaar33570922005-01-25 22:26:29 +00008433 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008434 int cc;
8435
8436 cc = name[*lenp];
8437 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008438 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008439 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008440 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008441 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008442 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008443 {
8444 *lenp = 0;
8445 return (char_u *)""; /* just in case */
8446 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008447 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008448 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008449 }
8450
8451 return name;
8452}
8453
8454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 * Allocate a variable for the result of a function.
8456 * Return OK or FAIL.
8457 */
8458 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008459get_func_tv(
8460 char_u *name, /* name of the function */
8461 int len, /* length of "name" */
8462 typval_T *rettv,
8463 char_u **arg, /* argument, pointing to the '(' */
8464 linenr_T firstline, /* first line of range */
8465 linenr_T lastline, /* last line of range */
8466 int *doesrange, /* return: function handled range */
8467 int evaluate,
8468 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469{
8470 char_u *argp;
8471 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008472 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 int argcount = 0; /* number of arguments found */
8474
8475 /*
8476 * Get the arguments.
8477 */
8478 argp = *arg;
8479 while (argcount < MAX_FUNC_ARGS)
8480 {
8481 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8482 if (*argp == ')' || *argp == ',' || *argp == NUL)
8483 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8485 {
8486 ret = FAIL;
8487 break;
8488 }
8489 ++argcount;
8490 if (*argp != ',')
8491 break;
8492 }
8493 if (*argp == ')')
8494 ++argp;
8495 else
8496 ret = FAIL;
8497
8498 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008499 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008500 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008502 {
8503 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008504 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008505 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008506 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508
8509 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008510 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511
8512 *arg = skipwhite(argp);
8513 return ret;
8514}
8515
8516
8517/*
8518 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008519 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008520 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008522 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008523call_func(
8524 char_u *funcname, /* name of the function */
8525 int len, /* length of "name" */
8526 typval_T *rettv, /* return value goes here */
8527 int argcount, /* number of "argvars" */
8528 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008529 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008530 linenr_T firstline, /* first line of range */
8531 linenr_T lastline, /* last line of range */
8532 int *doesrange, /* return: function handled range */
8533 int evaluate,
8534 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535{
8536 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537#define ERROR_UNKNOWN 0
8538#define ERROR_TOOMANY 1
8539#define ERROR_TOOFEW 2
8540#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008541#define ERROR_DICT 4
8542#define ERROR_NONE 5
8543#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 int error = ERROR_NONE;
8545 int i;
8546 int llen;
8547 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548#define FLEN_FIXED 40
8549 char_u fname_buf[FLEN_FIXED + 1];
8550 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008551 char_u *name;
8552
8553 /* Make a copy of the name, if it comes from a funcref variable it could
8554 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008555 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008556 if (name == NULL)
8557 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558
8559 /*
8560 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8561 * Change <SNR>123_name() to K_SNR 123_name().
8562 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 llen = eval_fname_script(name);
8565 if (llen > 0)
8566 {
8567 fname_buf[0] = K_SPECIAL;
8568 fname_buf[1] = KS_EXTRA;
8569 fname_buf[2] = (int)KE_SNR;
8570 i = 3;
8571 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8572 {
8573 if (current_SID <= 0)
8574 error = ERROR_SCRIPT;
8575 else
8576 {
8577 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8578 i = (int)STRLEN(fname_buf);
8579 }
8580 }
8581 if (i + STRLEN(name + llen) < FLEN_FIXED)
8582 {
8583 STRCPY(fname_buf + i, name + llen);
8584 fname = fname_buf;
8585 }
8586 else
8587 {
8588 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8589 if (fname == NULL)
8590 error = ERROR_OTHER;
8591 else
8592 {
8593 mch_memmove(fname, fname_buf, (size_t)i);
8594 STRCPY(fname + i, name + llen);
8595 }
8596 }
8597 }
8598 else
8599 fname = name;
8600
8601 *doesrange = FALSE;
8602
8603
8604 /* execute the function if no errors detected and executing */
8605 if (evaluate && error == ERROR_NONE)
8606 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008607 char_u *rfname = fname;
8608
8609 /* Ignore "g:" before a function name. */
8610 if (fname[0] == 'g' && fname[1] == ':')
8611 rfname = fname + 2;
8612
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008613 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8614 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 error = ERROR_UNKNOWN;
8616
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008617 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 {
8619 /*
8620 * User defined function.
8621 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008622 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008623
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008625 /* Trigger FuncUndefined event, may load the function. */
8626 if (fp == NULL
8627 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008628 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008629 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008631 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008632 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 }
8634#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008635 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008636 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008637 {
8638 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008639 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008640 }
8641
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 if (fp != NULL)
8643 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008644 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008646 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008648 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008650 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008651 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 else
8653 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008654 int did_save_redo = FALSE;
8655
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 /*
8657 * Call the user function.
8658 * Save and restore search patterns, script variables and
8659 * redo buffer.
8660 */
8661 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008662#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008663 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008664#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008665 {
8666 saveRedobuff();
8667 did_save_redo = TRUE;
8668 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008669 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008670 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008671 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008672 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8673 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8674 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008675 /* Function was unreferenced while being used, free it
8676 * now. */
8677 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008678 if (did_save_redo)
8679 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 restore_search_patterns();
8681 error = ERROR_NONE;
8682 }
8683 }
8684 }
8685 else
8686 {
8687 /*
8688 * Find the function name in the table, call its implementation.
8689 */
8690 i = find_internal_func(fname);
8691 if (i >= 0)
8692 {
8693 if (argcount < functions[i].f_min_argc)
8694 error = ERROR_TOOFEW;
8695 else if (argcount > functions[i].f_max_argc)
8696 error = ERROR_TOOMANY;
8697 else
8698 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008699 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008700 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 error = ERROR_NONE;
8702 }
8703 }
8704 }
8705 /*
8706 * The function call (or "FuncUndefined" autocommand sequence) might
8707 * have been aborted by an error, an interrupt, or an explicitly thrown
8708 * exception that has not been caught so far. This situation can be
8709 * tested for by calling aborting(). For an error in an internal
8710 * function or for the "E132" error in call_user_func(), however, the
8711 * throw point at which the "force_abort" flag (temporarily reset by
8712 * emsg()) is normally updated has not been reached yet. We need to
8713 * update that flag first to make aborting() reliable.
8714 */
8715 update_force_abort();
8716 }
8717 if (error == ERROR_NONE)
8718 ret = OK;
8719
8720 /*
8721 * Report an error unless the argument evaluation or function call has been
8722 * cancelled due to an aborting error, an interrupt, or an exception.
8723 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008724 if (!aborting())
8725 {
8726 switch (error)
8727 {
8728 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008729 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008730 break;
8731 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008732 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008733 break;
8734 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008735 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008736 name);
8737 break;
8738 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008739 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008740 name);
8741 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008742 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008743 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008744 name);
8745 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008746 }
8747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 if (fname != name && fname != fname_buf)
8750 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008751 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752
8753 return ret;
8754}
8755
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008756/*
8757 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008758 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008759 */
8760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008761emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008762{
8763 char_u *p;
8764
8765 if (*name == K_SPECIAL)
8766 p = concat_str((char_u *)"<SNR>", name + 3);
8767 else
8768 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008769 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008770 if (p != name)
8771 vim_free(p);
8772}
8773
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008774/*
8775 * Return TRUE for a non-zero Number and a non-empty String.
8776 */
8777 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008778non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008779{
8780 return ((argvars[0].v_type == VAR_NUMBER
8781 && argvars[0].vval.v_number != 0)
8782 || (argvars[0].v_type == VAR_STRING
8783 && argvars[0].vval.v_string != NULL
8784 && *argvars[0].vval.v_string != NUL));
8785}
8786
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787/*********************************************
8788 * Implementation of the built-in functions
8789 */
8790
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008791#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008792static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008793
8794/*
8795 * Get the float value of "argvars[0]" into "f".
8796 * Returns FAIL when the argument is not a Number or Float.
8797 */
8798 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008799get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008800{
8801 if (argvars[0].v_type == VAR_FLOAT)
8802 {
8803 *f = argvars[0].vval.v_float;
8804 return OK;
8805 }
8806 if (argvars[0].v_type == VAR_NUMBER)
8807 {
8808 *f = (float_T)argvars[0].vval.v_number;
8809 return OK;
8810 }
8811 EMSG(_("E808: Number or Float required"));
8812 return FAIL;
8813}
8814
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008815/*
8816 * "abs(expr)" function
8817 */
8818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008819f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008820{
8821 if (argvars[0].v_type == VAR_FLOAT)
8822 {
8823 rettv->v_type = VAR_FLOAT;
8824 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8825 }
8826 else
8827 {
8828 varnumber_T n;
8829 int error = FALSE;
8830
8831 n = get_tv_number_chk(&argvars[0], &error);
8832 if (error)
8833 rettv->vval.v_number = -1;
8834 else if (n > 0)
8835 rettv->vval.v_number = n;
8836 else
8837 rettv->vval.v_number = -n;
8838 }
8839}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008840
8841/*
8842 * "acos()" function
8843 */
8844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008845f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008846{
8847 float_T f;
8848
8849 rettv->v_type = VAR_FLOAT;
8850 if (get_float_arg(argvars, &f) == OK)
8851 rettv->vval.v_float = acos(f);
8852 else
8853 rettv->vval.v_float = 0.0;
8854}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008855#endif
8856
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008858 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 */
8860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008861f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862{
Bram Moolenaar33570922005-01-25 22:26:29 +00008863 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008865 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008866 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008868 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008869 && !tv_check_lock(l->lv_lock,
8870 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008871 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008872 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008873 }
8874 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008875 EMSG(_(e_listreq));
8876}
8877
8878/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008879 * "alloc_fail(id, countdown, repeat)" function
8880 */
8881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008882f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008883{
8884 if (argvars[0].v_type != VAR_NUMBER
8885 || argvars[0].vval.v_number <= 0
8886 || argvars[1].v_type != VAR_NUMBER
8887 || argvars[1].vval.v_number < 0
8888 || argvars[2].v_type != VAR_NUMBER)
8889 EMSG(_(e_invarg));
8890 else
8891 {
8892 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008893 if (alloc_fail_id >= aid_last)
8894 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008895 alloc_fail_countdown = argvars[1].vval.v_number;
8896 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008897 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008898 }
8899}
8900
8901/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008902 * "and(expr, expr)" function
8903 */
8904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008905f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008906{
8907 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8908 & get_tv_number_chk(&argvars[1], NULL);
8909}
8910
8911/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008912 * "append(lnum, string/list)" function
8913 */
8914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008915f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008916{
8917 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008918 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008919 list_T *l = NULL;
8920 listitem_T *li = NULL;
8921 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008922 long added = 0;
8923
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008924 /* When coming here from Insert mode, sync undo, so that this can be
8925 * undone separately from what was previously inserted. */
8926 if (u_sync_once == 2)
8927 {
8928 u_sync_once = 1; /* notify that u_sync() was called */
8929 u_sync(TRUE);
8930 }
8931
Bram Moolenaar0d660222005-01-07 21:51:51 +00008932 lnum = get_tv_lnum(argvars);
8933 if (lnum >= 0
8934 && lnum <= curbuf->b_ml.ml_line_count
8935 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008936 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008937 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008938 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008939 l = argvars[1].vval.v_list;
8940 if (l == NULL)
8941 return;
8942 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008943 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008944 for (;;)
8945 {
8946 if (l == NULL)
8947 tv = &argvars[1]; /* append a string */
8948 else if (li == NULL)
8949 break; /* end of list */
8950 else
8951 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008952 line = get_tv_string_chk(tv);
8953 if (line == NULL) /* type error */
8954 {
8955 rettv->vval.v_number = 1; /* Failed */
8956 break;
8957 }
8958 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008959 ++added;
8960 if (l == NULL)
8961 break;
8962 li = li->li_next;
8963 }
8964
8965 appended_lines_mark(lnum, added);
8966 if (curwin->w_cursor.lnum > lnum)
8967 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008969 else
8970 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971}
8972
8973/*
8974 * "argc()" function
8975 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008977f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008979 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980}
8981
8982/*
8983 * "argidx()" function
8984 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008986f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008988 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989}
8990
8991/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008992 * "arglistid()" function
8993 */
8994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008995f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008996{
8997 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008998
8999 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009000 wp = find_tabwin(&argvars[0], &argvars[1]);
9001 if (wp != NULL)
9002 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009003}
9004
9005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 * "argv(nr)" function
9007 */
9008 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009009f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010{
9011 int idx;
9012
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009013 if (argvars[0].v_type != VAR_UNKNOWN)
9014 {
9015 idx = get_tv_number_chk(&argvars[0], NULL);
9016 if (idx >= 0 && idx < ARGCOUNT)
9017 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9018 else
9019 rettv->vval.v_string = NULL;
9020 rettv->v_type = VAR_STRING;
9021 }
9022 else if (rettv_list_alloc(rettv) == OK)
9023 for (idx = 0; idx < ARGCOUNT; ++idx)
9024 list_append_string(rettv->vval.v_list,
9025 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026}
9027
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009028static void prepare_assert_error(garray_T*gap);
9029static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9030static void assert_error(garray_T *gap);
9031static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009032
9033/*
9034 * Prepare "gap" for an assert error and add the sourcing position.
9035 */
9036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009037prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009038{
9039 char buf[NUMBUFLEN];
9040
9041 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009042 if (sourcing_name != NULL)
9043 {
9044 ga_concat(gap, sourcing_name);
9045 if (sourcing_lnum > 0)
9046 ga_concat(gap, (char_u *)" ");
9047 }
9048 if (sourcing_lnum > 0)
9049 {
9050 sprintf(buf, "line %ld", (long)sourcing_lnum);
9051 ga_concat(gap, (char_u *)buf);
9052 }
9053 if (sourcing_name != NULL || sourcing_lnum > 0)
9054 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009055}
9056
9057/*
9058 * Fill "gap" with information about an assert error.
9059 */
9060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009061fill_assert_error(
9062 garray_T *gap,
9063 typval_T *opt_msg_tv,
9064 char_u *exp_str,
9065 typval_T *exp_tv,
9066 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009067{
9068 char_u numbuf[NUMBUFLEN];
9069 char_u *tofree;
9070
9071 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9072 {
9073 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9074 vim_free(tofree);
9075 }
9076 else
9077 {
9078 ga_concat(gap, (char_u *)"Expected ");
9079 if (exp_str == NULL)
9080 {
9081 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9082 vim_free(tofree);
9083 }
9084 else
9085 ga_concat(gap, exp_str);
9086 ga_concat(gap, (char_u *)" but got ");
9087 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9088 vim_free(tofree);
9089 }
9090}
Bram Moolenaar43345542015-11-29 17:35:35 +01009091
9092/*
9093 * Add an assert error to v:errors.
9094 */
9095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009096assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009097{
9098 struct vimvar *vp = &vimvars[VV_ERRORS];
9099
9100 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9101 /* Make sure v:errors is a list. */
9102 set_vim_var_list(VV_ERRORS, list_alloc());
9103 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9104}
9105
Bram Moolenaar43345542015-11-29 17:35:35 +01009106/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009107 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009108 */
9109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009110f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009111{
9112 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009113
9114 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9115 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009116 prepare_assert_error(&ga);
9117 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9118 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009119 ga_clear(&ga);
9120 }
9121}
9122
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009123/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009124 * "assert_exception(string[, msg])" function
9125 */
9126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009127f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009128{
9129 garray_T ga;
9130 char *error;
9131
9132 error = (char *)get_tv_string_chk(&argvars[0]);
9133 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9134 {
9135 prepare_assert_error(&ga);
9136 ga_concat(&ga, (char_u *)"v:exception is not set");
9137 assert_error(&ga);
9138 ga_clear(&ga);
9139 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009140 else if (error != NULL
9141 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009142 {
9143 prepare_assert_error(&ga);
9144 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9145 &vimvars[VV_EXCEPTION].vv_tv);
9146 assert_error(&ga);
9147 ga_clear(&ga);
9148 }
9149}
9150
9151/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009152 * "assert_fails(cmd [, error])" function
9153 */
9154 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009155f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009156{
9157 char_u *cmd = get_tv_string_chk(&argvars[0]);
9158 garray_T ga;
9159
9160 called_emsg = FALSE;
9161 suppress_errthrow = TRUE;
9162 emsg_silent = TRUE;
9163 do_cmdline_cmd(cmd);
9164 if (!called_emsg)
9165 {
9166 prepare_assert_error(&ga);
9167 ga_concat(&ga, (char_u *)"command did not fail: ");
9168 ga_concat(&ga, cmd);
9169 assert_error(&ga);
9170 ga_clear(&ga);
9171 }
9172 else if (argvars[1].v_type != VAR_UNKNOWN)
9173 {
9174 char_u buf[NUMBUFLEN];
9175 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9176
9177 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9178 {
9179 prepare_assert_error(&ga);
9180 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9181 &vimvars[VV_ERRMSG].vv_tv);
9182 assert_error(&ga);
9183 ga_clear(&ga);
9184 }
9185 }
9186
9187 called_emsg = FALSE;
9188 suppress_errthrow = FALSE;
9189 emsg_silent = FALSE;
9190 emsg_on_display = FALSE;
9191 set_vim_var_string(VV_ERRMSG, NULL, 0);
9192}
9193
9194/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009195 * Common for assert_true() and assert_false().
9196 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009198assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009199{
9200 int error = FALSE;
9201 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009202
9203 if (argvars[0].v_type != VAR_NUMBER
9204 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9205 || error)
9206 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009207 prepare_assert_error(&ga);
9208 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009209 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009210 NULL, &argvars[0]);
9211 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009212 ga_clear(&ga);
9213 }
9214}
9215
9216/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009217 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009218 */
9219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009220f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009221{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009222 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009223}
9224
9225/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009226 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009227 */
9228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009229f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009230{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009231 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009232}
9233
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009234#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009235/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009236 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009237 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009239f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009240{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009241 float_T f;
9242
9243 rettv->v_type = VAR_FLOAT;
9244 if (get_float_arg(argvars, &f) == OK)
9245 rettv->vval.v_float = asin(f);
9246 else
9247 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009248}
9249
9250/*
9251 * "atan()" function
9252 */
9253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009254f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009255{
9256 float_T f;
9257
9258 rettv->v_type = VAR_FLOAT;
9259 if (get_float_arg(argvars, &f) == OK)
9260 rettv->vval.v_float = atan(f);
9261 else
9262 rettv->vval.v_float = 0.0;
9263}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009264
9265/*
9266 * "atan2()" function
9267 */
9268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009269f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009270{
9271 float_T fx, fy;
9272
9273 rettv->v_type = VAR_FLOAT;
9274 if (get_float_arg(argvars, &fx) == OK
9275 && get_float_arg(&argvars[1], &fy) == OK)
9276 rettv->vval.v_float = atan2(fx, fy);
9277 else
9278 rettv->vval.v_float = 0.0;
9279}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009280#endif
9281
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282/*
9283 * "browse(save, title, initdir, default)" function
9284 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009286f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287{
9288#ifdef FEAT_BROWSE
9289 int save;
9290 char_u *title;
9291 char_u *initdir;
9292 char_u *defname;
9293 char_u buf[NUMBUFLEN];
9294 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009295 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009297 save = get_tv_number_chk(&argvars[0], &error);
9298 title = get_tv_string_chk(&argvars[1]);
9299 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9300 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009302 if (error || title == NULL || initdir == NULL || defname == NULL)
9303 rettv->vval.v_string = NULL;
9304 else
9305 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009306 do_browse(save ? BROWSE_SAVE : 0,
9307 title, defname, NULL, initdir, NULL, curbuf);
9308#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009309 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009310#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009311 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009312}
9313
9314/*
9315 * "browsedir(title, initdir)" function
9316 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009318f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009319{
9320#ifdef FEAT_BROWSE
9321 char_u *title;
9322 char_u *initdir;
9323 char_u buf[NUMBUFLEN];
9324
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009325 title = get_tv_string_chk(&argvars[0]);
9326 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009327
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009328 if (title == NULL || initdir == NULL)
9329 rettv->vval.v_string = NULL;
9330 else
9331 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009332 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009334 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009336 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337}
9338
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009339static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009340
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341/*
9342 * Find a buffer by number or exact name.
9343 */
9344 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009345find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346{
9347 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009349 if (avar->v_type == VAR_NUMBER)
9350 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009351 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009353 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009354 if (buf == NULL)
9355 {
9356 /* No full path name match, try a match with a URL or a "nofile"
9357 * buffer, these don't use the full path. */
9358 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9359 if (buf->b_fname != NULL
9360 && (path_with_url(buf->b_fname)
9361#ifdef FEAT_QUICKFIX
9362 || bt_nofile(buf)
9363#endif
9364 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009365 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009366 break;
9367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 }
9369 return buf;
9370}
9371
9372/*
9373 * "bufexists(expr)" function
9374 */
9375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009376f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009378 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379}
9380
9381/*
9382 * "buflisted(expr)" function
9383 */
9384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009385f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386{
9387 buf_T *buf;
9388
9389 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009390 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391}
9392
9393/*
9394 * "bufloaded(expr)" function
9395 */
9396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009397f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398{
9399 buf_T *buf;
9400
9401 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009402 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403}
9404
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009405static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009406
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407/*
9408 * Get buffer by number or pattern.
9409 */
9410 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009411get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009413 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 int save_magic;
9415 char_u *save_cpo;
9416 buf_T *buf;
9417
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009418 if (tv->v_type == VAR_NUMBER)
9419 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009420 if (tv->v_type != VAR_STRING)
9421 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 if (name == NULL || *name == NUL)
9423 return curbuf;
9424 if (name[0] == '$' && name[1] == NUL)
9425 return lastbuf;
9426
9427 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9428 save_magic = p_magic;
9429 p_magic = TRUE;
9430 save_cpo = p_cpo;
9431 p_cpo = (char_u *)"";
9432
9433 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009434 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435
9436 p_magic = save_magic;
9437 p_cpo = save_cpo;
9438
9439 /* If not found, try expanding the name, like done for bufexists(). */
9440 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009441 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442
9443 return buf;
9444}
9445
9446/*
9447 * "bufname(expr)" function
9448 */
9449 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009450f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451{
9452 buf_T *buf;
9453
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009454 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009456 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009457 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009459 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009461 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 --emsg_off;
9463}
9464
9465/*
9466 * "bufnr(expr)" function
9467 */
9468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009469f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470{
9471 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009472 int error = FALSE;
9473 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009475 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009477 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009478 --emsg_off;
9479
9480 /* If the buffer isn't found and the second argument is not zero create a
9481 * new buffer. */
9482 if (buf == NULL
9483 && argvars[1].v_type != VAR_UNKNOWN
9484 && get_tv_number_chk(&argvars[1], &error) != 0
9485 && !error
9486 && (name = get_tv_string_chk(&argvars[0])) != NULL
9487 && !error)
9488 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9489
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009491 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009493 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494}
9495
9496/*
9497 * "bufwinnr(nr)" function
9498 */
9499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009500f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501{
9502#ifdef FEAT_WINDOWS
9503 win_T *wp;
9504 int winnr = 0;
9505#endif
9506 buf_T *buf;
9507
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009508 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009510 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511#ifdef FEAT_WINDOWS
9512 for (wp = firstwin; wp; wp = wp->w_next)
9513 {
9514 ++winnr;
9515 if (wp->w_buffer == buf)
9516 break;
9517 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009518 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009520 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521#endif
9522 --emsg_off;
9523}
9524
9525/*
9526 * "byte2line(byte)" function
9527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009529f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530{
9531#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009532 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533#else
9534 long boff = 0;
9535
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009536 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009538 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009540 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 (linenr_T)0, &boff);
9542#endif
9543}
9544
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009546byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009547{
9548#ifdef FEAT_MBYTE
9549 char_u *t;
9550#endif
9551 char_u *str;
9552 long idx;
9553
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009554 str = get_tv_string_chk(&argvars[0]);
9555 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009556 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009557 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009558 return;
9559
9560#ifdef FEAT_MBYTE
9561 t = str;
9562 for ( ; idx > 0; idx--)
9563 {
9564 if (*t == NUL) /* EOL reached */
9565 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009566 if (enc_utf8 && comp)
9567 t += utf_ptr2len(t);
9568 else
9569 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009570 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009571 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009572#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009573 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009574 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009575#endif
9576}
9577
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009578/*
9579 * "byteidx()" function
9580 */
9581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009582f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009583{
9584 byteidx(argvars, rettv, FALSE);
9585}
9586
9587/*
9588 * "byteidxcomp()" function
9589 */
9590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009591f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009592{
9593 byteidx(argvars, rettv, TRUE);
9594}
9595
Bram Moolenaardb913952012-06-29 12:54:53 +02009596 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009597func_call(
9598 char_u *name,
9599 typval_T *args,
9600 dict_T *selfdict,
9601 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009602{
9603 listitem_T *item;
9604 typval_T argv[MAX_FUNC_ARGS + 1];
9605 int argc = 0;
9606 int dummy;
9607 int r = 0;
9608
9609 for (item = args->vval.v_list->lv_first; item != NULL;
9610 item = item->li_next)
9611 {
9612 if (argc == MAX_FUNC_ARGS)
9613 {
9614 EMSG(_("E699: Too many arguments"));
9615 break;
9616 }
9617 /* Make a copy of each argument. This is needed to be able to set
9618 * v_lock to VAR_FIXED in the copy without changing the original list.
9619 */
9620 copy_tv(&item->li_tv, &argv[argc++]);
9621 }
9622
9623 if (item == NULL)
9624 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9625 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9626 &dummy, TRUE, selfdict);
9627
9628 /* Free the arguments. */
9629 while (argc > 0)
9630 clear_tv(&argv[--argc]);
9631
9632 return r;
9633}
9634
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009635/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009636 * "call(func, arglist)" function
9637 */
9638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009639f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009640{
9641 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009642 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009643
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009644 if (argvars[1].v_type != VAR_LIST)
9645 {
9646 EMSG(_(e_listreq));
9647 return;
9648 }
9649 if (argvars[1].vval.v_list == NULL)
9650 return;
9651
9652 if (argvars[0].v_type == VAR_FUNC)
9653 func = argvars[0].vval.v_string;
9654 else
9655 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009656 if (*func == NUL)
9657 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009658
Bram Moolenaare9a41262005-01-15 22:18:47 +00009659 if (argvars[2].v_type != VAR_UNKNOWN)
9660 {
9661 if (argvars[2].v_type != VAR_DICT)
9662 {
9663 EMSG(_(e_dictreq));
9664 return;
9665 }
9666 selfdict = argvars[2].vval.v_dict;
9667 }
9668
Bram Moolenaardb913952012-06-29 12:54:53 +02009669 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009670}
9671
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009672#ifdef FEAT_FLOAT
9673/*
9674 * "ceil({float})" function
9675 */
9676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009677f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009678{
9679 float_T f;
9680
9681 rettv->v_type = VAR_FLOAT;
9682 if (get_float_arg(argvars, &f) == OK)
9683 rettv->vval.v_float = ceil(f);
9684 else
9685 rettv->vval.v_float = 0.0;
9686}
9687#endif
9688
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009689/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009690 * "changenr()" function
9691 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009693f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009694{
9695 rettv->vval.v_number = curbuf->b_u_seq_cur;
9696}
9697
9698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699 * "char2nr(string)" function
9700 */
9701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009702f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703{
9704#ifdef FEAT_MBYTE
9705 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009706 {
9707 int utf8 = 0;
9708
9709 if (argvars[1].v_type != VAR_UNKNOWN)
9710 utf8 = get_tv_number_chk(&argvars[1], NULL);
9711
9712 if (utf8)
9713 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9714 else
9715 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 else
9718#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009719 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720}
9721
9722/*
9723 * "cindent(lnum)" function
9724 */
9725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009726f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727{
9728#ifdef FEAT_CINDENT
9729 pos_T pos;
9730 linenr_T lnum;
9731
9732 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009733 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9735 {
9736 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009737 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 curwin->w_cursor = pos;
9739 }
9740 else
9741#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009742 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743}
9744
9745/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009746 * "clearmatches()" function
9747 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009749f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009750{
9751#ifdef FEAT_SEARCH_EXTRA
9752 clear_matches(curwin);
9753#endif
9754}
9755
9756/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 * "col(string)" function
9758 */
9759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009760f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761{
9762 colnr_T col = 0;
9763 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009764 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009766 fp = var2fpos(&argvars[0], FALSE, &fnum);
9767 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 {
9769 if (fp->col == MAXCOL)
9770 {
9771 /* '> can be MAXCOL, get the length of the line then */
9772 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009773 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 else
9775 col = MAXCOL;
9776 }
9777 else
9778 {
9779 col = fp->col + 1;
9780#ifdef FEAT_VIRTUALEDIT
9781 /* col(".") when the cursor is on the NUL at the end of the line
9782 * because of "coladd" can be seen as an extra column. */
9783 if (virtual_active() && fp == &curwin->w_cursor)
9784 {
9785 char_u *p = ml_get_cursor();
9786
9787 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9788 curwin->w_virtcol - curwin->w_cursor.coladd))
9789 {
9790# ifdef FEAT_MBYTE
9791 int l;
9792
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009793 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 col += l;
9795# else
9796 if (*p != NUL && p[1] == NUL)
9797 ++col;
9798# endif
9799 }
9800 }
9801#endif
9802 }
9803 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009804 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805}
9806
Bram Moolenaar572cb562005-08-05 21:35:02 +00009807#if defined(FEAT_INS_EXPAND)
9808/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009809 * "complete()" function
9810 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009812f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +00009813{
9814 int startcol;
9815
9816 if ((State & INSERT) == 0)
9817 {
9818 EMSG(_("E785: complete() can only be used in Insert mode"));
9819 return;
9820 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009821
9822 /* Check for undo allowed here, because if something was already inserted
9823 * the line was already saved for undo and this check isn't done. */
9824 if (!undo_allowed())
9825 return;
9826
Bram Moolenaarade00832006-03-10 21:46:58 +00009827 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9828 {
9829 EMSG(_(e_invarg));
9830 return;
9831 }
9832
9833 startcol = get_tv_number_chk(&argvars[0], NULL);
9834 if (startcol <= 0)
9835 return;
9836
9837 set_completion(startcol - 1, argvars[1].vval.v_list);
9838}
9839
9840/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009841 * "complete_add()" function
9842 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009844f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +00009845{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009846 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009847}
9848
9849/*
9850 * "complete_check()" function
9851 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009853f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +00009854{
9855 int saved = RedrawingDisabled;
9856
9857 RedrawingDisabled = 0;
9858 ins_compl_check_keys(0);
9859 rettv->vval.v_number = compl_interrupted;
9860 RedrawingDisabled = saved;
9861}
9862#endif
9863
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864/*
9865 * "confirm(message, buttons[, default [, type]])" function
9866 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009868f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869{
9870#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9871 char_u *message;
9872 char_u *buttons = NULL;
9873 char_u buf[NUMBUFLEN];
9874 char_u buf2[NUMBUFLEN];
9875 int def = 1;
9876 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009877 char_u *typestr;
9878 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009880 message = get_tv_string_chk(&argvars[0]);
9881 if (message == NULL)
9882 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009883 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009885 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9886 if (buttons == NULL)
9887 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009888 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009890 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009891 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009893 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9894 if (typestr == NULL)
9895 error = TRUE;
9896 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009898 switch (TOUPPER_ASC(*typestr))
9899 {
9900 case 'E': type = VIM_ERROR; break;
9901 case 'Q': type = VIM_QUESTION; break;
9902 case 'I': type = VIM_INFO; break;
9903 case 'W': type = VIM_WARNING; break;
9904 case 'G': type = VIM_GENERIC; break;
9905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 }
9907 }
9908 }
9909 }
9910
9911 if (buttons == NULL || *buttons == NUL)
9912 buttons = (char_u *)_("&Ok");
9913
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009914 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009915 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009916 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917#endif
9918}
9919
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009920/*
9921 * "copy()" function
9922 */
9923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009924f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009925{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009926 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009927}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009929#ifdef FEAT_FLOAT
9930/*
9931 * "cos()" function
9932 */
9933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009934f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009935{
9936 float_T f;
9937
9938 rettv->v_type = VAR_FLOAT;
9939 if (get_float_arg(argvars, &f) == OK)
9940 rettv->vval.v_float = cos(f);
9941 else
9942 rettv->vval.v_float = 0.0;
9943}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009944
9945/*
9946 * "cosh()" function
9947 */
9948 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009949f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009950{
9951 float_T f;
9952
9953 rettv->v_type = VAR_FLOAT;
9954 if (get_float_arg(argvars, &f) == OK)
9955 rettv->vval.v_float = cosh(f);
9956 else
9957 rettv->vval.v_float = 0.0;
9958}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009959#endif
9960
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009962 * "count()" function
9963 */
9964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009965f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009966{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009967 long n = 0;
9968 int ic = FALSE;
9969
Bram Moolenaare9a41262005-01-15 22:18:47 +00009970 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009971 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009972 listitem_T *li;
9973 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009974 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009975
Bram Moolenaare9a41262005-01-15 22:18:47 +00009976 if ((l = argvars[0].vval.v_list) != NULL)
9977 {
9978 li = l->lv_first;
9979 if (argvars[2].v_type != VAR_UNKNOWN)
9980 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009981 int error = FALSE;
9982
9983 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009984 if (argvars[3].v_type != VAR_UNKNOWN)
9985 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009986 idx = get_tv_number_chk(&argvars[3], &error);
9987 if (!error)
9988 {
9989 li = list_find(l, idx);
9990 if (li == NULL)
9991 EMSGN(_(e_listidx), idx);
9992 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009993 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009994 if (error)
9995 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009996 }
9997
9998 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009999 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010000 ++n;
10001 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010002 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010003 else if (argvars[0].v_type == VAR_DICT)
10004 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010005 int todo;
10006 dict_T *d;
10007 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010008
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010009 if ((d = argvars[0].vval.v_dict) != NULL)
10010 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010011 int error = FALSE;
10012
Bram Moolenaare9a41262005-01-15 22:18:47 +000010013 if (argvars[2].v_type != VAR_UNKNOWN)
10014 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010015 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010016 if (argvars[3].v_type != VAR_UNKNOWN)
10017 EMSG(_(e_invarg));
10018 }
10019
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010020 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010021 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010022 {
10023 if (!HASHITEM_EMPTY(hi))
10024 {
10025 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010026 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010027 ++n;
10028 }
10029 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010030 }
10031 }
10032 else
10033 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010034 rettv->vval.v_number = n;
10035}
10036
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010010037#ifdef FEAT_CHANNEL
10038/*
10039 * Get a callback from "arg". It can be a Funcref or a function name.
10040 * When "arg" is zero return an empty string.
10041 * Return NULL for an invalid argument.
10042 */
10043 static char_u *
10044get_callback(typval_T *arg)
10045{
10046 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
10047 return arg->vval.v_string;
10048 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
10049 return (char_u *)"";
10050 EMSG(_("E999: Invalid callback argument"));
10051 return NULL;
10052}
10053
10054/*
10055 * "connect()" function
10056 */
10057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010058f_connect(typval_T *argvars, typval_T *rettv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010010059{
10060 char_u *address;
10061 char_u *mode;
10062 char_u *callback = NULL;
10063 char_u buf1[NUMBUFLEN];
10064 char_u *p;
10065 int port;
10066 int json_mode = FALSE;
10067
10068 address = get_tv_string(&argvars[0]);
10069 mode = get_tv_string_buf(&argvars[1], buf1);
10070 if (argvars[2].v_type != VAR_UNKNOWN)
10071 {
10072 callback = get_callback(&argvars[2]);
10073 if (callback == NULL)
10074 return;
10075 }
10076
10077 /* parse address */
10078 p = vim_strchr(address, ':');
10079 if (p == NULL)
10080 {
10081 EMSG2(_(e_invarg2), address);
10082 return;
10083 }
10084 *p++ = NUL;
10085 port = atoi((char *)p);
10086 if (*address == NUL || port <= 0)
10087 {
10088 p[-1] = ':';
10089 EMSG2(_(e_invarg2), address);
10090 return;
10091 }
10092
10093 /* parse mode */
10094 if (STRCMP(mode, "json") == 0)
10095 json_mode = TRUE;
10096 else if (STRCMP(mode, "raw") != 0)
10097 {
10098 EMSG2(_(e_invarg2), mode);
10099 return;
10100 }
10101
10102 rettv->vval.v_number = channel_open((char *)address, port, NULL);
10103 if (rettv->vval.v_number >= 0)
10104 {
10105 channel_set_json_mode(rettv->vval.v_number, json_mode);
10106 if (callback != NULL && *callback != NUL)
10107 channel_set_callback(rettv->vval.v_number, callback);
10108 }
10109}
10110#endif
10111
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10114 *
10115 * Checks the existence of a cscope connection.
10116 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010118f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119{
10120#ifdef FEAT_CSCOPE
10121 int num = 0;
10122 char_u *dbpath = NULL;
10123 char_u *prepend = NULL;
10124 char_u buf[NUMBUFLEN];
10125
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010126 if (argvars[0].v_type != VAR_UNKNOWN
10127 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010129 num = (int)get_tv_number(&argvars[0]);
10130 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010131 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010132 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133 }
10134
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010135 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136#endif
10137}
10138
10139/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010140 * "cursor(lnum, col)" function, or
10141 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010143 * Moves the cursor to the specified line and column.
10144 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010147f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148{
10149 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010150#ifdef FEAT_VIRTUALEDIT
10151 long coladd = 0;
10152#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010153 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010155 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010156 if (argvars[1].v_type == VAR_UNKNOWN)
10157 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010158 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010159 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010160
Bram Moolenaar493c1782014-05-28 14:34:46 +020010161 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010162 {
10163 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010164 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010165 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010166 line = pos.lnum;
10167 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010168#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010169 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010170#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010171 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010172 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010173 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010174 set_curswant = FALSE;
10175 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010176 }
10177 else
10178 {
10179 line = get_tv_lnum(argvars);
10180 col = get_tv_number_chk(&argvars[1], NULL);
10181#ifdef FEAT_VIRTUALEDIT
10182 if (argvars[2].v_type != VAR_UNKNOWN)
10183 coladd = get_tv_number_chk(&argvars[2], NULL);
10184#endif
10185 }
10186 if (line < 0 || col < 0
10187#ifdef FEAT_VIRTUALEDIT
10188 || coladd < 0
10189#endif
10190 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010191 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192 if (line > 0)
10193 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194 if (col > 0)
10195 curwin->w_cursor.col = col - 1;
10196#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010197 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198#endif
10199
10200 /* Make sure the cursor is in a valid position. */
10201 check_cursor();
10202#ifdef FEAT_MBYTE
10203 /* Correct cursor for multi-byte character. */
10204 if (has_mbyte)
10205 mb_adjust_cursor();
10206#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010207
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010208 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010209 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210}
10211
10212/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010213 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 */
10215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010216f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010218 int noref = 0;
10219
10220 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010221 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010222 if (noref < 0 || noref > 1)
10223 EMSG(_(e_invarg));
10224 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010225 {
10226 current_copyID += COPYID_INC;
10227 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229}
10230
10231/*
10232 * "delete()" function
10233 */
10234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010235f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010237 char_u nbuf[NUMBUFLEN];
10238 char_u *name;
10239 char_u *flags;
10240
10241 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010243 return;
10244
10245 name = get_tv_string(&argvars[0]);
10246 if (name == NULL || *name == NUL)
10247 {
10248 EMSG(_(e_invarg));
10249 return;
10250 }
10251
10252 if (argvars[1].v_type != VAR_UNKNOWN)
10253 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010255 flags = (char_u *)"";
10256
10257 if (*flags == NUL)
10258 /* delete a file */
10259 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10260 else if (STRCMP(flags, "d") == 0)
10261 /* delete an empty directory */
10262 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10263 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010264 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010265 rettv->vval.v_number = delete_recursive(name);
10266 else
10267 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268}
10269
10270/*
10271 * "did_filetype()" function
10272 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010274f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275{
10276#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010277 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278#endif
10279}
10280
10281/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010282 * "diff_filler()" function
10283 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010285f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010286{
10287#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010288 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010289#endif
10290}
10291
10292/*
10293 * "diff_hlID()" function
10294 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010296f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010297{
10298#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010299 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010300 static linenr_T prev_lnum = 0;
10301 static int changedtick = 0;
10302 static int fnum = 0;
10303 static int change_start = 0;
10304 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010305 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010306 int filler_lines;
10307 int col;
10308
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010309 if (lnum < 0) /* ignore type error in {lnum} arg */
10310 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010311 if (lnum != prev_lnum
10312 || changedtick != curbuf->b_changedtick
10313 || fnum != curbuf->b_fnum)
10314 {
10315 /* New line, buffer, change: need to get the values. */
10316 filler_lines = diff_check(curwin, lnum);
10317 if (filler_lines < 0)
10318 {
10319 if (filler_lines == -1)
10320 {
10321 change_start = MAXCOL;
10322 change_end = -1;
10323 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10324 hlID = HLF_ADD; /* added line */
10325 else
10326 hlID = HLF_CHD; /* changed line */
10327 }
10328 else
10329 hlID = HLF_ADD; /* added line */
10330 }
10331 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010332 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010333 prev_lnum = lnum;
10334 changedtick = curbuf->b_changedtick;
10335 fnum = curbuf->b_fnum;
10336 }
10337
10338 if (hlID == HLF_CHD || hlID == HLF_TXD)
10339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010340 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010341 if (col >= change_start && col <= change_end)
10342 hlID = HLF_TXD; /* changed text */
10343 else
10344 hlID = HLF_CHD; /* changed line */
10345 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010346 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010347#endif
10348}
10349
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010010350#ifdef FEAT_CHANNEL
10351/*
10352 * Get the channel index from the handle argument.
10353 * Returns -1 if the handle is invalid or the channel is closed.
10354 */
10355 static int
10356get_channel_arg(typval_T *tv)
10357{
10358 int ch_idx;
10359
10360 if (tv->v_type != VAR_NUMBER)
10361 {
10362 EMSG2(_(e_invarg2), get_tv_string(tv));
10363 return -1;
10364 }
10365 ch_idx = tv->vval.v_number;
10366
10367 if (!channel_is_open(ch_idx))
10368 {
10369 EMSGN(_("E999: not an open channel"), ch_idx);
10370 return -1;
10371 }
10372 return ch_idx;
10373}
10374
10375/*
10376 * "disconnect()" function
10377 */
10378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010379f_disconnect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010010380{
10381 int ch_idx = get_channel_arg(&argvars[0]);
10382
10383 if (ch_idx >= 0)
10384 channel_close(ch_idx);
10385}
10386#endif
10387
Bram Moolenaar47136d72004-10-12 20:02:24 +000010388/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010389 * "empty({expr})" function
10390 */
10391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010392f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010393{
10394 int n;
10395
10396 switch (argvars[0].v_type)
10397 {
10398 case VAR_STRING:
10399 case VAR_FUNC:
10400 n = argvars[0].vval.v_string == NULL
10401 || *argvars[0].vval.v_string == NUL;
10402 break;
10403 case VAR_NUMBER:
10404 n = argvars[0].vval.v_number == 0;
10405 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010406#ifdef FEAT_FLOAT
10407 case VAR_FLOAT:
10408 n = argvars[0].vval.v_float == 0.0;
10409 break;
10410#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010411 case VAR_LIST:
10412 n = argvars[0].vval.v_list == NULL
10413 || argvars[0].vval.v_list->lv_first == NULL;
10414 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010415 case VAR_DICT:
10416 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010417 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010418 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010419 case VAR_SPECIAL:
10420 n = argvars[0].vval.v_number != VVAL_TRUE;
10421 break;
10422
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010423 default:
10424 EMSG2(_(e_intern2), "f_empty()");
10425 n = 0;
10426 }
10427
10428 rettv->vval.v_number = n;
10429}
10430
10431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432 * "escape({string}, {chars})" function
10433 */
10434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010435f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436{
10437 char_u buf[NUMBUFLEN];
10438
Bram Moolenaar758711c2005-02-02 23:11:38 +000010439 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10440 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010441 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442}
10443
10444/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010445 * "eval()" function
10446 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010448f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010449{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010450 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010451
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010452 s = get_tv_string_chk(&argvars[0]);
10453 if (s != NULL)
10454 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010455
Bram Moolenaar615b9972015-01-14 17:15:05 +010010456 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010457 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10458 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010459 if (p != NULL && !aborting())
10460 EMSG2(_(e_invexpr2), p);
10461 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010462 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010463 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010464 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010465 else if (*s != NUL)
10466 EMSG(_(e_trailing));
10467}
10468
10469/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470 * "eventhandler()" function
10471 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010473f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010475 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476}
10477
10478/*
10479 * "executable()" function
10480 */
10481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010482f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010484 char_u *name = get_tv_string(&argvars[0]);
10485
10486 /* Check in $PATH and also check directly if there is a directory name. */
10487 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10488 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010489}
10490
10491/*
10492 * "exepath()" function
10493 */
10494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010495f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010496{
10497 char_u *p = NULL;
10498
Bram Moolenaarb5971142015-03-21 17:32:19 +010010499 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010500 rettv->v_type = VAR_STRING;
10501 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502}
10503
10504/*
10505 * "exists()" function
10506 */
10507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010508f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509{
10510 char_u *p;
10511 char_u *name;
10512 int n = FALSE;
10513 int len = 0;
10514
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010515 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516 if (*p == '$') /* environment variable */
10517 {
10518 /* first try "normal" environment variables (fast) */
10519 if (mch_getenv(p + 1) != NULL)
10520 n = TRUE;
10521 else
10522 {
10523 /* try expanding things like $VIM and ${HOME} */
10524 p = expand_env_save(p);
10525 if (p != NULL && *p != '$')
10526 n = TRUE;
10527 vim_free(p);
10528 }
10529 }
10530 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010531 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010532 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010533 if (*skipwhite(p) != NUL)
10534 n = FALSE; /* trailing garbage */
10535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 else if (*p == '*') /* internal or user defined function */
10537 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010538 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 }
10540 else if (*p == ':')
10541 {
10542 n = cmd_exists(p + 1);
10543 }
10544 else if (*p == '#')
10545 {
10546#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010547 if (p[1] == '#')
10548 n = autocmd_supported(p + 2);
10549 else
10550 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551#endif
10552 }
10553 else /* internal variable */
10554 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010555 char_u *tofree;
10556 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010558 /* get_name_len() takes care of expanding curly braces */
10559 name = p;
10560 len = get_name_len(&p, &tofree, TRUE, FALSE);
10561 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010563 if (tofree != NULL)
10564 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010565 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010566 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010568 /* handle d.key, l[idx], f(expr) */
10569 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10570 if (n)
10571 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572 }
10573 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010574 if (*p != NUL)
10575 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010577 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578 }
10579
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010580 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581}
10582
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010583#ifdef FEAT_FLOAT
10584/*
10585 * "exp()" function
10586 */
10587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010588f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010589{
10590 float_T f;
10591
10592 rettv->v_type = VAR_FLOAT;
10593 if (get_float_arg(argvars, &f) == OK)
10594 rettv->vval.v_float = exp(f);
10595 else
10596 rettv->vval.v_float = 0.0;
10597}
10598#endif
10599
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600/*
10601 * "expand()" function
10602 */
10603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010604f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605{
10606 char_u *s;
10607 int len;
10608 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010609 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010611 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010612 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010614 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010615 if (argvars[1].v_type != VAR_UNKNOWN
10616 && argvars[2].v_type != VAR_UNKNOWN
10617 && get_tv_number_chk(&argvars[2], &error)
10618 && !error)
10619 {
10620 rettv->v_type = VAR_LIST;
10621 rettv->vval.v_list = NULL;
10622 }
10623
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010624 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625 if (*s == '%' || *s == '#' || *s == '<')
10626 {
10627 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010628 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010630 if (rettv->v_type == VAR_LIST)
10631 {
10632 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10633 list_append_string(rettv->vval.v_list, result, -1);
10634 else
10635 vim_free(result);
10636 }
10637 else
10638 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639 }
10640 else
10641 {
10642 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010643 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010644 if (argvars[1].v_type != VAR_UNKNOWN
10645 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010646 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010647 if (!error)
10648 {
10649 ExpandInit(&xpc);
10650 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010651 if (p_wic)
10652 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010653 if (rettv->v_type == VAR_STRING)
10654 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10655 options, WILD_ALL);
10656 else if (rettv_list_alloc(rettv) != FAIL)
10657 {
10658 int i;
10659
10660 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10661 for (i = 0; i < xpc.xp_numfiles; i++)
10662 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10663 ExpandCleanup(&xpc);
10664 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010665 }
10666 else
10667 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 }
10669}
10670
10671/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010672 * Go over all entries in "d2" and add them to "d1".
10673 * When "action" is "error" then a duplicate key is an error.
10674 * When "action" is "force" then a duplicate key is overwritten.
10675 * Otherwise duplicate keys are ignored ("action" is "keep").
10676 */
10677 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010678dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020010679{
10680 dictitem_T *di1;
10681 hashitem_T *hi2;
10682 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010683 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010684
10685 todo = (int)d2->dv_hashtab.ht_used;
10686 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10687 {
10688 if (!HASHITEM_EMPTY(hi2))
10689 {
10690 --todo;
10691 di1 = dict_find(d1, hi2->hi_key, -1);
10692 if (d1->dv_scope != 0)
10693 {
10694 /* Disallow replacing a builtin function in l: and g:.
10695 * Check the key to be valid when adding to any
10696 * scope. */
10697 if (d1->dv_scope == VAR_DEF_SCOPE
10698 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10699 && var_check_func_name(hi2->hi_key,
10700 di1 == NULL))
10701 break;
10702 if (!valid_varname(hi2->hi_key))
10703 break;
10704 }
10705 if (di1 == NULL)
10706 {
10707 di1 = dictitem_copy(HI2DI(hi2));
10708 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10709 dictitem_free(di1);
10710 }
10711 else if (*action == 'e')
10712 {
10713 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10714 break;
10715 }
10716 else if (*action == 'f' && HI2DI(hi2) != di1)
10717 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010718 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10719 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010720 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010721 clear_tv(&di1->di_tv);
10722 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10723 }
10724 }
10725 }
10726}
10727
10728/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010729 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010730 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010731 */
10732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010733f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010734{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010735 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010736
Bram Moolenaare9a41262005-01-15 22:18:47 +000010737 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010738 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010739 list_T *l1, *l2;
10740 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010741 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010742 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010743
Bram Moolenaare9a41262005-01-15 22:18:47 +000010744 l1 = argvars[0].vval.v_list;
10745 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010746 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010747 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010748 {
10749 if (argvars[2].v_type != VAR_UNKNOWN)
10750 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010751 before = get_tv_number_chk(&argvars[2], &error);
10752 if (error)
10753 return; /* type error; errmsg already given */
10754
Bram Moolenaar758711c2005-02-02 23:11:38 +000010755 if (before == l1->lv_len)
10756 item = NULL;
10757 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010758 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010759 item = list_find(l1, before);
10760 if (item == NULL)
10761 {
10762 EMSGN(_(e_listidx), before);
10763 return;
10764 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010765 }
10766 }
10767 else
10768 item = NULL;
10769 list_extend(l1, l2, item);
10770
Bram Moolenaare9a41262005-01-15 22:18:47 +000010771 copy_tv(&argvars[0], rettv);
10772 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010773 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010774 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10775 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010776 dict_T *d1, *d2;
10777 char_u *action;
10778 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010779
10780 d1 = argvars[0].vval.v_dict;
10781 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010782 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010783 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010784 {
10785 /* Check the third argument. */
10786 if (argvars[2].v_type != VAR_UNKNOWN)
10787 {
10788 static char *(av[]) = {"keep", "force", "error"};
10789
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010790 action = get_tv_string_chk(&argvars[2]);
10791 if (action == NULL)
10792 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010793 for (i = 0; i < 3; ++i)
10794 if (STRCMP(action, av[i]) == 0)
10795 break;
10796 if (i == 3)
10797 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010798 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010799 return;
10800 }
10801 }
10802 else
10803 action = (char_u *)"force";
10804
Bram Moolenaara9922d62013-05-30 13:01:18 +020010805 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010806
Bram Moolenaare9a41262005-01-15 22:18:47 +000010807 copy_tv(&argvars[0], rettv);
10808 }
10809 }
10810 else
10811 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010812}
10813
10814/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010815 * "feedkeys()" function
10816 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010818f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010819{
10820 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010821 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010822 char_u *keys, *flags;
10823 char_u nbuf[NUMBUFLEN];
10824 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010825 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010826 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010827
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010828 /* This is not allowed in the sandbox. If the commands would still be
10829 * executed in the sandbox it would be OK, but it probably happens later,
10830 * when "sandbox" is no longer set. */
10831 if (check_secure())
10832 return;
10833
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010834 keys = get_tv_string(&argvars[0]);
10835 if (*keys != NUL)
10836 {
10837 if (argvars[1].v_type != VAR_UNKNOWN)
10838 {
10839 flags = get_tv_string_buf(&argvars[1], nbuf);
10840 for ( ; *flags != NUL; ++flags)
10841 {
10842 switch (*flags)
10843 {
10844 case 'n': remap = FALSE; break;
10845 case 'm': remap = TRUE; break;
10846 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010847 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010848 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010849 }
10850 }
10851 }
10852
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010853 /* Need to escape K_SPECIAL and CSI before putting the string in the
10854 * typeahead buffer. */
10855 keys_esc = vim_strsave_escape_csi(keys);
10856 if (keys_esc != NULL)
10857 {
10858 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010859 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010860 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010861 if (vgetc_busy)
10862 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010863 if (execute)
10864 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010865 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010866 }
10867}
10868
10869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 * "filereadable()" function
10871 */
10872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010873f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010875 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 char_u *p;
10877 int n;
10878
Bram Moolenaarc236c162008-07-13 17:41:49 +000010879#ifndef O_NONBLOCK
10880# define O_NONBLOCK 0
10881#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010882 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010883 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10884 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 {
10886 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010887 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 }
10889 else
10890 n = FALSE;
10891
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010892 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893}
10894
10895/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010896 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010897 * rights to write into.
10898 */
10899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010900f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010902 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903}
10904
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010905 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010010906findfilendir(
10907 typval_T *argvars UNUSED,
10908 typval_T *rettv,
10909 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010910{
10911#ifdef FEAT_SEARCHPATH
10912 char_u *fname;
10913 char_u *fresult = NULL;
10914 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10915 char_u *p;
10916 char_u pathbuf[NUMBUFLEN];
10917 int count = 1;
10918 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010919 int error = FALSE;
10920#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010921
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010922 rettv->vval.v_string = NULL;
10923 rettv->v_type = VAR_STRING;
10924
10925#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010926 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010927
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010928 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010929 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010930 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10931 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010932 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010933 else
10934 {
10935 if (*p != NUL)
10936 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010937
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010938 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010939 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010940 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010941 }
10942
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010943 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10944 error = TRUE;
10945
10946 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010947 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010948 do
10949 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010950 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010951 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010952 fresult = find_file_in_path_option(first ? fname : NULL,
10953 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010954 0, first, path,
10955 find_what,
10956 curbuf->b_ffname,
10957 find_what == FINDFILE_DIR
10958 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010959 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010960
10961 if (fresult != NULL && rettv->v_type == VAR_LIST)
10962 list_append_string(rettv->vval.v_list, fresult, -1);
10963
10964 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010965 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010966
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010967 if (rettv->v_type == VAR_STRING)
10968 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010969#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010970}
10971
Bram Moolenaar48e697e2016-01-23 22:17:30 +010010972static void filter_map(typval_T *argvars, typval_T *rettv, int map);
10973static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010974
10975/*
10976 * Implementation of map() and filter().
10977 */
10978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010979filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010980{
10981 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010982 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010983 listitem_T *li, *nli;
10984 list_T *l = NULL;
10985 dictitem_T *di;
10986 hashtab_T *ht;
10987 hashitem_T *hi;
10988 dict_T *d = NULL;
10989 typval_T save_val;
10990 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010991 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010992 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010993 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020010994 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010995 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010996 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010997 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010998
Bram Moolenaare9a41262005-01-15 22:18:47 +000010999 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011000 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011001 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011002 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011003 return;
11004 }
11005 else if (argvars[0].v_type == VAR_DICT)
11006 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011007 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011008 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011009 return;
11010 }
11011 else
11012 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011013 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011014 return;
11015 }
11016
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011017 expr = get_tv_string_buf_chk(&argvars[1], buf);
11018 /* On type errors, the preceding call has already displayed an error
11019 * message. Avoid a misleading error message for an empty string that
11020 * was not passed as argument. */
11021 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011022 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011023 prepare_vimvar(VV_VAL, &save_val);
11024 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011025
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011026 /* We reset "did_emsg" to be able to detect whether an error
11027 * occurred during evaluation of the expression. */
11028 save_did_emsg = did_emsg;
11029 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011030
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011031 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011032 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011033 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011034 vimvars[VV_KEY].vv_type = VAR_STRING;
11035
11036 ht = &d->dv_hashtab;
11037 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011038 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011039 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011040 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011041 if (!HASHITEM_EMPTY(hi))
11042 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011043 int r;
11044
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011045 --todo;
11046 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011047 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011048 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11049 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011050 break;
11051 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011052 r = filter_map_one(&di->di_tv, expr, map, &rem);
11053 clear_tv(&vimvars[VV_KEY].vv_tv);
11054 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011055 break;
11056 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011057 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011058 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11059 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011060 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011061 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011062 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011063 }
11064 }
11065 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011066 }
11067 else
11068 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011069 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11070
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011071 for (li = l->lv_first; li != NULL; li = nli)
11072 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011073 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011074 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011075 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011076 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011077 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011078 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011079 break;
11080 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011081 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011082 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011083 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011084 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011085
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011086 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011087 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011088
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011089 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011090 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011091
11092 copy_tv(&argvars[0], rettv);
11093}
11094
11095 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011096filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011097{
Bram Moolenaar33570922005-01-25 22:26:29 +000011098 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011099 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011100 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011101
Bram Moolenaar33570922005-01-25 22:26:29 +000011102 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011103 s = expr;
11104 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011105 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011106 if (*s != NUL) /* check for trailing chars after expr */
11107 {
11108 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011109 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011110 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011111 }
11112 if (map)
11113 {
11114 /* map(): replace the list item value */
11115 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011116 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011117 *tv = rettv;
11118 }
11119 else
11120 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011121 int error = FALSE;
11122
Bram Moolenaare9a41262005-01-15 22:18:47 +000011123 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011124 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011125 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011126 /* On type error, nothing has been removed; return FAIL to stop the
11127 * loop. The error message was given by get_tv_number_chk(). */
11128 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011129 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011130 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011131 retval = OK;
11132theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011133 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011134 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011135}
11136
11137/*
11138 * "filter()" function
11139 */
11140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011141f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011142{
11143 filter_map(argvars, rettv, FALSE);
11144}
11145
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011146/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011147 * "finddir({fname}[, {path}[, {count}]])" function
11148 */
11149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011150f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011151{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011152 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011153}
11154
11155/*
11156 * "findfile({fname}[, {path}[, {count}]])" function
11157 */
11158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011159f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011160{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011161 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011162}
11163
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011164#ifdef FEAT_FLOAT
11165/*
11166 * "float2nr({float})" function
11167 */
11168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011169f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011170{
11171 float_T f;
11172
11173 if (get_float_arg(argvars, &f) == OK)
11174 {
11175 if (f < -0x7fffffff)
11176 rettv->vval.v_number = -0x7fffffff;
11177 else if (f > 0x7fffffff)
11178 rettv->vval.v_number = 0x7fffffff;
11179 else
11180 rettv->vval.v_number = (varnumber_T)f;
11181 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011182}
11183
11184/*
11185 * "floor({float})" function
11186 */
11187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011188f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011189{
11190 float_T f;
11191
11192 rettv->v_type = VAR_FLOAT;
11193 if (get_float_arg(argvars, &f) == OK)
11194 rettv->vval.v_float = floor(f);
11195 else
11196 rettv->vval.v_float = 0.0;
11197}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011198
11199/*
11200 * "fmod()" function
11201 */
11202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011203f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011204{
11205 float_T fx, fy;
11206
11207 rettv->v_type = VAR_FLOAT;
11208 if (get_float_arg(argvars, &fx) == OK
11209 && get_float_arg(&argvars[1], &fy) == OK)
11210 rettv->vval.v_float = fmod(fx, fy);
11211 else
11212 rettv->vval.v_float = 0.0;
11213}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011214#endif
11215
Bram Moolenaar0d660222005-01-07 21:51:51 +000011216/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011217 * "fnameescape({string})" function
11218 */
11219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011220f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011221{
11222 rettv->vval.v_string = vim_strsave_fnameescape(
11223 get_tv_string(&argvars[0]), FALSE);
11224 rettv->v_type = VAR_STRING;
11225}
11226
11227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228 * "fnamemodify({fname}, {mods})" function
11229 */
11230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011231f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232{
11233 char_u *fname;
11234 char_u *mods;
11235 int usedlen = 0;
11236 int len;
11237 char_u *fbuf = NULL;
11238 char_u buf[NUMBUFLEN];
11239
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011240 fname = get_tv_string_chk(&argvars[0]);
11241 mods = get_tv_string_buf_chk(&argvars[1], buf);
11242 if (fname == NULL || mods == NULL)
11243 fname = NULL;
11244 else
11245 {
11246 len = (int)STRLEN(fname);
11247 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011250 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011252 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011254 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255 vim_free(fbuf);
11256}
11257
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011258static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259
11260/*
11261 * "foldclosed()" function
11262 */
11263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011264foldclosed_both(
11265 typval_T *argvars UNUSED,
11266 typval_T *rettv,
11267 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268{
11269#ifdef FEAT_FOLDING
11270 linenr_T lnum;
11271 linenr_T first, last;
11272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011273 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11275 {
11276 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11277 {
11278 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011279 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011281 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282 return;
11283 }
11284 }
11285#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011286 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287}
11288
11289/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011290 * "foldclosed()" function
11291 */
11292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011293f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011294{
11295 foldclosed_both(argvars, rettv, FALSE);
11296}
11297
11298/*
11299 * "foldclosedend()" function
11300 */
11301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011302f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011303{
11304 foldclosed_both(argvars, rettv, TRUE);
11305}
11306
11307/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308 * "foldlevel()" function
11309 */
11310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011311f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312{
11313#ifdef FEAT_FOLDING
11314 linenr_T lnum;
11315
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011316 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011318 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011320}
11321
11322/*
11323 * "foldtext()" function
11324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011326f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327{
11328#ifdef FEAT_FOLDING
11329 linenr_T lnum;
11330 char_u *s;
11331 char_u *r;
11332 int len;
11333 char *txt;
11334#endif
11335
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011336 rettv->v_type = VAR_STRING;
11337 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011339 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11340 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11341 <= curbuf->b_ml.ml_line_count
11342 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343 {
11344 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011345 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11346 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347 {
11348 if (!linewhite(lnum))
11349 break;
11350 ++lnum;
11351 }
11352
11353 /* Find interesting text in this line. */
11354 s = skipwhite(ml_get(lnum));
11355 /* skip C comment-start */
11356 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011357 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011359 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011360 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011361 {
11362 s = skipwhite(ml_get(lnum + 1));
11363 if (*s == '*')
11364 s = skipwhite(s + 1);
11365 }
11366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367 txt = _("+-%s%3ld lines: ");
11368 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011369 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370 + 20 /* for %3ld */
11371 + STRLEN(s))); /* concatenated */
11372 if (r != NULL)
11373 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011374 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11375 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11376 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377 len = (int)STRLEN(r);
11378 STRCAT(r, s);
11379 /* remove 'foldmarker' and 'commentstring' */
11380 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011381 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382 }
11383 }
11384#endif
11385}
11386
11387/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011388 * "foldtextresult(lnum)" function
11389 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011391f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011392{
11393#ifdef FEAT_FOLDING
11394 linenr_T lnum;
11395 char_u *text;
11396 char_u buf[51];
11397 foldinfo_T foldinfo;
11398 int fold_count;
11399#endif
11400
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011401 rettv->v_type = VAR_STRING;
11402 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011403#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011404 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011405 /* treat illegal types and illegal string values for {lnum} the same */
11406 if (lnum < 0)
11407 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011408 fold_count = foldedCount(curwin, lnum, &foldinfo);
11409 if (fold_count > 0)
11410 {
11411 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11412 &foldinfo, buf);
11413 if (text == buf)
11414 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011415 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011416 }
11417#endif
11418}
11419
11420/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421 * "foreground()" function
11422 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011424f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426#ifdef FEAT_GUI
11427 if (gui.in_use)
11428 gui_mch_set_foreground();
11429#else
11430# ifdef WIN32
11431 win32_set_foreground();
11432# endif
11433#endif
11434}
11435
11436/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011437 * "function()" function
11438 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011440f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011441{
11442 char_u *s;
11443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011444 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011445 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011446 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011447 /* Don't check an autoload name for existence here. */
11448 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011449 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011450 else
11451 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011452 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011453 {
11454 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011455 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011456
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011457 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11458 * also be called from another script. Using trans_function_name()
11459 * would also work, but some plugins depend on the name being
11460 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011461 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011462 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011463 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011464 if (rettv->vval.v_string != NULL)
11465 {
11466 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011467 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011468 }
11469 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011470 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011471 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011472 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011473 }
11474}
11475
11476/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011477 * "garbagecollect()" function
11478 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011480f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011481{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011482 /* This is postponed until we are back at the toplevel, because we may be
11483 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11484 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011485
11486 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11487 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011488}
11489
11490/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011491 * "get()" function
11492 */
11493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011494f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011495{
Bram Moolenaar33570922005-01-25 22:26:29 +000011496 listitem_T *li;
11497 list_T *l;
11498 dictitem_T *di;
11499 dict_T *d;
11500 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011501
Bram Moolenaare9a41262005-01-15 22:18:47 +000011502 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011503 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011504 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011505 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011506 int error = FALSE;
11507
11508 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11509 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011510 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011511 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011512 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011513 else if (argvars[0].v_type == VAR_DICT)
11514 {
11515 if ((d = argvars[0].vval.v_dict) != NULL)
11516 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011517 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011518 if (di != NULL)
11519 tv = &di->di_tv;
11520 }
11521 }
11522 else
11523 EMSG2(_(e_listdictarg), "get()");
11524
11525 if (tv == NULL)
11526 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011527 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011528 copy_tv(&argvars[2], rettv);
11529 }
11530 else
11531 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011532}
11533
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011534static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011535
11536/*
11537 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011538 * Return a range (from start to end) of lines in rettv from the specified
11539 * buffer.
11540 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011541 */
11542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011543get_buffer_lines(
11544 buf_T *buf,
11545 linenr_T start,
11546 linenr_T end,
11547 int retlist,
11548 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011549{
11550 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011551
Bram Moolenaar959a1432013-12-14 12:17:38 +010011552 rettv->v_type = VAR_STRING;
11553 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011554 if (retlist && rettv_list_alloc(rettv) == FAIL)
11555 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011556
11557 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11558 return;
11559
11560 if (!retlist)
11561 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011562 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11563 p = ml_get_buf(buf, start, FALSE);
11564 else
11565 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011566 rettv->vval.v_string = vim_strsave(p);
11567 }
11568 else
11569 {
11570 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011571 return;
11572
11573 if (start < 1)
11574 start = 1;
11575 if (end > buf->b_ml.ml_line_count)
11576 end = buf->b_ml.ml_line_count;
11577 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011578 if (list_append_string(rettv->vval.v_list,
11579 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011580 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011581 }
11582}
11583
11584/*
11585 * "getbufline()" function
11586 */
11587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011588f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011589{
11590 linenr_T lnum;
11591 linenr_T end;
11592 buf_T *buf;
11593
11594 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11595 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011596 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011597 --emsg_off;
11598
Bram Moolenaar661b1822005-07-28 22:36:45 +000011599 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011600 if (argvars[2].v_type == VAR_UNKNOWN)
11601 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011602 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011603 end = get_tv_lnum_buf(&argvars[2], buf);
11604
Bram Moolenaar342337a2005-07-21 21:11:17 +000011605 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011606}
11607
Bram Moolenaar0d660222005-01-07 21:51:51 +000011608/*
11609 * "getbufvar()" function
11610 */
11611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011612f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011613{
11614 buf_T *buf;
11615 buf_T *save_curbuf;
11616 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011617 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011618 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011620 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11621 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011622 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011623 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011624
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011625 rettv->v_type = VAR_STRING;
11626 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011627
11628 if (buf != NULL && varname != NULL)
11629 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011630 /* set curbuf to be our buf, temporarily */
11631 save_curbuf = curbuf;
11632 curbuf = buf;
11633
Bram Moolenaar0d660222005-01-07 21:51:51 +000011634 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011635 {
11636 if (get_option_tv(&varname, rettv, TRUE) == OK)
11637 done = TRUE;
11638 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011639 else if (STRCMP(varname, "changedtick") == 0)
11640 {
11641 rettv->v_type = VAR_NUMBER;
11642 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011643 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011644 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011645 else
11646 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011647 /* Look up the variable. */
11648 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11649 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11650 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011651 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011652 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011653 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011654 done = TRUE;
11655 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011656 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011657
11658 /* restore previous notion of curbuf */
11659 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011660 }
11661
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011662 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11663 /* use the default value */
11664 copy_tv(&argvars[2], rettv);
11665
Bram Moolenaar0d660222005-01-07 21:51:51 +000011666 --emsg_off;
11667}
11668
11669/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670 * "getchar()" function
11671 */
11672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011673f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674{
11675 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011676 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011678 /* Position the cursor. Needed after a message that ends in a space. */
11679 windgoto(msg_row, msg_col);
11680
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681 ++no_mapping;
11682 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011683 for (;;)
11684 {
11685 if (argvars[0].v_type == VAR_UNKNOWN)
11686 /* getchar(): blocking wait. */
11687 n = safe_vgetc();
11688 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11689 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011690 n = vpeekc_any();
11691 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011692 /* illegal argument or getchar(0) and no char avail: return zero */
11693 n = 0;
11694 else
11695 /* getchar(0) and char avail: return char */
11696 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011697
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011698 if (n == K_IGNORE)
11699 continue;
11700 break;
11701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702 --no_mapping;
11703 --allow_keys;
11704
Bram Moolenaar219b8702006-11-01 14:32:36 +000011705 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11706 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11707 vimvars[VV_MOUSE_COL].vv_nr = 0;
11708
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011709 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710 if (IS_SPECIAL(n) || mod_mask != 0)
11711 {
11712 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11713 int i = 0;
11714
11715 /* Turn a special key into three bytes, plus modifier. */
11716 if (mod_mask != 0)
11717 {
11718 temp[i++] = K_SPECIAL;
11719 temp[i++] = KS_MODIFIER;
11720 temp[i++] = mod_mask;
11721 }
11722 if (IS_SPECIAL(n))
11723 {
11724 temp[i++] = K_SPECIAL;
11725 temp[i++] = K_SECOND(n);
11726 temp[i++] = K_THIRD(n);
11727 }
11728#ifdef FEAT_MBYTE
11729 else if (has_mbyte)
11730 i += (*mb_char2bytes)(n, temp + i);
11731#endif
11732 else
11733 temp[i++] = n;
11734 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011735 rettv->v_type = VAR_STRING;
11736 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011737
11738#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011739 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011740 {
11741 int row = mouse_row;
11742 int col = mouse_col;
11743 win_T *win;
11744 linenr_T lnum;
11745# ifdef FEAT_WINDOWS
11746 win_T *wp;
11747# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011748 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011749
11750 if (row >= 0 && col >= 0)
11751 {
11752 /* Find the window at the mouse coordinates and compute the
11753 * text position. */
11754 win = mouse_find_win(&row, &col);
11755 (void)mouse_comp_pos(win, &row, &col, &lnum);
11756# ifdef FEAT_WINDOWS
11757 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011758 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011759# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011760 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011761 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11762 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11763 }
11764 }
11765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766 }
11767}
11768
11769/*
11770 * "getcharmod()" function
11771 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011773f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011775 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011776}
11777
11778/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011779 * "getcharsearch()" function
11780 */
11781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011782f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011783{
11784 if (rettv_dict_alloc(rettv) != FAIL)
11785 {
11786 dict_T *dict = rettv->vval.v_dict;
11787
11788 dict_add_nr_str(dict, "char", 0L, last_csearch());
11789 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11790 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11791 }
11792}
11793
11794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795 * "getcmdline()" function
11796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011798f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011800 rettv->v_type = VAR_STRING;
11801 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011802}
11803
11804/*
11805 * "getcmdpos()" function
11806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011808f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011810 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811}
11812
11813/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011814 * "getcmdtype()" function
11815 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011817f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011818{
11819 rettv->v_type = VAR_STRING;
11820 rettv->vval.v_string = alloc(2);
11821 if (rettv->vval.v_string != NULL)
11822 {
11823 rettv->vval.v_string[0] = get_cmdline_type();
11824 rettv->vval.v_string[1] = NUL;
11825 }
11826}
11827
11828/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011829 * "getcmdwintype()" function
11830 */
11831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011832f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011833{
11834 rettv->v_type = VAR_STRING;
11835 rettv->vval.v_string = NULL;
11836#ifdef FEAT_CMDWIN
11837 rettv->vval.v_string = alloc(2);
11838 if (rettv->vval.v_string != NULL)
11839 {
11840 rettv->vval.v_string[0] = cmdwin_type;
11841 rettv->vval.v_string[1] = NUL;
11842 }
11843#endif
11844}
11845
11846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847 * "getcwd()" function
11848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011850f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851{
Bram Moolenaarc9703302016-01-17 21:49:33 +010011852 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011853 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011855 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011856 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010011857
11858 wp = find_tabwin(&argvars[0], &argvars[1]);
11859 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010011861 if (wp->w_localdir != NULL)
11862 rettv->vval.v_string = vim_strsave(wp->w_localdir);
11863 else if(globaldir != NULL)
11864 rettv->vval.v_string = vim_strsave(globaldir);
11865 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020011866 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010011867 cwd = alloc(MAXPATHL);
11868 if (cwd != NULL)
11869 {
11870 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11871 rettv->vval.v_string = vim_strsave(cwd);
11872 vim_free(cwd);
11873 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020011874 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010011875#ifdef BACKSLASH_IN_FILENAME
11876 if (rettv->vval.v_string != NULL)
11877 slash_adjust(rettv->vval.v_string);
11878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011879 }
11880}
11881
11882/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011883 * "getfontname()" function
11884 */
11885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011886f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011887{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011888 rettv->v_type = VAR_STRING;
11889 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011890#ifdef FEAT_GUI
11891 if (gui.in_use)
11892 {
11893 GuiFont font;
11894 char_u *name = NULL;
11895
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011896 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011897 {
11898 /* Get the "Normal" font. Either the name saved by
11899 * hl_set_font_name() or from the font ID. */
11900 font = gui.norm_font;
11901 name = hl_get_font_name();
11902 }
11903 else
11904 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011905 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011906 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11907 return;
11908 font = gui_mch_get_font(name, FALSE);
11909 if (font == NOFONT)
11910 return; /* Invalid font name, return empty string. */
11911 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011912 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011913 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011914 gui_mch_free_font(font);
11915 }
11916#endif
11917}
11918
11919/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011920 * "getfperm({fname})" function
11921 */
11922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011923f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011924{
11925 char_u *fname;
11926 struct stat st;
11927 char_u *perm = NULL;
11928 char_u flags[] = "rwx";
11929 int i;
11930
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011931 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011932
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011933 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011934 if (mch_stat((char *)fname, &st) >= 0)
11935 {
11936 perm = vim_strsave((char_u *)"---------");
11937 if (perm != NULL)
11938 {
11939 for (i = 0; i < 9; i++)
11940 {
11941 if (st.st_mode & (1 << (8 - i)))
11942 perm[i] = flags[i % 3];
11943 }
11944 }
11945 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011946 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011947}
11948
11949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950 * "getfsize({fname})" function
11951 */
11952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011953f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954{
11955 char_u *fname;
11956 struct stat st;
11957
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011958 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011960 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961
11962 if (mch_stat((char *)fname, &st) >= 0)
11963 {
11964 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011965 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011967 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011968 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011969
11970 /* non-perfect check for overflow */
11971 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11972 rettv->vval.v_number = -2;
11973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974 }
11975 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011976 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977}
11978
11979/*
11980 * "getftime({fname})" function
11981 */
11982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011983f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984{
11985 char_u *fname;
11986 struct stat st;
11987
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011988 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011989
11990 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011991 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011993 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994}
11995
11996/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011997 * "getftype({fname})" function
11998 */
11999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012000f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012001{
12002 char_u *fname;
12003 struct stat st;
12004 char_u *type = NULL;
12005 char *t;
12006
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012007 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012008
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012009 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012010 if (mch_lstat((char *)fname, &st) >= 0)
12011 {
12012#ifdef S_ISREG
12013 if (S_ISREG(st.st_mode))
12014 t = "file";
12015 else if (S_ISDIR(st.st_mode))
12016 t = "dir";
12017# ifdef S_ISLNK
12018 else if (S_ISLNK(st.st_mode))
12019 t = "link";
12020# endif
12021# ifdef S_ISBLK
12022 else if (S_ISBLK(st.st_mode))
12023 t = "bdev";
12024# endif
12025# ifdef S_ISCHR
12026 else if (S_ISCHR(st.st_mode))
12027 t = "cdev";
12028# endif
12029# ifdef S_ISFIFO
12030 else if (S_ISFIFO(st.st_mode))
12031 t = "fifo";
12032# endif
12033# ifdef S_ISSOCK
12034 else if (S_ISSOCK(st.st_mode))
12035 t = "fifo";
12036# endif
12037 else
12038 t = "other";
12039#else
12040# ifdef S_IFMT
12041 switch (st.st_mode & S_IFMT)
12042 {
12043 case S_IFREG: t = "file"; break;
12044 case S_IFDIR: t = "dir"; break;
12045# ifdef S_IFLNK
12046 case S_IFLNK: t = "link"; break;
12047# endif
12048# ifdef S_IFBLK
12049 case S_IFBLK: t = "bdev"; break;
12050# endif
12051# ifdef S_IFCHR
12052 case S_IFCHR: t = "cdev"; break;
12053# endif
12054# ifdef S_IFIFO
12055 case S_IFIFO: t = "fifo"; break;
12056# endif
12057# ifdef S_IFSOCK
12058 case S_IFSOCK: t = "socket"; break;
12059# endif
12060 default: t = "other";
12061 }
12062# else
12063 if (mch_isdir(fname))
12064 t = "dir";
12065 else
12066 t = "file";
12067# endif
12068#endif
12069 type = vim_strsave((char_u *)t);
12070 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012071 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012072}
12073
12074/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012075 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012076 */
12077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012078f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012079{
12080 linenr_T lnum;
12081 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012082 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012083
12084 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012085 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012086 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012087 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012088 retlist = FALSE;
12089 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012090 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012091 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012092 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012093 retlist = TRUE;
12094 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012095
Bram Moolenaar342337a2005-07-21 21:11:17 +000012096 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012097}
12098
12099/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012100 * "getmatches()" function
12101 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012103f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012104{
12105#ifdef FEAT_SEARCH_EXTRA
12106 dict_T *dict;
12107 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012108 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012109
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012110 if (rettv_list_alloc(rettv) == OK)
12111 {
12112 while (cur != NULL)
12113 {
12114 dict = dict_alloc();
12115 if (dict == NULL)
12116 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012117 if (cur->match.regprog == NULL)
12118 {
12119 /* match added with matchaddpos() */
12120 for (i = 0; i < MAXPOSMATCH; ++i)
12121 {
12122 llpos_T *llpos;
12123 char buf[6];
12124 list_T *l;
12125
12126 llpos = &cur->pos.pos[i];
12127 if (llpos->lnum == 0)
12128 break;
12129 l = list_alloc();
12130 if (l == NULL)
12131 break;
12132 list_append_number(l, (varnumber_T)llpos->lnum);
12133 if (llpos->col > 0)
12134 {
12135 list_append_number(l, (varnumber_T)llpos->col);
12136 list_append_number(l, (varnumber_T)llpos->len);
12137 }
12138 sprintf(buf, "pos%d", i + 1);
12139 dict_add_list(dict, buf, l);
12140 }
12141 }
12142 else
12143 {
12144 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12145 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012146 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012147 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12148 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012149# ifdef FEAT_CONCEAL
12150 if (cur->conceal_char)
12151 {
12152 char_u buf[MB_MAXBYTES + 1];
12153
12154 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12155 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12156 }
12157# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012158 list_append_dict(rettv->vval.v_list, dict);
12159 cur = cur->next;
12160 }
12161 }
12162#endif
12163}
12164
12165/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012166 * "getpid()" function
12167 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012169f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012170{
12171 rettv->vval.v_number = mch_get_pid();
12172}
12173
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012174static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012175
12176/*
12177 * "getcurpos()" function
12178 */
12179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012180f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012181{
12182 getpos_both(argvars, rettv, TRUE);
12183}
12184
Bram Moolenaar18081e32008-02-20 19:11:07 +000012185/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012186 * "getpos(string)" function
12187 */
12188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012189f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012190{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012191 getpos_both(argvars, rettv, FALSE);
12192}
12193
12194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012195getpos_both(
12196 typval_T *argvars,
12197 typval_T *rettv,
12198 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012199{
Bram Moolenaara5525202006-03-02 22:52:09 +000012200 pos_T *fp;
12201 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012202 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012203
12204 if (rettv_list_alloc(rettv) == OK)
12205 {
12206 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012207 if (getcurpos)
12208 fp = &curwin->w_cursor;
12209 else
12210 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012211 if (fnum != -1)
12212 list_append_number(l, (varnumber_T)fnum);
12213 else
12214 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012215 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12216 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012217 list_append_number(l, (fp != NULL)
12218 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012219 : (varnumber_T)0);
12220 list_append_number(l,
12221#ifdef FEAT_VIRTUALEDIT
12222 (fp != NULL) ? (varnumber_T)fp->coladd :
12223#endif
12224 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012225 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012226 list_append_number(l, curwin->w_curswant == MAXCOL ?
12227 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012228 }
12229 else
12230 rettv->vval.v_number = FALSE;
12231}
12232
12233/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012234 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012235 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012237f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012238{
12239#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012240 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012241#endif
12242
Bram Moolenaar2641f772005-03-25 21:58:17 +000012243#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012244 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012245 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012246 wp = NULL;
12247 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12248 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012249 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012250 if (wp == NULL)
12251 return;
12252 }
12253
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012254 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012255 }
12256#endif
12257}
12258
12259/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260 * "getreg()" function
12261 */
12262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012263f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012264{
12265 char_u *strregname;
12266 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012267 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012268 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012269 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012271 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012272 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012273 strregname = get_tv_string_chk(&argvars[0]);
12274 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012275 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012277 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012278 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12279 return_list = get_tv_number_chk(&argvars[2], &error);
12280 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012282 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012283 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012284
12285 if (error)
12286 return;
12287
Bram Moolenaar071d4272004-06-13 20:20:40 +000012288 regname = (strregname == NULL ? '"' : *strregname);
12289 if (regname == 0)
12290 regname = '"';
12291
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012292 if (return_list)
12293 {
12294 rettv->v_type = VAR_LIST;
12295 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12296 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012297 if (rettv->vval.v_list != NULL)
12298 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012299 }
12300 else
12301 {
12302 rettv->v_type = VAR_STRING;
12303 rettv->vval.v_string = get_reg_contents(regname,
12304 arg2 ? GREG_EXPR_SRC : 0);
12305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306}
12307
12308/*
12309 * "getregtype()" function
12310 */
12311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012312f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313{
12314 char_u *strregname;
12315 int regname;
12316 char_u buf[NUMBUFLEN + 2];
12317 long reglen = 0;
12318
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012319 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012320 {
12321 strregname = get_tv_string_chk(&argvars[0]);
12322 if (strregname == NULL) /* type error; errmsg already given */
12323 {
12324 rettv->v_type = VAR_STRING;
12325 rettv->vval.v_string = NULL;
12326 return;
12327 }
12328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329 else
12330 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012331 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012332
12333 regname = (strregname == NULL ? '"' : *strregname);
12334 if (regname == 0)
12335 regname = '"';
12336
12337 buf[0] = NUL;
12338 buf[1] = NUL;
12339 switch (get_reg_type(regname, &reglen))
12340 {
12341 case MLINE: buf[0] = 'V'; break;
12342 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343 case MBLOCK:
12344 buf[0] = Ctrl_V;
12345 sprintf((char *)buf + 1, "%ld", reglen + 1);
12346 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012348 rettv->v_type = VAR_STRING;
12349 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350}
12351
12352/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012353 * "gettabvar()" function
12354 */
12355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012356f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012357{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012358 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012359 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012360 dictitem_T *v;
12361 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012362 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012363
12364 rettv->v_type = VAR_STRING;
12365 rettv->vval.v_string = NULL;
12366
12367 varname = get_tv_string_chk(&argvars[1]);
12368 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12369 if (tp != NULL && varname != NULL)
12370 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012371 /* Set tp to be our tabpage, temporarily. Also set the window to the
12372 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012373 if (switch_win(&oldcurwin, &oldtabpage,
12374 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012375 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012376 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012377 /* look up the variable */
12378 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12379 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12380 if (v != NULL)
12381 {
12382 copy_tv(&v->di_tv, rettv);
12383 done = TRUE;
12384 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012385 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012386
12387 /* restore previous notion of curwin */
12388 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012389 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012390
12391 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012392 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012393}
12394
12395/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012396 * "gettabwinvar()" function
12397 */
12398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012399f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012400{
12401 getwinvar(argvars, rettv, 1);
12402}
12403
12404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405 * "getwinposx()" function
12406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012408f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012409{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012410 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012411#ifdef FEAT_GUI
12412 if (gui.in_use)
12413 {
12414 int x, y;
12415
12416 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012417 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012418 }
12419#endif
12420}
12421
12422/*
12423 * "getwinposy()" function
12424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012426f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012428 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429#ifdef FEAT_GUI
12430 if (gui.in_use)
12431 {
12432 int x, y;
12433
12434 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012435 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436 }
12437#endif
12438}
12439
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012440/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012441 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012442 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012443 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012444find_win_by_nr(
12445 typval_T *vp,
12446 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012447{
12448#ifdef FEAT_WINDOWS
12449 win_T *wp;
12450#endif
12451 int nr;
12452
12453 nr = get_tv_number_chk(vp, NULL);
12454
12455#ifdef FEAT_WINDOWS
12456 if (nr < 0)
12457 return NULL;
12458 if (nr == 0)
12459 return curwin;
12460
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012461 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12462 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012463 if (--nr <= 0)
12464 break;
12465 return wp;
12466#else
12467 if (nr == 0 || nr == 1)
12468 return curwin;
12469 return NULL;
12470#endif
12471}
12472
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012474 * Find window specified by "wvp" in tabpage "tvp".
12475 */
12476 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012477find_tabwin(
12478 typval_T *wvp, /* VAR_UNKNOWN for current window */
12479 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012480{
12481 win_T *wp = NULL;
12482 tabpage_T *tp = NULL;
12483 long n;
12484
12485 if (wvp->v_type != VAR_UNKNOWN)
12486 {
12487 if (tvp->v_type != VAR_UNKNOWN)
12488 {
12489 n = get_tv_number(tvp);
12490 if (n >= 0)
12491 tp = find_tabpage(n);
12492 }
12493 else
12494 tp = curtab;
12495
12496 if (tp != NULL)
12497 wp = find_win_by_nr(wvp, tp);
12498 }
12499 else
12500 wp = curwin;
12501
12502 return wp;
12503}
12504
12505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012506 * "getwinvar()" function
12507 */
12508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012509f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012511 getwinvar(argvars, rettv, 0);
12512}
12513
12514/*
12515 * getwinvar() and gettabwinvar()
12516 */
12517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012518getwinvar(
12519 typval_T *argvars,
12520 typval_T *rettv,
12521 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012522{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012523 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012524 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012525 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012526 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012527 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012528#ifdef FEAT_WINDOWS
12529 win_T *oldcurwin;
12530 tabpage_T *oldtabpage;
12531 int need_switch_win;
12532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012534#ifdef FEAT_WINDOWS
12535 if (off == 1)
12536 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12537 else
12538 tp = curtab;
12539#endif
12540 win = find_win_by_nr(&argvars[off], tp);
12541 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012542 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012543
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012544 rettv->v_type = VAR_STRING;
12545 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012546
12547 if (win != NULL && varname != NULL)
12548 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012549#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012550 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012551 * otherwise the window is not valid. Only do this when needed,
12552 * autocommands get blocked. */
12553 need_switch_win = !(tp == curtab && win == curwin);
12554 if (!need_switch_win
12555 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12556#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012557 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012558 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012559 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012560 if (get_option_tv(&varname, rettv, 1) == OK)
12561 done = TRUE;
12562 }
12563 else
12564 {
12565 /* Look up the variable. */
12566 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12567 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12568 varname, FALSE);
12569 if (v != NULL)
12570 {
12571 copy_tv(&v->di_tv, rettv);
12572 done = TRUE;
12573 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012575 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012576
Bram Moolenaarba117c22015-09-29 16:53:22 +020012577#ifdef FEAT_WINDOWS
12578 if (need_switch_win)
12579 /* restore previous notion of curwin */
12580 restore_win(oldcurwin, oldtabpage, TRUE);
12581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012582 }
12583
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012584 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12585 /* use the default return value */
12586 copy_tv(&argvars[off + 2], rettv);
12587
Bram Moolenaar071d4272004-06-13 20:20:40 +000012588 --emsg_off;
12589}
12590
12591/*
12592 * "glob()" function
12593 */
12594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012595f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012596{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012597 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012598 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012599 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012601 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012602 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012603 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012604 if (argvars[1].v_type != VAR_UNKNOWN)
12605 {
12606 if (get_tv_number_chk(&argvars[1], &error))
12607 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012608 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012609 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012610 if (get_tv_number_chk(&argvars[2], &error))
12611 {
12612 rettv->v_type = VAR_LIST;
12613 rettv->vval.v_list = NULL;
12614 }
12615 if (argvars[3].v_type != VAR_UNKNOWN
12616 && get_tv_number_chk(&argvars[3], &error))
12617 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012618 }
12619 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012620 if (!error)
12621 {
12622 ExpandInit(&xpc);
12623 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012624 if (p_wic)
12625 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012626 if (rettv->v_type == VAR_STRING)
12627 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012628 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012629 else if (rettv_list_alloc(rettv) != FAIL)
12630 {
12631 int i;
12632
12633 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12634 NULL, options, WILD_ALL_KEEP);
12635 for (i = 0; i < xpc.xp_numfiles; i++)
12636 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12637
12638 ExpandCleanup(&xpc);
12639 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012640 }
12641 else
12642 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012643}
12644
12645/*
12646 * "globpath()" function
12647 */
12648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012649f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012651 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012653 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012654 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012655 garray_T ga;
12656 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012657
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012658 /* When the optional second argument is non-zero, don't remove matches
12659 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012660 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012661 if (argvars[2].v_type != VAR_UNKNOWN)
12662 {
12663 if (get_tv_number_chk(&argvars[2], &error))
12664 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012665 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012666 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012667 if (get_tv_number_chk(&argvars[3], &error))
12668 {
12669 rettv->v_type = VAR_LIST;
12670 rettv->vval.v_list = NULL;
12671 }
12672 if (argvars[4].v_type != VAR_UNKNOWN
12673 && get_tv_number_chk(&argvars[4], &error))
12674 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012675 }
12676 }
12677 if (file != NULL && !error)
12678 {
12679 ga_init2(&ga, (int)sizeof(char_u *), 10);
12680 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12681 if (rettv->v_type == VAR_STRING)
12682 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12683 else if (rettv_list_alloc(rettv) != FAIL)
12684 for (i = 0; i < ga.ga_len; ++i)
12685 list_append_string(rettv->vval.v_list,
12686 ((char_u **)(ga.ga_data))[i], -1);
12687 ga_clear_strings(&ga);
12688 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012689 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012690 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012691}
12692
12693/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012694 * "glob2regpat()" function
12695 */
12696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012697f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012698{
12699 char_u *pat = get_tv_string_chk(&argvars[0]);
12700
12701 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010012702 rettv->vval.v_string = (pat == NULL)
12703 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012704}
12705
12706/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012707 * "has()" function
12708 */
12709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012710f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711{
12712 int i;
12713 char_u *name;
12714 int n = FALSE;
12715 static char *(has_list[]) =
12716 {
12717#ifdef AMIGA
12718 "amiga",
12719# ifdef FEAT_ARP
12720 "arp",
12721# endif
12722#endif
12723#ifdef __BEOS__
12724 "beos",
12725#endif
12726#ifdef MSDOS
12727# ifdef DJGPP
12728 "dos32",
12729# else
12730 "dos16",
12731# endif
12732#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012733#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012734 "mac",
12735#endif
12736#if defined(MACOS_X_UNIX)
12737 "macunix",
12738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012739#ifdef __QNX__
12740 "qnx",
12741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742#ifdef UNIX
12743 "unix",
12744#endif
12745#ifdef VMS
12746 "vms",
12747#endif
12748#ifdef WIN16
12749 "win16",
12750#endif
12751#ifdef WIN32
12752 "win32",
12753#endif
12754#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12755 "win32unix",
12756#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012757#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758 "win64",
12759#endif
12760#ifdef EBCDIC
12761 "ebcdic",
12762#endif
12763#ifndef CASE_INSENSITIVE_FILENAME
12764 "fname_case",
12765#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012766#ifdef HAVE_ACL
12767 "acl",
12768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769#ifdef FEAT_ARABIC
12770 "arabic",
12771#endif
12772#ifdef FEAT_AUTOCMD
12773 "autocmd",
12774#endif
12775#ifdef FEAT_BEVAL
12776 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012777# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12778 "balloon_multiline",
12779# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780#endif
12781#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12782 "builtin_terms",
12783# ifdef ALL_BUILTIN_TCAPS
12784 "all_builtin_terms",
12785# endif
12786#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012787#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12788 || defined(FEAT_GUI_W32) \
12789 || defined(FEAT_GUI_MOTIF))
12790 "browsefilter",
12791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792#ifdef FEAT_BYTEOFF
12793 "byte_offset",
12794#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010012795#ifdef FEAT_CHANNEL
12796 "channel",
12797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798#ifdef FEAT_CINDENT
12799 "cindent",
12800#endif
12801#ifdef FEAT_CLIENTSERVER
12802 "clientserver",
12803#endif
12804#ifdef FEAT_CLIPBOARD
12805 "clipboard",
12806#endif
12807#ifdef FEAT_CMDL_COMPL
12808 "cmdline_compl",
12809#endif
12810#ifdef FEAT_CMDHIST
12811 "cmdline_hist",
12812#endif
12813#ifdef FEAT_COMMENTS
12814 "comments",
12815#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012816#ifdef FEAT_CONCEAL
12817 "conceal",
12818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819#ifdef FEAT_CRYPT
12820 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010012821 "crypt-blowfish",
12822 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012823#endif
12824#ifdef FEAT_CSCOPE
12825 "cscope",
12826#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012827#ifdef FEAT_CURSORBIND
12828 "cursorbind",
12829#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012830#ifdef CURSOR_SHAPE
12831 "cursorshape",
12832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833#ifdef DEBUG
12834 "debug",
12835#endif
12836#ifdef FEAT_CON_DIALOG
12837 "dialog_con",
12838#endif
12839#ifdef FEAT_GUI_DIALOG
12840 "dialog_gui",
12841#endif
12842#ifdef FEAT_DIFF
12843 "diff",
12844#endif
12845#ifdef FEAT_DIGRAPHS
12846 "digraphs",
12847#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012848#ifdef FEAT_DIRECTX
12849 "directx",
12850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851#ifdef FEAT_DND
12852 "dnd",
12853#endif
12854#ifdef FEAT_EMACS_TAGS
12855 "emacs_tags",
12856#endif
12857 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010012858 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012859#ifdef FEAT_SEARCH_EXTRA
12860 "extra_search",
12861#endif
12862#ifdef FEAT_FKMAP
12863 "farsi",
12864#endif
12865#ifdef FEAT_SEARCHPATH
12866 "file_in_path",
12867#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012868#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012869 "filterpipe",
12870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871#ifdef FEAT_FIND_ID
12872 "find_in_path",
12873#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012874#ifdef FEAT_FLOAT
12875 "float",
12876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877#ifdef FEAT_FOLDING
12878 "folding",
12879#endif
12880#ifdef FEAT_FOOTER
12881 "footer",
12882#endif
12883#if !defined(USE_SYSTEM) && defined(UNIX)
12884 "fork",
12885#endif
12886#ifdef FEAT_GETTEXT
12887 "gettext",
12888#endif
12889#ifdef FEAT_GUI
12890 "gui",
12891#endif
12892#ifdef FEAT_GUI_ATHENA
12893# ifdef FEAT_GUI_NEXTAW
12894 "gui_neXtaw",
12895# else
12896 "gui_athena",
12897# endif
12898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899#ifdef FEAT_GUI_GTK
12900 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012901 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012903#ifdef FEAT_GUI_GNOME
12904 "gui_gnome",
12905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906#ifdef FEAT_GUI_MAC
12907 "gui_mac",
12908#endif
12909#ifdef FEAT_GUI_MOTIF
12910 "gui_motif",
12911#endif
12912#ifdef FEAT_GUI_PHOTON
12913 "gui_photon",
12914#endif
12915#ifdef FEAT_GUI_W16
12916 "gui_win16",
12917#endif
12918#ifdef FEAT_GUI_W32
12919 "gui_win32",
12920#endif
12921#ifdef FEAT_HANGULIN
12922 "hangul_input",
12923#endif
12924#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12925 "iconv",
12926#endif
12927#ifdef FEAT_INS_EXPAND
12928 "insert_expand",
12929#endif
12930#ifdef FEAT_JUMPLIST
12931 "jumplist",
12932#endif
12933#ifdef FEAT_KEYMAP
12934 "keymap",
12935#endif
12936#ifdef FEAT_LANGMAP
12937 "langmap",
12938#endif
12939#ifdef FEAT_LIBCALL
12940 "libcall",
12941#endif
12942#ifdef FEAT_LINEBREAK
12943 "linebreak",
12944#endif
12945#ifdef FEAT_LISP
12946 "lispindent",
12947#endif
12948#ifdef FEAT_LISTCMDS
12949 "listcmds",
12950#endif
12951#ifdef FEAT_LOCALMAP
12952 "localmap",
12953#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012954#ifdef FEAT_LUA
12955# ifndef DYNAMIC_LUA
12956 "lua",
12957# endif
12958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959#ifdef FEAT_MENU
12960 "menu",
12961#endif
12962#ifdef FEAT_SESSION
12963 "mksession",
12964#endif
12965#ifdef FEAT_MODIFY_FNAME
12966 "modify_fname",
12967#endif
12968#ifdef FEAT_MOUSE
12969 "mouse",
12970#endif
12971#ifdef FEAT_MOUSESHAPE
12972 "mouseshape",
12973#endif
12974#if defined(UNIX) || defined(VMS)
12975# ifdef FEAT_MOUSE_DEC
12976 "mouse_dec",
12977# endif
12978# ifdef FEAT_MOUSE_GPM
12979 "mouse_gpm",
12980# endif
12981# ifdef FEAT_MOUSE_JSB
12982 "mouse_jsbterm",
12983# endif
12984# ifdef FEAT_MOUSE_NET
12985 "mouse_netterm",
12986# endif
12987# ifdef FEAT_MOUSE_PTERM
12988 "mouse_pterm",
12989# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012990# ifdef FEAT_MOUSE_SGR
12991 "mouse_sgr",
12992# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012993# ifdef FEAT_SYSMOUSE
12994 "mouse_sysmouse",
12995# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012996# ifdef FEAT_MOUSE_URXVT
12997 "mouse_urxvt",
12998# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012999# ifdef FEAT_MOUSE_XTERM
13000 "mouse_xterm",
13001# endif
13002#endif
13003#ifdef FEAT_MBYTE
13004 "multi_byte",
13005#endif
13006#ifdef FEAT_MBYTE_IME
13007 "multi_byte_ime",
13008#endif
13009#ifdef FEAT_MULTI_LANG
13010 "multi_lang",
13011#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013012#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013013#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013014 "mzscheme",
13015#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013017#ifdef FEAT_OLE
13018 "ole",
13019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020#ifdef FEAT_PATH_EXTRA
13021 "path_extra",
13022#endif
13023#ifdef FEAT_PERL
13024#ifndef DYNAMIC_PERL
13025 "perl",
13026#endif
13027#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013028#ifdef FEAT_PERSISTENT_UNDO
13029 "persistent_undo",
13030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031#ifdef FEAT_PYTHON
13032#ifndef DYNAMIC_PYTHON
13033 "python",
13034#endif
13035#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013036#ifdef FEAT_PYTHON3
13037#ifndef DYNAMIC_PYTHON3
13038 "python3",
13039#endif
13040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013041#ifdef FEAT_POSTSCRIPT
13042 "postscript",
13043#endif
13044#ifdef FEAT_PRINTER
13045 "printer",
13046#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013047#ifdef FEAT_PROFILE
13048 "profile",
13049#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013050#ifdef FEAT_RELTIME
13051 "reltime",
13052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053#ifdef FEAT_QUICKFIX
13054 "quickfix",
13055#endif
13056#ifdef FEAT_RIGHTLEFT
13057 "rightleft",
13058#endif
13059#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13060 "ruby",
13061#endif
13062#ifdef FEAT_SCROLLBIND
13063 "scrollbind",
13064#endif
13065#ifdef FEAT_CMDL_INFO
13066 "showcmd",
13067 "cmdline_info",
13068#endif
13069#ifdef FEAT_SIGNS
13070 "signs",
13071#endif
13072#ifdef FEAT_SMARTINDENT
13073 "smartindent",
13074#endif
13075#ifdef FEAT_SNIFF
13076 "sniff",
13077#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013078#ifdef STARTUPTIME
13079 "startuptime",
13080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081#ifdef FEAT_STL_OPT
13082 "statusline",
13083#endif
13084#ifdef FEAT_SUN_WORKSHOP
13085 "sun_workshop",
13086#endif
13087#ifdef FEAT_NETBEANS_INTG
13088 "netbeans_intg",
13089#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013090#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013091 "spell",
13092#endif
13093#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094 "syntax",
13095#endif
13096#if defined(USE_SYSTEM) || !defined(UNIX)
13097 "system",
13098#endif
13099#ifdef FEAT_TAG_BINS
13100 "tag_binary",
13101#endif
13102#ifdef FEAT_TAG_OLDSTATIC
13103 "tag_old_static",
13104#endif
13105#ifdef FEAT_TAG_ANYWHITE
13106 "tag_any_white",
13107#endif
13108#ifdef FEAT_TCL
13109# ifndef DYNAMIC_TCL
13110 "tcl",
13111# endif
13112#endif
13113#ifdef TERMINFO
13114 "terminfo",
13115#endif
13116#ifdef FEAT_TERMRESPONSE
13117 "termresponse",
13118#endif
13119#ifdef FEAT_TEXTOBJ
13120 "textobjects",
13121#endif
13122#ifdef HAVE_TGETENT
13123 "tgetent",
13124#endif
13125#ifdef FEAT_TITLE
13126 "title",
13127#endif
13128#ifdef FEAT_TOOLBAR
13129 "toolbar",
13130#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013131#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13132 "unnamedplus",
13133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134#ifdef FEAT_USR_CMDS
13135 "user-commands", /* was accidentally included in 5.4 */
13136 "user_commands",
13137#endif
13138#ifdef FEAT_VIMINFO
13139 "viminfo",
13140#endif
13141#ifdef FEAT_VERTSPLIT
13142 "vertsplit",
13143#endif
13144#ifdef FEAT_VIRTUALEDIT
13145 "virtualedit",
13146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013148#ifdef FEAT_VISUALEXTRA
13149 "visualextra",
13150#endif
13151#ifdef FEAT_VREPLACE
13152 "vreplace",
13153#endif
13154#ifdef FEAT_WILDIGN
13155 "wildignore",
13156#endif
13157#ifdef FEAT_WILDMENU
13158 "wildmenu",
13159#endif
13160#ifdef FEAT_WINDOWS
13161 "windows",
13162#endif
13163#ifdef FEAT_WAK
13164 "winaltkeys",
13165#endif
13166#ifdef FEAT_WRITEBACKUP
13167 "writebackup",
13168#endif
13169#ifdef FEAT_XIM
13170 "xim",
13171#endif
13172#ifdef FEAT_XFONTSET
13173 "xfontset",
13174#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013175#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013176 "xpm",
13177 "xpm_w32", /* for backward compatibility */
13178#else
13179# if defined(HAVE_XPM)
13180 "xpm",
13181# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013183#ifdef USE_XSMP
13184 "xsmp",
13185#endif
13186#ifdef USE_XSMP_INTERACT
13187 "xsmp_interact",
13188#endif
13189#ifdef FEAT_XCLIPBOARD
13190 "xterm_clipboard",
13191#endif
13192#ifdef FEAT_XTERM_SAVE
13193 "xterm_save",
13194#endif
13195#if defined(UNIX) && defined(FEAT_X11)
13196 "X11",
13197#endif
13198 NULL
13199 };
13200
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013201 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013202 for (i = 0; has_list[i] != NULL; ++i)
13203 if (STRICMP(name, has_list[i]) == 0)
13204 {
13205 n = TRUE;
13206 break;
13207 }
13208
13209 if (n == FALSE)
13210 {
13211 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013212 {
13213 if (name[5] == '-'
13214 && STRLEN(name) > 11
13215 && vim_isdigit(name[6])
13216 && vim_isdigit(name[8])
13217 && vim_isdigit(name[10]))
13218 {
13219 int major = atoi((char *)name + 6);
13220 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013221
13222 /* Expect "patch-9.9.01234". */
13223 n = (major < VIM_VERSION_MAJOR
13224 || (major == VIM_VERSION_MAJOR
13225 && (minor < VIM_VERSION_MINOR
13226 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013227 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013228 }
13229 else
13230 n = has_patch(atoi((char *)name + 5));
13231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013232 else if (STRICMP(name, "vim_starting") == 0)
13233 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013234#ifdef FEAT_MBYTE
13235 else if (STRICMP(name, "multi_byte_encoding") == 0)
13236 n = has_mbyte;
13237#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013238#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13239 else if (STRICMP(name, "balloon_multiline") == 0)
13240 n = multiline_balloon_available();
13241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242#ifdef DYNAMIC_TCL
13243 else if (STRICMP(name, "tcl") == 0)
13244 n = tcl_enabled(FALSE);
13245#endif
13246#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13247 else if (STRICMP(name, "iconv") == 0)
13248 n = iconv_enabled(FALSE);
13249#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013250#ifdef DYNAMIC_LUA
13251 else if (STRICMP(name, "lua") == 0)
13252 n = lua_enabled(FALSE);
13253#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013254#ifdef DYNAMIC_MZSCHEME
13255 else if (STRICMP(name, "mzscheme") == 0)
13256 n = mzscheme_enabled(FALSE);
13257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013258#ifdef DYNAMIC_RUBY
13259 else if (STRICMP(name, "ruby") == 0)
13260 n = ruby_enabled(FALSE);
13261#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013262#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013263#ifdef DYNAMIC_PYTHON
13264 else if (STRICMP(name, "python") == 0)
13265 n = python_enabled(FALSE);
13266#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013267#endif
13268#ifdef FEAT_PYTHON3
13269#ifdef DYNAMIC_PYTHON3
13270 else if (STRICMP(name, "python3") == 0)
13271 n = python3_enabled(FALSE);
13272#endif
13273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274#ifdef DYNAMIC_PERL
13275 else if (STRICMP(name, "perl") == 0)
13276 n = perl_enabled(FALSE);
13277#endif
13278#ifdef FEAT_GUI
13279 else if (STRICMP(name, "gui_running") == 0)
13280 n = (gui.in_use || gui.starting);
13281# ifdef FEAT_GUI_W32
13282 else if (STRICMP(name, "gui_win32s") == 0)
13283 n = gui_is_win32s();
13284# endif
13285# ifdef FEAT_BROWSE
13286 else if (STRICMP(name, "browse") == 0)
13287 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13288# endif
13289#endif
13290#ifdef FEAT_SYN_HL
13291 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013292 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293#endif
13294#if defined(WIN3264)
13295 else if (STRICMP(name, "win95") == 0)
13296 n = mch_windows95();
13297#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013298#ifdef FEAT_NETBEANS_INTG
13299 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013300 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302 }
13303
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013304 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013305}
13306
13307/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013308 * "has_key()" function
13309 */
13310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013311f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013312{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013313 if (argvars[0].v_type != VAR_DICT)
13314 {
13315 EMSG(_(e_dictreq));
13316 return;
13317 }
13318 if (argvars[0].vval.v_dict == NULL)
13319 return;
13320
13321 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013322 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013323}
13324
13325/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013326 * "haslocaldir()" function
13327 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013329f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013330{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013331 win_T *wp = NULL;
13332
13333 wp = find_tabwin(&argvars[0], &argvars[1]);
13334 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013335}
13336
13337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338 * "hasmapto()" function
13339 */
13340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013341f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013342{
13343 char_u *name;
13344 char_u *mode;
13345 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013346 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013348 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013349 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013350 mode = (char_u *)"nvo";
13351 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013352 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013353 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013354 if (argvars[2].v_type != VAR_UNKNOWN)
13355 abbr = get_tv_number(&argvars[2]);
13356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357
Bram Moolenaar2c932302006-03-18 21:42:09 +000013358 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013359 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013361 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362}
13363
13364/*
13365 * "histadd()" function
13366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013368f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369{
13370#ifdef FEAT_CMDHIST
13371 int histype;
13372 char_u *str;
13373 char_u buf[NUMBUFLEN];
13374#endif
13375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013376 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013377 if (check_restricted() || check_secure())
13378 return;
13379#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013380 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13381 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013382 if (histype >= 0)
13383 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013384 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013385 if (*str != NUL)
13386 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013387 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013388 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013389 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390 return;
13391 }
13392 }
13393#endif
13394}
13395
13396/*
13397 * "histdel()" function
13398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013400f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401{
13402#ifdef FEAT_CMDHIST
13403 int n;
13404 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013405 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013407 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13408 if (str == NULL)
13409 n = 0;
13410 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013411 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013412 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013413 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013414 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013415 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013416 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013417 else
13418 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013419 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013420 get_tv_string_buf(&argvars[1], buf));
13421 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013422#endif
13423}
13424
13425/*
13426 * "histget()" function
13427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013429f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430{
13431#ifdef FEAT_CMDHIST
13432 int type;
13433 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013434 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013436 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13437 if (str == NULL)
13438 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013440 {
13441 type = get_histtype(str);
13442 if (argvars[1].v_type == VAR_UNKNOWN)
13443 idx = get_history_idx(type);
13444 else
13445 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13446 /* -1 on type error */
13447 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013449#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013450 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013451#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013452 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013453}
13454
13455/*
13456 * "histnr()" function
13457 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013459f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460{
13461 int i;
13462
13463#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013464 char_u *history = get_tv_string_chk(&argvars[0]);
13465
13466 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467 if (i >= HIST_CMD && i < HIST_COUNT)
13468 i = get_history_idx(i);
13469 else
13470#endif
13471 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013472 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013473}
13474
13475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476 * "highlightID(name)" function
13477 */
13478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013479f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013481 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482}
13483
13484/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013485 * "highlight_exists()" function
13486 */
13487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013488f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013489{
13490 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13491}
13492
13493/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013494 * "hostname()" function
13495 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013497f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498{
13499 char_u hostname[256];
13500
13501 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013502 rettv->v_type = VAR_STRING;
13503 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504}
13505
13506/*
13507 * iconv() function
13508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013510f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013511{
13512#ifdef FEAT_MBYTE
13513 char_u buf1[NUMBUFLEN];
13514 char_u buf2[NUMBUFLEN];
13515 char_u *from, *to, *str;
13516 vimconv_T vimconv;
13517#endif
13518
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013519 rettv->v_type = VAR_STRING;
13520 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521
13522#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013523 str = get_tv_string(&argvars[0]);
13524 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13525 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013526 vimconv.vc_type = CONV_NONE;
13527 convert_setup(&vimconv, from, to);
13528
13529 /* If the encodings are equal, no conversion needed. */
13530 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013531 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013532 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013533 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534
13535 convert_setup(&vimconv, NULL, NULL);
13536 vim_free(from);
13537 vim_free(to);
13538#endif
13539}
13540
13541/*
13542 * "indent()" function
13543 */
13544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013545f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546{
13547 linenr_T lnum;
13548
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013549 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013550 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013551 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013552 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013553 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554}
13555
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013556/*
13557 * "index()" function
13558 */
13559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013560f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013561{
Bram Moolenaar33570922005-01-25 22:26:29 +000013562 list_T *l;
13563 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013564 long idx = 0;
13565 int ic = FALSE;
13566
13567 rettv->vval.v_number = -1;
13568 if (argvars[0].v_type != VAR_LIST)
13569 {
13570 EMSG(_(e_listreq));
13571 return;
13572 }
13573 l = argvars[0].vval.v_list;
13574 if (l != NULL)
13575 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013576 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013577 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013578 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013579 int error = FALSE;
13580
Bram Moolenaar758711c2005-02-02 23:11:38 +000013581 /* Start at specified item. Use the cached index that list_find()
13582 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013583 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013584 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013585 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013586 ic = get_tv_number_chk(&argvars[3], &error);
13587 if (error)
13588 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013589 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013590
Bram Moolenaar758711c2005-02-02 23:11:38 +000013591 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013592 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013593 {
13594 rettv->vval.v_number = idx;
13595 break;
13596 }
13597 }
13598}
13599
Bram Moolenaar071d4272004-06-13 20:20:40 +000013600static int inputsecret_flag = 0;
13601
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013602static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013603
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013605 * This function is used by f_input() and f_inputdialog() functions. The third
13606 * argument to f_input() specifies the type of completion to use at the
13607 * prompt. The third argument to f_inputdialog() specifies the value to return
13608 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609 */
13610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013611get_user_input(
13612 typval_T *argvars,
13613 typval_T *rettv,
13614 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013616 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617 char_u *p = NULL;
13618 int c;
13619 char_u buf[NUMBUFLEN];
13620 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013621 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013622 int xp_type = EXPAND_NOTHING;
13623 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013625 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013626 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627
13628#ifdef NO_CONSOLE_INPUT
13629 /* While starting up, there is no place to enter text. */
13630 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632#endif
13633
13634 cmd_silent = FALSE; /* Want to see the prompt. */
13635 if (prompt != NULL)
13636 {
13637 /* Only the part of the message after the last NL is considered as
13638 * prompt for the command line */
13639 p = vim_strrchr(prompt, '\n');
13640 if (p == NULL)
13641 p = prompt;
13642 else
13643 {
13644 ++p;
13645 c = *p;
13646 *p = NUL;
13647 msg_start();
13648 msg_clr_eos();
13649 msg_puts_attr(prompt, echo_attr);
13650 msg_didout = FALSE;
13651 msg_starthere();
13652 *p = c;
13653 }
13654 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013655
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013656 if (argvars[1].v_type != VAR_UNKNOWN)
13657 {
13658 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13659 if (defstr != NULL)
13660 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013661
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013662 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013663 {
13664 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013665 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013666 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013667
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013668 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013669 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013670
Bram Moolenaar4463f292005-09-25 22:20:24 +000013671 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13672 if (xp_name == NULL)
13673 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013674
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013675 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013676
Bram Moolenaar4463f292005-09-25 22:20:24 +000013677 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13678 &xp_arg) == FAIL)
13679 return;
13680 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013681 }
13682
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013683 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013684 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013685 int save_ex_normal_busy = ex_normal_busy;
13686 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013687 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013688 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13689 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013690 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013691 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013692 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013693 && argvars[1].v_type != VAR_UNKNOWN
13694 && argvars[2].v_type != VAR_UNKNOWN)
13695 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13696 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013697
13698 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013700 /* since the user typed this, no need to wait for return */
13701 need_wait_return = FALSE;
13702 msg_didout = FALSE;
13703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704 cmd_silent = cmd_silent_save;
13705}
13706
13707/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013708 * "input()" function
13709 * Also handles inputsecret() when inputsecret is set.
13710 */
13711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013712f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013713{
13714 get_user_input(argvars, rettv, FALSE);
13715}
13716
13717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718 * "inputdialog()" function
13719 */
13720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013721f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722{
13723#if defined(FEAT_GUI_TEXTDIALOG)
13724 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13725 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13726 {
13727 char_u *message;
13728 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013729 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013731 message = get_tv_string_chk(&argvars[0]);
13732 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013733 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013734 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735 else
13736 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013737 if (message != NULL && defstr != NULL
13738 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013739 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013740 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741 else
13742 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013743 if (message != NULL && defstr != NULL
13744 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013745 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013746 rettv->vval.v_string = vim_strsave(
13747 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013749 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013750 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013751 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752 }
13753 else
13754#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013755 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756}
13757
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013758/*
13759 * "inputlist()" function
13760 */
13761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013762f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013763{
13764 listitem_T *li;
13765 int selected;
13766 int mouse_used;
13767
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013768#ifdef NO_CONSOLE_INPUT
13769 /* While starting up, there is no place to enter text. */
13770 if (no_console_input())
13771 return;
13772#endif
13773 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13774 {
13775 EMSG2(_(e_listarg), "inputlist()");
13776 return;
13777 }
13778
13779 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013780 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013781 lines_left = Rows; /* avoid more prompt */
13782 msg_scroll = TRUE;
13783 msg_clr_eos();
13784
13785 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13786 {
13787 msg_puts(get_tv_string(&li->li_tv));
13788 msg_putchar('\n');
13789 }
13790
13791 /* Ask for choice. */
13792 selected = prompt_for_number(&mouse_used);
13793 if (mouse_used)
13794 selected -= lines_left;
13795
13796 rettv->vval.v_number = selected;
13797}
13798
13799
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13801
13802/*
13803 * "inputrestore()" function
13804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013806f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807{
13808 if (ga_userinput.ga_len > 0)
13809 {
13810 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13812 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013813 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814 }
13815 else if (p_verbose > 1)
13816 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013817 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013818 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819 }
13820}
13821
13822/*
13823 * "inputsave()" function
13824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013826f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013828 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829 if (ga_grow(&ga_userinput, 1) == OK)
13830 {
13831 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13832 + ga_userinput.ga_len);
13833 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013834 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835 }
13836 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013837 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838}
13839
13840/*
13841 * "inputsecret()" function
13842 */
13843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013844f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013845{
13846 ++cmdline_star;
13847 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013848 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849 --cmdline_star;
13850 --inputsecret_flag;
13851}
13852
13853/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013854 * "insert()" function
13855 */
13856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013857f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013858{
13859 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013860 listitem_T *item;
13861 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013862 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013863
13864 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013865 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013866 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013867 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013868 {
13869 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013870 before = get_tv_number_chk(&argvars[2], &error);
13871 if (error)
13872 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013873
Bram Moolenaar758711c2005-02-02 23:11:38 +000013874 if (before == l->lv_len)
13875 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013876 else
13877 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013878 item = list_find(l, before);
13879 if (item == NULL)
13880 {
13881 EMSGN(_(e_listidx), before);
13882 l = NULL;
13883 }
13884 }
13885 if (l != NULL)
13886 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013887 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013888 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013889 }
13890 }
13891}
13892
13893/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013894 * "invert(expr)" function
13895 */
13896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013897f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013898{
13899 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13900}
13901
13902/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903 * "isdirectory()" function
13904 */
13905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013906f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013908 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909}
13910
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013911/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013912 * "islocked()" function
13913 */
13914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013915f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013916{
13917 lval_T lv;
13918 char_u *end;
13919 dictitem_T *di;
13920
13921 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013922 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13923 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013924 if (end != NULL && lv.ll_name != NULL)
13925 {
13926 if (*end != NUL)
13927 EMSG(_(e_trailing));
13928 else
13929 {
13930 if (lv.ll_tv == NULL)
13931 {
13932 if (check_changedtick(lv.ll_name))
13933 rettv->vval.v_number = 1; /* always locked */
13934 else
13935 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013936 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013937 if (di != NULL)
13938 {
13939 /* Consider a variable locked when:
13940 * 1. the variable itself is locked
13941 * 2. the value of the variable is locked.
13942 * 3. the List or Dict value is locked.
13943 */
13944 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13945 || tv_islocked(&di->di_tv));
13946 }
13947 }
13948 }
13949 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013950 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013951 else if (lv.ll_newkey != NULL)
13952 EMSG2(_(e_dictkey), lv.ll_newkey);
13953 else if (lv.ll_list != NULL)
13954 /* List item. */
13955 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13956 else
13957 /* Dictionary item. */
13958 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13959 }
13960 }
13961
13962 clear_lval(&lv);
13963}
13964
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013965static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013966
13967/*
13968 * Turn a dict into a list:
13969 * "what" == 0: list of keys
13970 * "what" == 1: list of values
13971 * "what" == 2: list of items
13972 */
13973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013974dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013975{
Bram Moolenaar33570922005-01-25 22:26:29 +000013976 list_T *l2;
13977 dictitem_T *di;
13978 hashitem_T *hi;
13979 listitem_T *li;
13980 listitem_T *li2;
13981 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013982 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013983
Bram Moolenaar8c711452005-01-14 21:53:12 +000013984 if (argvars[0].v_type != VAR_DICT)
13985 {
13986 EMSG(_(e_dictreq));
13987 return;
13988 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013989 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013990 return;
13991
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013992 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013993 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013994
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013995 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013996 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013997 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013998 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013999 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014000 --todo;
14001 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014002
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014003 li = listitem_alloc();
14004 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014005 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014006 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014007
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014008 if (what == 0)
14009 {
14010 /* keys() */
14011 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014012 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014013 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14014 }
14015 else if (what == 1)
14016 {
14017 /* values() */
14018 copy_tv(&di->di_tv, &li->li_tv);
14019 }
14020 else
14021 {
14022 /* items() */
14023 l2 = list_alloc();
14024 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014025 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014026 li->li_tv.vval.v_list = l2;
14027 if (l2 == NULL)
14028 break;
14029 ++l2->lv_refcount;
14030
14031 li2 = listitem_alloc();
14032 if (li2 == NULL)
14033 break;
14034 list_append(l2, li2);
14035 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014036 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014037 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14038
14039 li2 = listitem_alloc();
14040 if (li2 == NULL)
14041 break;
14042 list_append(l2, li2);
14043 copy_tv(&di->di_tv, &li2->li_tv);
14044 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014045 }
14046 }
14047}
14048
14049/*
14050 * "items(dict)" function
14051 */
14052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014053f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014054{
14055 dict_list(argvars, rettv, 2);
14056}
14057
Bram Moolenaar071d4272004-06-13 20:20:40 +000014058/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014059 * "join()" function
14060 */
14061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014062f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014063{
14064 garray_T ga;
14065 char_u *sep;
14066
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014067 if (argvars[0].v_type != VAR_LIST)
14068 {
14069 EMSG(_(e_listreq));
14070 return;
14071 }
14072 if (argvars[0].vval.v_list == NULL)
14073 return;
14074 if (argvars[1].v_type == VAR_UNKNOWN)
14075 sep = (char_u *)" ";
14076 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014077 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014078
14079 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014080
14081 if (sep != NULL)
14082 {
14083 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014084 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014085 ga_append(&ga, NUL);
14086 rettv->vval.v_string = (char_u *)ga.ga_data;
14087 }
14088 else
14089 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014090}
14091
14092/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014093 * "jsondecode()" function
14094 */
14095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014096f_jsondecode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014097{
14098 js_read_T reader;
14099
14100 reader.js_buf = get_tv_string(&argvars[0]);
14101 reader.js_eof = TRUE;
14102 reader.js_used = 0;
14103 json_decode(&reader, rettv);
14104}
14105
14106/*
14107 * "jsonencode()" function
14108 */
14109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014110f_jsonencode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014111{
14112 rettv->v_type = VAR_STRING;
14113 rettv->vval.v_string = json_encode(&argvars[0]);
14114}
14115
14116/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014117 * "keys()" function
14118 */
14119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014120f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014121{
14122 dict_list(argvars, rettv, 0);
14123}
14124
14125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014126 * "last_buffer_nr()" function.
14127 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014129f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130{
14131 int n = 0;
14132 buf_T *buf;
14133
14134 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14135 if (n < buf->b_fnum)
14136 n = buf->b_fnum;
14137
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014138 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014139}
14140
14141/*
14142 * "len()" function
14143 */
14144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014145f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014146{
14147 switch (argvars[0].v_type)
14148 {
14149 case VAR_STRING:
14150 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014151 rettv->vval.v_number = (varnumber_T)STRLEN(
14152 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014153 break;
14154 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014155 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014156 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014157 case VAR_DICT:
14158 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14159 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014160 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014161 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014162 break;
14163 }
14164}
14165
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014166static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014167
14168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014169libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014170{
14171#ifdef FEAT_LIBCALL
14172 char_u *string_in;
14173 char_u **string_result;
14174 int nr_result;
14175#endif
14176
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014177 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014178 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014179 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014180
14181 if (check_restricted() || check_secure())
14182 return;
14183
14184#ifdef FEAT_LIBCALL
14185 /* The first two args must be strings, otherwise its meaningless */
14186 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14187 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014188 string_in = NULL;
14189 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014190 string_in = argvars[2].vval.v_string;
14191 if (type == VAR_NUMBER)
14192 string_result = NULL;
14193 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014194 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014195 if (mch_libcall(argvars[0].vval.v_string,
14196 argvars[1].vval.v_string,
14197 string_in,
14198 argvars[2].vval.v_number,
14199 string_result,
14200 &nr_result) == OK
14201 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014202 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014203 }
14204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014205}
14206
14207/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014208 * "libcall()" function
14209 */
14210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014211f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014212{
14213 libcall_common(argvars, rettv, VAR_STRING);
14214}
14215
14216/*
14217 * "libcallnr()" function
14218 */
14219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014220f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014221{
14222 libcall_common(argvars, rettv, VAR_NUMBER);
14223}
14224
14225/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014226 * "line(string)" function
14227 */
14228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014229f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014230{
14231 linenr_T lnum = 0;
14232 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014233 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014235 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236 if (fp != NULL)
14237 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014238 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239}
14240
14241/*
14242 * "line2byte(lnum)" function
14243 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014245f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014246{
14247#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014248 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014249#else
14250 linenr_T lnum;
14251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014252 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014253 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014254 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014256 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14257 if (rettv->vval.v_number >= 0)
14258 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014259#endif
14260}
14261
14262/*
14263 * "lispindent(lnum)" function
14264 */
14265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014266f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014267{
14268#ifdef FEAT_LISP
14269 pos_T pos;
14270 linenr_T lnum;
14271
14272 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014273 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14275 {
14276 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014277 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278 curwin->w_cursor = pos;
14279 }
14280 else
14281#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014282 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283}
14284
14285/*
14286 * "localtime()" function
14287 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014289f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014290{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014291 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014292}
14293
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014294static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295
14296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014297get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298{
14299 char_u *keys;
14300 char_u *which;
14301 char_u buf[NUMBUFLEN];
14302 char_u *keys_buf = NULL;
14303 char_u *rhs;
14304 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014305 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014306 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014307 mapblock_T *mp;
14308 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309
14310 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014311 rettv->v_type = VAR_STRING;
14312 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014313
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014314 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315 if (*keys == NUL)
14316 return;
14317
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014318 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014319 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014320 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014321 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014322 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014323 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014324 if (argvars[3].v_type != VAR_UNKNOWN)
14325 get_dict = get_tv_number(&argvars[3]);
14326 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014328 else
14329 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014330 if (which == NULL)
14331 return;
14332
Bram Moolenaar071d4272004-06-13 20:20:40 +000014333 mode = get_map_mode(&which, 0);
14334
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014335 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014336 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014338
14339 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014341 /* Return a string. */
14342 if (rhs != NULL)
14343 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344
Bram Moolenaarbd743252010-10-20 21:23:33 +020014345 }
14346 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14347 {
14348 /* Return a dictionary. */
14349 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14350 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14351 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352
Bram Moolenaarbd743252010-10-20 21:23:33 +020014353 dict_add_nr_str(dict, "lhs", 0L, lhs);
14354 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14355 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14356 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14357 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14358 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14359 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014360 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014361 dict_add_nr_str(dict, "mode", 0L, mapmode);
14362
14363 vim_free(lhs);
14364 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365 }
14366}
14367
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014368#ifdef FEAT_FLOAT
14369/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014370 * "log()" function
14371 */
14372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014373f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014374{
14375 float_T f;
14376
14377 rettv->v_type = VAR_FLOAT;
14378 if (get_float_arg(argvars, &f) == OK)
14379 rettv->vval.v_float = log(f);
14380 else
14381 rettv->vval.v_float = 0.0;
14382}
14383
14384/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014385 * "log10()" function
14386 */
14387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014388f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014389{
14390 float_T f;
14391
14392 rettv->v_type = VAR_FLOAT;
14393 if (get_float_arg(argvars, &f) == OK)
14394 rettv->vval.v_float = log10(f);
14395 else
14396 rettv->vval.v_float = 0.0;
14397}
14398#endif
14399
Bram Moolenaar1dced572012-04-05 16:54:08 +020014400#ifdef FEAT_LUA
14401/*
14402 * "luaeval()" function
14403 */
14404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014405f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020014406{
14407 char_u *str;
14408 char_u buf[NUMBUFLEN];
14409
14410 str = get_tv_string_buf(&argvars[0], buf);
14411 do_luaeval(str, argvars + 1, rettv);
14412}
14413#endif
14414
Bram Moolenaar071d4272004-06-13 20:20:40 +000014415/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014416 * "map()" function
14417 */
14418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014419f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014420{
14421 filter_map(argvars, rettv, TRUE);
14422}
14423
14424/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014425 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426 */
14427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014428f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014430 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014431}
14432
14433/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014434 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435 */
14436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014437f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014438{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014439 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440}
14441
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014442static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014443
14444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014445find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014447 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014448 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014449 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014450 char_u *pat;
14451 regmatch_T regmatch;
14452 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014453 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014454 char_u *save_cpo;
14455 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014456 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014457 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014458 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014459 list_T *l = NULL;
14460 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014461 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014462 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463
14464 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14465 save_cpo = p_cpo;
14466 p_cpo = (char_u *)"";
14467
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014468 rettv->vval.v_number = -1;
14469 if (type == 3)
14470 {
14471 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014472 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014473 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014474 }
14475 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014477 rettv->v_type = VAR_STRING;
14478 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014480
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014481 if (argvars[0].v_type == VAR_LIST)
14482 {
14483 if ((l = argvars[0].vval.v_list) == NULL)
14484 goto theend;
14485 li = l->lv_first;
14486 }
14487 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014488 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014489 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014490 len = (long)STRLEN(str);
14491 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014492
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014493 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14494 if (pat == NULL)
14495 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014496
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014497 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014499 int error = FALSE;
14500
14501 start = get_tv_number_chk(&argvars[2], &error);
14502 if (error)
14503 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014504 if (l != NULL)
14505 {
14506 li = list_find(l, start);
14507 if (li == NULL)
14508 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014509 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014510 }
14511 else
14512 {
14513 if (start < 0)
14514 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014515 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014516 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014517 /* When "count" argument is there ignore matches before "start",
14518 * otherwise skip part of the string. Differs when pattern is "^"
14519 * or "\<". */
14520 if (argvars[3].v_type != VAR_UNKNOWN)
14521 startcol = start;
14522 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014523 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014524 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014525 len -= start;
14526 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014527 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014528
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014529 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014530 nth = get_tv_number_chk(&argvars[3], &error);
14531 if (error)
14532 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014533 }
14534
14535 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14536 if (regmatch.regprog != NULL)
14537 {
14538 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014539
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014540 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014541 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014542 if (l != NULL)
14543 {
14544 if (li == NULL)
14545 {
14546 match = FALSE;
14547 break;
14548 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014549 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014550 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014551 if (str == NULL)
14552 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014553 }
14554
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014555 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014556
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014557 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014558 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014559 if (l == NULL && !match)
14560 break;
14561
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014562 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014563 if (l != NULL)
14564 {
14565 li = li->li_next;
14566 ++idx;
14567 }
14568 else
14569 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014570#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014571 startcol = (colnr_T)(regmatch.startp[0]
14572 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014573#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014574 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014575#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014576 if (startcol > (colnr_T)len
14577 || str + startcol <= regmatch.startp[0])
14578 {
14579 match = FALSE;
14580 break;
14581 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014582 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014583 }
14584
14585 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014587 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014588 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014589 int i;
14590
14591 /* return list with matched string and submatches */
14592 for (i = 0; i < NSUBEXP; ++i)
14593 {
14594 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014595 {
14596 if (list_append_string(rettv->vval.v_list,
14597 (char_u *)"", 0) == FAIL)
14598 break;
14599 }
14600 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014601 regmatch.startp[i],
14602 (int)(regmatch.endp[i] - regmatch.startp[i]))
14603 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014604 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014605 }
14606 }
14607 else if (type == 2)
14608 {
14609 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014610 if (l != NULL)
14611 copy_tv(&li->li_tv, rettv);
14612 else
14613 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014614 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014615 }
14616 else if (l != NULL)
14617 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014618 else
14619 {
14620 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014621 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622 (varnumber_T)(regmatch.startp[0] - str);
14623 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014624 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014626 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014627 }
14628 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014629 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014630 }
14631
14632theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014633 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014634 p_cpo = save_cpo;
14635}
14636
14637/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014638 * "match()" function
14639 */
14640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014641f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014642{
14643 find_some_match(argvars, rettv, 1);
14644}
14645
14646/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014647 * "matchadd()" function
14648 */
14649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014650f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014651{
14652#ifdef FEAT_SEARCH_EXTRA
14653 char_u buf[NUMBUFLEN];
14654 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14655 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14656 int prio = 10; /* default priority */
14657 int id = -1;
14658 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014659 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014660
14661 rettv->vval.v_number = -1;
14662
14663 if (grp == NULL || pat == NULL)
14664 return;
14665 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014666 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014667 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014668 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014669 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014670 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014671 if (argvars[4].v_type != VAR_UNKNOWN)
14672 {
14673 if (argvars[4].v_type != VAR_DICT)
14674 {
14675 EMSG(_(e_dictreq));
14676 return;
14677 }
14678 if (dict_find(argvars[4].vval.v_dict,
14679 (char_u *)"conceal", -1) != NULL)
14680 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14681 (char_u *)"conceal", FALSE);
14682 }
14683 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014684 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014685 if (error == TRUE)
14686 return;
14687 if (id >= 1 && id <= 3)
14688 {
14689 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14690 return;
14691 }
14692
Bram Moolenaar6561d522015-07-21 15:48:27 +020014693 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14694 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014695#endif
14696}
14697
14698/*
14699 * "matchaddpos()" function
14700 */
14701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014702f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020014703{
14704#ifdef FEAT_SEARCH_EXTRA
14705 char_u buf[NUMBUFLEN];
14706 char_u *group;
14707 int prio = 10;
14708 int id = -1;
14709 int error = FALSE;
14710 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014711 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014712
14713 rettv->vval.v_number = -1;
14714
14715 group = get_tv_string_buf_chk(&argvars[0], buf);
14716 if (group == NULL)
14717 return;
14718
14719 if (argvars[1].v_type != VAR_LIST)
14720 {
14721 EMSG2(_(e_listarg), "matchaddpos()");
14722 return;
14723 }
14724 l = argvars[1].vval.v_list;
14725 if (l == NULL)
14726 return;
14727
14728 if (argvars[2].v_type != VAR_UNKNOWN)
14729 {
14730 prio = get_tv_number_chk(&argvars[2], &error);
14731 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014732 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014733 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014734 if (argvars[4].v_type != VAR_UNKNOWN)
14735 {
14736 if (argvars[4].v_type != VAR_DICT)
14737 {
14738 EMSG(_(e_dictreq));
14739 return;
14740 }
14741 if (dict_find(argvars[4].vval.v_dict,
14742 (char_u *)"conceal", -1) != NULL)
14743 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14744 (char_u *)"conceal", FALSE);
14745 }
14746 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014747 }
14748 if (error == TRUE)
14749 return;
14750
14751 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14752 if (id == 1 || id == 2)
14753 {
14754 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14755 return;
14756 }
14757
Bram Moolenaar6561d522015-07-21 15:48:27 +020014758 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14759 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014760#endif
14761}
14762
14763/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014764 * "matcharg()" function
14765 */
14766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014767f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014768{
14769 if (rettv_list_alloc(rettv) == OK)
14770 {
14771#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014772 int id = get_tv_number(&argvars[0]);
14773 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014774
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014775 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014776 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014777 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14778 {
14779 list_append_string(rettv->vval.v_list,
14780 syn_id2name(m->hlg_id), -1);
14781 list_append_string(rettv->vval.v_list, m->pattern, -1);
14782 }
14783 else
14784 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014785 list_append_string(rettv->vval.v_list, NULL, -1);
14786 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014787 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014788 }
14789#endif
14790 }
14791}
14792
14793/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014794 * "matchdelete()" function
14795 */
14796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014797f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014798{
14799#ifdef FEAT_SEARCH_EXTRA
14800 rettv->vval.v_number = match_delete(curwin,
14801 (int)get_tv_number(&argvars[0]), TRUE);
14802#endif
14803}
14804
14805/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014806 * "matchend()" function
14807 */
14808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014809f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014810{
14811 find_some_match(argvars, rettv, 0);
14812}
14813
14814/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014815 * "matchlist()" function
14816 */
14817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014818f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014819{
14820 find_some_match(argvars, rettv, 3);
14821}
14822
14823/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014824 * "matchstr()" function
14825 */
14826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014827f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014828{
14829 find_some_match(argvars, rettv, 2);
14830}
14831
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014832static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014833
14834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014835max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014836{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014837 long n = 0;
14838 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014839 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014840
14841 if (argvars[0].v_type == VAR_LIST)
14842 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014843 list_T *l;
14844 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014845
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014846 l = argvars[0].vval.v_list;
14847 if (l != NULL)
14848 {
14849 li = l->lv_first;
14850 if (li != NULL)
14851 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014852 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014853 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014854 {
14855 li = li->li_next;
14856 if (li == NULL)
14857 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014858 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014859 if (domax ? i > n : i < n)
14860 n = i;
14861 }
14862 }
14863 }
14864 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014865 else if (argvars[0].v_type == VAR_DICT)
14866 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014867 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014868 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014869 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014870 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014871
14872 d = argvars[0].vval.v_dict;
14873 if (d != NULL)
14874 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014875 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014876 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014877 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014878 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014879 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014880 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014881 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014882 if (first)
14883 {
14884 n = i;
14885 first = FALSE;
14886 }
14887 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014888 n = i;
14889 }
14890 }
14891 }
14892 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014893 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014894 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014895 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014896}
14897
14898/*
14899 * "max()" function
14900 */
14901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014902f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014903{
14904 max_min(argvars, rettv, TRUE);
14905}
14906
14907/*
14908 * "min()" function
14909 */
14910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014911f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014912{
14913 max_min(argvars, rettv, FALSE);
14914}
14915
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014916static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014917
14918/*
14919 * Create the directory in which "dir" is located, and higher levels when
14920 * needed.
14921 */
14922 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010014923mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014924{
14925 char_u *p;
14926 char_u *updir;
14927 int r = FAIL;
14928
14929 /* Get end of directory name in "dir".
14930 * We're done when it's "/" or "c:/". */
14931 p = gettail_sep(dir);
14932 if (p <= get_past_head(dir))
14933 return OK;
14934
14935 /* If the directory exists we're done. Otherwise: create it.*/
14936 updir = vim_strnsave(dir, (int)(p - dir));
14937 if (updir == NULL)
14938 return FAIL;
14939 if (mch_isdir(updir))
14940 r = OK;
14941 else if (mkdir_recurse(updir, prot) == OK)
14942 r = vim_mkdir_emsg(updir, prot);
14943 vim_free(updir);
14944 return r;
14945}
14946
14947#ifdef vim_mkdir
14948/*
14949 * "mkdir()" function
14950 */
14951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014952f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014953{
14954 char_u *dir;
14955 char_u buf[NUMBUFLEN];
14956 int prot = 0755;
14957
14958 rettv->vval.v_number = FAIL;
14959 if (check_restricted() || check_secure())
14960 return;
14961
14962 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014963 if (*dir == NUL)
14964 rettv->vval.v_number = FAIL;
14965 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014966 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014967 if (*gettail(dir) == NUL)
14968 /* remove trailing slashes */
14969 *gettail_sep(dir) = NUL;
14970
14971 if (argvars[1].v_type != VAR_UNKNOWN)
14972 {
14973 if (argvars[2].v_type != VAR_UNKNOWN)
14974 prot = get_tv_number_chk(&argvars[2], NULL);
14975 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14976 mkdir_recurse(dir, prot);
14977 }
14978 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014979 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014980}
14981#endif
14982
Bram Moolenaar0d660222005-01-07 21:51:51 +000014983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014984 * "mode()" function
14985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014987f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014988{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014989 char_u buf[3];
14990
14991 buf[1] = NUL;
14992 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014993
Bram Moolenaar071d4272004-06-13 20:20:40 +000014994 if (VIsual_active)
14995 {
14996 if (VIsual_select)
14997 buf[0] = VIsual_mode + 's' - 'v';
14998 else
14999 buf[0] = VIsual_mode;
15000 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015001 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015002 || State == CONFIRM)
15003 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015004 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015005 if (State == ASKMORE)
15006 buf[1] = 'm';
15007 else if (State == CONFIRM)
15008 buf[1] = '?';
15009 }
15010 else if (State == EXTERNCMD)
15011 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015012 else if (State & INSERT)
15013 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015014#ifdef FEAT_VREPLACE
15015 if (State & VREPLACE_FLAG)
15016 {
15017 buf[0] = 'R';
15018 buf[1] = 'v';
15019 }
15020 else
15021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015022 if (State & REPLACE_FLAG)
15023 buf[0] = 'R';
15024 else
15025 buf[0] = 'i';
15026 }
15027 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015028 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015030 if (exmode_active)
15031 buf[1] = 'v';
15032 }
15033 else if (exmode_active)
15034 {
15035 buf[0] = 'c';
15036 buf[1] = 'e';
15037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015038 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015039 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015040 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015041 if (finish_op)
15042 buf[1] = 'o';
15043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015045 /* Clear out the minor mode when the argument is not a non-zero number or
15046 * non-empty string. */
15047 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015048 buf[1] = NUL;
15049
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015050 rettv->vval.v_string = vim_strsave(buf);
15051 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015052}
15053
Bram Moolenaar429fa852013-04-15 12:27:36 +020015054#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015055/*
15056 * "mzeval()" function
15057 */
15058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015059f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015060{
15061 char_u *str;
15062 char_u buf[NUMBUFLEN];
15063
15064 str = get_tv_string_buf(&argvars[0], buf);
15065 do_mzeval(str, rettv);
15066}
Bram Moolenaar75676462013-01-30 14:55:42 +010015067
15068 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015069mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015070{
15071 typval_T argvars[3];
15072
15073 argvars[0].v_type = VAR_STRING;
15074 argvars[0].vval.v_string = name;
15075 copy_tv(args, &argvars[1]);
15076 argvars[2].v_type = VAR_UNKNOWN;
15077 f_call(argvars, rettv);
15078 clear_tv(&argvars[1]);
15079}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015080#endif
15081
Bram Moolenaar071d4272004-06-13 20:20:40 +000015082/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015083 * "nextnonblank()" function
15084 */
15085 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015086f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015087{
15088 linenr_T lnum;
15089
15090 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15091 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015092 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015093 {
15094 lnum = 0;
15095 break;
15096 }
15097 if (*skipwhite(ml_get(lnum)) != NUL)
15098 break;
15099 }
15100 rettv->vval.v_number = lnum;
15101}
15102
15103/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015104 * "nr2char()" function
15105 */
15106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015107f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015108{
15109 char_u buf[NUMBUFLEN];
15110
15111#ifdef FEAT_MBYTE
15112 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015113 {
15114 int utf8 = 0;
15115
15116 if (argvars[1].v_type != VAR_UNKNOWN)
15117 utf8 = get_tv_number_chk(&argvars[1], NULL);
15118 if (utf8)
15119 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15120 else
15121 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015123 else
15124#endif
15125 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015126 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015127 buf[1] = NUL;
15128 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015129 rettv->v_type = VAR_STRING;
15130 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015131}
15132
15133/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015134 * "or(expr, expr)" function
15135 */
15136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015137f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015138{
15139 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15140 | get_tv_number_chk(&argvars[1], NULL);
15141}
15142
15143/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015144 * "pathshorten()" function
15145 */
15146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015147f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015148{
15149 char_u *p;
15150
15151 rettv->v_type = VAR_STRING;
15152 p = get_tv_string_chk(&argvars[0]);
15153 if (p == NULL)
15154 rettv->vval.v_string = NULL;
15155 else
15156 {
15157 p = vim_strsave(p);
15158 rettv->vval.v_string = p;
15159 if (p != NULL)
15160 shorten_dir(p);
15161 }
15162}
15163
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015164#ifdef FEAT_PERL
15165/*
15166 * "perleval()" function
15167 */
15168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015169f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015170{
15171 char_u *str;
15172 char_u buf[NUMBUFLEN];
15173
15174 str = get_tv_string_buf(&argvars[0], buf);
15175 do_perleval(str, rettv);
15176}
15177#endif
15178
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015179#ifdef FEAT_FLOAT
15180/*
15181 * "pow()" function
15182 */
15183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015184f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015185{
15186 float_T fx, fy;
15187
15188 rettv->v_type = VAR_FLOAT;
15189 if (get_float_arg(argvars, &fx) == OK
15190 && get_float_arg(&argvars[1], &fy) == OK)
15191 rettv->vval.v_float = pow(fx, fy);
15192 else
15193 rettv->vval.v_float = 0.0;
15194}
15195#endif
15196
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015197/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015198 * "prevnonblank()" function
15199 */
15200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015201f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015202{
15203 linenr_T lnum;
15204
15205 lnum = get_tv_lnum(argvars);
15206 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15207 lnum = 0;
15208 else
15209 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15210 --lnum;
15211 rettv->vval.v_number = lnum;
15212}
15213
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015214/* This dummy va_list is here because:
15215 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15216 * - locally in the function results in a "used before set" warning
15217 * - using va_start() to initialize it gives "function with fixed args" error */
15218static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015219
Bram Moolenaar8c711452005-01-14 21:53:12 +000015220/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015221 * "printf()" function
15222 */
15223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015224f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015225{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015226 char_u buf[NUMBUFLEN];
15227 int len;
15228 char_u *s;
15229 int saved_did_emsg = did_emsg;
15230 char *fmt;
15231
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015232 rettv->v_type = VAR_STRING;
15233 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015234
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015235 /* Get the required length, allocate the buffer and do it for real. */
15236 did_emsg = FALSE;
15237 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15238 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15239 if (!did_emsg)
15240 {
15241 s = alloc(len + 1);
15242 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015243 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015244 rettv->vval.v_string = s;
15245 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015246 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015247 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015248 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015249}
15250
15251/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015252 * "pumvisible()" function
15253 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015255f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015256{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015257#ifdef FEAT_INS_EXPAND
15258 if (pum_visible())
15259 rettv->vval.v_number = 1;
15260#endif
15261}
15262
Bram Moolenaardb913952012-06-29 12:54:53 +020015263#ifdef FEAT_PYTHON3
15264/*
15265 * "py3eval()" function
15266 */
15267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015268f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015269{
15270 char_u *str;
15271 char_u buf[NUMBUFLEN];
15272
15273 str = get_tv_string_buf(&argvars[0], buf);
15274 do_py3eval(str, rettv);
15275}
15276#endif
15277
15278#ifdef FEAT_PYTHON
15279/*
15280 * "pyeval()" function
15281 */
15282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015283f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015284{
15285 char_u *str;
15286 char_u buf[NUMBUFLEN];
15287
15288 str = get_tv_string_buf(&argvars[0], buf);
15289 do_pyeval(str, rettv);
15290}
15291#endif
15292
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015293/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015294 * "range()" function
15295 */
15296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015297f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015298{
15299 long start;
15300 long end;
15301 long stride = 1;
15302 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015303 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015304
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015305 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015306 if (argvars[1].v_type == VAR_UNKNOWN)
15307 {
15308 end = start - 1;
15309 start = 0;
15310 }
15311 else
15312 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015313 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015314 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015315 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015316 }
15317
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015318 if (error)
15319 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015320 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015321 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015322 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015323 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015324 else
15325 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015326 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015327 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015328 if (list_append_number(rettv->vval.v_list,
15329 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015330 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015331 }
15332}
15333
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015334/*
15335 * "readfile()" function
15336 */
15337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015338f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015339{
15340 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015341 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015342 char_u *fname;
15343 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015344 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15345 int io_size = sizeof(buf);
15346 int readlen; /* size of last fread() */
15347 char_u *prev = NULL; /* previously read bytes, if any */
15348 long prevlen = 0; /* length of data in prev */
15349 long prevsize = 0; /* size of prev buffer */
15350 long maxline = MAXLNUM;
15351 long cnt = 0;
15352 char_u *p; /* position in buf */
15353 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015354
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015355 if (argvars[1].v_type != VAR_UNKNOWN)
15356 {
15357 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15358 binary = TRUE;
15359 if (argvars[2].v_type != VAR_UNKNOWN)
15360 maxline = get_tv_number(&argvars[2]);
15361 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015362
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015363 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015364 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015365
15366 /* Always open the file in binary mode, library functions have a mind of
15367 * their own about CR-LF conversion. */
15368 fname = get_tv_string(&argvars[0]);
15369 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15370 {
15371 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15372 return;
15373 }
15374
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015375 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015376 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015377 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015378
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015379 /* This for loop processes what was read, but is also entered at end
15380 * of file so that either:
15381 * - an incomplete line gets written
15382 * - a "binary" file gets an empty line at the end if it ends in a
15383 * newline. */
15384 for (p = buf, start = buf;
15385 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15386 ++p)
15387 {
15388 if (*p == '\n' || readlen <= 0)
15389 {
15390 listitem_T *li;
15391 char_u *s = NULL;
15392 long_u len = p - start;
15393
15394 /* Finished a line. Remove CRs before NL. */
15395 if (readlen > 0 && !binary)
15396 {
15397 while (len > 0 && start[len - 1] == '\r')
15398 --len;
15399 /* removal may cross back to the "prev" string */
15400 if (len == 0)
15401 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15402 --prevlen;
15403 }
15404 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015405 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015406 else
15407 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015408 /* Change "prev" buffer to be the right size. This way
15409 * the bytes are only copied once, and very long lines are
15410 * allocated only once. */
15411 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015412 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015413 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015414 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015415 prev = NULL; /* the list will own the string */
15416 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015417 }
15418 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015419 if (s == NULL)
15420 {
15421 do_outofmem_msg((long_u) prevlen + len + 1);
15422 failed = TRUE;
15423 break;
15424 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015425
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015426 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015427 {
15428 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015429 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015430 break;
15431 }
15432 li->li_tv.v_type = VAR_STRING;
15433 li->li_tv.v_lock = 0;
15434 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015435 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015436
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015437 start = p + 1; /* step over newline */
15438 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015439 break;
15440 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015441 else if (*p == NUL)
15442 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015443#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015444 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15445 * when finding the BF and check the previous two bytes. */
15446 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015447 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015448 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15449 * + 1, these may be in the "prev" string. */
15450 char_u back1 = p >= buf + 1 ? p[-1]
15451 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15452 char_u back2 = p >= buf + 2 ? p[-2]
15453 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15454 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015455
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015456 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015457 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015458 char_u *dest = p - 2;
15459
15460 /* Usually a BOM is at the beginning of a file, and so at
15461 * the beginning of a line; then we can just step over it.
15462 */
15463 if (start == dest)
15464 start = p + 1;
15465 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015466 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015467 /* have to shuffle buf to close gap */
15468 int adjust_prevlen = 0;
15469
15470 if (dest < buf)
15471 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015472 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015473 dest = buf;
15474 }
15475 if (readlen > p - buf + 1)
15476 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15477 readlen -= 3 - adjust_prevlen;
15478 prevlen -= adjust_prevlen;
15479 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015480 }
15481 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015482 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015483#endif
15484 } /* for */
15485
15486 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15487 break;
15488 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015489 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015490 /* There's part of a line in buf, store it in "prev". */
15491 if (p - start + prevlen >= prevsize)
15492 {
15493 /* need bigger "prev" buffer */
15494 char_u *newprev;
15495
15496 /* A common use case is ordinary text files and "prev" gets a
15497 * fragment of a line, so the first allocation is made
15498 * small, to avoid repeatedly 'allocing' large and
15499 * 'reallocing' small. */
15500 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015501 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015502 else
15503 {
15504 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015505 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015506 prevsize = grow50pc > growmin ? grow50pc : growmin;
15507 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015508 newprev = prev == NULL ? alloc(prevsize)
15509 : vim_realloc(prev, prevsize);
15510 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015511 {
15512 do_outofmem_msg((long_u)prevsize);
15513 failed = TRUE;
15514 break;
15515 }
15516 prev = newprev;
15517 }
15518 /* Add the line part to end of "prev". */
15519 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015520 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015521 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015522 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015523
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015524 /*
15525 * For a negative line count use only the lines at the end of the file,
15526 * free the rest.
15527 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015528 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015529 while (cnt > -maxline)
15530 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015531 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015532 --cnt;
15533 }
15534
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015535 if (failed)
15536 {
15537 list_free(rettv->vval.v_list, TRUE);
15538 /* readfile doc says an empty list is returned on error */
15539 rettv->vval.v_list = list_alloc();
15540 }
15541
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015542 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015543 fclose(fd);
15544}
15545
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015546#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015547static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015548
15549/*
15550 * Convert a List to proftime_T.
15551 * Return FAIL when there is something wrong.
15552 */
15553 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015554list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015555{
15556 long n1, n2;
15557 int error = FALSE;
15558
15559 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15560 || arg->vval.v_list->lv_len != 2)
15561 return FAIL;
15562 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15563 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15564# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015565 tm->HighPart = n1;
15566 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015567# else
15568 tm->tv_sec = n1;
15569 tm->tv_usec = n2;
15570# endif
15571 return error ? FAIL : OK;
15572}
15573#endif /* FEAT_RELTIME */
15574
15575/*
15576 * "reltime()" function
15577 */
15578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015579f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015580{
15581#ifdef FEAT_RELTIME
15582 proftime_T res;
15583 proftime_T start;
15584
15585 if (argvars[0].v_type == VAR_UNKNOWN)
15586 {
15587 /* No arguments: get current time. */
15588 profile_start(&res);
15589 }
15590 else if (argvars[1].v_type == VAR_UNKNOWN)
15591 {
15592 if (list2proftime(&argvars[0], &res) == FAIL)
15593 return;
15594 profile_end(&res);
15595 }
15596 else
15597 {
15598 /* Two arguments: compute the difference. */
15599 if (list2proftime(&argvars[0], &start) == FAIL
15600 || list2proftime(&argvars[1], &res) == FAIL)
15601 return;
15602 profile_sub(&res, &start);
15603 }
15604
15605 if (rettv_list_alloc(rettv) == OK)
15606 {
15607 long n1, n2;
15608
15609# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015610 n1 = res.HighPart;
15611 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015612# else
15613 n1 = res.tv_sec;
15614 n2 = res.tv_usec;
15615# endif
15616 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15617 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15618 }
15619#endif
15620}
15621
15622/*
15623 * "reltimestr()" function
15624 */
15625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015626f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015627{
15628#ifdef FEAT_RELTIME
15629 proftime_T tm;
15630#endif
15631
15632 rettv->v_type = VAR_STRING;
15633 rettv->vval.v_string = NULL;
15634#ifdef FEAT_RELTIME
15635 if (list2proftime(&argvars[0], &tm) == OK)
15636 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15637#endif
15638}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015639
Bram Moolenaar0d660222005-01-07 21:51:51 +000015640#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015641static void make_connection(void);
15642static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015643
15644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015645make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015646{
15647 if (X_DISPLAY == NULL
15648# ifdef FEAT_GUI
15649 && !gui.in_use
15650# endif
15651 )
15652 {
15653 x_force_connect = TRUE;
15654 setup_term_clip();
15655 x_force_connect = FALSE;
15656 }
15657}
15658
15659 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015660check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015661{
15662 make_connection();
15663 if (X_DISPLAY == NULL)
15664 {
15665 EMSG(_("E240: No connection to Vim server"));
15666 return FAIL;
15667 }
15668 return OK;
15669}
15670#endif
15671
15672#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015673static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015674
15675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015676remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015677{
15678 char_u *server_name;
15679 char_u *keys;
15680 char_u *r = NULL;
15681 char_u buf[NUMBUFLEN];
15682# ifdef WIN32
15683 HWND w;
15684# else
15685 Window w;
15686# endif
15687
15688 if (check_restricted() || check_secure())
15689 return;
15690
15691# ifdef FEAT_X11
15692 if (check_connection() == FAIL)
15693 return;
15694# endif
15695
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015696 server_name = get_tv_string_chk(&argvars[0]);
15697 if (server_name == NULL)
15698 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015699 keys = get_tv_string_buf(&argvars[1], buf);
15700# ifdef WIN32
15701 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15702# else
15703 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15704 < 0)
15705# endif
15706 {
15707 if (r != NULL)
15708 EMSG(r); /* sending worked but evaluation failed */
15709 else
15710 EMSG2(_("E241: Unable to send to %s"), server_name);
15711 return;
15712 }
15713
15714 rettv->vval.v_string = r;
15715
15716 if (argvars[2].v_type != VAR_UNKNOWN)
15717 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015718 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015719 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015720 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015721
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015722 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015723 v.di_tv.v_type = VAR_STRING;
15724 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015725 idvar = get_tv_string_chk(&argvars[2]);
15726 if (idvar != NULL)
15727 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015728 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015729 }
15730}
15731#endif
15732
15733/*
15734 * "remote_expr()" function
15735 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015737f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015738{
15739 rettv->v_type = VAR_STRING;
15740 rettv->vval.v_string = NULL;
15741#ifdef FEAT_CLIENTSERVER
15742 remote_common(argvars, rettv, TRUE);
15743#endif
15744}
15745
15746/*
15747 * "remote_foreground()" function
15748 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015750f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015751{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015752#ifdef FEAT_CLIENTSERVER
15753# ifdef WIN32
15754 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015755 {
15756 char_u *server_name = get_tv_string_chk(&argvars[0]);
15757
15758 if (server_name != NULL)
15759 serverForeground(server_name);
15760 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015761# else
15762 /* Send a foreground() expression to the server. */
15763 argvars[1].v_type = VAR_STRING;
15764 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15765 argvars[2].v_type = VAR_UNKNOWN;
15766 remote_common(argvars, rettv, TRUE);
15767 vim_free(argvars[1].vval.v_string);
15768# endif
15769#endif
15770}
15771
Bram Moolenaar0d660222005-01-07 21:51:51 +000015772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015773f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015774{
15775#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015776 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015777 char_u *s = NULL;
15778# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015779 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015780# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015781 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015782
15783 if (check_restricted() || check_secure())
15784 {
15785 rettv->vval.v_number = -1;
15786 return;
15787 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015788 serverid = get_tv_string_chk(&argvars[0]);
15789 if (serverid == NULL)
15790 {
15791 rettv->vval.v_number = -1;
15792 return; /* type error; errmsg already given */
15793 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015794# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015795 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015796 if (n == 0)
15797 rettv->vval.v_number = -1;
15798 else
15799 {
15800 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15801 rettv->vval.v_number = (s != NULL);
15802 }
15803# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015804 if (check_connection() == FAIL)
15805 return;
15806
15807 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015808 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015809# endif
15810
15811 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15812 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015813 char_u *retvar;
15814
Bram Moolenaar33570922005-01-25 22:26:29 +000015815 v.di_tv.v_type = VAR_STRING;
15816 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015817 retvar = get_tv_string_chk(&argvars[1]);
15818 if (retvar != NULL)
15819 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015820 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015821 }
15822#else
15823 rettv->vval.v_number = -1;
15824#endif
15825}
15826
Bram Moolenaar0d660222005-01-07 21:51:51 +000015827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015828f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015829{
15830 char_u *r = NULL;
15831
15832#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015833 char_u *serverid = get_tv_string_chk(&argvars[0]);
15834
15835 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015836 {
15837# ifdef WIN32
15838 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015839 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015840
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015841 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015842 if (n != 0)
15843 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15844 if (r == NULL)
15845# else
15846 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015847 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015848# endif
15849 EMSG(_("E277: Unable to read a server reply"));
15850 }
15851#endif
15852 rettv->v_type = VAR_STRING;
15853 rettv->vval.v_string = r;
15854}
15855
15856/*
15857 * "remote_send()" function
15858 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015860f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015861{
15862 rettv->v_type = VAR_STRING;
15863 rettv->vval.v_string = NULL;
15864#ifdef FEAT_CLIENTSERVER
15865 remote_common(argvars, rettv, FALSE);
15866#endif
15867}
15868
15869/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015870 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015871 */
15872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015873f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015874{
Bram Moolenaar33570922005-01-25 22:26:29 +000015875 list_T *l;
15876 listitem_T *item, *item2;
15877 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015878 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015879 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015880 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015881 dict_T *d;
15882 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020015883 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015884
Bram Moolenaar8c711452005-01-14 21:53:12 +000015885 if (argvars[0].v_type == VAR_DICT)
15886 {
15887 if (argvars[2].v_type != VAR_UNKNOWN)
15888 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015889 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015890 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015891 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015892 key = get_tv_string_chk(&argvars[1]);
15893 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015894 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015895 di = dict_find(d, key, -1);
15896 if (di == NULL)
15897 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020015898 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
15899 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015900 {
15901 *rettv = di->di_tv;
15902 init_tv(&di->di_tv);
15903 dictitem_remove(d, di);
15904 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015905 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015906 }
15907 }
15908 else if (argvars[0].v_type != VAR_LIST)
15909 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015910 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015911 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015912 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015913 int error = FALSE;
15914
15915 idx = get_tv_number_chk(&argvars[1], &error);
15916 if (error)
15917 ; /* type error: do nothing, errmsg already given */
15918 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015919 EMSGN(_(e_listidx), idx);
15920 else
15921 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015922 if (argvars[2].v_type == VAR_UNKNOWN)
15923 {
15924 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015925 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015926 *rettv = item->li_tv;
15927 vim_free(item);
15928 }
15929 else
15930 {
15931 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015932 end = get_tv_number_chk(&argvars[2], &error);
15933 if (error)
15934 ; /* type error: do nothing */
15935 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015936 EMSGN(_(e_listidx), end);
15937 else
15938 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015939 int cnt = 0;
15940
15941 for (li = item; li != NULL; li = li->li_next)
15942 {
15943 ++cnt;
15944 if (li == item2)
15945 break;
15946 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015947 if (li == NULL) /* didn't find "item2" after "item" */
15948 EMSG(_(e_invrange));
15949 else
15950 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015951 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015952 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015953 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015954 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015955 l->lv_first = item;
15956 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015957 item->li_prev = NULL;
15958 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015959 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015960 }
15961 }
15962 }
15963 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015964 }
15965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015966}
15967
15968/*
15969 * "rename({from}, {to})" function
15970 */
15971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015972f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015973{
15974 char_u buf[NUMBUFLEN];
15975
15976 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015977 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015978 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015979 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15980 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015981}
15982
15983/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015984 * "repeat()" function
15985 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015987f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015988{
15989 char_u *p;
15990 int n;
15991 int slen;
15992 int len;
15993 char_u *r;
15994 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015995
15996 n = get_tv_number(&argvars[1]);
15997 if (argvars[0].v_type == VAR_LIST)
15998 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015999 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016000 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016001 if (list_extend(rettv->vval.v_list,
16002 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016003 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016004 }
16005 else
16006 {
16007 p = get_tv_string(&argvars[0]);
16008 rettv->v_type = VAR_STRING;
16009 rettv->vval.v_string = NULL;
16010
16011 slen = (int)STRLEN(p);
16012 len = slen * n;
16013 if (len <= 0)
16014 return;
16015
16016 r = alloc(len + 1);
16017 if (r != NULL)
16018 {
16019 for (i = 0; i < n; i++)
16020 mch_memmove(r + i * slen, p, (size_t)slen);
16021 r[len] = NUL;
16022 }
16023
16024 rettv->vval.v_string = r;
16025 }
16026}
16027
16028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016029 * "resolve()" function
16030 */
16031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016032f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016033{
16034 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016035#ifdef HAVE_READLINK
16036 char_u *buf = NULL;
16037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016038
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016039 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016040#ifdef FEAT_SHORTCUT
16041 {
16042 char_u *v = NULL;
16043
16044 v = mch_resolve_shortcut(p);
16045 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016046 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016048 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016049 }
16050#else
16051# ifdef HAVE_READLINK
16052 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016053 char_u *cpy;
16054 int len;
16055 char_u *remain = NULL;
16056 char_u *q;
16057 int is_relative_to_current = FALSE;
16058 int has_trailing_pathsep = FALSE;
16059 int limit = 100;
16060
16061 p = vim_strsave(p);
16062
16063 if (p[0] == '.' && (vim_ispathsep(p[1])
16064 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16065 is_relative_to_current = TRUE;
16066
16067 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016068 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016069 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016070 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016071 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016073
16074 q = getnextcomp(p);
16075 if (*q != NUL)
16076 {
16077 /* Separate the first path component in "p", and keep the
16078 * remainder (beginning with the path separator). */
16079 remain = vim_strsave(q - 1);
16080 q[-1] = NUL;
16081 }
16082
Bram Moolenaard9462e32011-04-11 21:35:11 +020016083 buf = alloc(MAXPATHL + 1);
16084 if (buf == NULL)
16085 goto fail;
16086
Bram Moolenaar071d4272004-06-13 20:20:40 +000016087 for (;;)
16088 {
16089 for (;;)
16090 {
16091 len = readlink((char *)p, (char *)buf, MAXPATHL);
16092 if (len <= 0)
16093 break;
16094 buf[len] = NUL;
16095
16096 if (limit-- == 0)
16097 {
16098 vim_free(p);
16099 vim_free(remain);
16100 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016101 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016102 goto fail;
16103 }
16104
16105 /* Ensure that the result will have a trailing path separator
16106 * if the argument has one. */
16107 if (remain == NULL && has_trailing_pathsep)
16108 add_pathsep(buf);
16109
16110 /* Separate the first path component in the link value and
16111 * concatenate the remainders. */
16112 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16113 if (*q != NUL)
16114 {
16115 if (remain == NULL)
16116 remain = vim_strsave(q - 1);
16117 else
16118 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016119 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016120 if (cpy != NULL)
16121 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016122 vim_free(remain);
16123 remain = cpy;
16124 }
16125 }
16126 q[-1] = NUL;
16127 }
16128
16129 q = gettail(p);
16130 if (q > p && *q == NUL)
16131 {
16132 /* Ignore trailing path separator. */
16133 q[-1] = NUL;
16134 q = gettail(p);
16135 }
16136 if (q > p && !mch_isFullName(buf))
16137 {
16138 /* symlink is relative to directory of argument */
16139 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16140 if (cpy != NULL)
16141 {
16142 STRCPY(cpy, p);
16143 STRCPY(gettail(cpy), buf);
16144 vim_free(p);
16145 p = cpy;
16146 }
16147 }
16148 else
16149 {
16150 vim_free(p);
16151 p = vim_strsave(buf);
16152 }
16153 }
16154
16155 if (remain == NULL)
16156 break;
16157
16158 /* Append the first path component of "remain" to "p". */
16159 q = getnextcomp(remain + 1);
16160 len = q - remain - (*q != NUL);
16161 cpy = vim_strnsave(p, STRLEN(p) + len);
16162 if (cpy != NULL)
16163 {
16164 STRNCAT(cpy, remain, len);
16165 vim_free(p);
16166 p = cpy;
16167 }
16168 /* Shorten "remain". */
16169 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016170 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171 else
16172 {
16173 vim_free(remain);
16174 remain = NULL;
16175 }
16176 }
16177
16178 /* If the result is a relative path name, make it explicitly relative to
16179 * the current directory if and only if the argument had this form. */
16180 if (!vim_ispathsep(*p))
16181 {
16182 if (is_relative_to_current
16183 && *p != NUL
16184 && !(p[0] == '.'
16185 && (p[1] == NUL
16186 || vim_ispathsep(p[1])
16187 || (p[1] == '.'
16188 && (p[2] == NUL
16189 || vim_ispathsep(p[2]))))))
16190 {
16191 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016192 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016193 if (cpy != NULL)
16194 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016195 vim_free(p);
16196 p = cpy;
16197 }
16198 }
16199 else if (!is_relative_to_current)
16200 {
16201 /* Strip leading "./". */
16202 q = p;
16203 while (q[0] == '.' && vim_ispathsep(q[1]))
16204 q += 2;
16205 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016206 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207 }
16208 }
16209
16210 /* Ensure that the result will have no trailing path separator
16211 * if the argument had none. But keep "/" or "//". */
16212 if (!has_trailing_pathsep)
16213 {
16214 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016215 if (after_pathsep(p, q))
16216 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016217 }
16218
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016219 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016220 }
16221# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016222 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016223# endif
16224#endif
16225
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016226 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227
16228#ifdef HAVE_READLINK
16229fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016230 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016231#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016232 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233}
16234
16235/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016236 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016237 */
16238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016239f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016240{
Bram Moolenaar33570922005-01-25 22:26:29 +000016241 list_T *l;
16242 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016243
Bram Moolenaar0d660222005-01-07 21:51:51 +000016244 if (argvars[0].v_type != VAR_LIST)
16245 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016246 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016247 && !tv_check_lock(l->lv_lock,
16248 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016249 {
16250 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016251 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016252 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016253 while (li != NULL)
16254 {
16255 ni = li->li_prev;
16256 list_append(l, li);
16257 li = ni;
16258 }
16259 rettv->vval.v_list = l;
16260 rettv->v_type = VAR_LIST;
16261 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016262 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016264}
16265
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016266#define SP_NOMOVE 0x01 /* don't move cursor */
16267#define SP_REPEAT 0x02 /* repeat to find outer pair */
16268#define SP_RETCOUNT 0x04 /* return matchcount */
16269#define SP_SETPCMARK 0x08 /* set previous context mark */
16270#define SP_START 0x10 /* accept match at start position */
16271#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16272#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016273#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016274
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016275static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016276
16277/*
16278 * Get flags for a search function.
16279 * Possibly sets "p_ws".
16280 * Returns BACKWARD, FORWARD or zero (for an error).
16281 */
16282 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016283get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016284{
16285 int dir = FORWARD;
16286 char_u *flags;
16287 char_u nbuf[NUMBUFLEN];
16288 int mask;
16289
16290 if (varp->v_type != VAR_UNKNOWN)
16291 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016292 flags = get_tv_string_buf_chk(varp, nbuf);
16293 if (flags == NULL)
16294 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016295 while (*flags != NUL)
16296 {
16297 switch (*flags)
16298 {
16299 case 'b': dir = BACKWARD; break;
16300 case 'w': p_ws = TRUE; break;
16301 case 'W': p_ws = FALSE; break;
16302 default: mask = 0;
16303 if (flagsp != NULL)
16304 switch (*flags)
16305 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016306 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016307 case 'e': mask = SP_END; break;
16308 case 'm': mask = SP_RETCOUNT; break;
16309 case 'n': mask = SP_NOMOVE; break;
16310 case 'p': mask = SP_SUBPAT; break;
16311 case 'r': mask = SP_REPEAT; break;
16312 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016313 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016314 }
16315 if (mask == 0)
16316 {
16317 EMSG2(_(e_invarg2), flags);
16318 dir = 0;
16319 }
16320 else
16321 *flagsp |= mask;
16322 }
16323 if (dir == 0)
16324 break;
16325 ++flags;
16326 }
16327 }
16328 return dir;
16329}
16330
Bram Moolenaar071d4272004-06-13 20:20:40 +000016331/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016332 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016333 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016334 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016335search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016337 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016338 char_u *pat;
16339 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016340 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016341 int save_p_ws = p_ws;
16342 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016343 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016344 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016345 proftime_T tm;
16346#ifdef FEAT_RELTIME
16347 long time_limit = 0;
16348#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016349 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016350 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016351
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016352 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016353 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016354 if (dir == 0)
16355 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016356 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016357 if (flags & SP_START)
16358 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016359 if (flags & SP_END)
16360 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016361 if (flags & SP_COLUMN)
16362 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016363
Bram Moolenaar76929292008-01-06 19:07:36 +000016364 /* Optional arguments: line number to stop searching and timeout. */
16365 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016366 {
16367 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16368 if (lnum_stop < 0)
16369 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016370#ifdef FEAT_RELTIME
16371 if (argvars[3].v_type != VAR_UNKNOWN)
16372 {
16373 time_limit = get_tv_number_chk(&argvars[3], NULL);
16374 if (time_limit < 0)
16375 goto theend;
16376 }
16377#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016378 }
16379
Bram Moolenaar76929292008-01-06 19:07:36 +000016380#ifdef FEAT_RELTIME
16381 /* Set the time limit, if there is one. */
16382 profile_setlimit(time_limit, &tm);
16383#endif
16384
Bram Moolenaar231334e2005-07-25 20:46:57 +000016385 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016386 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016387 * Check to make sure only those flags are set.
16388 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16389 * flags cannot be set. Check for that condition also.
16390 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016391 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016392 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016393 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016394 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016395 goto theend;
16396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016397
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016398 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016399 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016400 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016401 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016402 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016403 if (flags & SP_SUBPAT)
16404 retval = subpatnum;
16405 else
16406 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016407 if (flags & SP_SETPCMARK)
16408 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016409 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016410 if (match_pos != NULL)
16411 {
16412 /* Store the match cursor position */
16413 match_pos->lnum = pos.lnum;
16414 match_pos->col = pos.col + 1;
16415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016416 /* "/$" will put the cursor after the end of the line, may need to
16417 * correct that here */
16418 check_cursor();
16419 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016420
16421 /* If 'n' flag is used: restore cursor position. */
16422 if (flags & SP_NOMOVE)
16423 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016424 else
16425 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016426theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016427 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016428
16429 return retval;
16430}
16431
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016432#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016433
16434/*
16435 * round() is not in C90, use ceil() or floor() instead.
16436 */
16437 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010016438vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016439{
16440 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16441}
16442
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016443/*
16444 * "round({float})" function
16445 */
16446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016447f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016448{
16449 float_T f;
16450
16451 rettv->v_type = VAR_FLOAT;
16452 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016453 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016454 else
16455 rettv->vval.v_float = 0.0;
16456}
16457#endif
16458
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016459/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016460 * "screenattr()" function
16461 */
16462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016463f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016464{
16465 int row;
16466 int col;
16467 int c;
16468
16469 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16470 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16471 if (row < 0 || row >= screen_Rows
16472 || col < 0 || col >= screen_Columns)
16473 c = -1;
16474 else
16475 c = ScreenAttrs[LineOffset[row] + col];
16476 rettv->vval.v_number = c;
16477}
16478
16479/*
16480 * "screenchar()" function
16481 */
16482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016483f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016484{
16485 int row;
16486 int col;
16487 int off;
16488 int c;
16489
16490 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16491 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16492 if (row < 0 || row >= screen_Rows
16493 || col < 0 || col >= screen_Columns)
16494 c = -1;
16495 else
16496 {
16497 off = LineOffset[row] + col;
16498#ifdef FEAT_MBYTE
16499 if (enc_utf8 && ScreenLinesUC[off] != 0)
16500 c = ScreenLinesUC[off];
16501 else
16502#endif
16503 c = ScreenLines[off];
16504 }
16505 rettv->vval.v_number = c;
16506}
16507
16508/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016509 * "screencol()" function
16510 *
16511 * First column is 1 to be consistent with virtcol().
16512 */
16513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016514f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016515{
16516 rettv->vval.v_number = screen_screencol() + 1;
16517}
16518
16519/*
16520 * "screenrow()" function
16521 */
16522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016523f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016524{
16525 rettv->vval.v_number = screen_screenrow() + 1;
16526}
16527
16528/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016529 * "search()" function
16530 */
16531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016532f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016533{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016534 int flags = 0;
16535
16536 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016537}
16538
Bram Moolenaar071d4272004-06-13 20:20:40 +000016539/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016540 * "searchdecl()" function
16541 */
16542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016543f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016544{
16545 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016546 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016547 int error = FALSE;
16548 char_u *name;
16549
16550 rettv->vval.v_number = 1; /* default: FAIL */
16551
16552 name = get_tv_string_chk(&argvars[0]);
16553 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016554 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016555 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016556 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16557 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16558 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016559 if (!error && name != NULL)
16560 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016561 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016562}
16563
16564/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016565 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016566 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016567 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016568searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016569{
16570 char_u *spat, *mpat, *epat;
16571 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016572 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016573 int dir;
16574 int flags = 0;
16575 char_u nbuf1[NUMBUFLEN];
16576 char_u nbuf2[NUMBUFLEN];
16577 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016578 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016579 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016580 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016581
Bram Moolenaar071d4272004-06-13 20:20:40 +000016582 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016583 spat = get_tv_string_chk(&argvars[0]);
16584 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16585 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16586 if (spat == NULL || mpat == NULL || epat == NULL)
16587 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589 /* Handle the optional fourth argument: flags */
16590 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016591 if (dir == 0)
16592 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016593
16594 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016595 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16596 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016597 if ((flags & (SP_END | SP_SUBPAT)) != 0
16598 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016599 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016600 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016601 goto theend;
16602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016603
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016604 /* Using 'r' implies 'W', otherwise it doesn't work. */
16605 if (flags & SP_REPEAT)
16606 p_ws = FALSE;
16607
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016608 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016609 if (argvars[3].v_type == VAR_UNKNOWN
16610 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016611 skip = (char_u *)"";
16612 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016613 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016614 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016615 if (argvars[5].v_type != VAR_UNKNOWN)
16616 {
16617 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16618 if (lnum_stop < 0)
16619 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016620#ifdef FEAT_RELTIME
16621 if (argvars[6].v_type != VAR_UNKNOWN)
16622 {
16623 time_limit = get_tv_number_chk(&argvars[6], NULL);
16624 if (time_limit < 0)
16625 goto theend;
16626 }
16627#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016628 }
16629 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016630 if (skip == NULL)
16631 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016633 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016634 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016635
16636theend:
16637 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016638
16639 return retval;
16640}
16641
16642/*
16643 * "searchpair()" function
16644 */
16645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016646f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016647{
16648 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16649}
16650
16651/*
16652 * "searchpairpos()" function
16653 */
16654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016655f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016656{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016657 pos_T match_pos;
16658 int lnum = 0;
16659 int col = 0;
16660
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016661 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016662 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016663
16664 if (searchpair_cmn(argvars, &match_pos) > 0)
16665 {
16666 lnum = match_pos.lnum;
16667 col = match_pos.col;
16668 }
16669
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016670 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16671 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016672}
16673
16674/*
16675 * Search for a start/middle/end thing.
16676 * Used by searchpair(), see its documentation for the details.
16677 * Returns 0 or -1 for no match,
16678 */
16679 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010016680do_searchpair(
16681 char_u *spat, /* start pattern */
16682 char_u *mpat, /* middle pattern */
16683 char_u *epat, /* end pattern */
16684 int dir, /* BACKWARD or FORWARD */
16685 char_u *skip, /* skip expression */
16686 int flags, /* SP_SETPCMARK and other SP_ values */
16687 pos_T *match_pos,
16688 linenr_T lnum_stop, /* stop at this line if not zero */
16689 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016690{
16691 char_u *save_cpo;
16692 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16693 long retval = 0;
16694 pos_T pos;
16695 pos_T firstpos;
16696 pos_T foundpos;
16697 pos_T save_cursor;
16698 pos_T save_pos;
16699 int n;
16700 int r;
16701 int nest = 1;
16702 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016703 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016704 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016705
16706 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16707 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016708 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016709
Bram Moolenaar76929292008-01-06 19:07:36 +000016710#ifdef FEAT_RELTIME
16711 /* Set the time limit, if there is one. */
16712 profile_setlimit(time_limit, &tm);
16713#endif
16714
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016715 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16716 * start/middle/end (pat3, for the top pair). */
16717 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16718 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16719 if (pat2 == NULL || pat3 == NULL)
16720 goto theend;
16721 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16722 if (*mpat == NUL)
16723 STRCPY(pat3, pat2);
16724 else
16725 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16726 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016727 if (flags & SP_START)
16728 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016729
Bram Moolenaar071d4272004-06-13 20:20:40 +000016730 save_cursor = curwin->w_cursor;
16731 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016732 clearpos(&firstpos);
16733 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016734 pat = pat3;
16735 for (;;)
16736 {
16737 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016738 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016739 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16740 /* didn't find it or found the first match again: FAIL */
16741 break;
16742
16743 if (firstpos.lnum == 0)
16744 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016745 if (equalpos(pos, foundpos))
16746 {
16747 /* Found the same position again. Can happen with a pattern that
16748 * has "\zs" at the end and searching backwards. Advance one
16749 * character and try again. */
16750 if (dir == BACKWARD)
16751 decl(&pos);
16752 else
16753 incl(&pos);
16754 }
16755 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016757 /* clear the start flag to avoid getting stuck here */
16758 options &= ~SEARCH_START;
16759
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760 /* If the skip pattern matches, ignore this match. */
16761 if (*skip != NUL)
16762 {
16763 save_pos = curwin->w_cursor;
16764 curwin->w_cursor = pos;
16765 r = eval_to_bool(skip, &err, NULL, FALSE);
16766 curwin->w_cursor = save_pos;
16767 if (err)
16768 {
16769 /* Evaluating {skip} caused an error, break here. */
16770 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016771 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016772 break;
16773 }
16774 if (r)
16775 continue;
16776 }
16777
16778 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16779 {
16780 /* Found end when searching backwards or start when searching
16781 * forward: nested pair. */
16782 ++nest;
16783 pat = pat2; /* nested, don't search for middle */
16784 }
16785 else
16786 {
16787 /* Found end when searching forward or start when searching
16788 * backward: end of (nested) pair; or found middle in outer pair. */
16789 if (--nest == 1)
16790 pat = pat3; /* outer level, search for middle */
16791 }
16792
16793 if (nest == 0)
16794 {
16795 /* Found the match: return matchcount or line number. */
16796 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016797 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016798 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016799 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016800 if (flags & SP_SETPCMARK)
16801 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802 curwin->w_cursor = pos;
16803 if (!(flags & SP_REPEAT))
16804 break;
16805 nest = 1; /* search for next unmatched */
16806 }
16807 }
16808
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016809 if (match_pos != NULL)
16810 {
16811 /* Store the match cursor position */
16812 match_pos->lnum = curwin->w_cursor.lnum;
16813 match_pos->col = curwin->w_cursor.col + 1;
16814 }
16815
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016817 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016818 curwin->w_cursor = save_cursor;
16819
16820theend:
16821 vim_free(pat2);
16822 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016823 if (p_cpo == empty_option)
16824 p_cpo = save_cpo;
16825 else
16826 /* Darn, evaluating the {skip} expression changed the value. */
16827 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016828
16829 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830}
16831
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016832/*
16833 * "searchpos()" function
16834 */
16835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016836f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016837{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016838 pos_T match_pos;
16839 int lnum = 0;
16840 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016841 int n;
16842 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016843
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016844 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016845 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016846
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016847 n = search_cmn(argvars, &match_pos, &flags);
16848 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016849 {
16850 lnum = match_pos.lnum;
16851 col = match_pos.col;
16852 }
16853
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016854 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16855 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016856 if (flags & SP_SUBPAT)
16857 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016858}
16859
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016860#ifdef FEAT_CHANNEL
16861/*
16862 * common for "sendexpr()" and "sendraw()"
16863 * Returns the channel index if the caller should read the response.
16864 * Otherwise returns -1.
16865 */
16866 static int
16867send_common(typval_T *argvars, char_u *text, char *fun)
16868{
16869 int ch_idx;
16870 char_u *callback = NULL;
16871
16872 ch_idx = get_channel_arg(&argvars[0]);
16873 if (ch_idx < 0)
16874 return -1;
16875
16876 if (argvars[2].v_type != VAR_UNKNOWN)
16877 {
16878 callback = get_callback(&argvars[2]);
16879 if (callback == NULL)
16880 return -1;
16881 }
16882 /* Set the callback or clear it. An empty callback means no callback and
16883 * not reading the response. */
16884 channel_set_req_callback(ch_idx,
16885 callback != NULL && *callback == NUL ? NULL : callback);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016886
16887 if (channel_send(ch_idx, text, fun) == OK && callback == NULL)
16888 return ch_idx;
16889 return -1;
16890}
16891
16892/*
16893 * "sendexpr()" function
16894 */
16895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016896f_sendexpr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016897{
16898 char_u *text;
16899 char_u *resp;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010016900 typval_T typetv;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016901 int ch_idx;
16902
16903 /* return an empty string by default */
16904 rettv->v_type = VAR_STRING;
16905 rettv->vval.v_string = NULL;
16906
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010016907 text = json_encode_nr_expr(channel_get_id(), &argvars[1]);
16908 if (text == NULL)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016909 return;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016910
16911 ch_idx = send_common(argvars, text, "sendexpr");
16912 if (ch_idx >= 0)
16913 {
16914 /* TODO: read until the whole JSON message is received */
16915 /* TODO: only use the message with the right message ID */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010016916 /* TODO: check sequence number */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016917 resp = channel_read_block(ch_idx);
16918 if (resp != NULL)
16919 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010016920 channel_decode_json(resp, &typetv, rettv, NULL);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016921 vim_free(resp);
16922 }
16923 }
16924}
16925
16926/*
16927 * "sendraw()" function
16928 */
16929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016930f_sendraw(typval_T *argvars, typval_T *rettv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016931{
16932 char_u buf[NUMBUFLEN];
16933 char_u *text;
16934 int ch_idx;
16935
16936 /* return an empty string by default */
16937 rettv->v_type = VAR_STRING;
16938 rettv->vval.v_string = NULL;
16939
16940 text = get_tv_string_buf(&argvars[1], buf);
16941 ch_idx = send_common(argvars, text, "sendraw");
16942 if (ch_idx >= 0)
16943 rettv->vval.v_string = channel_read_block(ch_idx);
16944}
16945#endif
16946
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016947
Bram Moolenaar0d660222005-01-07 21:51:51 +000016948 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016949f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016950{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016951#ifdef FEAT_CLIENTSERVER
16952 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016953 char_u *server = get_tv_string_chk(&argvars[0]);
16954 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016955
Bram Moolenaar0d660222005-01-07 21:51:51 +000016956 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016957 if (server == NULL || reply == NULL)
16958 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016959 if (check_restricted() || check_secure())
16960 return;
16961# ifdef FEAT_X11
16962 if (check_connection() == FAIL)
16963 return;
16964# endif
16965
16966 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016967 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016968 EMSG(_("E258: Unable to send to client"));
16969 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016970 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016971 rettv->vval.v_number = 0;
16972#else
16973 rettv->vval.v_number = -1;
16974#endif
16975}
16976
Bram Moolenaar0d660222005-01-07 21:51:51 +000016977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016978f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016979{
16980 char_u *r = NULL;
16981
16982#ifdef FEAT_CLIENTSERVER
16983# ifdef WIN32
16984 r = serverGetVimNames();
16985# else
16986 make_connection();
16987 if (X_DISPLAY != NULL)
16988 r = serverGetVimNames(X_DISPLAY);
16989# endif
16990#endif
16991 rettv->v_type = VAR_STRING;
16992 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016993}
16994
16995/*
16996 * "setbufvar()" function
16997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016999f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000{
17001 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017004 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017005 char_u nbuf[NUMBUFLEN];
17006
17007 if (check_restricted() || check_secure())
17008 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017009 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17010 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017011 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017012 varp = &argvars[2];
17013
17014 if (buf != NULL && varname != NULL && varp != NULL)
17015 {
17016 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017017 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018
17019 if (*varname == '&')
17020 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017021 long numval;
17022 char_u *strval;
17023 int error = FALSE;
17024
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017026 numval = get_tv_number_chk(varp, &error);
17027 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017028 if (!error && strval != NULL)
17029 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030 }
17031 else
17032 {
17033 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17034 if (bufvarname != NULL)
17035 {
17036 STRCPY(bufvarname, "b:");
17037 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017038 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039 vim_free(bufvarname);
17040 }
17041 }
17042
17043 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017046}
17047
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017049f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017050{
17051 dict_T *d;
17052 dictitem_T *di;
17053 char_u *csearch;
17054
17055 if (argvars[0].v_type != VAR_DICT)
17056 {
17057 EMSG(_(e_dictreq));
17058 return;
17059 }
17060
17061 if ((d = argvars[0].vval.v_dict) != NULL)
17062 {
17063 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17064 if (csearch != NULL)
17065 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017066#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017067 if (enc_utf8)
17068 {
17069 int pcc[MAX_MCO];
17070 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017071
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017072 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17073 }
17074 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017075#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017076 set_last_csearch(PTR2CHAR(csearch),
17077 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017078 }
17079
17080 di = dict_find(d, (char_u *)"forward", -1);
17081 if (di != NULL)
17082 set_csearch_direction(get_tv_number(&di->di_tv)
17083 ? FORWARD : BACKWARD);
17084
17085 di = dict_find(d, (char_u *)"until", -1);
17086 if (di != NULL)
17087 set_csearch_until(!!get_tv_number(&di->di_tv));
17088 }
17089}
17090
Bram Moolenaar071d4272004-06-13 20:20:40 +000017091/*
17092 * "setcmdpos()" function
17093 */
17094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017095f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017096{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017097 int pos = (int)get_tv_number(&argvars[0]) - 1;
17098
17099 if (pos >= 0)
17100 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017101}
17102
17103/*
17104 * "setline()" function
17105 */
17106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017107f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017108{
17109 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017110 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017111 list_T *l = NULL;
17112 listitem_T *li = NULL;
17113 long added = 0;
17114 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017116 lnum = get_tv_lnum(&argvars[0]);
17117 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017118 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017119 l = argvars[1].vval.v_list;
17120 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017121 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017122 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017123 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017124
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017125 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017126 for (;;)
17127 {
17128 if (l != NULL)
17129 {
17130 /* list argument, get next string */
17131 if (li == NULL)
17132 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017133 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017134 li = li->li_next;
17135 }
17136
17137 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017138 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017139 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017140
17141 /* When coming here from Insert mode, sync undo, so that this can be
17142 * undone separately from what was previously inserted. */
17143 if (u_sync_once == 2)
17144 {
17145 u_sync_once = 1; /* notify that u_sync() was called */
17146 u_sync(TRUE);
17147 }
17148
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017149 if (lnum <= curbuf->b_ml.ml_line_count)
17150 {
17151 /* existing line, replace it */
17152 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17153 {
17154 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017155 if (lnum == curwin->w_cursor.lnum)
17156 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017157 rettv->vval.v_number = 0; /* OK */
17158 }
17159 }
17160 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17161 {
17162 /* lnum is one past the last line, append the line */
17163 ++added;
17164 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17165 rettv->vval.v_number = 0; /* OK */
17166 }
17167
17168 if (l == NULL) /* only one string argument */
17169 break;
17170 ++lnum;
17171 }
17172
17173 if (added > 0)
17174 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175}
17176
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017177static 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 +000017178
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017180 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017181 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017183set_qf_ll_list(
17184 win_T *wp UNUSED,
17185 typval_T *list_arg UNUSED,
17186 typval_T *action_arg UNUSED,
17187 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017188{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017189#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017190 char_u *act;
17191 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017192#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017193
Bram Moolenaar2641f772005-03-25 21:58:17 +000017194 rettv->vval.v_number = -1;
17195
17196#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017197 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017198 EMSG(_(e_listreq));
17199 else
17200 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017201 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017202
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017203 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017204 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017205 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017206 if (act == NULL)
17207 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017208 if (*act == 'a' || *act == 'r')
17209 action = *act;
17210 }
17211
Bram Moolenaar81484f42012-12-05 15:16:47 +010017212 if (l != NULL && set_errorlist(wp, l, action,
17213 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017214 rettv->vval.v_number = 0;
17215 }
17216#endif
17217}
17218
17219/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017220 * "setloclist()" function
17221 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017223f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017224{
17225 win_T *win;
17226
17227 rettv->vval.v_number = -1;
17228
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017229 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017230 if (win != NULL)
17231 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17232}
17233
17234/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017235 * "setmatches()" function
17236 */
17237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017238f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017239{
17240#ifdef FEAT_SEARCH_EXTRA
17241 list_T *l;
17242 listitem_T *li;
17243 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017244 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017245
17246 rettv->vval.v_number = -1;
17247 if (argvars[0].v_type != VAR_LIST)
17248 {
17249 EMSG(_(e_listreq));
17250 return;
17251 }
17252 if ((l = argvars[0].vval.v_list) != NULL)
17253 {
17254
17255 /* To some extent make sure that we are dealing with a list from
17256 * "getmatches()". */
17257 li = l->lv_first;
17258 while (li != NULL)
17259 {
17260 if (li->li_tv.v_type != VAR_DICT
17261 || (d = li->li_tv.vval.v_dict) == NULL)
17262 {
17263 EMSG(_(e_invarg));
17264 return;
17265 }
17266 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017267 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17268 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017269 && dict_find(d, (char_u *)"priority", -1) != NULL
17270 && dict_find(d, (char_u *)"id", -1) != NULL))
17271 {
17272 EMSG(_(e_invarg));
17273 return;
17274 }
17275 li = li->li_next;
17276 }
17277
17278 clear_matches(curwin);
17279 li = l->lv_first;
17280 while (li != NULL)
17281 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017282 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017283 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017284 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017285 char_u *group;
17286 int priority;
17287 int id;
17288 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017289
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017290 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017291 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17292 {
17293 if (s == NULL)
17294 {
17295 s = list_alloc();
17296 if (s == NULL)
17297 return;
17298 }
17299
17300 /* match from matchaddpos() */
17301 for (i = 1; i < 9; i++)
17302 {
17303 sprintf((char *)buf, (char *)"pos%d", i);
17304 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17305 {
17306 if (di->di_tv.v_type != VAR_LIST)
17307 return;
17308
17309 list_append_tv(s, &di->di_tv);
17310 s->lv_refcount++;
17311 }
17312 else
17313 break;
17314 }
17315 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017316
17317 group = get_dict_string(d, (char_u *)"group", FALSE);
17318 priority = (int)get_dict_number(d, (char_u *)"priority");
17319 id = (int)get_dict_number(d, (char_u *)"id");
17320 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17321 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17322 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017323 if (i == 0)
17324 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017325 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017326 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017327 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017328 }
17329 else
17330 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017331 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017332 list_unref(s);
17333 s = NULL;
17334 }
17335
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017336 li = li->li_next;
17337 }
17338 rettv->vval.v_number = 0;
17339 }
17340#endif
17341}
17342
17343/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017344 * "setpos()" function
17345 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017347f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017348{
17349 pos_T pos;
17350 int fnum;
17351 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017352 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017353
Bram Moolenaar08250432008-02-13 11:42:46 +000017354 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017355 name = get_tv_string_chk(argvars);
17356 if (name != NULL)
17357 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017358 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017359 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017360 if (--pos.col < 0)
17361 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017362 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017363 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017364 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017365 if (fnum == curbuf->b_fnum)
17366 {
17367 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017368 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017369 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017370 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017371 curwin->w_set_curswant = FALSE;
17372 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017373 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017374 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017375 }
17376 else
17377 EMSG(_(e_invarg));
17378 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017379 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17380 {
17381 /* set mark */
17382 if (setmark_pos(name[1], &pos, fnum) == OK)
17383 rettv->vval.v_number = 0;
17384 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017385 else
17386 EMSG(_(e_invarg));
17387 }
17388 }
17389}
17390
17391/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017392 * "setqflist()" function
17393 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017395f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017396{
17397 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17398}
17399
17400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401 * "setreg()" function
17402 */
17403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017404f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405{
17406 int regname;
17407 char_u *strregname;
17408 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017409 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017410 int append;
17411 char_u yank_type;
17412 long block_len;
17413
17414 block_len = -1;
17415 yank_type = MAUTO;
17416 append = FALSE;
17417
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017418 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017419 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017420
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017421 if (strregname == NULL)
17422 return; /* type error; errmsg already given */
17423 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424 if (regname == 0 || regname == '@')
17425 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017427 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017428 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017429 stropt = get_tv_string_chk(&argvars[2]);
17430 if (stropt == NULL)
17431 return; /* type error */
17432 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017433 switch (*stropt)
17434 {
17435 case 'a': case 'A': /* append */
17436 append = TRUE;
17437 break;
17438 case 'v': case 'c': /* character-wise selection */
17439 yank_type = MCHAR;
17440 break;
17441 case 'V': case 'l': /* line-wise selection */
17442 yank_type = MLINE;
17443 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017444 case 'b': case Ctrl_V: /* block-wise selection */
17445 yank_type = MBLOCK;
17446 if (VIM_ISDIGIT(stropt[1]))
17447 {
17448 ++stropt;
17449 block_len = getdigits(&stropt) - 1;
17450 --stropt;
17451 }
17452 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453 }
17454 }
17455
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017456 if (argvars[1].v_type == VAR_LIST)
17457 {
17458 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017459 char_u **allocval;
17460 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017461 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017462 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017463 int len = argvars[1].vval.v_list->lv_len;
17464 listitem_T *li;
17465
Bram Moolenaar7d647822014-04-05 21:28:56 +020017466 /* First half: use for pointers to result lines; second half: use for
17467 * pointers to allocated copies. */
17468 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017469 if (lstval == NULL)
17470 return;
17471 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017472 allocval = lstval + len + 2;
17473 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017474
17475 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17476 li = li->li_next)
17477 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017478 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017479 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017480 goto free_lstval;
17481 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017482 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017483 /* Need to make a copy, next get_tv_string_buf_chk() will
17484 * overwrite the string. */
17485 strval = vim_strsave(buf);
17486 if (strval == NULL)
17487 goto free_lstval;
17488 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017489 }
17490 *curval++ = strval;
17491 }
17492 *curval++ = NULL;
17493
17494 write_reg_contents_lst(regname, lstval, -1,
17495 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017496free_lstval:
17497 while (curallocval > allocval)
17498 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017499 vim_free(lstval);
17500 }
17501 else
17502 {
17503 strval = get_tv_string_chk(&argvars[1]);
17504 if (strval == NULL)
17505 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017506 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017507 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017508 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017509 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017510}
17511
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017512/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017513 * "settabvar()" function
17514 */
17515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017516f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017517{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017518#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017519 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017520 tabpage_T *tp;
17521#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017522 char_u *varname, *tabvarname;
17523 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017524
17525 rettv->vval.v_number = 0;
17526
17527 if (check_restricted() || check_secure())
17528 return;
17529
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017530#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017531 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017532#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017533 varname = get_tv_string_chk(&argvars[1]);
17534 varp = &argvars[2];
17535
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017536 if (varname != NULL && varp != NULL
17537#ifdef FEAT_WINDOWS
17538 && tp != NULL
17539#endif
17540 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017541 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017542#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017543 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017544 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017545#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017546
17547 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17548 if (tabvarname != NULL)
17549 {
17550 STRCPY(tabvarname, "t:");
17551 STRCPY(tabvarname + 2, varname);
17552 set_var(tabvarname, varp, TRUE);
17553 vim_free(tabvarname);
17554 }
17555
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017556#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017557 /* Restore current tabpage */
17558 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017559 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017560#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017561 }
17562}
17563
17564/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017565 * "settabwinvar()" function
17566 */
17567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017568f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017569{
17570 setwinvar(argvars, rettv, 1);
17571}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017572
17573/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017574 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017577f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017579 setwinvar(argvars, rettv, 0);
17580}
17581
17582/*
17583 * "setwinvar()" and "settabwinvar()" functions
17584 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017585
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017587setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017588{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589 win_T *win;
17590#ifdef FEAT_WINDOWS
17591 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017592 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017593 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017594#endif
17595 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017596 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017597 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017598 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017599
17600 if (check_restricted() || check_secure())
17601 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017602
17603#ifdef FEAT_WINDOWS
17604 if (off == 1)
17605 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17606 else
17607 tp = curtab;
17608#endif
17609 win = find_win_by_nr(&argvars[off], tp);
17610 varname = get_tv_string_chk(&argvars[off + 1]);
17611 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612
17613 if (win != NULL && varname != NULL && varp != NULL)
17614 {
17615#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017616 need_switch_win = !(tp == curtab && win == curwin);
17617 if (!need_switch_win
17618 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017620 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017621 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017623 long numval;
17624 char_u *strval;
17625 int error = FALSE;
17626
17627 ++varname;
17628 numval = get_tv_number_chk(varp, &error);
17629 strval = get_tv_string_buf_chk(varp, nbuf);
17630 if (!error && strval != NULL)
17631 set_option_value(varname, numval, strval, OPT_LOCAL);
17632 }
17633 else
17634 {
17635 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17636 if (winvarname != NULL)
17637 {
17638 STRCPY(winvarname, "w:");
17639 STRCPY(winvarname + 2, varname);
17640 set_var(winvarname, varp, TRUE);
17641 vim_free(winvarname);
17642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017643 }
17644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017646 if (need_switch_win)
17647 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017648#endif
17649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017650}
17651
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017652#ifdef FEAT_CRYPT
17653/*
17654 * "sha256({string})" function
17655 */
17656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017657f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017658{
17659 char_u *p;
17660
17661 p = get_tv_string(&argvars[0]);
17662 rettv->vval.v_string = vim_strsave(
17663 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17664 rettv->v_type = VAR_STRING;
17665}
17666#endif /* FEAT_CRYPT */
17667
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017669 * "shellescape({string})" function
17670 */
17671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017672f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017673{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017674 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017675 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017676 rettv->v_type = VAR_STRING;
17677}
17678
17679/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017680 * shiftwidth() function
17681 */
17682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017683f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017684{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017685 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017686}
17687
17688/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017689 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017690 */
17691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017692f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017693{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017694 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017695
Bram Moolenaar0d660222005-01-07 21:51:51 +000017696 p = get_tv_string(&argvars[0]);
17697 rettv->vval.v_string = vim_strsave(p);
17698 simplify_filename(rettv->vval.v_string); /* simplify in place */
17699 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017700}
17701
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017702#ifdef FEAT_FLOAT
17703/*
17704 * "sin()" function
17705 */
17706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017707f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017708{
17709 float_T f;
17710
17711 rettv->v_type = VAR_FLOAT;
17712 if (get_float_arg(argvars, &f) == OK)
17713 rettv->vval.v_float = sin(f);
17714 else
17715 rettv->vval.v_float = 0.0;
17716}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017717
17718/*
17719 * "sinh()" function
17720 */
17721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017722f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017723{
17724 float_T f;
17725
17726 rettv->v_type = VAR_FLOAT;
17727 if (get_float_arg(argvars, &f) == OK)
17728 rettv->vval.v_float = sinh(f);
17729 else
17730 rettv->vval.v_float = 0.0;
17731}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017732#endif
17733
Bram Moolenaar0d660222005-01-07 21:51:51 +000017734static int
17735#ifdef __BORLANDC__
17736 _RTLENTRYF
17737#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017738 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017739static int
17740#ifdef __BORLANDC__
17741 _RTLENTRYF
17742#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017743 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017744
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017745/* struct used in the array that's given to qsort() */
17746typedef struct
17747{
17748 listitem_T *item;
17749 int idx;
17750} sortItem_T;
17751
Bram Moolenaar0d660222005-01-07 21:51:51 +000017752static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017753static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017754static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017755#ifdef FEAT_FLOAT
17756static int item_compare_float;
17757#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017758static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017759static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017760static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017761static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017762static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017763#define ITEM_COMPARE_FAIL 999
17764
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017766 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017767 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017768 static int
17769#ifdef __BORLANDC__
17770_RTLENTRYF
17771#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010017772item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017774 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017775 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017776 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017777 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017778 int res;
17779 char_u numbuf1[NUMBUFLEN];
17780 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017781
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017782 si1 = (sortItem_T *)s1;
17783 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017784 tv1 = &si1->item->li_tv;
17785 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017786
17787 if (item_compare_numbers)
17788 {
17789 long v1 = get_tv_number(tv1);
17790 long v2 = get_tv_number(tv2);
17791
17792 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17793 }
17794
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017795#ifdef FEAT_FLOAT
17796 if (item_compare_float)
17797 {
17798 float_T v1 = get_tv_float(tv1);
17799 float_T v2 = get_tv_float(tv2);
17800
17801 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17802 }
17803#endif
17804
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017805 /* tv2string() puts quotes around a string and allocates memory. Don't do
17806 * that for string variables. Use a single quote when comparing with a
17807 * non-string to do what the docs promise. */
17808 if (tv1->v_type == VAR_STRING)
17809 {
17810 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17811 p1 = (char_u *)"'";
17812 else
17813 p1 = tv1->vval.v_string;
17814 }
17815 else
17816 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17817 if (tv2->v_type == VAR_STRING)
17818 {
17819 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17820 p2 = (char_u *)"'";
17821 else
17822 p2 = tv2->vval.v_string;
17823 }
17824 else
17825 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017826 if (p1 == NULL)
17827 p1 = (char_u *)"";
17828 if (p2 == NULL)
17829 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017830 if (!item_compare_numeric)
17831 {
17832 if (item_compare_ic)
17833 res = STRICMP(p1, p2);
17834 else
17835 res = STRCMP(p1, p2);
17836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017837 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017838 {
17839 double n1, n2;
17840 n1 = strtod((char *)p1, (char **)&p1);
17841 n2 = strtod((char *)p2, (char **)&p2);
17842 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17843 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017844
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017845 /* When the result would be zero, compare the item indexes. Makes the
17846 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017847 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017848 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017849
Bram Moolenaar0d660222005-01-07 21:51:51 +000017850 vim_free(tofree1);
17851 vim_free(tofree2);
17852 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017853}
17854
17855 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017856#ifdef __BORLANDC__
17857_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017858#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010017859item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017860{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017861 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017862 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017863 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017864 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017865 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017866
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017867 /* shortcut after failure in previous call; compare all items equal */
17868 if (item_compare_func_err)
17869 return 0;
17870
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017871 si1 = (sortItem_T *)s1;
17872 si2 = (sortItem_T *)s2;
17873
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017874 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017875 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017876 copy_tv(&si1->item->li_tv, &argv[0]);
17877 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017878
17879 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017880 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017881 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17882 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017883 clear_tv(&argv[0]);
17884 clear_tv(&argv[1]);
17885
17886 if (res == FAIL)
17887 res = ITEM_COMPARE_FAIL;
17888 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017889 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17890 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017891 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017892 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017893
17894 /* When the result would be zero, compare the pointers themselves. Makes
17895 * the sort stable. */
17896 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017897 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017898
Bram Moolenaar0d660222005-01-07 21:51:51 +000017899 return res;
17900}
17901
17902/*
17903 * "sort({list})" function
17904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017906do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017907{
Bram Moolenaar33570922005-01-25 22:26:29 +000017908 list_T *l;
17909 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017910 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017911 long len;
17912 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017913
Bram Moolenaar0d660222005-01-07 21:51:51 +000017914 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017915 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017916 else
17917 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017918 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017919 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020017920 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
17921 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017922 return;
17923 rettv->vval.v_list = l;
17924 rettv->v_type = VAR_LIST;
17925 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017926
Bram Moolenaar0d660222005-01-07 21:51:51 +000017927 len = list_len(l);
17928 if (len <= 1)
17929 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017930
Bram Moolenaar0d660222005-01-07 21:51:51 +000017931 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017932 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017933 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017934#ifdef FEAT_FLOAT
17935 item_compare_float = FALSE;
17936#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017937 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017938 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017939 if (argvars[1].v_type != VAR_UNKNOWN)
17940 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017941 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017942 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017943 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017944 else
17945 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017946 int error = FALSE;
17947
17948 i = get_tv_number_chk(&argvars[1], &error);
17949 if (error)
17950 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017951 if (i == 1)
17952 item_compare_ic = TRUE;
17953 else
17954 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017955 if (item_compare_func != NULL)
17956 {
17957 if (STRCMP(item_compare_func, "n") == 0)
17958 {
17959 item_compare_func = NULL;
17960 item_compare_numeric = TRUE;
17961 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017962 else if (STRCMP(item_compare_func, "N") == 0)
17963 {
17964 item_compare_func = NULL;
17965 item_compare_numbers = TRUE;
17966 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017967#ifdef FEAT_FLOAT
17968 else if (STRCMP(item_compare_func, "f") == 0)
17969 {
17970 item_compare_func = NULL;
17971 item_compare_float = TRUE;
17972 }
17973#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020017974 else if (STRCMP(item_compare_func, "i") == 0)
17975 {
17976 item_compare_func = NULL;
17977 item_compare_ic = TRUE;
17978 }
17979 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017980 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017981
17982 if (argvars[2].v_type != VAR_UNKNOWN)
17983 {
17984 /* optional third argument: {dict} */
17985 if (argvars[2].v_type != VAR_DICT)
17986 {
17987 EMSG(_(e_dictreq));
17988 return;
17989 }
17990 item_compare_selfdict = argvars[2].vval.v_dict;
17991 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017993
Bram Moolenaar0d660222005-01-07 21:51:51 +000017994 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017995 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017996 if (ptrs == NULL)
17997 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017998
Bram Moolenaar327aa022014-03-25 18:24:23 +010017999 i = 0;
18000 if (sort)
18001 {
18002 /* sort(): ptrs will be the list to sort */
18003 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018004 {
18005 ptrs[i].item = li;
18006 ptrs[i].idx = i;
18007 ++i;
18008 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018009
18010 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018011 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018012 /* test the compare function */
18013 if (item_compare_func != NULL
18014 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018015 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018016 EMSG(_("E702: Sort compare function failed"));
18017 else
18018 {
18019 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018020 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018021 item_compare_func == NULL ? item_compare : item_compare2);
18022
18023 if (!item_compare_func_err)
18024 {
18025 /* Clear the List and append the items in sorted order. */
18026 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18027 l->lv_len = 0;
18028 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018029 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018030 }
18031 }
18032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018034 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018035 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018036
18037 /* f_uniq(): ptrs will be a stack of items to remove */
18038 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018039 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018040 item_compare_func_ptr = item_compare_func
18041 ? item_compare2 : item_compare;
18042
18043 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18044 li = li->li_next)
18045 {
18046 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18047 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018048 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018049 if (item_compare_func_err)
18050 {
18051 EMSG(_("E882: Uniq compare function failed"));
18052 break;
18053 }
18054 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018055
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018056 if (!item_compare_func_err)
18057 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018058 while (--i >= 0)
18059 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018060 li = ptrs[i].item->li_next;
18061 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018062 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018063 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018064 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018065 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018066 list_fix_watch(l, li);
18067 listitem_free(li);
18068 l->lv_len--;
18069 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018070 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018071 }
18072
18073 vim_free(ptrs);
18074 }
18075}
18076
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018077/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018078 * "sort({list})" function
18079 */
18080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018081f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018082{
18083 do_sort_uniq(argvars, rettv, TRUE);
18084}
18085
18086/*
18087 * "uniq({list})" function
18088 */
18089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018090f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018091{
18092 do_sort_uniq(argvars, rettv, FALSE);
18093}
18094
18095/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018096 * "soundfold({word})" function
18097 */
18098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018099f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018100{
18101 char_u *s;
18102
18103 rettv->v_type = VAR_STRING;
18104 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018105#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018106 rettv->vval.v_string = eval_soundfold(s);
18107#else
18108 rettv->vval.v_string = vim_strsave(s);
18109#endif
18110}
18111
18112/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018113 * "spellbadword()" function
18114 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018116f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018117{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018118 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018119 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018120 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018121
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018122 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018123 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018124
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018125#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018126 if (argvars[0].v_type == VAR_UNKNOWN)
18127 {
18128 /* Find the start and length of the badly spelled word. */
18129 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18130 if (len != 0)
18131 word = ml_get_cursor();
18132 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018133 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018134 {
18135 char_u *str = get_tv_string_chk(&argvars[0]);
18136 int capcol = -1;
18137
18138 if (str != NULL)
18139 {
18140 /* Check the argument for spelling. */
18141 while (*str != NUL)
18142 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018143 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018144 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018145 {
18146 word = str;
18147 break;
18148 }
18149 str += len;
18150 }
18151 }
18152 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018153#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018154
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018155 list_append_string(rettv->vval.v_list, word, len);
18156 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018157 attr == HLF_SPB ? "bad" :
18158 attr == HLF_SPR ? "rare" :
18159 attr == HLF_SPL ? "local" :
18160 attr == HLF_SPC ? "caps" :
18161 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018162}
18163
18164/*
18165 * "spellsuggest()" function
18166 */
18167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018168f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018169{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018170#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018171 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018172 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018173 int maxcount;
18174 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018175 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018176 listitem_T *li;
18177 int need_capital = FALSE;
18178#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018179
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018180 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018181 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018182
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018183#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018184 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018185 {
18186 str = get_tv_string(&argvars[0]);
18187 if (argvars[1].v_type != VAR_UNKNOWN)
18188 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018189 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018190 if (maxcount <= 0)
18191 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018192 if (argvars[2].v_type != VAR_UNKNOWN)
18193 {
18194 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18195 if (typeerr)
18196 return;
18197 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018198 }
18199 else
18200 maxcount = 25;
18201
Bram Moolenaar4770d092006-01-12 23:22:24 +000018202 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018203
18204 for (i = 0; i < ga.ga_len; ++i)
18205 {
18206 str = ((char_u **)ga.ga_data)[i];
18207
18208 li = listitem_alloc();
18209 if (li == NULL)
18210 vim_free(str);
18211 else
18212 {
18213 li->li_tv.v_type = VAR_STRING;
18214 li->li_tv.v_lock = 0;
18215 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018216 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018217 }
18218 }
18219 ga_clear(&ga);
18220 }
18221#endif
18222}
18223
Bram Moolenaar0d660222005-01-07 21:51:51 +000018224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018225f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018226{
18227 char_u *str;
18228 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018229 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018230 regmatch_T regmatch;
18231 char_u patbuf[NUMBUFLEN];
18232 char_u *save_cpo;
18233 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018234 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018235 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018236 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018237
18238 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18239 save_cpo = p_cpo;
18240 p_cpo = (char_u *)"";
18241
18242 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018243 if (argvars[1].v_type != VAR_UNKNOWN)
18244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018245 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18246 if (pat == NULL)
18247 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018248 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018249 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018250 }
18251 if (pat == NULL || *pat == NUL)
18252 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018253
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018254 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018255 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018256 if (typeerr)
18257 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258
Bram Moolenaar0d660222005-01-07 21:51:51 +000018259 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18260 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018262 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018263 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018264 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018265 if (*str == NUL)
18266 match = FALSE; /* empty item at the end */
18267 else
18268 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018269 if (match)
18270 end = regmatch.startp[0];
18271 else
18272 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018273 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18274 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018275 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018276 if (list_append_string(rettv->vval.v_list, str,
18277 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018278 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018279 }
18280 if (!match)
18281 break;
18282 /* Advance to just after the match. */
18283 if (regmatch.endp[0] > str)
18284 col = 0;
18285 else
18286 {
18287 /* Don't get stuck at the same match. */
18288#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018289 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018290#else
18291 col = 1;
18292#endif
18293 }
18294 str = regmatch.endp[0];
18295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018296
Bram Moolenaar473de612013-06-08 18:19:48 +020018297 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018299
Bram Moolenaar0d660222005-01-07 21:51:51 +000018300 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301}
18302
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018303#ifdef FEAT_FLOAT
18304/*
18305 * "sqrt()" function
18306 */
18307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018308f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018309{
18310 float_T f;
18311
18312 rettv->v_type = VAR_FLOAT;
18313 if (get_float_arg(argvars, &f) == OK)
18314 rettv->vval.v_float = sqrt(f);
18315 else
18316 rettv->vval.v_float = 0.0;
18317}
18318
18319/*
18320 * "str2float()" function
18321 */
18322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018323f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018324{
18325 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18326
18327 if (*p == '+')
18328 p = skipwhite(p + 1);
18329 (void)string2float(p, &rettv->vval.v_float);
18330 rettv->v_type = VAR_FLOAT;
18331}
18332#endif
18333
Bram Moolenaar2c932302006-03-18 21:42:09 +000018334/*
18335 * "str2nr()" function
18336 */
18337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018338f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018339{
18340 int base = 10;
18341 char_u *p;
18342 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018343 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018344
18345 if (argvars[1].v_type != VAR_UNKNOWN)
18346 {
18347 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018348 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018349 {
18350 EMSG(_(e_invarg));
18351 return;
18352 }
18353 }
18354
18355 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018356 if (*p == '+')
18357 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018358 switch (base)
18359 {
18360 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18361 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18362 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18363 default: what = 0;
18364 }
18365 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018366 rettv->vval.v_number = n;
18367}
18368
Bram Moolenaar071d4272004-06-13 20:20:40 +000018369#ifdef HAVE_STRFTIME
18370/*
18371 * "strftime({format}[, {time}])" function
18372 */
18373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018374f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018375{
18376 char_u result_buf[256];
18377 struct tm *curtime;
18378 time_t seconds;
18379 char_u *p;
18380
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018381 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018383 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018384 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018385 seconds = time(NULL);
18386 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018387 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018388 curtime = localtime(&seconds);
18389 /* MSVC returns NULL for an invalid value of seconds. */
18390 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018391 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018392 else
18393 {
18394# ifdef FEAT_MBYTE
18395 vimconv_T conv;
18396 char_u *enc;
18397
18398 conv.vc_type = CONV_NONE;
18399 enc = enc_locale();
18400 convert_setup(&conv, p_enc, enc);
18401 if (conv.vc_type != CONV_NONE)
18402 p = string_convert(&conv, p, NULL);
18403# endif
18404 if (p != NULL)
18405 (void)strftime((char *)result_buf, sizeof(result_buf),
18406 (char *)p, curtime);
18407 else
18408 result_buf[0] = NUL;
18409
18410# ifdef FEAT_MBYTE
18411 if (conv.vc_type != CONV_NONE)
18412 vim_free(p);
18413 convert_setup(&conv, enc, p_enc);
18414 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018415 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018416 else
18417# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018418 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419
18420# ifdef FEAT_MBYTE
18421 /* Release conversion descriptors */
18422 convert_setup(&conv, NULL, NULL);
18423 vim_free(enc);
18424# endif
18425 }
18426}
18427#endif
18428
18429/*
18430 * "stridx()" function
18431 */
18432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018433f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018434{
18435 char_u buf[NUMBUFLEN];
18436 char_u *needle;
18437 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018438 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018439 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018440 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018441
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018442 needle = get_tv_string_chk(&argvars[1]);
18443 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018444 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018445 if (needle == NULL || haystack == NULL)
18446 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018447
Bram Moolenaar33570922005-01-25 22:26:29 +000018448 if (argvars[2].v_type != VAR_UNKNOWN)
18449 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018450 int error = FALSE;
18451
18452 start_idx = get_tv_number_chk(&argvars[2], &error);
18453 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018454 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018455 if (start_idx >= 0)
18456 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018457 }
18458
18459 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18460 if (pos != NULL)
18461 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018462}
18463
18464/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018465 * "string()" function
18466 */
18467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018468f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018469{
18470 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018471 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018472
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018473 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018474 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018475 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018476 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018477 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018478}
18479
18480/*
18481 * "strlen()" function
18482 */
18483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018484f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018486 rettv->vval.v_number = (varnumber_T)(STRLEN(
18487 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018488}
18489
18490/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018491 * "strchars()" function
18492 */
18493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018494f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018495{
18496 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018497 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018498#ifdef FEAT_MBYTE
18499 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018500 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018501#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018502
18503 if (argvars[1].v_type != VAR_UNKNOWN)
18504 skipcc = get_tv_number_chk(&argvars[1], NULL);
18505 if (skipcc < 0 || skipcc > 1)
18506 EMSG(_(e_invarg));
18507 else
18508 {
18509#ifdef FEAT_MBYTE
18510 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18511 while (*s != NUL)
18512 {
18513 func_mb_ptr2char_adv(&s);
18514 ++len;
18515 }
18516 rettv->vval.v_number = len;
18517#else
18518 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18519#endif
18520 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018521}
18522
18523/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018524 * "strdisplaywidth()" function
18525 */
18526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018527f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020018528{
18529 char_u *s = get_tv_string(&argvars[0]);
18530 int col = 0;
18531
18532 if (argvars[1].v_type != VAR_UNKNOWN)
18533 col = get_tv_number(&argvars[1]);
18534
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018535 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018536}
18537
18538/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018539 * "strwidth()" function
18540 */
18541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018542f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018543{
18544 char_u *s = get_tv_string(&argvars[0]);
18545
18546 rettv->vval.v_number = (varnumber_T)(
18547#ifdef FEAT_MBYTE
18548 mb_string2cells(s, -1)
18549#else
18550 STRLEN(s)
18551#endif
18552 );
18553}
18554
18555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018556 * "strpart()" function
18557 */
18558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018559f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018560{
18561 char_u *p;
18562 int n;
18563 int len;
18564 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018565 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018567 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568 slen = (int)STRLEN(p);
18569
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018570 n = get_tv_number_chk(&argvars[1], &error);
18571 if (error)
18572 len = 0;
18573 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018574 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575 else
18576 len = slen - n; /* default len: all bytes that are available. */
18577
18578 /*
18579 * Only return the overlap between the specified part and the actual
18580 * string.
18581 */
18582 if (n < 0)
18583 {
18584 len += n;
18585 n = 0;
18586 }
18587 else if (n > slen)
18588 n = slen;
18589 if (len < 0)
18590 len = 0;
18591 else if (n + len > slen)
18592 len = slen - n;
18593
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018594 rettv->v_type = VAR_STRING;
18595 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596}
18597
18598/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018599 * "strridx()" function
18600 */
18601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018602f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018603{
18604 char_u buf[NUMBUFLEN];
18605 char_u *needle;
18606 char_u *haystack;
18607 char_u *rest;
18608 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018609 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018610
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018611 needle = get_tv_string_chk(&argvars[1]);
18612 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018613
18614 rettv->vval.v_number = -1;
18615 if (needle == NULL || haystack == NULL)
18616 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018617
18618 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018619 if (argvars[2].v_type != VAR_UNKNOWN)
18620 {
18621 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018622 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018623 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018624 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018625 }
18626 else
18627 end_idx = haystack_len;
18628
Bram Moolenaar0d660222005-01-07 21:51:51 +000018629 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018630 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018631 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018632 lastmatch = haystack + end_idx;
18633 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018634 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018635 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018636 for (rest = haystack; *rest != '\0'; ++rest)
18637 {
18638 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018639 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018640 break;
18641 lastmatch = rest;
18642 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018643 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018644
18645 if (lastmatch == NULL)
18646 rettv->vval.v_number = -1;
18647 else
18648 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18649}
18650
18651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018652 * "strtrans()" function
18653 */
18654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018655f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018656{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018657 rettv->v_type = VAR_STRING;
18658 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018659}
18660
18661/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018662 * "submatch()" function
18663 */
18664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018665f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018666{
Bram Moolenaar41571762014-04-02 19:00:58 +020018667 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018668 int no;
18669 int retList = 0;
18670
18671 no = (int)get_tv_number_chk(&argvars[0], &error);
18672 if (error)
18673 return;
18674 error = FALSE;
18675 if (argvars[1].v_type != VAR_UNKNOWN)
18676 retList = get_tv_number_chk(&argvars[1], &error);
18677 if (error)
18678 return;
18679
18680 if (retList == 0)
18681 {
18682 rettv->v_type = VAR_STRING;
18683 rettv->vval.v_string = reg_submatch(no);
18684 }
18685 else
18686 {
18687 rettv->v_type = VAR_LIST;
18688 rettv->vval.v_list = reg_submatch_list(no);
18689 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018690}
18691
18692/*
18693 * "substitute()" function
18694 */
18695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018696f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018697{
18698 char_u patbuf[NUMBUFLEN];
18699 char_u subbuf[NUMBUFLEN];
18700 char_u flagsbuf[NUMBUFLEN];
18701
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018702 char_u *str = get_tv_string_chk(&argvars[0]);
18703 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18704 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18705 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18706
Bram Moolenaar0d660222005-01-07 21:51:51 +000018707 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018708 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18709 rettv->vval.v_string = NULL;
18710 else
18711 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018712}
18713
18714/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018715 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018716 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018718f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018719{
18720 int id = 0;
18721#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018722 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018723 long col;
18724 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018725 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018726
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018727 lnum = get_tv_lnum(argvars); /* -1 on type error */
18728 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18729 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018730
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018731 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018732 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018733 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734#endif
18735
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018736 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737}
18738
18739/*
18740 * "synIDattr(id, what [, mode])" function
18741 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018743f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744{
18745 char_u *p = NULL;
18746#ifdef FEAT_SYN_HL
18747 int id;
18748 char_u *what;
18749 char_u *mode;
18750 char_u modebuf[NUMBUFLEN];
18751 int modec;
18752
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018753 id = get_tv_number(&argvars[0]);
18754 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018755 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018757 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018759 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018760 modec = 0; /* replace invalid with current */
18761 }
18762 else
18763 {
18764#ifdef FEAT_GUI
18765 if (gui.in_use)
18766 modec = 'g';
18767 else
18768#endif
18769 if (t_colors > 1)
18770 modec = 'c';
18771 else
18772 modec = 't';
18773 }
18774
18775
18776 switch (TOLOWER_ASC(what[0]))
18777 {
18778 case 'b':
18779 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18780 p = highlight_color(id, what, modec);
18781 else /* bold */
18782 p = highlight_has_attr(id, HL_BOLD, modec);
18783 break;
18784
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018785 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786 p = highlight_color(id, what, modec);
18787 break;
18788
18789 case 'i':
18790 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18791 p = highlight_has_attr(id, HL_INVERSE, modec);
18792 else /* italic */
18793 p = highlight_has_attr(id, HL_ITALIC, modec);
18794 break;
18795
18796 case 'n': /* name */
18797 p = get_highlight_name(NULL, id - 1);
18798 break;
18799
18800 case 'r': /* reverse */
18801 p = highlight_has_attr(id, HL_INVERSE, modec);
18802 break;
18803
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018804 case 's':
18805 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18806 p = highlight_color(id, what, modec);
18807 else /* standout */
18808 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809 break;
18810
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018811 case 'u':
18812 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18813 /* underline */
18814 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18815 else
18816 /* undercurl */
18817 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018818 break;
18819 }
18820
18821 if (p != NULL)
18822 p = vim_strsave(p);
18823#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018824 rettv->v_type = VAR_STRING;
18825 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018826}
18827
18828/*
18829 * "synIDtrans(id)" function
18830 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018832f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018833{
18834 int id;
18835
18836#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018837 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838
18839 if (id > 0)
18840 id = syn_get_final_id(id);
18841 else
18842#endif
18843 id = 0;
18844
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018845 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018846}
18847
18848/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018849 * "synconcealed(lnum, col)" function
18850 */
18851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018852f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018853{
18854#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18855 long lnum;
18856 long col;
18857 int syntax_flags = 0;
18858 int cchar;
18859 int matchid = 0;
18860 char_u str[NUMBUFLEN];
18861#endif
18862
18863 rettv->v_type = VAR_LIST;
18864 rettv->vval.v_list = NULL;
18865
18866#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18867 lnum = get_tv_lnum(argvars); /* -1 on type error */
18868 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18869
18870 vim_memset(str, NUL, sizeof(str));
18871
18872 if (rettv_list_alloc(rettv) != FAIL)
18873 {
18874 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18875 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18876 && curwin->w_p_cole > 0)
18877 {
18878 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18879 syntax_flags = get_syntax_info(&matchid);
18880
18881 /* get the conceal character */
18882 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18883 {
18884 cchar = syn_get_sub_char();
18885 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18886 cchar = lcs_conceal;
18887 if (cchar != NUL)
18888 {
18889# ifdef FEAT_MBYTE
18890 if (has_mbyte)
18891 (*mb_char2bytes)(cchar, str);
18892 else
18893# endif
18894 str[0] = cchar;
18895 }
18896 }
18897 }
18898
18899 list_append_number(rettv->vval.v_list,
18900 (syntax_flags & HL_CONCEAL) != 0);
18901 /* -1 to auto-determine strlen */
18902 list_append_string(rettv->vval.v_list, str, -1);
18903 list_append_number(rettv->vval.v_list, matchid);
18904 }
18905#endif
18906}
18907
18908/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018909 * "synstack(lnum, col)" function
18910 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018912f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018913{
18914#ifdef FEAT_SYN_HL
18915 long lnum;
18916 long col;
18917 int i;
18918 int id;
18919#endif
18920
18921 rettv->v_type = VAR_LIST;
18922 rettv->vval.v_list = NULL;
18923
18924#ifdef FEAT_SYN_HL
18925 lnum = get_tv_lnum(argvars); /* -1 on type error */
18926 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18927
18928 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018929 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018930 && rettv_list_alloc(rettv) != FAIL)
18931 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018932 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018933 for (i = 0; ; ++i)
18934 {
18935 id = syn_get_stack_item(i);
18936 if (id < 0)
18937 break;
18938 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18939 break;
18940 }
18941 }
18942#endif
18943}
18944
Bram Moolenaar071d4272004-06-13 20:20:40 +000018945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018946get_cmd_output_as_rettv(
18947 typval_T *argvars,
18948 typval_T *rettv,
18949 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018950{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018951 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018952 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018953 char_u *infile = NULL;
18954 char_u buf[NUMBUFLEN];
18955 int err = FALSE;
18956 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018957 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020018958 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018960 rettv->v_type = VAR_STRING;
18961 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018962 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018963 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018964
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018965 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018966 {
18967 /*
18968 * Write the string to a temp file, to be used for input of the shell
18969 * command.
18970 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020018971 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018972 {
18973 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018974 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018975 }
18976
18977 fd = mch_fopen((char *)infile, WRITEBIN);
18978 if (fd == NULL)
18979 {
18980 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018981 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018982 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018983 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018984 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018985 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18986 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018987 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018988 else
18989 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018990 size_t len;
18991
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018992 p = get_tv_string_buf_chk(&argvars[1], buf);
18993 if (p == NULL)
18994 {
18995 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018996 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018997 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018998 len = STRLEN(p);
18999 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019000 err = TRUE;
19001 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019002 if (fclose(fd) != 0)
19003 err = TRUE;
19004 if (err)
19005 {
19006 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019007 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019008 }
19009 }
19010
Bram Moolenaar52a72462014-08-29 15:53:52 +020019011 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19012 * echoes typeahead, that messes up the display. */
19013 if (!msg_silent)
19014 flags += SHELL_COOKED;
19015
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019016 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019017 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019018 int len;
19019 listitem_T *li;
19020 char_u *s = NULL;
19021 char_u *start;
19022 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019023 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019024
Bram Moolenaar52a72462014-08-29 15:53:52 +020019025 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019026 if (res == NULL)
19027 goto errret;
19028
19029 list = list_alloc();
19030 if (list == NULL)
19031 goto errret;
19032
19033 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019034 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019035 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019036 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019037 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019038 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019039
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019040 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019041 if (s == NULL)
19042 goto errret;
19043
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019044 for (p = s; start < end; ++p, ++start)
19045 *p = *start == NUL ? NL : *start;
19046 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019047
19048 li = listitem_alloc();
19049 if (li == NULL)
19050 {
19051 vim_free(s);
19052 goto errret;
19053 }
19054 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019055 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019056 li->li_tv.vval.v_string = s;
19057 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019058 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019059
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019060 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019061 rettv->v_type = VAR_LIST;
19062 rettv->vval.v_list = list;
19063 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019064 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019065 else
19066 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019067 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019068#ifdef USE_CR
19069 /* translate <CR> into <NL> */
19070 if (res != NULL)
19071 {
19072 char_u *s;
19073
19074 for (s = res; *s; ++s)
19075 {
19076 if (*s == CAR)
19077 *s = NL;
19078 }
19079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019080#else
19081# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019082 /* translate <CR><NL> into <NL> */
19083 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019084 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019085 char_u *s, *d;
19086
19087 d = res;
19088 for (s = res; *s; ++s)
19089 {
19090 if (s[0] == CAR && s[1] == NL)
19091 ++s;
19092 *d++ = *s;
19093 }
19094 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019096# endif
19097#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019098 rettv->vval.v_string = res;
19099 res = NULL;
19100 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019101
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019102errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019103 if (infile != NULL)
19104 {
19105 mch_remove(infile);
19106 vim_free(infile);
19107 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019108 if (res != NULL)
19109 vim_free(res);
19110 if (list != NULL)
19111 list_free(list, TRUE);
19112}
19113
19114/*
19115 * "system()" function
19116 */
19117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019118f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019119{
19120 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19121}
19122
19123/*
19124 * "systemlist()" function
19125 */
19126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019127f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019128{
19129 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019130}
19131
19132/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019133 * "tabpagebuflist()" function
19134 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019136f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019137{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019138#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019139 tabpage_T *tp;
19140 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019141
19142 if (argvars[0].v_type == VAR_UNKNOWN)
19143 wp = firstwin;
19144 else
19145 {
19146 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19147 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019148 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019149 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019150 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019151 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019152 for (; wp != NULL; wp = wp->w_next)
19153 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019154 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019155 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019156 }
19157#endif
19158}
19159
19160
19161/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019162 * "tabpagenr()" function
19163 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019165f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019166{
19167 int nr = 1;
19168#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019169 char_u *arg;
19170
19171 if (argvars[0].v_type != VAR_UNKNOWN)
19172 {
19173 arg = get_tv_string_chk(&argvars[0]);
19174 nr = 0;
19175 if (arg != NULL)
19176 {
19177 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019178 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019179 else
19180 EMSG2(_(e_invexpr2), arg);
19181 }
19182 }
19183 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019184 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019185#endif
19186 rettv->vval.v_number = nr;
19187}
19188
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019189
19190#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019191static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019192
19193/*
19194 * Common code for tabpagewinnr() and winnr().
19195 */
19196 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019197get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019198{
19199 win_T *twin;
19200 int nr = 1;
19201 win_T *wp;
19202 char_u *arg;
19203
19204 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19205 if (argvar->v_type != VAR_UNKNOWN)
19206 {
19207 arg = get_tv_string_chk(argvar);
19208 if (arg == NULL)
19209 nr = 0; /* type error; errmsg already given */
19210 else if (STRCMP(arg, "$") == 0)
19211 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19212 else if (STRCMP(arg, "#") == 0)
19213 {
19214 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19215 if (twin == NULL)
19216 nr = 0;
19217 }
19218 else
19219 {
19220 EMSG2(_(e_invexpr2), arg);
19221 nr = 0;
19222 }
19223 }
19224
19225 if (nr > 0)
19226 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19227 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019228 {
19229 if (wp == NULL)
19230 {
19231 /* didn't find it in this tabpage */
19232 nr = 0;
19233 break;
19234 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019235 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019236 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019237 return nr;
19238}
19239#endif
19240
19241/*
19242 * "tabpagewinnr()" function
19243 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019245f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019246{
19247 int nr = 1;
19248#ifdef FEAT_WINDOWS
19249 tabpage_T *tp;
19250
19251 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19252 if (tp == NULL)
19253 nr = 0;
19254 else
19255 nr = get_winnr(tp, &argvars[1]);
19256#endif
19257 rettv->vval.v_number = nr;
19258}
19259
19260
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019261/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019262 * "tagfiles()" function
19263 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019265f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019266{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019267 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019268 tagname_T tn;
19269 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019270
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019271 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019272 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019273 fname = alloc(MAXPATHL);
19274 if (fname == NULL)
19275 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019276
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019277 for (first = TRUE; ; first = FALSE)
19278 if (get_tagfname(&tn, first, fname) == FAIL
19279 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019280 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019281 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019282 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019283}
19284
19285/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019286 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019287 */
19288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019289f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019290{
19291 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019292
19293 tag_pattern = get_tv_string(&argvars[0]);
19294
19295 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019296 if (*tag_pattern == NUL)
19297 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019298
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019299 if (rettv_list_alloc(rettv) == OK)
19300 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019301}
19302
19303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304 * "tempname()" function
19305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019306 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019307f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019308{
19309 static int x = 'A';
19310
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019311 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019312 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313
19314 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19315 * names. Skip 'I' and 'O', they are used for shell redirection. */
19316 do
19317 {
19318 if (x == 'Z')
19319 x = '0';
19320 else if (x == '9')
19321 x = 'A';
19322 else
19323 {
19324#ifdef EBCDIC
19325 if (x == 'I')
19326 x = 'J';
19327 else if (x == 'R')
19328 x = 'S';
19329 else
19330#endif
19331 ++x;
19332 }
19333 } while (x == 'I' || x == 'O');
19334}
19335
19336/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019337 * "test(list)" function: Just checking the walls...
19338 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019339 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019340f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019341{
19342 /* Used for unit testing. Change the code below to your liking. */
19343#if 0
19344 listitem_T *li;
19345 list_T *l;
19346 char_u *bad, *good;
19347
19348 if (argvars[0].v_type != VAR_LIST)
19349 return;
19350 l = argvars[0].vval.v_list;
19351 if (l == NULL)
19352 return;
19353 li = l->lv_first;
19354 if (li == NULL)
19355 return;
19356 bad = get_tv_string(&li->li_tv);
19357 li = li->li_next;
19358 if (li == NULL)
19359 return;
19360 good = get_tv_string(&li->li_tv);
19361 rettv->vval.v_number = test_edit_score(bad, good);
19362#endif
19363}
19364
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019365#ifdef FEAT_FLOAT
19366/*
19367 * "tan()" function
19368 */
19369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019370f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019371{
19372 float_T f;
19373
19374 rettv->v_type = VAR_FLOAT;
19375 if (get_float_arg(argvars, &f) == OK)
19376 rettv->vval.v_float = tan(f);
19377 else
19378 rettv->vval.v_float = 0.0;
19379}
19380
19381/*
19382 * "tanh()" function
19383 */
19384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019385f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019386{
19387 float_T f;
19388
19389 rettv->v_type = VAR_FLOAT;
19390 if (get_float_arg(argvars, &f) == OK)
19391 rettv->vval.v_float = tanh(f);
19392 else
19393 rettv->vval.v_float = 0.0;
19394}
19395#endif
19396
Bram Moolenaard52d9742005-08-21 22:20:28 +000019397/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019398 * "tolower(string)" function
19399 */
19400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019401f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019402{
19403 char_u *p;
19404
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019405 p = vim_strsave(get_tv_string(&argvars[0]));
19406 rettv->v_type = VAR_STRING;
19407 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019408
19409 if (p != NULL)
19410 while (*p != NUL)
19411 {
19412#ifdef FEAT_MBYTE
19413 int l;
19414
19415 if (enc_utf8)
19416 {
19417 int c, lc;
19418
19419 c = utf_ptr2char(p);
19420 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019421 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019422 /* TODO: reallocate string when byte count changes. */
19423 if (utf_char2len(lc) == l)
19424 utf_char2bytes(lc, p);
19425 p += l;
19426 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019427 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019428 p += l; /* skip multi-byte character */
19429 else
19430#endif
19431 {
19432 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19433 ++p;
19434 }
19435 }
19436}
19437
19438/*
19439 * "toupper(string)" function
19440 */
19441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019442f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019443{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019444 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019445 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019446}
19447
19448/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019449 * "tr(string, fromstr, tostr)" function
19450 */
19451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019452f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019453{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019454 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019455 char_u *fromstr;
19456 char_u *tostr;
19457 char_u *p;
19458#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019459 int inlen;
19460 int fromlen;
19461 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019462 int idx;
19463 char_u *cpstr;
19464 int cplen;
19465 int first = TRUE;
19466#endif
19467 char_u buf[NUMBUFLEN];
19468 char_u buf2[NUMBUFLEN];
19469 garray_T ga;
19470
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019471 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019472 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19473 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019474
19475 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019476 rettv->v_type = VAR_STRING;
19477 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019478 if (fromstr == NULL || tostr == NULL)
19479 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019480 ga_init2(&ga, (int)sizeof(char), 80);
19481
19482#ifdef FEAT_MBYTE
19483 if (!has_mbyte)
19484#endif
19485 /* not multi-byte: fromstr and tostr must be the same length */
19486 if (STRLEN(fromstr) != STRLEN(tostr))
19487 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019488#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019489error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019490#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019491 EMSG2(_(e_invarg2), fromstr);
19492 ga_clear(&ga);
19493 return;
19494 }
19495
19496 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019497 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019498 {
19499#ifdef FEAT_MBYTE
19500 if (has_mbyte)
19501 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019502 inlen = (*mb_ptr2len)(in_str);
19503 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019504 cplen = inlen;
19505 idx = 0;
19506 for (p = fromstr; *p != NUL; p += fromlen)
19507 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019508 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019509 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019510 {
19511 for (p = tostr; *p != NUL; p += tolen)
19512 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019513 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019514 if (idx-- == 0)
19515 {
19516 cplen = tolen;
19517 cpstr = p;
19518 break;
19519 }
19520 }
19521 if (*p == NUL) /* tostr is shorter than fromstr */
19522 goto error;
19523 break;
19524 }
19525 ++idx;
19526 }
19527
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019528 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019529 {
19530 /* Check that fromstr and tostr have the same number of
19531 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019532 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019533 first = FALSE;
19534 for (p = tostr; *p != NUL; p += tolen)
19535 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019536 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019537 --idx;
19538 }
19539 if (idx != 0)
19540 goto error;
19541 }
19542
Bram Moolenaarcde88542015-08-11 19:14:00 +020019543 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019544 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019545 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019546
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019547 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019548 }
19549 else
19550#endif
19551 {
19552 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019553 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019554 if (p != NULL)
19555 ga_append(&ga, tostr[p - fromstr]);
19556 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019557 ga_append(&ga, *in_str);
19558 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019559 }
19560 }
19561
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019562 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019563 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019564 ga_append(&ga, NUL);
19565
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019566 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019567}
19568
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019569#ifdef FEAT_FLOAT
19570/*
19571 * "trunc({float})" function
19572 */
19573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019574f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019575{
19576 float_T f;
19577
19578 rettv->v_type = VAR_FLOAT;
19579 if (get_float_arg(argvars, &f) == OK)
19580 /* trunc() is not in C90, use floor() or ceil() instead. */
19581 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19582 else
19583 rettv->vval.v_float = 0.0;
19584}
19585#endif
19586
Bram Moolenaar8299df92004-07-10 09:47:34 +000019587/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019588 * "type(expr)" function
19589 */
19590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019591f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019592{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019593 int n;
19594
19595 switch (argvars[0].v_type)
19596 {
19597 case VAR_NUMBER: n = 0; break;
19598 case VAR_STRING: n = 1; break;
19599 case VAR_FUNC: n = 2; break;
19600 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019601 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019602#ifdef FEAT_FLOAT
19603 case VAR_FLOAT: n = 5; break;
19604#endif
Bram Moolenaarf95534c2016-01-23 21:59:52 +010019605 case VAR_SPECIAL:
19606 if (argvars[0].vval.v_number == VVAL_FALSE
19607 || argvars[0].vval.v_number == VVAL_TRUE)
19608 n = 6;
19609 else
19610 n = 7;
19611 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019612 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19613 }
19614 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019615}
19616
19617/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019618 * "undofile(name)" function
19619 */
19620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019621f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019622{
19623 rettv->v_type = VAR_STRING;
19624#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019625 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019626 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019627
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019628 if (*fname == NUL)
19629 {
19630 /* If there is no file name there will be no undo file. */
19631 rettv->vval.v_string = NULL;
19632 }
19633 else
19634 {
19635 char_u *ffname = FullName_save(fname, FALSE);
19636
19637 if (ffname != NULL)
19638 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19639 vim_free(ffname);
19640 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019641 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019642#else
19643 rettv->vval.v_string = NULL;
19644#endif
19645}
19646
19647/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019648 * "undotree()" function
19649 */
19650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019651f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020019652{
19653 if (rettv_dict_alloc(rettv) == OK)
19654 {
19655 dict_T *dict = rettv->vval.v_dict;
19656 list_T *list;
19657
Bram Moolenaar730cde92010-06-27 05:18:54 +020019658 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019659 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019660 dict_add_nr_str(dict, "save_last",
19661 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019662 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19663 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019664 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019665
19666 list = list_alloc();
19667 if (list != NULL)
19668 {
19669 u_eval_tree(curbuf->b_u_oldhead, list);
19670 dict_add_list(dict, "entries", list);
19671 }
19672 }
19673}
19674
19675/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019676 * "values(dict)" function
19677 */
19678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019679f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000019680{
19681 dict_list(argvars, rettv, 1);
19682}
19683
19684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685 * "virtcol(string)" function
19686 */
19687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019688f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019689{
19690 colnr_T vcol = 0;
19691 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019692 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019693
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019694 fp = var2fpos(&argvars[0], FALSE, &fnum);
19695 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19696 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019697 {
19698 getvvcol(curwin, fp, NULL, NULL, &vcol);
19699 ++vcol;
19700 }
19701
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019702 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019703}
19704
19705/*
19706 * "visualmode()" function
19707 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019709f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019710{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019711 char_u str[2];
19712
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019713 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019714 str[0] = curbuf->b_visual_mode_eval;
19715 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019716 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019717
19718 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019719 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019721}
19722
19723/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019724 * "wildmenumode()" function
19725 */
19726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019727f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019728{
19729#ifdef FEAT_WILDMENU
19730 if (wild_menu_showing)
19731 rettv->vval.v_number = 1;
19732#endif
19733}
19734
19735/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019736 * "winbufnr(nr)" function
19737 */
19738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019739f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019740{
19741 win_T *wp;
19742
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019743 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019744 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019745 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019746 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019747 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748}
19749
19750/*
19751 * "wincol()" function
19752 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019754f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019755{
19756 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019757 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758}
19759
19760/*
19761 * "winheight(nr)" function
19762 */
19763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019764f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019765{
19766 win_T *wp;
19767
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019768 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019770 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019772 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773}
19774
19775/*
19776 * "winline()" function
19777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019779f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019780{
19781 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019782 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783}
19784
19785/*
19786 * "winnr()" function
19787 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019789f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790{
19791 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019792
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019794 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019795#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019796 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019797}
19798
19799/*
19800 * "winrestcmd()" function
19801 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019803f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804{
19805#ifdef FEAT_WINDOWS
19806 win_T *wp;
19807 int winnr = 1;
19808 garray_T ga;
19809 char_u buf[50];
19810
19811 ga_init2(&ga, (int)sizeof(char), 70);
19812 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19813 {
19814 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19815 ga_concat(&ga, buf);
19816# ifdef FEAT_VERTSPLIT
19817 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19818 ga_concat(&ga, buf);
19819# endif
19820 ++winnr;
19821 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019822 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019823
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019824 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019825#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019826 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019827#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019828 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019829}
19830
19831/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019832 * "winrestview()" function
19833 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019835f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019836{
19837 dict_T *dict;
19838
19839 if (argvars[0].v_type != VAR_DICT
19840 || (dict = argvars[0].vval.v_dict) == NULL)
19841 EMSG(_(e_invarg));
19842 else
19843 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019844 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19845 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19846 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19847 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019848#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019849 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19850 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019851#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019852 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19853 {
19854 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19855 curwin->w_set_curswant = FALSE;
19856 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019857
Bram Moolenaar82c25852014-05-28 16:47:16 +020019858 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19859 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019860#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019861 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19862 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019863#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019864 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19865 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19866 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19867 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019868
19869 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019870 win_new_height(curwin, curwin->w_height);
19871# ifdef FEAT_VERTSPLIT
19872 win_new_width(curwin, W_WIDTH(curwin));
19873# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019874 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019875
Bram Moolenaarb851a962014-10-31 15:45:52 +010019876 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019877 curwin->w_topline = 1;
19878 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19879 curwin->w_topline = curbuf->b_ml.ml_line_count;
19880#ifdef FEAT_DIFF
19881 check_topfill(curwin, TRUE);
19882#endif
19883 }
19884}
19885
19886/*
19887 * "winsaveview()" function
19888 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019890f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019891{
19892 dict_T *dict;
19893
Bram Moolenaara800b422010-06-27 01:15:55 +020019894 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019895 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019896 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019897
19898 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19899 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19900#ifdef FEAT_VIRTUALEDIT
19901 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19902#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019903 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019904 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19905
19906 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19907#ifdef FEAT_DIFF
19908 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19909#endif
19910 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19911 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19912}
19913
19914/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019915 * "winwidth(nr)" function
19916 */
19917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019918f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019919{
19920 win_T *wp;
19921
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019922 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019923 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019924 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925 else
19926#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019927 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019928#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019929 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019930#endif
19931}
19932
Bram Moolenaar071d4272004-06-13 20:20:40 +000019933/*
Bram Moolenaared767a22016-01-03 22:49:16 +010019934 * "wordcount()" function
19935 */
19936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019937f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010019938{
19939 if (rettv_dict_alloc(rettv) == FAIL)
19940 return;
19941 cursor_pos_info(rettv->vval.v_dict);
19942}
19943
19944/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019945 * Write list of strings to file
19946 */
19947 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019948write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019949{
19950 listitem_T *li;
19951 int c;
19952 int ret = OK;
19953 char_u *s;
19954
19955 for (li = list->lv_first; li != NULL; li = li->li_next)
19956 {
19957 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19958 {
19959 if (*s == '\n')
19960 c = putc(NUL, fd);
19961 else
19962 c = putc(*s, fd);
19963 if (c == EOF)
19964 {
19965 ret = FAIL;
19966 break;
19967 }
19968 }
19969 if (!binary || li->li_next != NULL)
19970 if (putc('\n', fd) == EOF)
19971 {
19972 ret = FAIL;
19973 break;
19974 }
19975 if (ret == FAIL)
19976 {
19977 EMSG(_(e_write));
19978 break;
19979 }
19980 }
19981 return ret;
19982}
19983
19984/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019985 * "writefile()" function
19986 */
19987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019988f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019989{
19990 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019991 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019992 char_u *fname;
19993 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019994 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019995
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019996 if (check_restricted() || check_secure())
19997 return;
19998
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019999 if (argvars[0].v_type != VAR_LIST)
20000 {
20001 EMSG2(_(e_listarg), "writefile()");
20002 return;
20003 }
20004 if (argvars[0].vval.v_list == NULL)
20005 return;
20006
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020007 if (argvars[2].v_type != VAR_UNKNOWN)
20008 {
20009 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20010 binary = TRUE;
20011 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20012 append = TRUE;
20013 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020014
20015 /* Always open the file in binary mode, library functions have a mind of
20016 * their own about CR-LF conversion. */
20017 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020018 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20019 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020020 {
20021 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20022 ret = -1;
20023 }
20024 else
20025 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020026 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20027 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020028 fclose(fd);
20029 }
20030
20031 rettv->vval.v_number = ret;
20032}
20033
20034/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020035 * "xor(expr, expr)" function
20036 */
20037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020038f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020039{
20040 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20041 ^ get_tv_number_chk(&argvars[1], NULL);
20042}
20043
20044
20045/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020046 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020047 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048 */
20049 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020050var2fpos(
20051 typval_T *varp,
20052 int dollar_lnum, /* TRUE when $ is last line */
20053 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020054{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020055 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020057 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058
Bram Moolenaara5525202006-03-02 22:52:09 +000020059 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020060 if (varp->v_type == VAR_LIST)
20061 {
20062 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020063 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020064 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020065 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020066
20067 l = varp->vval.v_list;
20068 if (l == NULL)
20069 return NULL;
20070
20071 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020072 pos.lnum = list_find_nr(l, 0L, &error);
20073 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020074 return NULL; /* invalid line number */
20075
20076 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020077 pos.col = list_find_nr(l, 1L, &error);
20078 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020079 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020080 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020081
20082 /* We accept "$" for the column number: last column. */
20083 li = list_find(l, 1L);
20084 if (li != NULL && li->li_tv.v_type == VAR_STRING
20085 && li->li_tv.vval.v_string != NULL
20086 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20087 pos.col = len + 1;
20088
Bram Moolenaara5525202006-03-02 22:52:09 +000020089 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020090 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020091 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020092 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020093
Bram Moolenaara5525202006-03-02 22:52:09 +000020094#ifdef FEAT_VIRTUALEDIT
20095 /* Get the virtual offset. Defaults to zero. */
20096 pos.coladd = list_find_nr(l, 2L, &error);
20097 if (error)
20098 pos.coladd = 0;
20099#endif
20100
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020101 return &pos;
20102 }
20103
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020104 name = get_tv_string_chk(varp);
20105 if (name == NULL)
20106 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020107 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020109 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20110 {
20111 if (VIsual_active)
20112 return &VIsual;
20113 return &curwin->w_cursor;
20114 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020115 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020116 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020117 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020118 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20119 return NULL;
20120 return pp;
20121 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020122
20123#ifdef FEAT_VIRTUALEDIT
20124 pos.coladd = 0;
20125#endif
20126
Bram Moolenaar477933c2007-07-17 14:32:23 +000020127 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020128 {
20129 pos.col = 0;
20130 if (name[1] == '0') /* "w0": first visible line */
20131 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020132 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020133 pos.lnum = curwin->w_topline;
20134 return &pos;
20135 }
20136 else if (name[1] == '$') /* "w$": last visible line */
20137 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020138 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020139 pos.lnum = curwin->w_botline - 1;
20140 return &pos;
20141 }
20142 }
20143 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020145 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020146 {
20147 pos.lnum = curbuf->b_ml.ml_line_count;
20148 pos.col = 0;
20149 }
20150 else
20151 {
20152 pos.lnum = curwin->w_cursor.lnum;
20153 pos.col = (colnr_T)STRLEN(ml_get_curline());
20154 }
20155 return &pos;
20156 }
20157 return NULL;
20158}
20159
20160/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020161 * Convert list in "arg" into a position and optional file number.
20162 * When "fnump" is NULL there is no file number, only 3 items.
20163 * Note that the column is passed on as-is, the caller may want to decrement
20164 * it to use 1 for the first column.
20165 * Return FAIL when conversion is not possible, doesn't check the position for
20166 * validity.
20167 */
20168 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020169list2fpos(
20170 typval_T *arg,
20171 pos_T *posp,
20172 int *fnump,
20173 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020174{
20175 list_T *l = arg->vval.v_list;
20176 long i = 0;
20177 long n;
20178
Bram Moolenaar493c1782014-05-28 14:34:46 +020020179 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20180 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020181 if (arg->v_type != VAR_LIST
20182 || l == NULL
20183 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020184 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020185 return FAIL;
20186
20187 if (fnump != NULL)
20188 {
20189 n = list_find_nr(l, i++, NULL); /* fnum */
20190 if (n < 0)
20191 return FAIL;
20192 if (n == 0)
20193 n = curbuf->b_fnum; /* current buffer */
20194 *fnump = n;
20195 }
20196
20197 n = list_find_nr(l, i++, NULL); /* lnum */
20198 if (n < 0)
20199 return FAIL;
20200 posp->lnum = n;
20201
20202 n = list_find_nr(l, i++, NULL); /* col */
20203 if (n < 0)
20204 return FAIL;
20205 posp->col = n;
20206
20207#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020208 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020209 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020210 posp->coladd = 0;
20211 else
20212 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020213#endif
20214
Bram Moolenaar493c1782014-05-28 14:34:46 +020020215 if (curswantp != NULL)
20216 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20217
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020218 return OK;
20219}
20220
20221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222 * Get the length of an environment variable name.
20223 * Advance "arg" to the first character after the name.
20224 * Return 0 for error.
20225 */
20226 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020227get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228{
20229 char_u *p;
20230 int len;
20231
20232 for (p = *arg; vim_isIDc(*p); ++p)
20233 ;
20234 if (p == *arg) /* no name found */
20235 return 0;
20236
20237 len = (int)(p - *arg);
20238 *arg = p;
20239 return len;
20240}
20241
20242/*
20243 * Get the length of the name of a function or internal variable.
20244 * "arg" is advanced to the first non-white character after the name.
20245 * Return 0 if something is wrong.
20246 */
20247 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020248get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249{
20250 char_u *p;
20251 int len;
20252
20253 /* Find the end of the name. */
20254 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020255 {
20256 if (*p == ':')
20257 {
20258 /* "s:" is start of "s:var", but "n:" is not and can be used in
20259 * slice "[n:]". Also "xx:" is not a namespace. */
20260 len = (int)(p - *arg);
20261 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20262 || len > 1)
20263 break;
20264 }
20265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020266 if (p == *arg) /* no name found */
20267 return 0;
20268
20269 len = (int)(p - *arg);
20270 *arg = skipwhite(p);
20271
20272 return len;
20273}
20274
20275/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020276 * Get the length of the name of a variable or function.
20277 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020278 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020279 * Return -1 if curly braces expansion failed.
20280 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020281 * If the name contains 'magic' {}'s, expand them and return the
20282 * expanded name in an allocated string via 'alias' - caller must free.
20283 */
20284 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020285get_name_len(
20286 char_u **arg,
20287 char_u **alias,
20288 int evaluate,
20289 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020290{
20291 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292 char_u *p;
20293 char_u *expr_start;
20294 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295
20296 *alias = NULL; /* default to no alias */
20297
20298 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20299 && (*arg)[2] == (int)KE_SNR)
20300 {
20301 /* hard coded <SNR>, already translated */
20302 *arg += 3;
20303 return get_id_len(arg) + 3;
20304 }
20305 len = eval_fname_script(*arg);
20306 if (len > 0)
20307 {
20308 /* literal "<SID>", "s:" or "<SNR>" */
20309 *arg += len;
20310 }
20311
Bram Moolenaar071d4272004-06-13 20:20:40 +000020312 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020313 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020314 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020315 p = find_name_end(*arg, &expr_start, &expr_end,
20316 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020317 if (expr_start != NULL)
20318 {
20319 char_u *temp_string;
20320
20321 if (!evaluate)
20322 {
20323 len += (int)(p - *arg);
20324 *arg = skipwhite(p);
20325 return len;
20326 }
20327
20328 /*
20329 * Include any <SID> etc in the expanded string:
20330 * Thus the -len here.
20331 */
20332 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20333 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020334 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020335 *alias = temp_string;
20336 *arg = skipwhite(p);
20337 return (int)STRLEN(temp_string);
20338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020339
20340 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020341 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020342 EMSG2(_(e_invexpr2), *arg);
20343
20344 return len;
20345}
20346
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020347/*
20348 * Find the end of a variable or function name, taking care of magic braces.
20349 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20350 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020351 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020352 * Return a pointer to just after the name. Equal to "arg" if there is no
20353 * valid name.
20354 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020355 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020356find_name_end(
20357 char_u *arg,
20358 char_u **expr_start,
20359 char_u **expr_end,
20360 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020361{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020362 int mb_nest = 0;
20363 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020364 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020365 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020366
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020367 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020368 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020369 *expr_start = NULL;
20370 *expr_end = NULL;
20371 }
20372
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020373 /* Quick check for valid starting character. */
20374 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20375 return arg;
20376
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020377 for (p = arg; *p != NUL
20378 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020379 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020380 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020381 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020382 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020383 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020384 if (*p == '\'')
20385 {
20386 /* skip over 'string' to avoid counting [ and ] inside it. */
20387 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20388 ;
20389 if (*p == NUL)
20390 break;
20391 }
20392 else if (*p == '"')
20393 {
20394 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20395 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20396 if (*p == '\\' && p[1] != NUL)
20397 ++p;
20398 if (*p == NUL)
20399 break;
20400 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020401 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20402 {
20403 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020404 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020405 len = (int)(p - arg);
20406 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020407 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020408 break;
20409 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020410
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020411 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020412 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020413 if (*p == '[')
20414 ++br_nest;
20415 else if (*p == ']')
20416 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020417 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020418
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020419 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020420 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020421 if (*p == '{')
20422 {
20423 mb_nest++;
20424 if (expr_start != NULL && *expr_start == NULL)
20425 *expr_start = p;
20426 }
20427 else if (*p == '}')
20428 {
20429 mb_nest--;
20430 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20431 *expr_end = p;
20432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020434 }
20435
20436 return p;
20437}
20438
20439/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020440 * Expands out the 'magic' {}'s in a variable/function name.
20441 * Note that this can call itself recursively, to deal with
20442 * constructs like foo{bar}{baz}{bam}
20443 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20444 * "in_start" ^
20445 * "expr_start" ^
20446 * "expr_end" ^
20447 * "in_end" ^
20448 *
20449 * Returns a new allocated string, which the caller must free.
20450 * Returns NULL for failure.
20451 */
20452 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020453make_expanded_name(
20454 char_u *in_start,
20455 char_u *expr_start,
20456 char_u *expr_end,
20457 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020458{
20459 char_u c1;
20460 char_u *retval = NULL;
20461 char_u *temp_result;
20462 char_u *nextcmd = NULL;
20463
20464 if (expr_end == NULL || in_end == NULL)
20465 return NULL;
20466 *expr_start = NUL;
20467 *expr_end = NUL;
20468 c1 = *in_end;
20469 *in_end = NUL;
20470
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020471 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020472 if (temp_result != NULL && nextcmd == NULL)
20473 {
20474 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20475 + (in_end - expr_end) + 1));
20476 if (retval != NULL)
20477 {
20478 STRCPY(retval, in_start);
20479 STRCAT(retval, temp_result);
20480 STRCAT(retval, expr_end + 1);
20481 }
20482 }
20483 vim_free(temp_result);
20484
20485 *in_end = c1; /* put char back for error messages */
20486 *expr_start = '{';
20487 *expr_end = '}';
20488
20489 if (retval != NULL)
20490 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020491 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020492 if (expr_start != NULL)
20493 {
20494 /* Further expansion! */
20495 temp_result = make_expanded_name(retval, expr_start,
20496 expr_end, temp_result);
20497 vim_free(retval);
20498 retval = temp_result;
20499 }
20500 }
20501
20502 return retval;
20503}
20504
20505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020506 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020507 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020508 */
20509 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020510eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020511{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020512 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20513}
20514
20515/*
20516 * Return TRUE if character "c" can be used as the first character in a
20517 * variable or function name (excluding '{' and '}').
20518 */
20519 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020520eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020521{
20522 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020523}
20524
20525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020526 * Set number v: variable to "val".
20527 */
20528 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020529set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020530{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020531 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020532}
20533
20534/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020535 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536 */
20537 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010020538get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020539{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020540 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020541}
20542
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020543/*
20544 * Get string v: variable value. Uses a static buffer, can only be used once.
20545 */
20546 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020547get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020548{
20549 return get_tv_string(&vimvars[idx].vv_tv);
20550}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020551
Bram Moolenaar071d4272004-06-13 20:20:40 +000020552/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020553 * Get List v: variable value. Caller must take care of reference count when
20554 * needed.
20555 */
20556 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020557get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000020558{
20559 return vimvars[idx].vv_list;
20560}
20561
20562/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020563 * Set v:char to character "c".
20564 */
20565 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020566set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020567{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020568 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020569
20570#ifdef FEAT_MBYTE
20571 if (has_mbyte)
20572 buf[(*mb_char2bytes)(c, buf)] = NUL;
20573 else
20574#endif
20575 {
20576 buf[0] = c;
20577 buf[1] = NUL;
20578 }
20579 set_vim_var_string(VV_CHAR, buf, -1);
20580}
20581
20582/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020583 * Set v:count to "count" and v:count1 to "count1".
20584 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020585 */
20586 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020587set_vcount(
20588 long count,
20589 long count1,
20590 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020591{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020592 if (set_prevcount)
20593 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020594 vimvars[VV_COUNT].vv_nr = count;
20595 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020596}
20597
20598/*
20599 * Set string v: variable to a copy of "val".
20600 */
20601 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020602set_vim_var_string(
20603 int idx,
20604 char_u *val,
20605 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020606{
Bram Moolenaara542c682016-01-31 16:28:04 +010020607 clear_tv(&vimvars[idx].vv_di.di_tv);
20608 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020609 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020610 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020612 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020613 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020614 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020615}
20616
20617/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020618 * Set List v: variable to "val".
20619 */
20620 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020621set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000020622{
Bram Moolenaara542c682016-01-31 16:28:04 +010020623 clear_tv(&vimvars[idx].vv_di.di_tv);
20624 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000020625 vimvars[idx].vv_list = val;
20626 if (val != NULL)
20627 ++val->lv_refcount;
20628}
20629
20630/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020631 * Set Dictionary v: variable to "val".
20632 */
20633 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020634set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020020635{
20636 int todo;
20637 hashitem_T *hi;
20638
Bram Moolenaara542c682016-01-31 16:28:04 +010020639 clear_tv(&vimvars[idx].vv_di.di_tv);
20640 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020020641 vimvars[idx].vv_dict = val;
20642 if (val != NULL)
20643 {
20644 ++val->dv_refcount;
20645
20646 /* Set readonly */
20647 todo = (int)val->dv_hashtab.ht_used;
20648 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20649 {
20650 if (HASHITEM_EMPTY(hi))
20651 continue;
20652 --todo;
20653 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20654 }
20655 }
20656}
20657
20658/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020659 * Set v:register if needed.
20660 */
20661 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020662set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020663{
20664 char_u regname;
20665
20666 if (c == 0 || c == ' ')
20667 regname = '"';
20668 else
20669 regname = c;
20670 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020671 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020672 set_vim_var_string(VV_REG, &regname, 1);
20673}
20674
20675/*
20676 * Get or set v:exception. If "oldval" == NULL, return the current value.
20677 * Otherwise, restore the value to "oldval" and return NULL.
20678 * Must always be called in pairs to save and restore v:exception! Does not
20679 * take care of memory allocations.
20680 */
20681 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020682v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020683{
20684 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020685 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020686
Bram Moolenaare9a41262005-01-15 22:18:47 +000020687 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020688 return NULL;
20689}
20690
20691/*
20692 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20693 * Otherwise, restore the value to "oldval" and return NULL.
20694 * Must always be called in pairs to save and restore v:throwpoint! Does not
20695 * take care of memory allocations.
20696 */
20697 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020698v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020699{
20700 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020701 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702
Bram Moolenaare9a41262005-01-15 22:18:47 +000020703 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020704 return NULL;
20705}
20706
20707#if defined(FEAT_AUTOCMD) || defined(PROTO)
20708/*
20709 * Set v:cmdarg.
20710 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20711 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20712 * Must always be called in pairs!
20713 */
20714 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020715set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020716{
20717 char_u *oldval;
20718 char_u *newval;
20719 unsigned len;
20720
Bram Moolenaare9a41262005-01-15 22:18:47 +000020721 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020722 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020724 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020725 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020726 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020727 }
20728
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020729 if (eap->force_bin == FORCE_BIN)
20730 len = 6;
20731 else if (eap->force_bin == FORCE_NOBIN)
20732 len = 8;
20733 else
20734 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020735
20736 if (eap->read_edit)
20737 len += 7;
20738
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020739 if (eap->force_ff != 0)
20740 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20741# ifdef FEAT_MBYTE
20742 if (eap->force_enc != 0)
20743 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020744 if (eap->bad_char != 0)
20745 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020746# endif
20747
20748 newval = alloc(len + 1);
20749 if (newval == NULL)
20750 return NULL;
20751
20752 if (eap->force_bin == FORCE_BIN)
20753 sprintf((char *)newval, " ++bin");
20754 else if (eap->force_bin == FORCE_NOBIN)
20755 sprintf((char *)newval, " ++nobin");
20756 else
20757 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020758
20759 if (eap->read_edit)
20760 STRCAT(newval, " ++edit");
20761
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020762 if (eap->force_ff != 0)
20763 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20764 eap->cmd + eap->force_ff);
20765# ifdef FEAT_MBYTE
20766 if (eap->force_enc != 0)
20767 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20768 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020769 if (eap->bad_char == BAD_KEEP)
20770 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20771 else if (eap->bad_char == BAD_DROP)
20772 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20773 else if (eap->bad_char != 0)
20774 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020775# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020776 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020777 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020778}
20779#endif
20780
20781/*
20782 * Get the value of internal variable "name".
20783 * Return OK or FAIL.
20784 */
20785 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020786get_var_tv(
20787 char_u *name,
20788 int len, /* length of "name" */
20789 typval_T *rettv, /* NULL when only checking existence */
20790 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
20791 int verbose, /* may give error message */
20792 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020793{
20794 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020795 typval_T *tv = NULL;
20796 typval_T atv;
20797 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020798 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020799
20800 /* truncate the name, so that we can use strcmp() */
20801 cc = name[len];
20802 name[len] = NUL;
20803
20804 /*
20805 * Check for "b:changedtick".
20806 */
20807 if (STRCMP(name, "b:changedtick") == 0)
20808 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020809 atv.v_type = VAR_NUMBER;
20810 atv.vval.v_number = curbuf->b_changedtick;
20811 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020812 }
20813
20814 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020815 * Check for user-defined variables.
20816 */
20817 else
20818 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020819 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020820 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020821 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020822 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020823 if (dip != NULL)
20824 *dip = v;
20825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020826 }
20827
Bram Moolenaare9a41262005-01-15 22:18:47 +000020828 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020829 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020830 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020831 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020832 ret = FAIL;
20833 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020834 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020835 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020836
20837 name[len] = cc;
20838
20839 return ret;
20840}
20841
20842/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020843 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20844 * Also handle function call with Funcref variable: func(expr)
20845 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20846 */
20847 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020848handle_subscript(
20849 char_u **arg,
20850 typval_T *rettv,
20851 int evaluate, /* do more than finding the end */
20852 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020853{
20854 int ret = OK;
20855 dict_T *selfdict = NULL;
20856 char_u *s;
20857 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020858 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020859
20860 while (ret == OK
20861 && (**arg == '['
20862 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020863 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020864 && !vim_iswhite(*(*arg - 1)))
20865 {
20866 if (**arg == '(')
20867 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020868 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020869 if (evaluate)
20870 {
20871 functv = *rettv;
20872 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020873
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020874 /* Invoke the function. Recursive! */
20875 s = functv.vval.v_string;
20876 }
20877 else
20878 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020879 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020880 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20881 &len, evaluate, selfdict);
20882
20883 /* Clear the funcref afterwards, so that deleting it while
20884 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020885 if (evaluate)
20886 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020887
20888 /* Stop the expression evaluation when immediately aborting on
20889 * error, or when an interrupt occurred or an exception was thrown
20890 * but not caught. */
20891 if (aborting())
20892 {
20893 if (ret == OK)
20894 clear_tv(rettv);
20895 ret = FAIL;
20896 }
20897 dict_unref(selfdict);
20898 selfdict = NULL;
20899 }
20900 else /* **arg == '[' || **arg == '.' */
20901 {
20902 dict_unref(selfdict);
20903 if (rettv->v_type == VAR_DICT)
20904 {
20905 selfdict = rettv->vval.v_dict;
20906 if (selfdict != NULL)
20907 ++selfdict->dv_refcount;
20908 }
20909 else
20910 selfdict = NULL;
20911 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20912 {
20913 clear_tv(rettv);
20914 ret = FAIL;
20915 }
20916 }
20917 }
20918 dict_unref(selfdict);
20919 return ret;
20920}
20921
20922/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020923 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020924 * value).
20925 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020926 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020927alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020928{
Bram Moolenaar33570922005-01-25 22:26:29 +000020929 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020930}
20931
20932/*
20933 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020934 * The string "s" must have been allocated, it is consumed.
20935 * Return NULL for out of memory, the variable otherwise.
20936 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020937 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020938alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020939{
Bram Moolenaar33570922005-01-25 22:26:29 +000020940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020941
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020942 rettv = alloc_tv();
20943 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020944 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020945 rettv->v_type = VAR_STRING;
20946 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020947 }
20948 else
20949 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020950 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020951}
20952
20953/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020954 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020955 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020956 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020957free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958{
20959 if (varp != NULL)
20960 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020961 switch (varp->v_type)
20962 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020963 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020964 func_unref(varp->vval.v_string);
20965 /*FALLTHROUGH*/
20966 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020967 vim_free(varp->vval.v_string);
20968 break;
20969 case VAR_LIST:
20970 list_unref(varp->vval.v_list);
20971 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020972 case VAR_DICT:
20973 dict_unref(varp->vval.v_dict);
20974 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020975 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020976#ifdef FEAT_FLOAT
20977 case VAR_FLOAT:
20978#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020979 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010020980 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020981 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020982 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020983 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020984 break;
20985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986 vim_free(varp);
20987 }
20988}
20989
20990/*
20991 * Free the memory for a variable value and set the value to NULL or 0.
20992 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020993 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020994clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020995{
20996 if (varp != NULL)
20997 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020998 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020999 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021000 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021001 func_unref(varp->vval.v_string);
21002 /*FALLTHROUGH*/
21003 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021004 vim_free(varp->vval.v_string);
21005 varp->vval.v_string = NULL;
21006 break;
21007 case VAR_LIST:
21008 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021009 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021010 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021011 case VAR_DICT:
21012 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021013 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021014 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021015 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021016 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021017 varp->vval.v_number = 0;
21018 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021019#ifdef FEAT_FLOAT
21020 case VAR_FLOAT:
21021 varp->vval.v_float = 0.0;
21022 break;
21023#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021024 case VAR_UNKNOWN:
21025 break;
21026 default:
21027 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021028 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021029 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021030 }
21031}
21032
21033/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021034 * Set the value of a variable to NULL without freeing items.
21035 */
21036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021037init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021038{
21039 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021040 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021041}
21042
21043/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021044 * Get the number value of a variable.
21045 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021046 * For incompatible types, return 0.
21047 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21048 * caller of incompatible types: it sets *denote to TRUE if "denote"
21049 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021050 */
21051 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021052get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021053{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021054 int error = FALSE;
21055
21056 return get_tv_number_chk(varp, &error); /* return 0L on error */
21057}
21058
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021059 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021060get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021061{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021062 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021063
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021064 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021065 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021066 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021067 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021068#ifdef FEAT_FLOAT
21069 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021070 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021071 break;
21072#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021073 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021074 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021075 break;
21076 case VAR_STRING:
21077 if (varp->vval.v_string != NULL)
21078 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021079 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021080 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021081 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021082 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021083 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021084 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021085 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021086 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021087 case VAR_SPECIAL:
21088 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21089 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021090 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021091 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021092 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021094 if (denote == NULL) /* useful for values that must be unsigned */
21095 n = -1;
21096 else
21097 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021098 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021099}
21100
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021101#ifdef FEAT_FLOAT
21102 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021103get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021104{
21105 switch (varp->v_type)
21106 {
21107 case VAR_NUMBER:
21108 return (float_T)(varp->vval.v_number);
21109#ifdef FEAT_FLOAT
21110 case VAR_FLOAT:
21111 return varp->vval.v_float;
21112 break;
21113#endif
21114 case VAR_FUNC:
21115 EMSG(_("E891: Using a Funcref as a Float"));
21116 break;
21117 case VAR_STRING:
21118 EMSG(_("E892: Using a String as a Float"));
21119 break;
21120 case VAR_LIST:
21121 EMSG(_("E893: Using a List as a Float"));
21122 break;
21123 case VAR_DICT:
21124 EMSG(_("E894: Using a Dictionary as a Float"));
21125 break;
21126 default:
21127 EMSG2(_(e_intern2), "get_tv_float()");
21128 break;
21129 }
21130 return 0;
21131}
21132#endif
21133
Bram Moolenaar071d4272004-06-13 20:20:40 +000021134/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021135 * Get the lnum from the first argument.
21136 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021137 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021138 */
21139 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021140get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141{
Bram Moolenaar33570922005-01-25 22:26:29 +000021142 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143 linenr_T lnum;
21144
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021145 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146 if (lnum == 0) /* no valid number, try using line() */
21147 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021148 rettv.v_type = VAR_NUMBER;
21149 f_line(argvars, &rettv);
21150 lnum = rettv.vval.v_number;
21151 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021152 }
21153 return lnum;
21154}
21155
21156/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021157 * Get the lnum from the first argument.
21158 * Also accepts "$", then "buf" is used.
21159 * Returns 0 on error.
21160 */
21161 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021162get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021163{
21164 if (argvars[0].v_type == VAR_STRING
21165 && argvars[0].vval.v_string != NULL
21166 && argvars[0].vval.v_string[0] == '$'
21167 && buf != NULL)
21168 return buf->b_ml.ml_line_count;
21169 return get_tv_number_chk(&argvars[0], NULL);
21170}
21171
21172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021173 * Get the string value of a variable.
21174 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021175 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21176 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021177 * If the String variable has never been set, return an empty string.
21178 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021179 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21180 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181 */
21182 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021183get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021184{
21185 static char_u mybuf[NUMBUFLEN];
21186
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021187 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021188}
21189
21190 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021191get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021193 char_u *res = get_tv_string_buf_chk(varp, buf);
21194
21195 return res != NULL ? res : (char_u *)"";
21196}
21197
Bram Moolenaar7d647822014-04-05 21:28:56 +020021198/*
21199 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21200 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021201 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021202get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021203{
21204 static char_u mybuf[NUMBUFLEN];
21205
21206 return get_tv_string_buf_chk(varp, mybuf);
21207}
21208
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021209 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021210get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021211{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021212 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021213 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021214 case VAR_NUMBER:
21215 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21216 return buf;
21217 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021218 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021219 break;
21220 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021221 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021222 break;
21223 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021224 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021225 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021226#ifdef FEAT_FLOAT
21227 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021228 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021229 break;
21230#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021231 case VAR_STRING:
21232 if (varp->vval.v_string != NULL)
21233 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021234 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021235 case VAR_SPECIAL:
21236 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21237 return buf;
21238
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021239 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021240 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021241 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021242 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021243 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244}
21245
21246/*
21247 * Find variable "name" in the list of variables.
21248 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021249 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021250 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021251 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021252 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021253 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021254find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021255{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021256 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021257 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021258
Bram Moolenaara7043832005-01-21 11:56:39 +000021259 ht = find_var_ht(name, &varname);
21260 if (htp != NULL)
21261 *htp = ht;
21262 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021264 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021265}
21266
21267/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021268 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021269 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021270 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021271 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021272find_var_in_ht(
21273 hashtab_T *ht,
21274 int htname,
21275 char_u *varname,
21276 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021277{
Bram Moolenaar33570922005-01-25 22:26:29 +000021278 hashitem_T *hi;
21279
21280 if (*varname == NUL)
21281 {
21282 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021283 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021284 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021285 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021286 case 'g': return &globvars_var;
21287 case 'v': return &vimvars_var;
21288 case 'b': return &curbuf->b_bufvar;
21289 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021290#ifdef FEAT_WINDOWS
21291 case 't': return &curtab->tp_winvar;
21292#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021293 case 'l': return current_funccal == NULL
21294 ? NULL : &current_funccal->l_vars_var;
21295 case 'a': return current_funccal == NULL
21296 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021297 }
21298 return NULL;
21299 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021300
21301 hi = hash_find(ht, varname);
21302 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021303 {
21304 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021305 * worked find the variable again. Don't auto-load a script if it was
21306 * loaded already, otherwise it would be loaded every time when
21307 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021308 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021309 {
21310 /* Note: script_autoload() may make "hi" invalid. It must either
21311 * be obtained again or not used. */
21312 if (!script_autoload(varname, FALSE) || aborting())
21313 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021314 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021315 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021316 if (HASHITEM_EMPTY(hi))
21317 return NULL;
21318 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021319 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021320}
21321
21322/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021323 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021324 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021325 * Set "varname" to the start of name without ':'.
21326 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021327 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021328find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021329{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021330 hashitem_T *hi;
21331
Bram Moolenaar73627d02015-08-11 15:46:09 +020021332 if (name[0] == NUL)
21333 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021334 if (name[1] != ':')
21335 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021336 /* The name must not start with a colon or #. */
21337 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021338 return NULL;
21339 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021340
21341 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021342 hi = hash_find(&compat_hashtab, name);
21343 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021344 return &compat_hashtab;
21345
Bram Moolenaar071d4272004-06-13 20:20:40 +000021346 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021347 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021348 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021349 }
21350 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021351 if (*name == 'g') /* global variable */
21352 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021353 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21354 */
21355 if (vim_strchr(name + 2, ':') != NULL
21356 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021357 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021358 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021359 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021360 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021361 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021362#ifdef FEAT_WINDOWS
21363 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021364 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021365#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021366 if (*name == 'v') /* v: variable */
21367 return &vimvarht;
21368 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021369 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021370 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021371 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021372 if (*name == 's' /* script variable */
21373 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21374 return &SCRIPT_VARS(current_SID);
21375 return NULL;
21376}
21377
21378/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021379 * Get function call environment based on bactrace debug level
21380 */
21381 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021382get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021383{
21384 int i;
21385 funccall_T *funccal;
21386 funccall_T *temp_funccal;
21387
21388 funccal = current_funccal;
21389 if (debug_backtrace_level > 0)
21390 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021391 for (i = 0; i < debug_backtrace_level; i++)
21392 {
21393 temp_funccal = funccal->caller;
21394 if (temp_funccal)
21395 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021396 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021397 /* backtrace level overflow. reset to max */
21398 debug_backtrace_level = i;
21399 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021400 }
21401 return funccal;
21402}
21403
21404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021405 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021406 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021407 * Returns NULL when it doesn't exist.
21408 */
21409 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021410get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021411{
Bram Moolenaar33570922005-01-25 22:26:29 +000021412 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021413
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021414 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021415 if (v == NULL)
21416 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021417 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021418}
21419
21420/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021421 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021422 * sourcing this script and when executing functions defined in the script.
21423 */
21424 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021425new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021426{
Bram Moolenaara7043832005-01-21 11:56:39 +000021427 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021428 hashtab_T *ht;
21429 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021430
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21432 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021433 /* Re-allocating ga_data means that an ht_array pointing to
21434 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021435 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021436 for (i = 1; i <= ga_scripts.ga_len; ++i)
21437 {
21438 ht = &SCRIPT_VARS(i);
21439 if (ht->ht_mask == HT_INIT_SIZE - 1)
21440 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021441 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021442 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021443 }
21444
Bram Moolenaar071d4272004-06-13 20:20:40 +000021445 while (ga_scripts.ga_len < id)
21446 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021447 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021448 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021449 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021450 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021451 }
21452 }
21453}
21454
21455/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021456 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21457 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021458 */
21459 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021460init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461{
Bram Moolenaar33570922005-01-25 22:26:29 +000021462 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021463 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021464 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021465 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021466 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021467 dict_var->di_tv.vval.v_dict = dict;
21468 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021469 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021470 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21471 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021472}
21473
21474/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021475 * Unreference a dictionary initialized by init_var_dict().
21476 */
21477 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021478unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020021479{
21480 /* Now the dict needs to be freed if no one else is using it, go back to
21481 * normal reference counting. */
21482 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21483 dict_unref(dict);
21484}
21485
21486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021487 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021488 * Frees all allocated variables and the value they contain.
21489 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021490 */
21491 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021492vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021493{
21494 vars_clear_ext(ht, TRUE);
21495}
21496
21497/*
21498 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21499 */
21500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021501vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021502{
Bram Moolenaara7043832005-01-21 11:56:39 +000021503 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021504 hashitem_T *hi;
21505 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021506
Bram Moolenaar33570922005-01-25 22:26:29 +000021507 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021508 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021509 for (hi = ht->ht_array; todo > 0; ++hi)
21510 {
21511 if (!HASHITEM_EMPTY(hi))
21512 {
21513 --todo;
21514
Bram Moolenaar33570922005-01-25 22:26:29 +000021515 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021516 * ht_array might change then. hash_clear() takes care of it
21517 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021518 v = HI2DI(hi);
21519 if (free_val)
21520 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021521 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021522 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021523 }
21524 }
21525 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021526 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021527}
21528
Bram Moolenaara7043832005-01-21 11:56:39 +000021529/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021530 * Delete a variable from hashtab "ht" at item "hi".
21531 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021532 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021534delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021535{
Bram Moolenaar33570922005-01-25 22:26:29 +000021536 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021537
21538 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021539 clear_tv(&di->di_tv);
21540 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021541}
21542
21543/*
21544 * List the value of one internal variable.
21545 */
21546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021547list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021548{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021549 char_u *tofree;
21550 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021551 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021552
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021553 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000021554 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021555 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021556 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021557}
21558
Bram Moolenaar071d4272004-06-13 20:20:40 +000021559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021560list_one_var_a(
21561 char_u *prefix,
21562 char_u *name,
21563 int type,
21564 char_u *string,
21565 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566{
Bram Moolenaar31859182007-08-14 20:41:13 +000021567 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21568 msg_start();
21569 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570 if (name != NULL) /* "a:" vars don't have a name stored */
21571 msg_puts(name);
21572 msg_putchar(' ');
21573 msg_advance(22);
21574 if (type == VAR_NUMBER)
21575 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021576 else if (type == VAR_FUNC)
21577 msg_putchar('*');
21578 else if (type == VAR_LIST)
21579 {
21580 msg_putchar('[');
21581 if (*string == '[')
21582 ++string;
21583 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021584 else if (type == VAR_DICT)
21585 {
21586 msg_putchar('{');
21587 if (*string == '{')
21588 ++string;
21589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021590 else
21591 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021592
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021594
21595 if (type == VAR_FUNC)
21596 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021597 if (*first)
21598 {
21599 msg_clr_eos();
21600 *first = FALSE;
21601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602}
21603
21604/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021605 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021606 * If the variable already exists, the value is updated.
21607 * Otherwise the variable is created.
21608 */
21609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021610set_var(
21611 char_u *name,
21612 typval_T *tv,
21613 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614{
Bram Moolenaar33570922005-01-25 22:26:29 +000021615 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021617 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021619 ht = find_var_ht(name, &varname);
21620 if (ht == NULL || *varname == NUL)
21621 {
21622 EMSG2(_(e_illvar), name);
21623 return;
21624 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021625 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021626
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021627 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21628 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021629
Bram Moolenaar33570922005-01-25 22:26:29 +000021630 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021631 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021632 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021633 if (var_check_ro(v->di_flags, name, FALSE)
21634 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021635 return;
21636 if (v->di_tv.v_type != tv->v_type
21637 && !((v->di_tv.v_type == VAR_STRING
21638 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021639 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021640 || tv->v_type == VAR_NUMBER))
21641#ifdef FEAT_FLOAT
21642 && !((v->di_tv.v_type == VAR_NUMBER
21643 || v->di_tv.v_type == VAR_FLOAT)
21644 && (tv->v_type == VAR_NUMBER
21645 || tv->v_type == VAR_FLOAT))
21646#endif
21647 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021648 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021649 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021650 return;
21651 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021652
21653 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021654 * Handle setting internal v: variables separately where needed to
21655 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021656 */
21657 if (ht == &vimvarht)
21658 {
21659 if (v->di_tv.v_type == VAR_STRING)
21660 {
21661 vim_free(v->di_tv.vval.v_string);
21662 if (copy || tv->v_type != VAR_STRING)
21663 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21664 else
21665 {
21666 /* Take over the string to avoid an extra alloc/free. */
21667 v->di_tv.vval.v_string = tv->vval.v_string;
21668 tv->vval.v_string = NULL;
21669 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021670 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021671 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021672 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021673 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021674 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021675 if (STRCMP(varname, "searchforward") == 0)
21676 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021677#ifdef FEAT_SEARCH_EXTRA
21678 else if (STRCMP(varname, "hlsearch") == 0)
21679 {
21680 no_hlsearch = !v->di_tv.vval.v_number;
21681 redraw_all_later(SOME_VALID);
21682 }
21683#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021684 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021685 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021686 else if (v->di_tv.v_type != tv->v_type)
21687 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021688 }
21689
21690 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021691 }
21692 else /* add a new variable */
21693 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021694 /* Can't add "v:" variable. */
21695 if (ht == &vimvarht)
21696 {
21697 EMSG2(_(e_illvar), name);
21698 return;
21699 }
21700
Bram Moolenaar92124a32005-06-17 22:03:40 +000021701 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021702 if (!valid_varname(varname))
21703 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021704
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021705 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21706 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021707 if (v == NULL)
21708 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021709 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021710 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021711 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021712 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021713 return;
21714 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021715 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021716 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021717
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021718 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021719 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021720 else
21721 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021722 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021723 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021724 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021726}
21727
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021728/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021729 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021730 * Also give an error message.
21731 */
21732 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021733var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000021734{
21735 if (flags & DI_FLAGS_RO)
21736 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021737 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021738 return TRUE;
21739 }
21740 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21741 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021742 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021743 return TRUE;
21744 }
21745 return FALSE;
21746}
21747
21748/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021749 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21750 * Also give an error message.
21751 */
21752 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021753var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021754{
21755 if (flags & DI_FLAGS_FIX)
21756 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021757 EMSG2(_("E795: Cannot delete variable %s"),
21758 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021759 return TRUE;
21760 }
21761 return FALSE;
21762}
21763
21764/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021765 * Check if a funcref is assigned to a valid variable name.
21766 * Return TRUE and give an error if not.
21767 */
21768 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021769var_check_func_name(
21770 char_u *name, /* points to start of variable name */
21771 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021772{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021773 /* Allow for w: b: s: and t:. */
21774 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021775 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21776 ? name[2] : name[0]))
21777 {
21778 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21779 name);
21780 return TRUE;
21781 }
21782 /* Don't allow hiding a function. When "v" is not NULL we might be
21783 * assigning another function to the same var, the type is checked
21784 * below. */
21785 if (new_var && function_exists(name))
21786 {
21787 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21788 name);
21789 return TRUE;
21790 }
21791 return FALSE;
21792}
21793
21794/*
21795 * Check if a variable name is valid.
21796 * Return FALSE and give an error if not.
21797 */
21798 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021799valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021800{
21801 char_u *p;
21802
21803 for (p = varname; *p != NUL; ++p)
21804 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21805 && *p != AUTOLOAD_CHAR)
21806 {
21807 EMSG2(_(e_illvar), varname);
21808 return FALSE;
21809 }
21810 return TRUE;
21811}
21812
21813/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021814 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020021815 * Also give an error message, using "name" or _("name") when use_gettext is
21816 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021817 */
21818 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021819tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021820{
21821 if (lock & VAR_LOCKED)
21822 {
21823 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021824 name == NULL ? (char_u *)_("Unknown")
21825 : use_gettext ? (char_u *)_(name)
21826 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021827 return TRUE;
21828 }
21829 if (lock & VAR_FIXED)
21830 {
21831 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021832 name == NULL ? (char_u *)_("Unknown")
21833 : use_gettext ? (char_u *)_(name)
21834 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021835 return TRUE;
21836 }
21837 return FALSE;
21838}
21839
21840/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021841 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021842 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021843 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021844 * It is OK for "from" and "to" to point to the same item. This is used to
21845 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021846 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021847 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021848copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021849{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021850 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021851 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021852 switch (from->v_type)
21853 {
21854 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021855 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021856 to->vval.v_number = from->vval.v_number;
21857 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021858#ifdef FEAT_FLOAT
21859 case VAR_FLOAT:
21860 to->vval.v_float = from->vval.v_float;
21861 break;
21862#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021863 case VAR_STRING:
21864 case VAR_FUNC:
21865 if (from->vval.v_string == NULL)
21866 to->vval.v_string = NULL;
21867 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021868 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021869 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021870 if (from->v_type == VAR_FUNC)
21871 func_ref(to->vval.v_string);
21872 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021873 break;
21874 case VAR_LIST:
21875 if (from->vval.v_list == NULL)
21876 to->vval.v_list = NULL;
21877 else
21878 {
21879 to->vval.v_list = from->vval.v_list;
21880 ++to->vval.v_list->lv_refcount;
21881 }
21882 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021883 case VAR_DICT:
21884 if (from->vval.v_dict == NULL)
21885 to->vval.v_dict = NULL;
21886 else
21887 {
21888 to->vval.v_dict = from->vval.v_dict;
21889 ++to->vval.v_dict->dv_refcount;
21890 }
21891 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021892 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021893 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021894 break;
21895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021896}
21897
21898/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021899 * Make a copy of an item.
21900 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021901 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21902 * reference to an already copied list/dict can be used.
21903 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021904 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021905 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021906item_copy(
21907 typval_T *from,
21908 typval_T *to,
21909 int deep,
21910 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021911{
21912 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021913 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021914
Bram Moolenaar33570922005-01-25 22:26:29 +000021915 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021916 {
21917 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021918 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021919 }
21920 ++recurse;
21921
21922 switch (from->v_type)
21923 {
21924 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021925#ifdef FEAT_FLOAT
21926 case VAR_FLOAT:
21927#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021928 case VAR_STRING:
21929 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010021930 case VAR_SPECIAL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000021931 copy_tv(from, to);
21932 break;
21933 case VAR_LIST:
21934 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021935 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021936 if (from->vval.v_list == NULL)
21937 to->vval.v_list = NULL;
21938 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21939 {
21940 /* use the copy made earlier */
21941 to->vval.v_list = from->vval.v_list->lv_copylist;
21942 ++to->vval.v_list->lv_refcount;
21943 }
21944 else
21945 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21946 if (to->vval.v_list == NULL)
21947 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021948 break;
21949 case VAR_DICT:
21950 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021951 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021952 if (from->vval.v_dict == NULL)
21953 to->vval.v_dict = NULL;
21954 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21955 {
21956 /* use the copy made earlier */
21957 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21958 ++to->vval.v_dict->dv_refcount;
21959 }
21960 else
21961 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21962 if (to->vval.v_dict == NULL)
21963 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021964 break;
21965 default:
21966 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021967 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021968 }
21969 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021970 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021971}
21972
21973/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021974 * ":echo expr1 ..." print each argument separated with a space, add a
21975 * newline at the end.
21976 * ":echon expr1 ..." print each argument plain.
21977 */
21978 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021979ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021980{
21981 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021982 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021983 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021984 char_u *p;
21985 int needclr = TRUE;
21986 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021987 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988
21989 if (eap->skip)
21990 ++emsg_skip;
21991 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21992 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021993 /* If eval1() causes an error message the text from the command may
21994 * still need to be cleared. E.g., "echo 22,44". */
21995 need_clr_eos = needclr;
21996
Bram Moolenaar071d4272004-06-13 20:20:40 +000021997 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021998 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021999 {
22000 /*
22001 * Report the invalid expression unless the expression evaluation
22002 * has been cancelled due to an aborting error, an interrupt, or an
22003 * exception.
22004 */
22005 if (!aborting())
22006 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022007 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022008 break;
22009 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022010 need_clr_eos = FALSE;
22011
Bram Moolenaar071d4272004-06-13 20:20:40 +000022012 if (!eap->skip)
22013 {
22014 if (atstart)
22015 {
22016 atstart = FALSE;
22017 /* Call msg_start() after eval1(), evaluating the expression
22018 * may cause a message to appear. */
22019 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022020 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022021 /* Mark the saved text as finishing the line, so that what
22022 * follows is displayed on a new line when scrolling back
22023 * at the more prompt. */
22024 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022025 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022027 }
22028 else if (eap->cmdidx == CMD_echo)
22029 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022030 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022031 if (p != NULL)
22032 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022033 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022034 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022035 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022036 if (*p != TAB && needclr)
22037 {
22038 /* remove any text still there from the command */
22039 msg_clr_eos();
22040 needclr = FALSE;
22041 }
22042 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043 }
22044 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022045 {
22046#ifdef FEAT_MBYTE
22047 if (has_mbyte)
22048 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022049 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022050
22051 (void)msg_outtrans_len_attr(p, i, echo_attr);
22052 p += i - 1;
22053 }
22054 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022055#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022056 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022058 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022059 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022060 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022061 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022062 arg = skipwhite(arg);
22063 }
22064 eap->nextcmd = check_nextcmd(arg);
22065
22066 if (eap->skip)
22067 --emsg_skip;
22068 else
22069 {
22070 /* remove text that may still be there from the command */
22071 if (needclr)
22072 msg_clr_eos();
22073 if (eap->cmdidx == CMD_echo)
22074 msg_end();
22075 }
22076}
22077
22078/*
22079 * ":echohl {name}".
22080 */
22081 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022082ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022083{
22084 int id;
22085
22086 id = syn_name2id(eap->arg);
22087 if (id == 0)
22088 echo_attr = 0;
22089 else
22090 echo_attr = syn_id2attr(id);
22091}
22092
22093/*
22094 * ":execute expr1 ..." execute the result of an expression.
22095 * ":echomsg expr1 ..." Print a message
22096 * ":echoerr expr1 ..." Print an error
22097 * Each gets spaces around each argument and a newline at the end for
22098 * echo commands
22099 */
22100 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022101ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022102{
22103 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022104 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022105 int ret = OK;
22106 char_u *p;
22107 garray_T ga;
22108 int len;
22109 int save_did_emsg;
22110
22111 ga_init2(&ga, 1, 80);
22112
22113 if (eap->skip)
22114 ++emsg_skip;
22115 while (*arg != NUL && *arg != '|' && *arg != '\n')
22116 {
22117 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022118 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022119 {
22120 /*
22121 * Report the invalid expression unless the expression evaluation
22122 * has been cancelled due to an aborting error, an interrupt, or an
22123 * exception.
22124 */
22125 if (!aborting())
22126 EMSG2(_(e_invexpr2), p);
22127 ret = FAIL;
22128 break;
22129 }
22130
22131 if (!eap->skip)
22132 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022133 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022134 len = (int)STRLEN(p);
22135 if (ga_grow(&ga, len + 2) == FAIL)
22136 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022137 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022138 ret = FAIL;
22139 break;
22140 }
22141 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022142 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022143 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022144 ga.ga_len += len;
22145 }
22146
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022147 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022148 arg = skipwhite(arg);
22149 }
22150
22151 if (ret != FAIL && ga.ga_data != NULL)
22152 {
22153 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022154 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022156 out_flush();
22157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158 else if (eap->cmdidx == CMD_echoerr)
22159 {
22160 /* We don't want to abort following commands, restore did_emsg. */
22161 save_did_emsg = did_emsg;
22162 EMSG((char_u *)ga.ga_data);
22163 if (!force_abort)
22164 did_emsg = save_did_emsg;
22165 }
22166 else if (eap->cmdidx == CMD_execute)
22167 do_cmdline((char_u *)ga.ga_data,
22168 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22169 }
22170
22171 ga_clear(&ga);
22172
22173 if (eap->skip)
22174 --emsg_skip;
22175
22176 eap->nextcmd = check_nextcmd(arg);
22177}
22178
22179/*
22180 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22181 * "arg" points to the "&" or '+' when called, to "option" when returning.
22182 * Returns NULL when no option name found. Otherwise pointer to the char
22183 * after the option name.
22184 */
22185 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022186find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022187{
22188 char_u *p = *arg;
22189
22190 ++p;
22191 if (*p == 'g' && p[1] == ':')
22192 {
22193 *opt_flags = OPT_GLOBAL;
22194 p += 2;
22195 }
22196 else if (*p == 'l' && p[1] == ':')
22197 {
22198 *opt_flags = OPT_LOCAL;
22199 p += 2;
22200 }
22201 else
22202 *opt_flags = 0;
22203
22204 if (!ASCII_ISALPHA(*p))
22205 return NULL;
22206 *arg = p;
22207
22208 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22209 p += 4; /* termcap option */
22210 else
22211 while (ASCII_ISALPHA(*p))
22212 ++p;
22213 return p;
22214}
22215
22216/*
22217 * ":function"
22218 */
22219 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022220ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022221{
22222 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022223 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022224 int j;
22225 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022226 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022227 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022228 char_u *name = NULL;
22229 char_u *p;
22230 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022231 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022232 garray_T newargs;
22233 garray_T newlines;
22234 int varargs = FALSE;
22235 int mustend = FALSE;
22236 int flags = 0;
22237 ufunc_T *fp;
22238 int indent;
22239 int nesting;
22240 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022241 dictitem_T *v;
22242 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022243 static int func_nr = 0; /* number for nameless function */
22244 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022245 hashtab_T *ht;
22246 int todo;
22247 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022248 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022249
22250 /*
22251 * ":function" without argument: list functions.
22252 */
22253 if (ends_excmd(*eap->arg))
22254 {
22255 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022256 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022257 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022258 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022259 {
22260 if (!HASHITEM_EMPTY(hi))
22261 {
22262 --todo;
22263 fp = HI2UF(hi);
22264 if (!isdigit(*fp->uf_name))
22265 list_func_head(fp, FALSE);
22266 }
22267 }
22268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022269 eap->nextcmd = check_nextcmd(eap->arg);
22270 return;
22271 }
22272
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022273 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022274 * ":function /pat": list functions matching pattern.
22275 */
22276 if (*eap->arg == '/')
22277 {
22278 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22279 if (!eap->skip)
22280 {
22281 regmatch_T regmatch;
22282
22283 c = *p;
22284 *p = NUL;
22285 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22286 *p = c;
22287 if (regmatch.regprog != NULL)
22288 {
22289 regmatch.rm_ic = p_ic;
22290
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022291 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022292 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22293 {
22294 if (!HASHITEM_EMPTY(hi))
22295 {
22296 --todo;
22297 fp = HI2UF(hi);
22298 if (!isdigit(*fp->uf_name)
22299 && vim_regexec(&regmatch, fp->uf_name, 0))
22300 list_func_head(fp, FALSE);
22301 }
22302 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022303 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022304 }
22305 }
22306 if (*p == '/')
22307 ++p;
22308 eap->nextcmd = check_nextcmd(p);
22309 return;
22310 }
22311
22312 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022313 * Get the function name. There are these situations:
22314 * func normal function name
22315 * "name" == func, "fudi.fd_dict" == NULL
22316 * dict.func new dictionary entry
22317 * "name" == NULL, "fudi.fd_dict" set,
22318 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22319 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022320 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022321 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22322 * dict.func existing dict entry that's not a Funcref
22323 * "name" == NULL, "fudi.fd_dict" set,
22324 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022325 * s:func script-local function name
22326 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022327 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022328 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022329 name = trans_function_name(&p, eap->skip, 0, &fudi);
22330 paren = (vim_strchr(p, '(') != NULL);
22331 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022332 {
22333 /*
22334 * Return on an invalid expression in braces, unless the expression
22335 * evaluation has been cancelled due to an aborting error, an
22336 * interrupt, or an exception.
22337 */
22338 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022339 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022340 if (!eap->skip && fudi.fd_newkey != NULL)
22341 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022342 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022343 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022345 else
22346 eap->skip = TRUE;
22347 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022348
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349 /* An error in a function call during evaluation of an expression in magic
22350 * braces should not cause the function not to be defined. */
22351 saved_did_emsg = did_emsg;
22352 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022353
22354 /*
22355 * ":function func" with only function name: list function.
22356 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022357 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022358 {
22359 if (!ends_excmd(*skipwhite(p)))
22360 {
22361 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022362 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022363 }
22364 eap->nextcmd = check_nextcmd(p);
22365 if (eap->nextcmd != NULL)
22366 *p = NUL;
22367 if (!eap->skip && !got_int)
22368 {
22369 fp = find_func(name);
22370 if (fp != NULL)
22371 {
22372 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022373 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022374 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022375 if (FUNCLINE(fp, j) == NULL)
22376 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022377 msg_putchar('\n');
22378 msg_outnum((long)(j + 1));
22379 if (j < 9)
22380 msg_putchar(' ');
22381 if (j < 99)
22382 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022383 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022384 out_flush(); /* show a line at a time */
22385 ui_breakcheck();
22386 }
22387 if (!got_int)
22388 {
22389 msg_putchar('\n');
22390 msg_puts((char_u *)" endfunction");
22391 }
22392 }
22393 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022394 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022395 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022396 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022397 }
22398
22399 /*
22400 * ":function name(arg1, arg2)" Define function.
22401 */
22402 p = skipwhite(p);
22403 if (*p != '(')
22404 {
22405 if (!eap->skip)
22406 {
22407 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022408 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022409 }
22410 /* attempt to continue by skipping some text */
22411 if (vim_strchr(p, '(') != NULL)
22412 p = vim_strchr(p, '(');
22413 }
22414 p = skipwhite(p + 1);
22415
22416 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22417 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22418
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022419 if (!eap->skip)
22420 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022421 /* Check the name of the function. Unless it's a dictionary function
22422 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022423 if (name != NULL)
22424 arg = name;
22425 else
22426 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022427 if (arg != NULL && (fudi.fd_di == NULL
22428 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022429 {
22430 if (*arg == K_SPECIAL)
22431 j = 3;
22432 else
22433 j = 0;
22434 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22435 : eval_isnamec(arg[j])))
22436 ++j;
22437 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022438 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022439 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022440 /* Disallow using the g: dict. */
22441 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22442 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022443 }
22444
Bram Moolenaar071d4272004-06-13 20:20:40 +000022445 /*
22446 * Isolate the arguments: "arg1, arg2, ...)"
22447 */
22448 while (*p != ')')
22449 {
22450 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22451 {
22452 varargs = TRUE;
22453 p += 3;
22454 mustend = TRUE;
22455 }
22456 else
22457 {
22458 arg = p;
22459 while (ASCII_ISALNUM(*p) || *p == '_')
22460 ++p;
22461 if (arg == p || isdigit(*arg)
22462 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22463 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22464 {
22465 if (!eap->skip)
22466 EMSG2(_("E125: Illegal argument: %s"), arg);
22467 break;
22468 }
22469 if (ga_grow(&newargs, 1) == FAIL)
22470 goto erret;
22471 c = *p;
22472 *p = NUL;
22473 arg = vim_strsave(arg);
22474 if (arg == NULL)
22475 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022476
22477 /* Check for duplicate argument name. */
22478 for (i = 0; i < newargs.ga_len; ++i)
22479 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22480 {
22481 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022482 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022483 goto erret;
22484 }
22485
Bram Moolenaar071d4272004-06-13 20:20:40 +000022486 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22487 *p = c;
22488 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022489 if (*p == ',')
22490 ++p;
22491 else
22492 mustend = TRUE;
22493 }
22494 p = skipwhite(p);
22495 if (mustend && *p != ')')
22496 {
22497 if (!eap->skip)
22498 EMSG2(_(e_invarg2), eap->arg);
22499 break;
22500 }
22501 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022502 if (*p != ')')
22503 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022504 ++p; /* skip the ')' */
22505
Bram Moolenaare9a41262005-01-15 22:18:47 +000022506 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022507 for (;;)
22508 {
22509 p = skipwhite(p);
22510 if (STRNCMP(p, "range", 5) == 0)
22511 {
22512 flags |= FC_RANGE;
22513 p += 5;
22514 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022515 else if (STRNCMP(p, "dict", 4) == 0)
22516 {
22517 flags |= FC_DICT;
22518 p += 4;
22519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022520 else if (STRNCMP(p, "abort", 5) == 0)
22521 {
22522 flags |= FC_ABORT;
22523 p += 5;
22524 }
22525 else
22526 break;
22527 }
22528
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022529 /* When there is a line break use what follows for the function body.
22530 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22531 if (*p == '\n')
22532 line_arg = p + 1;
22533 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022534 EMSG(_(e_trailing));
22535
22536 /*
22537 * Read the body of the function, until ":endfunction" is found.
22538 */
22539 if (KeyTyped)
22540 {
22541 /* Check if the function already exists, don't let the user type the
22542 * whole function before telling him it doesn't work! For a script we
22543 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022544 if (!eap->skip && !eap->forceit)
22545 {
22546 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22547 EMSG(_(e_funcdict));
22548 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022549 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022551
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022552 if (!eap->skip && did_emsg)
22553 goto erret;
22554
Bram Moolenaar071d4272004-06-13 20:20:40 +000022555 msg_putchar('\n'); /* don't overwrite the function name */
22556 cmdline_row = msg_row;
22557 }
22558
22559 indent = 2;
22560 nesting = 0;
22561 for (;;)
22562 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022563 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022564 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022565 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022566 saved_wait_return = FALSE;
22567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022568 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022569 sourcing_lnum_off = sourcing_lnum;
22570
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022571 if (line_arg != NULL)
22572 {
22573 /* Use eap->arg, split up in parts by line breaks. */
22574 theline = line_arg;
22575 p = vim_strchr(theline, '\n');
22576 if (p == NULL)
22577 line_arg += STRLEN(line_arg);
22578 else
22579 {
22580 *p = NUL;
22581 line_arg = p + 1;
22582 }
22583 }
22584 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022585 theline = getcmdline(':', 0L, indent);
22586 else
22587 theline = eap->getline(':', eap->cookie, indent);
22588 if (KeyTyped)
22589 lines_left = Rows - 1;
22590 if (theline == NULL)
22591 {
22592 EMSG(_("E126: Missing :endfunction"));
22593 goto erret;
22594 }
22595
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022596 /* Detect line continuation: sourcing_lnum increased more than one. */
22597 if (sourcing_lnum > sourcing_lnum_off + 1)
22598 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22599 else
22600 sourcing_lnum_off = 0;
22601
Bram Moolenaar071d4272004-06-13 20:20:40 +000022602 if (skip_until != NULL)
22603 {
22604 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22605 * don't check for ":endfunc". */
22606 if (STRCMP(theline, skip_until) == 0)
22607 {
22608 vim_free(skip_until);
22609 skip_until = NULL;
22610 }
22611 }
22612 else
22613 {
22614 /* skip ':' and blanks*/
22615 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22616 ;
22617
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022618 /* Check for "endfunction". */
22619 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022620 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022621 if (line_arg == NULL)
22622 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022623 break;
22624 }
22625
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022626 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022627 * at "end". */
22628 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22629 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022630 else if (STRNCMP(p, "if", 2) == 0
22631 || STRNCMP(p, "wh", 2) == 0
22632 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022633 || STRNCMP(p, "try", 3) == 0)
22634 indent += 2;
22635
22636 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022637 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022638 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022639 if (*p == '!')
22640 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022641 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022642 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22643 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022644 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022645 ++nesting;
22646 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022647 }
22648 }
22649
22650 /* Check for ":append" or ":insert". */
22651 p = skip_range(p, NULL);
22652 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22653 || (p[0] == 'i'
22654 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22655 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22656 skip_until = vim_strsave((char_u *)".");
22657
22658 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22659 arg = skipwhite(skiptowhite(p));
22660 if (arg[0] == '<' && arg[1] =='<'
22661 && ((p[0] == 'p' && p[1] == 'y'
22662 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22663 || (p[0] == 'p' && p[1] == 'e'
22664 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22665 || (p[0] == 't' && p[1] == 'c'
22666 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022667 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22668 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022669 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22670 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022671 || (p[0] == 'm' && p[1] == 'z'
22672 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022673 ))
22674 {
22675 /* ":python <<" continues until a dot, like ":append" */
22676 p = skipwhite(arg + 2);
22677 if (*p == NUL)
22678 skip_until = vim_strsave((char_u *)".");
22679 else
22680 skip_until = vim_strsave(p);
22681 }
22682 }
22683
22684 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022685 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022686 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022687 if (line_arg == NULL)
22688 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022689 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022690 }
22691
22692 /* Copy the line to newly allocated memory. get_one_sourceline()
22693 * allocates 250 bytes per line, this saves 80% on average. The cost
22694 * is an extra alloc/free. */
22695 p = vim_strsave(theline);
22696 if (p != NULL)
22697 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022698 if (line_arg == NULL)
22699 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022700 theline = p;
22701 }
22702
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022703 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22704
22705 /* Add NULL lines for continuation lines, so that the line count is
22706 * equal to the index in the growarray. */
22707 while (sourcing_lnum_off-- > 0)
22708 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022709
22710 /* Check for end of eap->arg. */
22711 if (line_arg != NULL && *line_arg == NUL)
22712 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022713 }
22714
22715 /* Don't define the function when skipping commands or when an error was
22716 * detected. */
22717 if (eap->skip || did_emsg)
22718 goto erret;
22719
22720 /*
22721 * If there are no errors, add the function
22722 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022723 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022724 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022725 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022726 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022727 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022728 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022729 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022730 goto erret;
22731 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022732
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022733 fp = find_func(name);
22734 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022735 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022736 if (!eap->forceit)
22737 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022738 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022739 goto erret;
22740 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022741 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022742 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022743 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022744 name);
22745 goto erret;
22746 }
22747 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022748 ga_clear_strings(&(fp->uf_args));
22749 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022750 vim_free(name);
22751 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022753 }
22754 else
22755 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022756 char numbuf[20];
22757
22758 fp = NULL;
22759 if (fudi.fd_newkey == NULL && !eap->forceit)
22760 {
22761 EMSG(_(e_funcdict));
22762 goto erret;
22763 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022764 if (fudi.fd_di == NULL)
22765 {
22766 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022767 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022768 goto erret;
22769 }
22770 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022771 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022772 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022773
22774 /* Give the function a sequential number. Can only be used with a
22775 * Funcref! */
22776 vim_free(name);
22777 sprintf(numbuf, "%d", ++func_nr);
22778 name = vim_strsave((char_u *)numbuf);
22779 if (name == NULL)
22780 goto erret;
22781 }
22782
22783 if (fp == NULL)
22784 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022785 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022786 {
22787 int slen, plen;
22788 char_u *scriptname;
22789
22790 /* Check that the autoload name matches the script name. */
22791 j = FAIL;
22792 if (sourcing_name != NULL)
22793 {
22794 scriptname = autoload_name(name);
22795 if (scriptname != NULL)
22796 {
22797 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022798 plen = (int)STRLEN(p);
22799 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022800 if (slen > plen && fnamecmp(p,
22801 sourcing_name + slen - plen) == 0)
22802 j = OK;
22803 vim_free(scriptname);
22804 }
22805 }
22806 if (j == FAIL)
22807 {
22808 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22809 goto erret;
22810 }
22811 }
22812
22813 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022814 if (fp == NULL)
22815 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022816
22817 if (fudi.fd_dict != NULL)
22818 {
22819 if (fudi.fd_di == NULL)
22820 {
22821 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022822 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022823 if (fudi.fd_di == NULL)
22824 {
22825 vim_free(fp);
22826 goto erret;
22827 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022828 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22829 {
22830 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022831 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022832 goto erret;
22833 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022834 }
22835 else
22836 /* overwrite existing dict entry */
22837 clear_tv(&fudi.fd_di->di_tv);
22838 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022839 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022840 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022841 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022842
22843 /* behave like "dict" was used */
22844 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022845 }
22846
Bram Moolenaar071d4272004-06-13 20:20:40 +000022847 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022848 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010022849 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
22850 {
22851 vim_free(fp);
22852 goto erret;
22853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022854 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022855 fp->uf_args = newargs;
22856 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022857#ifdef FEAT_PROFILE
22858 fp->uf_tml_count = NULL;
22859 fp->uf_tml_total = NULL;
22860 fp->uf_tml_self = NULL;
22861 fp->uf_profiling = FALSE;
22862 if (prof_def_func())
22863 func_do_profile(fp);
22864#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022865 fp->uf_varargs = varargs;
22866 fp->uf_flags = flags;
22867 fp->uf_calls = 0;
22868 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022869 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022870
22871erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872 ga_clear_strings(&newargs);
22873 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022874ret_free:
22875 vim_free(skip_until);
22876 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022877 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022878 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022879 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022880}
22881
22882/*
22883 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022884 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022885 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022886 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010022887 * TFN_INT: internal function name OK
22888 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022889 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022890 * Advances "pp" to just after the function name (if no error).
22891 */
22892 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022893trans_function_name(
22894 char_u **pp,
22895 int skip, /* only find the end, don't evaluate */
22896 int flags,
22897 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022898{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022899 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022900 char_u *start;
22901 char_u *end;
22902 int lead;
22903 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022904 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022905 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022906
22907 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022908 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022909 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022910
22911 /* Check for hard coded <SNR>: already translated function ID (from a user
22912 * command). */
22913 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22914 && (*pp)[2] == (int)KE_SNR)
22915 {
22916 *pp += 3;
22917 len = get_id_len(pp) + 3;
22918 return vim_strnsave(start, len);
22919 }
22920
22921 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22922 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022923 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022924 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022925 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022926
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022927 /* Note that TFN_ flags use the same values as GLV_ flags. */
22928 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022929 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022930 if (end == start)
22931 {
22932 if (!skip)
22933 EMSG(_("E129: Function name required"));
22934 goto theend;
22935 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022936 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022937 {
22938 /*
22939 * Report an invalid expression in braces, unless the expression
22940 * evaluation has been cancelled due to an aborting error, an
22941 * interrupt, or an exception.
22942 */
22943 if (!aborting())
22944 {
22945 if (end != NULL)
22946 EMSG2(_(e_invarg2), start);
22947 }
22948 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022949 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022950 goto theend;
22951 }
22952
22953 if (lv.ll_tv != NULL)
22954 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022955 if (fdp != NULL)
22956 {
22957 fdp->fd_dict = lv.ll_dict;
22958 fdp->fd_newkey = lv.ll_newkey;
22959 lv.ll_newkey = NULL;
22960 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022961 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022962 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22963 {
22964 name = vim_strsave(lv.ll_tv->vval.v_string);
22965 *pp = end;
22966 }
22967 else
22968 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022969 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22970 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022971 EMSG(_(e_funcref));
22972 else
22973 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022974 name = NULL;
22975 }
22976 goto theend;
22977 }
22978
22979 if (lv.ll_name == NULL)
22980 {
22981 /* Error found, but continue after the function name. */
22982 *pp = end;
22983 goto theend;
22984 }
22985
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022986 /* Check if the name is a Funcref. If so, use the value. */
22987 if (lv.ll_exp_name != NULL)
22988 {
22989 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022990 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022991 if (name == lv.ll_exp_name)
22992 name = NULL;
22993 }
22994 else
22995 {
22996 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022997 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022998 if (name == *pp)
22999 name = NULL;
23000 }
23001 if (name != NULL)
23002 {
23003 name = vim_strsave(name);
23004 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023005 if (STRNCMP(name, "<SNR>", 5) == 0)
23006 {
23007 /* Change "<SNR>" to the byte sequence. */
23008 name[0] = K_SPECIAL;
23009 name[1] = KS_EXTRA;
23010 name[2] = (int)KE_SNR;
23011 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23012 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023013 goto theend;
23014 }
23015
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023016 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023017 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023018 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023019 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23020 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23021 {
23022 /* When there was "s:" already or the name expanded to get a
23023 * leading "s:" then remove it. */
23024 lv.ll_name += 2;
23025 len -= 2;
23026 lead = 2;
23027 }
23028 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023029 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023030 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023031 /* skip over "s:" and "g:" */
23032 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023033 lv.ll_name += 2;
23034 len = (int)(end - lv.ll_name);
23035 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023036
23037 /*
23038 * Copy the function name to allocated memory.
23039 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23040 * Accept <SNR>123_name() outside a script.
23041 */
23042 if (skip)
23043 lead = 0; /* do nothing */
23044 else if (lead > 0)
23045 {
23046 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023047 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23048 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023049 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023050 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023051 if (current_SID <= 0)
23052 {
23053 EMSG(_(e_usingsid));
23054 goto theend;
23055 }
23056 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23057 lead += (int)STRLEN(sid_buf);
23058 }
23059 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023060 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023061 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023062 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023063 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023064 goto theend;
23065 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023066 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023067 {
23068 char_u *cp = vim_strchr(lv.ll_name, ':');
23069
23070 if (cp != NULL && cp < end)
23071 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023072 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023073 goto theend;
23074 }
23075 }
23076
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023077 name = alloc((unsigned)(len + lead + 1));
23078 if (name != NULL)
23079 {
23080 if (lead > 0)
23081 {
23082 name[0] = K_SPECIAL;
23083 name[1] = KS_EXTRA;
23084 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023085 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023086 STRCPY(name + 3, sid_buf);
23087 }
23088 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023089 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023090 }
23091 *pp = end;
23092
23093theend:
23094 clear_lval(&lv);
23095 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023096}
23097
23098/*
23099 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23100 * Return 2 if "p" starts with "s:".
23101 * Return 0 otherwise.
23102 */
23103 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023104eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023105{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023106 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23107 * the standard library function. */
23108 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23109 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023110 return 5;
23111 if (p[0] == 's' && p[1] == ':')
23112 return 2;
23113 return 0;
23114}
23115
23116/*
23117 * Return TRUE if "p" starts with "<SID>" or "s:".
23118 * Only works if eval_fname_script() returned non-zero for "p"!
23119 */
23120 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023121eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023122{
23123 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23124}
23125
23126/*
23127 * List the head of the function: "name(arg1, arg2)".
23128 */
23129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023130list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023131{
23132 int j;
23133
23134 msg_start();
23135 if (indent)
23136 MSG_PUTS(" ");
23137 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023138 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023139 {
23140 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023141 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023142 }
23143 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023144 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023146 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023147 {
23148 if (j)
23149 MSG_PUTS(", ");
23150 msg_puts(FUNCARG(fp, j));
23151 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023152 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023153 {
23154 if (j)
23155 MSG_PUTS(", ");
23156 MSG_PUTS("...");
23157 }
23158 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023159 if (fp->uf_flags & FC_ABORT)
23160 MSG_PUTS(" abort");
23161 if (fp->uf_flags & FC_RANGE)
23162 MSG_PUTS(" range");
23163 if (fp->uf_flags & FC_DICT)
23164 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023165 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023166 if (p_verbose > 0)
23167 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023168}
23169
23170/*
23171 * Find a function by name, return pointer to it in ufuncs.
23172 * Return NULL for unknown function.
23173 */
23174 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023175find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023176{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023177 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023178
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023179 hi = hash_find(&func_hashtab, name);
23180 if (!HASHITEM_EMPTY(hi))
23181 return HI2UF(hi);
23182 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023183}
23184
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023185#if defined(EXITFREE) || defined(PROTO)
23186 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023187free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023188{
23189 hashitem_T *hi;
23190
23191 /* Need to start all over every time, because func_free() may change the
23192 * hash table. */
23193 while (func_hashtab.ht_used > 0)
23194 for (hi = func_hashtab.ht_array; ; ++hi)
23195 if (!HASHITEM_EMPTY(hi))
23196 {
23197 func_free(HI2UF(hi));
23198 break;
23199 }
23200}
23201#endif
23202
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023203 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023204translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023205{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023206 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023207 return find_internal_func(name) >= 0;
23208 return find_func(name) != NULL;
23209}
23210
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023211/*
23212 * Return TRUE if a function "name" exists.
23213 */
23214 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023215function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023216{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023217 char_u *nm = name;
23218 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023219 int n = FALSE;
23220
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023221 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23222 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023223 nm = skipwhite(nm);
23224
23225 /* Only accept "funcname", "funcname ", "funcname (..." and
23226 * "funcname(...", not "funcname!...". */
23227 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023228 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023229 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023230 return n;
23231}
23232
Bram Moolenaara1544c02013-05-30 12:35:52 +020023233 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023234get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023235{
23236 char_u *nm = name;
23237 char_u *p;
23238
23239 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23240
23241 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023242 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023243 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023244
Bram Moolenaara1544c02013-05-30 12:35:52 +020023245 vim_free(p);
23246 return NULL;
23247}
23248
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023249/*
23250 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023251 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23252 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023253 */
23254 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023255builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023256{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023257 char_u *p;
23258
23259 if (!ASCII_ISLOWER(name[0]))
23260 return FALSE;
23261 p = vim_strchr(name, AUTOLOAD_CHAR);
23262 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023263}
23264
Bram Moolenaar05159a02005-02-26 23:04:13 +000023265#if defined(FEAT_PROFILE) || defined(PROTO)
23266/*
23267 * Start profiling function "fp".
23268 */
23269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023270func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023271{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023272 int len = fp->uf_lines.ga_len;
23273
23274 if (len == 0)
23275 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023276 fp->uf_tm_count = 0;
23277 profile_zero(&fp->uf_tm_self);
23278 profile_zero(&fp->uf_tm_total);
23279 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023280 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023281 if (fp->uf_tml_total == NULL)
23282 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023283 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023284 if (fp->uf_tml_self == NULL)
23285 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023286 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023287 fp->uf_tml_idx = -1;
23288 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23289 || fp->uf_tml_self == NULL)
23290 return; /* out of memory */
23291
23292 fp->uf_profiling = TRUE;
23293}
23294
23295/*
23296 * Dump the profiling results for all functions in file "fd".
23297 */
23298 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023299func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023300{
23301 hashitem_T *hi;
23302 int todo;
23303 ufunc_T *fp;
23304 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023305 ufunc_T **sorttab;
23306 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023307
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023308 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023309 if (todo == 0)
23310 return; /* nothing to dump */
23311
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023312 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023313
Bram Moolenaar05159a02005-02-26 23:04:13 +000023314 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23315 {
23316 if (!HASHITEM_EMPTY(hi))
23317 {
23318 --todo;
23319 fp = HI2UF(hi);
23320 if (fp->uf_profiling)
23321 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023322 if (sorttab != NULL)
23323 sorttab[st_len++] = fp;
23324
Bram Moolenaar05159a02005-02-26 23:04:13 +000023325 if (fp->uf_name[0] == K_SPECIAL)
23326 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23327 else
23328 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23329 if (fp->uf_tm_count == 1)
23330 fprintf(fd, "Called 1 time\n");
23331 else
23332 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23333 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23334 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23335 fprintf(fd, "\n");
23336 fprintf(fd, "count total (s) self (s)\n");
23337
23338 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23339 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023340 if (FUNCLINE(fp, i) == NULL)
23341 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023342 prof_func_line(fd, fp->uf_tml_count[i],
23343 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023344 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23345 }
23346 fprintf(fd, "\n");
23347 }
23348 }
23349 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023350
23351 if (sorttab != NULL && st_len > 0)
23352 {
23353 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23354 prof_total_cmp);
23355 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23356 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23357 prof_self_cmp);
23358 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23359 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023360
23361 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023362}
Bram Moolenaar73830342005-02-28 22:48:19 +000023363
23364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023365prof_sort_list(
23366 FILE *fd,
23367 ufunc_T **sorttab,
23368 int st_len,
23369 char *title,
23370 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023371{
23372 int i;
23373 ufunc_T *fp;
23374
23375 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23376 fprintf(fd, "count total (s) self (s) function\n");
23377 for (i = 0; i < 20 && i < st_len; ++i)
23378 {
23379 fp = sorttab[i];
23380 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23381 prefer_self);
23382 if (fp->uf_name[0] == K_SPECIAL)
23383 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23384 else
23385 fprintf(fd, " %s()\n", fp->uf_name);
23386 }
23387 fprintf(fd, "\n");
23388}
23389
23390/*
23391 * Print the count and times for one function or function line.
23392 */
23393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023394prof_func_line(
23395 FILE *fd,
23396 int count,
23397 proftime_T *total,
23398 proftime_T *self,
23399 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023400{
23401 if (count > 0)
23402 {
23403 fprintf(fd, "%5d ", count);
23404 if (prefer_self && profile_equal(total, self))
23405 fprintf(fd, " ");
23406 else
23407 fprintf(fd, "%s ", profile_msg(total));
23408 if (!prefer_self && profile_equal(total, self))
23409 fprintf(fd, " ");
23410 else
23411 fprintf(fd, "%s ", profile_msg(self));
23412 }
23413 else
23414 fprintf(fd, " ");
23415}
23416
23417/*
23418 * Compare function for total time sorting.
23419 */
23420 static int
23421#ifdef __BORLANDC__
23422_RTLENTRYF
23423#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023424prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023425{
23426 ufunc_T *p1, *p2;
23427
23428 p1 = *(ufunc_T **)s1;
23429 p2 = *(ufunc_T **)s2;
23430 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23431}
23432
23433/*
23434 * Compare function for self time sorting.
23435 */
23436 static int
23437#ifdef __BORLANDC__
23438_RTLENTRYF
23439#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023440prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023441{
23442 ufunc_T *p1, *p2;
23443
23444 p1 = *(ufunc_T **)s1;
23445 p2 = *(ufunc_T **)s2;
23446 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23447}
23448
Bram Moolenaar05159a02005-02-26 23:04:13 +000023449#endif
23450
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023451/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023452 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023453 * Return TRUE if a package was loaded.
23454 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023455 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023456script_autoload(
23457 char_u *name,
23458 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023459{
23460 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023461 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023462 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023463 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023464
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023465 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023466 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023467 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023468 return FALSE;
23469
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023470 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023471
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023472 /* Find the name in the list of previously loaded package names. Skip
23473 * "autoload/", it's always the same. */
23474 for (i = 0; i < ga_loaded.ga_len; ++i)
23475 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23476 break;
23477 if (!reload && i < ga_loaded.ga_len)
23478 ret = FALSE; /* was loaded already */
23479 else
23480 {
23481 /* Remember the name if it wasn't loaded already. */
23482 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23483 {
23484 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23485 tofree = NULL;
23486 }
23487
23488 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023489 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023490 ret = TRUE;
23491 }
23492
23493 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023494 return ret;
23495}
23496
23497/*
23498 * Return the autoload script name for a function or variable name.
23499 * Returns NULL when out of memory.
23500 */
23501 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023502autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023503{
23504 char_u *p;
23505 char_u *scriptname;
23506
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023507 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023508 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23509 if (scriptname == NULL)
23510 return FALSE;
23511 STRCPY(scriptname, "autoload/");
23512 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023513 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023514 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023515 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023516 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023517 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023518}
23519
Bram Moolenaar071d4272004-06-13 20:20:40 +000023520#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23521
23522/*
23523 * Function given to ExpandGeneric() to obtain the list of user defined
23524 * function names.
23525 */
23526 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023527get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023528{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023529 static long_u done;
23530 static hashitem_T *hi;
23531 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023532
23533 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023534 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023535 done = 0;
23536 hi = func_hashtab.ht_array;
23537 }
23538 if (done < func_hashtab.ht_used)
23539 {
23540 if (done++ > 0)
23541 ++hi;
23542 while (HASHITEM_EMPTY(hi))
23543 ++hi;
23544 fp = HI2UF(hi);
23545
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023546 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023547 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023548
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023549 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23550 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023551
23552 cat_func_name(IObuff, fp);
23553 if (xp->xp_context != EXPAND_USER_FUNC)
23554 {
23555 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023556 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023557 STRCAT(IObuff, ")");
23558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023559 return IObuff;
23560 }
23561 return NULL;
23562}
23563
23564#endif /* FEAT_CMDL_COMPL */
23565
23566/*
23567 * Copy the function name of "fp" to buffer "buf".
23568 * "buf" must be able to hold the function name plus three bytes.
23569 * Takes care of script-local function names.
23570 */
23571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023572cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023573{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023574 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023575 {
23576 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023577 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023578 }
23579 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023580 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023581}
23582
23583/*
23584 * ":delfunction {name}"
23585 */
23586 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023587ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023588{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023589 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023590 char_u *p;
23591 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023592 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023593
23594 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023595 name = trans_function_name(&p, eap->skip, 0, &fudi);
23596 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023597 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023598 {
23599 if (fudi.fd_dict != NULL && !eap->skip)
23600 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023601 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023603 if (!ends_excmd(*skipwhite(p)))
23604 {
23605 vim_free(name);
23606 EMSG(_(e_trailing));
23607 return;
23608 }
23609 eap->nextcmd = check_nextcmd(p);
23610 if (eap->nextcmd != NULL)
23611 *p = NUL;
23612
23613 if (!eap->skip)
23614 fp = find_func(name);
23615 vim_free(name);
23616
23617 if (!eap->skip)
23618 {
23619 if (fp == NULL)
23620 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023621 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023622 return;
23623 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023624 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023625 {
23626 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23627 return;
23628 }
23629
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023630 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023631 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023632 /* Delete the dict item that refers to the function, it will
23633 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023634 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023635 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023636 else
23637 func_free(fp);
23638 }
23639}
23640
23641/*
23642 * Free a function and remove it from the list of functions.
23643 */
23644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023645func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023646{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023647 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023648
23649 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023650 ga_clear_strings(&(fp->uf_args));
23651 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023652#ifdef FEAT_PROFILE
23653 vim_free(fp->uf_tml_count);
23654 vim_free(fp->uf_tml_total);
23655 vim_free(fp->uf_tml_self);
23656#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023657
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023658 /* remove the function from the function hashtable */
23659 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23660 if (HASHITEM_EMPTY(hi))
23661 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023662 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023663 hash_remove(&func_hashtab, hi);
23664
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023665 vim_free(fp);
23666}
23667
23668/*
23669 * Unreference a Function: decrement the reference count and free it when it
23670 * becomes zero. Only for numbered functions.
23671 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023672 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023673func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023674{
23675 ufunc_T *fp;
23676
23677 if (name != NULL && isdigit(*name))
23678 {
23679 fp = find_func(name);
23680 if (fp == NULL)
23681 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023682 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023683 {
23684 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023685 * when "uf_calls" becomes zero. */
23686 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023687 func_free(fp);
23688 }
23689 }
23690}
23691
23692/*
23693 * Count a reference to a Function.
23694 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023695 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023696func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023697{
23698 ufunc_T *fp;
23699
23700 if (name != NULL && isdigit(*name))
23701 {
23702 fp = find_func(name);
23703 if (fp == NULL)
23704 EMSG2(_(e_intern2), "func_ref()");
23705 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023706 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023707 }
23708}
23709
23710/*
23711 * Call a user function.
23712 */
23713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023714call_user_func(
23715 ufunc_T *fp, /* pointer to function */
23716 int argcount, /* nr of args */
23717 typval_T *argvars, /* arguments */
23718 typval_T *rettv, /* return value */
23719 linenr_T firstline, /* first line of range */
23720 linenr_T lastline, /* last line of range */
23721 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023722{
Bram Moolenaar33570922005-01-25 22:26:29 +000023723 char_u *save_sourcing_name;
23724 linenr_T save_sourcing_lnum;
23725 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023726 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023727 int save_did_emsg;
23728 static int depth = 0;
23729 dictitem_T *v;
23730 int fixvar_idx = 0; /* index in fixvar[] */
23731 int i;
23732 int ai;
23733 char_u numbuf[NUMBUFLEN];
23734 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023735 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023736#ifdef FEAT_PROFILE
23737 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023738 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023740
23741 /* If depth of calling is getting too high, don't execute the function */
23742 if (depth >= p_mfd)
23743 {
23744 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023745 rettv->v_type = VAR_NUMBER;
23746 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023747 return;
23748 }
23749 ++depth;
23750
23751 line_breakcheck(); /* check for CTRL-C hit */
23752
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023753 fc = (funccall_T *)alloc(sizeof(funccall_T));
23754 fc->caller = current_funccal;
23755 current_funccal = fc;
23756 fc->func = fp;
23757 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023758 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023759 fc->linenr = 0;
23760 fc->returned = FALSE;
23761 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023762 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023763 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23764 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023765
Bram Moolenaar33570922005-01-25 22:26:29 +000023766 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023767 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023768 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23769 * each argument variable and saves a lot of time.
23770 */
23771 /*
23772 * Init l: variables.
23773 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023774 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023775 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023776 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023777 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23778 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023779 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023780 name = v->di_key;
23781 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023782 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023783 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023784 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023785 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023786 v->di_tv.vval.v_dict = selfdict;
23787 ++selfdict->dv_refcount;
23788 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023789
Bram Moolenaar33570922005-01-25 22:26:29 +000023790 /*
23791 * Init a: variables.
23792 * Set a:0 to "argcount".
23793 * Set a:000 to a list with room for the "..." arguments.
23794 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023795 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023796 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023797 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023798 /* Use "name" to avoid a warning from some compiler that checks the
23799 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023800 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023801 name = v->di_key;
23802 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023803 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023804 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023805 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023806 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023807 v->di_tv.vval.v_list = &fc->l_varlist;
23808 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23809 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23810 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023811
23812 /*
23813 * Set a:firstline to "firstline" and a:lastline to "lastline".
23814 * Set a:name to named arguments.
23815 * Set a:N to the "..." arguments.
23816 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023817 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023818 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023819 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023820 (varnumber_T)lastline);
23821 for (i = 0; i < argcount; ++i)
23822 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023823 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023824 if (ai < 0)
23825 /* named argument a:name */
23826 name = FUNCARG(fp, i);
23827 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023828 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023829 /* "..." argument a:1, a:2, etc. */
23830 sprintf((char *)numbuf, "%d", ai + 1);
23831 name = numbuf;
23832 }
23833 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23834 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023835 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023836 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23837 }
23838 else
23839 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023840 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23841 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023842 if (v == NULL)
23843 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023844 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000023845 }
23846 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023847 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023848
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023849 /* Note: the values are copied directly to avoid alloc/free.
23850 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023851 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023852 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023853
23854 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23855 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023856 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23857 fc->l_listitems[ai].li_tv = argvars[i];
23858 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023859 }
23860 }
23861
Bram Moolenaar071d4272004-06-13 20:20:40 +000023862 /* Don't redraw while executing the function. */
23863 ++RedrawingDisabled;
23864 save_sourcing_name = sourcing_name;
23865 save_sourcing_lnum = sourcing_lnum;
23866 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023867 /* need space for function name + ("function " + 3) or "[number]" */
23868 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
23869 + STRLEN(fp->uf_name) + 20;
23870 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023871 if (sourcing_name != NULL)
23872 {
23873 if (save_sourcing_name != NULL
23874 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023875 sprintf((char *)sourcing_name, "%s[%d]..",
23876 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023877 else
23878 STRCPY(sourcing_name, "function ");
23879 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23880
23881 if (p_verbose >= 12)
23882 {
23883 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023884 verbose_enter_scroll();
23885
Bram Moolenaar555b2802005-05-19 21:08:39 +000023886 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023887 if (p_verbose >= 14)
23888 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023889 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023890 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023891 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023892 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023893
23894 msg_puts((char_u *)"(");
23895 for (i = 0; i < argcount; ++i)
23896 {
23897 if (i > 0)
23898 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023899 if (argvars[i].v_type == VAR_NUMBER)
23900 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023901 else
23902 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023903 /* Do not want errors such as E724 here. */
23904 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023905 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023906 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023907 if (s != NULL)
23908 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023909 if (vim_strsize(s) > MSG_BUF_CLEN)
23910 {
23911 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23912 s = buf;
23913 }
23914 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023915 vim_free(tofree);
23916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023917 }
23918 }
23919 msg_puts((char_u *)")");
23920 }
23921 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023922
23923 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023924 --no_wait_return;
23925 }
23926 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023927#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023928 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023929 {
23930 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23931 func_do_profile(fp);
23932 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023933 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023934 {
23935 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023936 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023937 profile_zero(&fp->uf_tm_children);
23938 }
23939 script_prof_save(&wait_start);
23940 }
23941#endif
23942
Bram Moolenaar071d4272004-06-13 20:20:40 +000023943 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023944 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023945 save_did_emsg = did_emsg;
23946 did_emsg = FALSE;
23947
23948 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023949 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023950 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23951
23952 --RedrawingDisabled;
23953
23954 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023955 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023956 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023957 clear_tv(rettv);
23958 rettv->v_type = VAR_NUMBER;
23959 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023960 }
23961
Bram Moolenaar05159a02005-02-26 23:04:13 +000023962#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023963 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023964 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023965 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023966 profile_end(&call_start);
23967 profile_sub_wait(&wait_start, &call_start);
23968 profile_add(&fp->uf_tm_total, &call_start);
23969 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023970 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023971 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023972 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23973 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023974 }
23975 }
23976#endif
23977
Bram Moolenaar071d4272004-06-13 20:20:40 +000023978 /* when being verbose, mention the return value */
23979 if (p_verbose >= 12)
23980 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023981 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023982 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023983
Bram Moolenaar071d4272004-06-13 20:20:40 +000023984 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023985 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023986 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023987 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023988 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023989 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023990 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023991 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023992 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023993 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023994 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023995
Bram Moolenaar555b2802005-05-19 21:08:39 +000023996 /* The value may be very long. Skip the middle part, so that we
23997 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020023998 * truncate it at the end. Don't want errors such as E724 here. */
23999 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024000 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024001 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024002 if (s != NULL)
24003 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024004 if (vim_strsize(s) > MSG_BUF_CLEN)
24005 {
24006 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24007 s = buf;
24008 }
24009 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024010 vim_free(tofree);
24011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024012 }
24013 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024014
24015 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024016 --no_wait_return;
24017 }
24018
24019 vim_free(sourcing_name);
24020 sourcing_name = save_sourcing_name;
24021 sourcing_lnum = save_sourcing_lnum;
24022 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024023#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024024 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024025 script_prof_restore(&wait_start);
24026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024027
24028 if (p_verbose >= 12 && sourcing_name != NULL)
24029 {
24030 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024031 verbose_enter_scroll();
24032
Bram Moolenaar555b2802005-05-19 21:08:39 +000024033 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024034 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024035
24036 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024037 --no_wait_return;
24038 }
24039
24040 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024041 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024042 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024043
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024044 /* If the a:000 list and the l: and a: dicts are not referenced we can
24045 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024046 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24047 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24048 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24049 {
24050 free_funccal(fc, FALSE);
24051 }
24052 else
24053 {
24054 hashitem_T *hi;
24055 listitem_T *li;
24056 int todo;
24057
24058 /* "fc" is still in use. This can happen when returning "a:000" or
24059 * assigning "l:" to a global variable.
24060 * Link "fc" in the list for garbage collection later. */
24061 fc->caller = previous_funccal;
24062 previous_funccal = fc;
24063
24064 /* Make a copy of the a: variables, since we didn't do that above. */
24065 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24066 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24067 {
24068 if (!HASHITEM_EMPTY(hi))
24069 {
24070 --todo;
24071 v = HI2DI(hi);
24072 copy_tv(&v->di_tv, &v->di_tv);
24073 }
24074 }
24075
24076 /* Make a copy of the a:000 items, since we didn't do that above. */
24077 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24078 copy_tv(&li->li_tv, &li->li_tv);
24079 }
24080}
24081
24082/*
24083 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024084 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024085 */
24086 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024087can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024088{
24089 return (fc->l_varlist.lv_copyID != copyID
24090 && fc->l_vars.dv_copyID != copyID
24091 && fc->l_avars.dv_copyID != copyID);
24092}
24093
24094/*
24095 * Free "fc" and what it contains.
24096 */
24097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024098free_funccal(
24099 funccall_T *fc,
24100 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024101{
24102 listitem_T *li;
24103
24104 /* The a: variables typevals may not have been allocated, only free the
24105 * allocated variables. */
24106 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24107
24108 /* free all l: variables */
24109 vars_clear(&fc->l_vars.dv_hashtab);
24110
24111 /* Free the a:000 variables if they were allocated. */
24112 if (free_val)
24113 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24114 clear_tv(&li->li_tv);
24115
24116 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024117}
24118
24119/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024120 * Add a number variable "name" to dict "dp" with value "nr".
24121 */
24122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024123add_nr_var(
24124 dict_T *dp,
24125 dictitem_T *v,
24126 char *name,
24127 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024128{
24129 STRCPY(v->di_key, name);
24130 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24131 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24132 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024133 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024134 v->di_tv.vval.v_number = nr;
24135}
24136
24137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024138 * ":return [expr]"
24139 */
24140 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024141ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024142{
24143 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024144 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024145 int returning = FALSE;
24146
24147 if (current_funccal == NULL)
24148 {
24149 EMSG(_("E133: :return not inside a function"));
24150 return;
24151 }
24152
24153 if (eap->skip)
24154 ++emsg_skip;
24155
24156 eap->nextcmd = NULL;
24157 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024158 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024159 {
24160 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024161 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024162 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024163 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024164 }
24165 /* It's safer to return also on error. */
24166 else if (!eap->skip)
24167 {
24168 /*
24169 * Return unless the expression evaluation has been cancelled due to an
24170 * aborting error, an interrupt, or an exception.
24171 */
24172 if (!aborting())
24173 returning = do_return(eap, FALSE, TRUE, NULL);
24174 }
24175
24176 /* When skipping or the return gets pending, advance to the next command
24177 * in this line (!returning). Otherwise, ignore the rest of the line.
24178 * Following lines will be ignored by get_func_line(). */
24179 if (returning)
24180 eap->nextcmd = NULL;
24181 else if (eap->nextcmd == NULL) /* no argument */
24182 eap->nextcmd = check_nextcmd(arg);
24183
24184 if (eap->skip)
24185 --emsg_skip;
24186}
24187
24188/*
24189 * Return from a function. Possibly makes the return pending. Also called
24190 * for a pending return at the ":endtry" or after returning from an extra
24191 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024192 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024193 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024194 * FALSE when the return gets pending.
24195 */
24196 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024197do_return(
24198 exarg_T *eap,
24199 int reanimate,
24200 int is_cmd,
24201 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024202{
24203 int idx;
24204 struct condstack *cstack = eap->cstack;
24205
24206 if (reanimate)
24207 /* Undo the return. */
24208 current_funccal->returned = FALSE;
24209
24210 /*
24211 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24212 * not in its finally clause (which then is to be executed next) is found.
24213 * In this case, make the ":return" pending for execution at the ":endtry".
24214 * Otherwise, return normally.
24215 */
24216 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24217 if (idx >= 0)
24218 {
24219 cstack->cs_pending[idx] = CSTP_RETURN;
24220
24221 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024222 /* A pending return again gets pending. "rettv" points to an
24223 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024224 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024225 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024226 else
24227 {
24228 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024229 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024230 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024231 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024232
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024233 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024234 {
24235 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024236 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024237 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024238 else
24239 EMSG(_(e_outofmem));
24240 }
24241 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024242 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024243
24244 if (reanimate)
24245 {
24246 /* The pending return value could be overwritten by a ":return"
24247 * without argument in a finally clause; reset the default
24248 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024249 current_funccal->rettv->v_type = VAR_NUMBER;
24250 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024251 }
24252 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024253 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024254 }
24255 else
24256 {
24257 current_funccal->returned = TRUE;
24258
24259 /* If the return is carried out now, store the return value. For
24260 * a return immediately after reanimation, the value is already
24261 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024262 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024263 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024264 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024265 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024266 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024267 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024268 }
24269 }
24270
24271 return idx < 0;
24272}
24273
24274/*
24275 * Free the variable with a pending return value.
24276 */
24277 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024278discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024279{
Bram Moolenaar33570922005-01-25 22:26:29 +000024280 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024281}
24282
24283/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024284 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024285 * is an allocated string. Used by report_pending() for verbose messages.
24286 */
24287 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024288get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024289{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024290 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024291 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024292 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024293
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024294 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024295 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024296 if (s == NULL)
24297 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024298
24299 STRCPY(IObuff, ":return ");
24300 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24301 if (STRLEN(s) + 8 >= IOSIZE)
24302 STRCPY(IObuff + IOSIZE - 4, "...");
24303 vim_free(tofree);
24304 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024305}
24306
24307/*
24308 * Get next function line.
24309 * Called by do_cmdline() to get the next line.
24310 * Returns allocated string, or NULL for end of function.
24311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024312 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024313get_func_line(
24314 int c UNUSED,
24315 void *cookie,
24316 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024317{
Bram Moolenaar33570922005-01-25 22:26:29 +000024318 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024319 ufunc_T *fp = fcp->func;
24320 char_u *retval;
24321 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024322
24323 /* If breakpoints have been added/deleted need to check for it. */
24324 if (fcp->dbg_tick != debug_tick)
24325 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024326 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024327 sourcing_lnum);
24328 fcp->dbg_tick = debug_tick;
24329 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024330#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024331 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024332 func_line_end(cookie);
24333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024334
Bram Moolenaar05159a02005-02-26 23:04:13 +000024335 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024336 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24337 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024338 retval = NULL;
24339 else
24340 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024341 /* Skip NULL lines (continuation lines). */
24342 while (fcp->linenr < gap->ga_len
24343 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24344 ++fcp->linenr;
24345 if (fcp->linenr >= gap->ga_len)
24346 retval = NULL;
24347 else
24348 {
24349 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24350 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024351#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024352 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024353 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024354#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024356 }
24357
24358 /* Did we encounter a breakpoint? */
24359 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24360 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024361 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024362 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024363 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024364 sourcing_lnum);
24365 fcp->dbg_tick = debug_tick;
24366 }
24367
24368 return retval;
24369}
24370
Bram Moolenaar05159a02005-02-26 23:04:13 +000024371#if defined(FEAT_PROFILE) || defined(PROTO)
24372/*
24373 * Called when starting to read a function line.
24374 * "sourcing_lnum" must be correct!
24375 * When skipping lines it may not actually be executed, but we won't find out
24376 * until later and we need to store the time now.
24377 */
24378 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024379func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024380{
24381 funccall_T *fcp = (funccall_T *)cookie;
24382 ufunc_T *fp = fcp->func;
24383
24384 if (fp->uf_profiling && sourcing_lnum >= 1
24385 && sourcing_lnum <= fp->uf_lines.ga_len)
24386 {
24387 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024388 /* Skip continuation lines. */
24389 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24390 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024391 fp->uf_tml_execed = FALSE;
24392 profile_start(&fp->uf_tml_start);
24393 profile_zero(&fp->uf_tml_children);
24394 profile_get_wait(&fp->uf_tml_wait);
24395 }
24396}
24397
24398/*
24399 * Called when actually executing a function line.
24400 */
24401 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024402func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024403{
24404 funccall_T *fcp = (funccall_T *)cookie;
24405 ufunc_T *fp = fcp->func;
24406
24407 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24408 fp->uf_tml_execed = TRUE;
24409}
24410
24411/*
24412 * Called when done with a function line.
24413 */
24414 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024415func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024416{
24417 funccall_T *fcp = (funccall_T *)cookie;
24418 ufunc_T *fp = fcp->func;
24419
24420 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24421 {
24422 if (fp->uf_tml_execed)
24423 {
24424 ++fp->uf_tml_count[fp->uf_tml_idx];
24425 profile_end(&fp->uf_tml_start);
24426 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024427 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024428 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24429 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024430 }
24431 fp->uf_tml_idx = -1;
24432 }
24433}
24434#endif
24435
Bram Moolenaar071d4272004-06-13 20:20:40 +000024436/*
24437 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024438 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024439 */
24440 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024441func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024442{
Bram Moolenaar33570922005-01-25 22:26:29 +000024443 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024444
24445 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24446 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024447 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024448 || fcp->returned);
24449}
24450
24451/*
24452 * return TRUE if cookie indicates a function which "abort"s on errors.
24453 */
24454 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024455func_has_abort(
24456 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024457{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024458 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024459}
24460
24461#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24462typedef enum
24463{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024464 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24465 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24466 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024467} var_flavour_T;
24468
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024469static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024470
24471 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010024472var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024473{
24474 char_u *p = varname;
24475
24476 if (ASCII_ISUPPER(*p))
24477 {
24478 while (*(++p))
24479 if (ASCII_ISLOWER(*p))
24480 return VAR_FLAVOUR_SESSION;
24481 return VAR_FLAVOUR_VIMINFO;
24482 }
24483 else
24484 return VAR_FLAVOUR_DEFAULT;
24485}
24486#endif
24487
24488#if defined(FEAT_VIMINFO) || defined(PROTO)
24489/*
24490 * Restore global vars that start with a capital from the viminfo file
24491 */
24492 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024493read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024494{
24495 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024496 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024497 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024498 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024499
24500 if (!writing && (find_viminfo_parameter('!') != NULL))
24501 {
24502 tab = vim_strchr(virp->vir_line + 1, '\t');
24503 if (tab != NULL)
24504 {
24505 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024506 switch (*tab)
24507 {
24508 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024509#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024510 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024511#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024512 case 'D': type = VAR_DICT; break;
24513 case 'L': type = VAR_LIST; break;
24514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024515
24516 tab = vim_strchr(tab, '\t');
24517 if (tab != NULL)
24518 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024519 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024520 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024521 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024522 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024523#ifdef FEAT_FLOAT
24524 else if (type == VAR_FLOAT)
24525 (void)string2float(tab + 1, &tv.vval.v_float);
24526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024527 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024528 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024529 if (type == VAR_DICT || type == VAR_LIST)
24530 {
24531 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24532
24533 if (etv == NULL)
24534 /* Failed to parse back the dict or list, use it as a
24535 * string. */
24536 tv.v_type = VAR_STRING;
24537 else
24538 {
24539 vim_free(tv.vval.v_string);
24540 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024541 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024542 }
24543 }
24544
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024545 /* when in a function use global variables */
24546 save_funccal = current_funccal;
24547 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024548 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024549 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024550
24551 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024552 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024553 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24554 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024555 }
24556 }
24557 }
24558
24559 return viminfo_readline(virp);
24560}
24561
24562/*
24563 * Write global vars that start with a capital to the viminfo file
24564 */
24565 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024566write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024567{
Bram Moolenaar33570922005-01-25 22:26:29 +000024568 hashitem_T *hi;
24569 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024570 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024571 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024572 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024573 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024574 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024575
24576 if (find_viminfo_parameter('!') == NULL)
24577 return;
24578
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024579 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024580
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024581 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024582 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024583 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024584 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024585 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024586 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024587 this_var = HI2DI(hi);
24588 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024589 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024590 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024591 {
24592 case VAR_STRING: s = "STR"; break;
24593 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024594#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024595 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024596#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024597 case VAR_DICT: s = "DIC"; break;
24598 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024599 default: continue;
24600 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024601 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024602 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024603 if (p != NULL)
24604 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024605 vim_free(tofree);
24606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024607 }
24608 }
24609}
24610#endif
24611
24612#if defined(FEAT_SESSION) || defined(PROTO)
24613 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024614store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024615{
Bram Moolenaar33570922005-01-25 22:26:29 +000024616 hashitem_T *hi;
24617 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024618 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024619 char_u *p, *t;
24620
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024621 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024622 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024623 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024624 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024625 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024626 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024627 this_var = HI2DI(hi);
24628 if ((this_var->di_tv.v_type == VAR_NUMBER
24629 || this_var->di_tv.v_type == VAR_STRING)
24630 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024631 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024632 /* Escape special characters with a backslash. Turn a LF and
24633 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024634 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024635 (char_u *)"\\\"\n\r");
24636 if (p == NULL) /* out of memory */
24637 break;
24638 for (t = p; *t != NUL; ++t)
24639 if (*t == '\n')
24640 *t = 'n';
24641 else if (*t == '\r')
24642 *t = 'r';
24643 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024644 this_var->di_key,
24645 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24646 : ' ',
24647 p,
24648 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24649 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024650 || put_eol(fd) == FAIL)
24651 {
24652 vim_free(p);
24653 return FAIL;
24654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024655 vim_free(p);
24656 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024657#ifdef FEAT_FLOAT
24658 else if (this_var->di_tv.v_type == VAR_FLOAT
24659 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24660 {
24661 float_T f = this_var->di_tv.vval.v_float;
24662 int sign = ' ';
24663
24664 if (f < 0)
24665 {
24666 f = -f;
24667 sign = '-';
24668 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024669 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024670 this_var->di_key, sign, f) < 0)
24671 || put_eol(fd) == FAIL)
24672 return FAIL;
24673 }
24674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024675 }
24676 }
24677 return OK;
24678}
24679#endif
24680
Bram Moolenaar661b1822005-07-28 22:36:45 +000024681/*
24682 * Display script name where an item was last set.
24683 * Should only be invoked when 'verbose' is non-zero.
24684 */
24685 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024686last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000024687{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024688 char_u *p;
24689
Bram Moolenaar661b1822005-07-28 22:36:45 +000024690 if (scriptID != 0)
24691 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024692 p = home_replace_save(NULL, get_scriptname(scriptID));
24693 if (p != NULL)
24694 {
24695 verbose_enter();
24696 MSG_PUTS(_("\n\tLast set from "));
24697 MSG_PUTS(p);
24698 vim_free(p);
24699 verbose_leave();
24700 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024701 }
24702}
24703
Bram Moolenaard812df62008-11-09 12:46:09 +000024704/*
24705 * List v:oldfiles in a nice way.
24706 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024707 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024708ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000024709{
24710 list_T *l = vimvars[VV_OLDFILES].vv_list;
24711 listitem_T *li;
24712 int nr = 0;
24713
24714 if (l == NULL)
24715 msg((char_u *)_("No old files"));
24716 else
24717 {
24718 msg_start();
24719 msg_scroll = TRUE;
24720 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24721 {
24722 msg_outnum((long)++nr);
24723 MSG_PUTS(": ");
24724 msg_outtrans(get_tv_string(&li->li_tv));
24725 msg_putchar('\n');
24726 out_flush(); /* output one line at a time */
24727 ui_breakcheck();
24728 }
24729 /* Assume "got_int" was set to truncate the listing. */
24730 got_int = FALSE;
24731
24732#ifdef FEAT_BROWSE_CMD
24733 if (cmdmod.browse)
24734 {
24735 quit_more = FALSE;
24736 nr = prompt_for_number(FALSE);
24737 msg_starthere();
24738 if (nr > 0)
24739 {
24740 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24741 (long)nr);
24742
24743 if (p != NULL)
24744 {
24745 p = expand_env_save(p);
24746 eap->arg = p;
24747 eap->cmdidx = CMD_edit;
24748 cmdmod.browse = FALSE;
24749 do_exedit(eap, NULL);
24750 vim_free(p);
24751 }
24752 }
24753 }
24754#endif
24755 }
24756}
24757
Bram Moolenaar53744302015-07-17 17:38:22 +020024758/* reset v:option_new, v:option_old and v:option_type */
24759 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024760reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020024761{
24762 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
24763 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
24764 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
24765}
24766
24767
Bram Moolenaar071d4272004-06-13 20:20:40 +000024768#endif /* FEAT_EVAL */
24769
Bram Moolenaar071d4272004-06-13 20:20:40 +000024770
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024771#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024772
24773#ifdef WIN3264
24774/*
24775 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24776 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024777static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
24778static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
24779static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024780
24781/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024782 * Get the short path (8.3) for the filename in "fnamep".
24783 * Only works for a valid file name.
24784 * When the path gets longer "fnamep" is changed and the allocated buffer
24785 * is put in "bufp".
24786 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24787 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024788 */
24789 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024790get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024791{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024792 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024793 char_u *newbuf;
24794
24795 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024796 l = GetShortPathName(*fnamep, *fnamep, len);
24797 if (l > len - 1)
24798 {
24799 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024800 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024801 newbuf = vim_strnsave(*fnamep, l);
24802 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024803 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024804
24805 vim_free(*bufp);
24806 *fnamep = *bufp = newbuf;
24807
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024808 /* Really should always succeed, as the buffer is big enough. */
24809 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024810 }
24811
24812 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024813 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024814}
24815
24816/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024817 * Get the short path (8.3) for the filename in "fname". The converted
24818 * path is returned in "bufp".
24819 *
24820 * Some of the directories specified in "fname" may not exist. This function
24821 * will shorten the existing directories at the beginning of the path and then
24822 * append the remaining non-existing path.
24823 *
24824 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024825 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024826 * bufp - Pointer to an allocated buffer for the filename.
24827 * fnamelen - Length of the filename pointed to by fname
24828 *
24829 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024830 */
24831 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024832shortpath_for_invalid_fname(
24833 char_u **fname,
24834 char_u **bufp,
24835 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024836{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024837 char_u *short_fname, *save_fname, *pbuf_unused;
24838 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024839 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024840 int old_len, len;
24841 int new_len, sfx_len;
24842 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024843
24844 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024845 old_len = *fnamelen;
24846 save_fname = vim_strnsave(*fname, old_len);
24847 pbuf_unused = NULL;
24848 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024849
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024850 endp = save_fname + old_len - 1; /* Find the end of the copy */
24851 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024852
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024853 /*
24854 * Try shortening the supplied path till it succeeds by removing one
24855 * directory at a time from the tail of the path.
24856 */
24857 len = 0;
24858 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024859 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024860 /* go back one path-separator */
24861 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24862 --endp;
24863 if (endp <= save_fname)
24864 break; /* processed the complete path */
24865
24866 /*
24867 * Replace the path separator with a NUL and try to shorten the
24868 * resulting path.
24869 */
24870 ch = *endp;
24871 *endp = 0;
24872 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024873 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024874 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24875 {
24876 retval = FAIL;
24877 goto theend;
24878 }
24879 *endp = ch; /* preserve the string */
24880
24881 if (len > 0)
24882 break; /* successfully shortened the path */
24883
24884 /* failed to shorten the path. Skip the path separator */
24885 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024886 }
24887
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024888 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024889 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024890 /*
24891 * Succeeded in shortening the path. Now concatenate the shortened
24892 * path with the remaining path at the tail.
24893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024894
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024895 /* Compute the length of the new path. */
24896 sfx_len = (int)(save_endp - endp) + 1;
24897 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024898
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024899 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024900 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024901 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024902 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024903 /* There is not enough space in the currently allocated string,
24904 * copy it to a buffer big enough. */
24905 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024906 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024907 {
24908 retval = FAIL;
24909 goto theend;
24910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024911 }
24912 else
24913 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024914 /* Transfer short_fname to the main buffer (it's big enough),
24915 * unless get_short_pathname() did its work in-place. */
24916 *fname = *bufp = save_fname;
24917 if (short_fname != save_fname)
24918 vim_strncpy(save_fname, short_fname, len);
24919 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024920 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024921
24922 /* concat the not-shortened part of the path */
24923 vim_strncpy(*fname + len, endp, sfx_len);
24924 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024925 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024926
24927theend:
24928 vim_free(pbuf_unused);
24929 vim_free(save_fname);
24930
24931 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024932}
24933
24934/*
24935 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024936 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024937 */
24938 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024939shortpath_for_partial(
24940 char_u **fnamep,
24941 char_u **bufp,
24942 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024943{
24944 int sepcount, len, tflen;
24945 char_u *p;
24946 char_u *pbuf, *tfname;
24947 int hasTilde;
24948
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024949 /* Count up the path separators from the RHS.. so we know which part
24950 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024951 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024952 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024953 if (vim_ispathsep(*p))
24954 ++sepcount;
24955
24956 /* Need full path first (use expand_env() to remove a "~/") */
24957 hasTilde = (**fnamep == '~');
24958 if (hasTilde)
24959 pbuf = tfname = expand_env_save(*fnamep);
24960 else
24961 pbuf = tfname = FullName_save(*fnamep, FALSE);
24962
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024963 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024964
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024965 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24966 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024967
24968 if (len == 0)
24969 {
24970 /* Don't have a valid filename, so shorten the rest of the
24971 * path if we can. This CAN give us invalid 8.3 filenames, but
24972 * there's not a lot of point in guessing what it might be.
24973 */
24974 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024975 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24976 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024977 }
24978
24979 /* Count the paths backward to find the beginning of the desired string. */
24980 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024981 {
24982#ifdef FEAT_MBYTE
24983 if (has_mbyte)
24984 p -= mb_head_off(tfname, p);
24985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024986 if (vim_ispathsep(*p))
24987 {
24988 if (sepcount == 0 || (hasTilde && sepcount == 1))
24989 break;
24990 else
24991 sepcount --;
24992 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024994 if (hasTilde)
24995 {
24996 --p;
24997 if (p >= tfname)
24998 *p = '~';
24999 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025000 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025001 }
25002 else
25003 ++p;
25004
25005 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25006 vim_free(*bufp);
25007 *fnamelen = (int)STRLEN(p);
25008 *bufp = pbuf;
25009 *fnamep = p;
25010
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025011 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025012}
25013#endif /* WIN3264 */
25014
25015/*
25016 * Adjust a filename, according to a string of modifiers.
25017 * *fnamep must be NUL terminated when called. When returning, the length is
25018 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025019 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025020 * When there is an error, *fnamep is set to NULL.
25021 */
25022 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025023modify_fname(
25024 char_u *src, /* string with modifiers */
25025 int *usedlen, /* characters after src that are used */
25026 char_u **fnamep, /* file name so far */
25027 char_u **bufp, /* buffer for allocated file name or NULL */
25028 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025029{
25030 int valid = 0;
25031 char_u *tail;
25032 char_u *s, *p, *pbuf;
25033 char_u dirname[MAXPATHL];
25034 int c;
25035 int has_fullname = 0;
25036#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025037 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025038 int has_shortname = 0;
25039#endif
25040
25041repeat:
25042 /* ":p" - full path/file_name */
25043 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25044 {
25045 has_fullname = 1;
25046
25047 valid |= VALID_PATH;
25048 *usedlen += 2;
25049
25050 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25051 if ((*fnamep)[0] == '~'
25052#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25053 && ((*fnamep)[1] == '/'
25054# ifdef BACKSLASH_IN_FILENAME
25055 || (*fnamep)[1] == '\\'
25056# endif
25057 || (*fnamep)[1] == NUL)
25058
25059#endif
25060 )
25061 {
25062 *fnamep = expand_env_save(*fnamep);
25063 vim_free(*bufp); /* free any allocated file name */
25064 *bufp = *fnamep;
25065 if (*fnamep == NULL)
25066 return -1;
25067 }
25068
25069 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025070 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025071 {
25072 if (vim_ispathsep(*p)
25073 && p[1] == '.'
25074 && (p[2] == NUL
25075 || vim_ispathsep(p[2])
25076 || (p[2] == '.'
25077 && (p[3] == NUL || vim_ispathsep(p[3])))))
25078 break;
25079 }
25080
25081 /* FullName_save() is slow, don't use it when not needed. */
25082 if (*p != NUL || !vim_isAbsName(*fnamep))
25083 {
25084 *fnamep = FullName_save(*fnamep, *p != NUL);
25085 vim_free(*bufp); /* free any allocated file name */
25086 *bufp = *fnamep;
25087 if (*fnamep == NULL)
25088 return -1;
25089 }
25090
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025091#ifdef WIN3264
25092# if _WIN32_WINNT >= 0x0500
25093 if (vim_strchr(*fnamep, '~') != NULL)
25094 {
25095 /* Expand 8.3 filename to full path. Needed to make sure the same
25096 * file does not have two different names.
25097 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25098 p = alloc(_MAX_PATH + 1);
25099 if (p != NULL)
25100 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025101 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025102 {
25103 vim_free(*bufp);
25104 *bufp = *fnamep = p;
25105 }
25106 else
25107 vim_free(p);
25108 }
25109 }
25110# endif
25111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025112 /* Append a path separator to a directory. */
25113 if (mch_isdir(*fnamep))
25114 {
25115 /* Make room for one or two extra characters. */
25116 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25117 vim_free(*bufp); /* free any allocated file name */
25118 *bufp = *fnamep;
25119 if (*fnamep == NULL)
25120 return -1;
25121 add_pathsep(*fnamep);
25122 }
25123 }
25124
25125 /* ":." - path relative to the current directory */
25126 /* ":~" - path relative to the home directory */
25127 /* ":8" - shortname path - postponed till after */
25128 while (src[*usedlen] == ':'
25129 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25130 {
25131 *usedlen += 2;
25132 if (c == '8')
25133 {
25134#ifdef WIN3264
25135 has_shortname = 1; /* Postpone this. */
25136#endif
25137 continue;
25138 }
25139 pbuf = NULL;
25140 /* Need full path first (use expand_env() to remove a "~/") */
25141 if (!has_fullname)
25142 {
25143 if (c == '.' && **fnamep == '~')
25144 p = pbuf = expand_env_save(*fnamep);
25145 else
25146 p = pbuf = FullName_save(*fnamep, FALSE);
25147 }
25148 else
25149 p = *fnamep;
25150
25151 has_fullname = 0;
25152
25153 if (p != NULL)
25154 {
25155 if (c == '.')
25156 {
25157 mch_dirname(dirname, MAXPATHL);
25158 s = shorten_fname(p, dirname);
25159 if (s != NULL)
25160 {
25161 *fnamep = s;
25162 if (pbuf != NULL)
25163 {
25164 vim_free(*bufp); /* free any allocated file name */
25165 *bufp = pbuf;
25166 pbuf = NULL;
25167 }
25168 }
25169 }
25170 else
25171 {
25172 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25173 /* Only replace it when it starts with '~' */
25174 if (*dirname == '~')
25175 {
25176 s = vim_strsave(dirname);
25177 if (s != NULL)
25178 {
25179 *fnamep = s;
25180 vim_free(*bufp);
25181 *bufp = s;
25182 }
25183 }
25184 }
25185 vim_free(pbuf);
25186 }
25187 }
25188
25189 tail = gettail(*fnamep);
25190 *fnamelen = (int)STRLEN(*fnamep);
25191
25192 /* ":h" - head, remove "/file_name", can be repeated */
25193 /* Don't remove the first "/" or "c:\" */
25194 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25195 {
25196 valid |= VALID_HEAD;
25197 *usedlen += 2;
25198 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025199 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025200 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025201 *fnamelen = (int)(tail - *fnamep);
25202#ifdef VMS
25203 if (*fnamelen > 0)
25204 *fnamelen += 1; /* the path separator is part of the path */
25205#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025206 if (*fnamelen == 0)
25207 {
25208 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25209 p = vim_strsave((char_u *)".");
25210 if (p == NULL)
25211 return -1;
25212 vim_free(*bufp);
25213 *bufp = *fnamep = tail = p;
25214 *fnamelen = 1;
25215 }
25216 else
25217 {
25218 while (tail > s && !after_pathsep(s, tail))
25219 mb_ptr_back(*fnamep, tail);
25220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025221 }
25222
25223 /* ":8" - shortname */
25224 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25225 {
25226 *usedlen += 2;
25227#ifdef WIN3264
25228 has_shortname = 1;
25229#endif
25230 }
25231
25232#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025233 /*
25234 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025235 */
25236 if (has_shortname)
25237 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025238 /* Copy the string if it is shortened by :h and when it wasn't copied
25239 * yet, because we are going to change it in place. Avoids changing
25240 * the buffer name for "%:8". */
25241 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025242 {
25243 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025244 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025245 return -1;
25246 vim_free(*bufp);
25247 *bufp = *fnamep = p;
25248 }
25249
25250 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025251 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025252 if (!has_fullname && !vim_isAbsName(*fnamep))
25253 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025254 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025255 return -1;
25256 }
25257 else
25258 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025259 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025260
Bram Moolenaardc935552011-08-17 15:23:23 +020025261 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025262 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025263 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025264 return -1;
25265
25266 if (l == 0)
25267 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025268 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025269 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025270 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025271 return -1;
25272 }
25273 *fnamelen = l;
25274 }
25275 }
25276#endif /* WIN3264 */
25277
25278 /* ":t" - tail, just the basename */
25279 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25280 {
25281 *usedlen += 2;
25282 *fnamelen -= (int)(tail - *fnamep);
25283 *fnamep = tail;
25284 }
25285
25286 /* ":e" - extension, can be repeated */
25287 /* ":r" - root, without extension, can be repeated */
25288 while (src[*usedlen] == ':'
25289 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25290 {
25291 /* find a '.' in the tail:
25292 * - for second :e: before the current fname
25293 * - otherwise: The last '.'
25294 */
25295 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25296 s = *fnamep - 2;
25297 else
25298 s = *fnamep + *fnamelen - 1;
25299 for ( ; s > tail; --s)
25300 if (s[0] == '.')
25301 break;
25302 if (src[*usedlen + 1] == 'e') /* :e */
25303 {
25304 if (s > tail)
25305 {
25306 *fnamelen += (int)(*fnamep - (s + 1));
25307 *fnamep = s + 1;
25308#ifdef VMS
25309 /* cut version from the extension */
25310 s = *fnamep + *fnamelen - 1;
25311 for ( ; s > *fnamep; --s)
25312 if (s[0] == ';')
25313 break;
25314 if (s > *fnamep)
25315 *fnamelen = s - *fnamep;
25316#endif
25317 }
25318 else if (*fnamep <= tail)
25319 *fnamelen = 0;
25320 }
25321 else /* :r */
25322 {
25323 if (s > tail) /* remove one extension */
25324 *fnamelen = (int)(s - *fnamep);
25325 }
25326 *usedlen += 2;
25327 }
25328
25329 /* ":s?pat?foo?" - substitute */
25330 /* ":gs?pat?foo?" - global substitute */
25331 if (src[*usedlen] == ':'
25332 && (src[*usedlen + 1] == 's'
25333 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25334 {
25335 char_u *str;
25336 char_u *pat;
25337 char_u *sub;
25338 int sep;
25339 char_u *flags;
25340 int didit = FALSE;
25341
25342 flags = (char_u *)"";
25343 s = src + *usedlen + 2;
25344 if (src[*usedlen + 1] == 'g')
25345 {
25346 flags = (char_u *)"g";
25347 ++s;
25348 }
25349
25350 sep = *s++;
25351 if (sep)
25352 {
25353 /* find end of pattern */
25354 p = vim_strchr(s, sep);
25355 if (p != NULL)
25356 {
25357 pat = vim_strnsave(s, (int)(p - s));
25358 if (pat != NULL)
25359 {
25360 s = p + 1;
25361 /* find end of substitution */
25362 p = vim_strchr(s, sep);
25363 if (p != NULL)
25364 {
25365 sub = vim_strnsave(s, (int)(p - s));
25366 str = vim_strnsave(*fnamep, *fnamelen);
25367 if (sub != NULL && str != NULL)
25368 {
25369 *usedlen = (int)(p + 1 - src);
25370 s = do_string_sub(str, pat, sub, flags);
25371 if (s != NULL)
25372 {
25373 *fnamep = s;
25374 *fnamelen = (int)STRLEN(s);
25375 vim_free(*bufp);
25376 *bufp = s;
25377 didit = TRUE;
25378 }
25379 }
25380 vim_free(sub);
25381 vim_free(str);
25382 }
25383 vim_free(pat);
25384 }
25385 }
25386 /* after using ":s", repeat all the modifiers */
25387 if (didit)
25388 goto repeat;
25389 }
25390 }
25391
Bram Moolenaar26df0922014-02-23 23:39:13 +010025392 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25393 {
25394 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25395 if (p == NULL)
25396 return -1;
25397 vim_free(*bufp);
25398 *bufp = *fnamep = p;
25399 *fnamelen = (int)STRLEN(p);
25400 *usedlen += 2;
25401 }
25402
Bram Moolenaar071d4272004-06-13 20:20:40 +000025403 return valid;
25404}
25405
25406/*
25407 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25408 * "flags" can be "g" to do a global substitute.
25409 * Returns an allocated string, NULL for error.
25410 */
25411 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025412do_string_sub(
25413 char_u *str,
25414 char_u *pat,
25415 char_u *sub,
25416 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025417{
25418 int sublen;
25419 regmatch_T regmatch;
25420 int i;
25421 int do_all;
25422 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025423 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025424 garray_T ga;
25425 char_u *ret;
25426 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025427 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025428
25429 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25430 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025431 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025432
25433 ga_init2(&ga, 1, 200);
25434
25435 do_all = (flags[0] == 'g');
25436
25437 regmatch.rm_ic = p_ic;
25438 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25439 if (regmatch.regprog != NULL)
25440 {
25441 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025442 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025443 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25444 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025445 /* Skip empty match except for first match. */
25446 if (regmatch.startp[0] == regmatch.endp[0])
25447 {
25448 if (zero_width == regmatch.startp[0])
25449 {
25450 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025451 i = MB_PTR2LEN(tail);
25452 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25453 (size_t)i);
25454 ga.ga_len += i;
25455 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025456 continue;
25457 }
25458 zero_width = regmatch.startp[0];
25459 }
25460
Bram Moolenaar071d4272004-06-13 20:20:40 +000025461 /*
25462 * Get some space for a temporary buffer to do the substitution
25463 * into. It will contain:
25464 * - The text up to where the match is.
25465 * - The substituted text.
25466 * - The text after the match.
25467 */
25468 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025469 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025470 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25471 {
25472 ga_clear(&ga);
25473 break;
25474 }
25475
25476 /* copy the text up to where the match is */
25477 i = (int)(regmatch.startp[0] - tail);
25478 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25479 /* add the substituted text */
25480 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25481 + ga.ga_len + i, TRUE, TRUE, FALSE);
25482 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025483 tail = regmatch.endp[0];
25484 if (*tail == NUL)
25485 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025486 if (!do_all)
25487 break;
25488 }
25489
25490 if (ga.ga_data != NULL)
25491 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25492
Bram Moolenaar473de612013-06-08 18:19:48 +020025493 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025494 }
25495
25496 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25497 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025498 if (p_cpo == empty_option)
25499 p_cpo = save_cpo;
25500 else
25501 /* Darn, evaluating {sub} expression changed the value. */
25502 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025503
25504 return ret;
25505}
25506
25507#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */