blob: db7ae756a51531d47856100a5eb9670d6685b60c [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 Moolenaar49cd9572005-01-03 21:06:01 +00006004listitem_alloc()
6005{
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 Moolenaar48e697e2016-01-23 22:17:30 +010010905static void findfilendir(typval_T *argvars, typval_T *rettv, int find_what);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010906
10907 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010908findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010909 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010910 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010911 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010912{
10913#ifdef FEAT_SEARCHPATH
10914 char_u *fname;
10915 char_u *fresult = NULL;
10916 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10917 char_u *p;
10918 char_u pathbuf[NUMBUFLEN];
10919 int count = 1;
10920 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010921 int error = FALSE;
10922#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010923
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010924 rettv->vval.v_string = NULL;
10925 rettv->v_type = VAR_STRING;
10926
10927#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010928 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010929
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010930 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010931 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010932 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10933 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010934 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010935 else
10936 {
10937 if (*p != NUL)
10938 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010939
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010940 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010941 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010942 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010943 }
10944
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010945 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10946 error = TRUE;
10947
10948 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010949 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010950 do
10951 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010952 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010953 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010954 fresult = find_file_in_path_option(first ? fname : NULL,
10955 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010956 0, first, path,
10957 find_what,
10958 curbuf->b_ffname,
10959 find_what == FINDFILE_DIR
10960 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010961 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010962
10963 if (fresult != NULL && rettv->v_type == VAR_LIST)
10964 list_append_string(rettv->vval.v_list, fresult, -1);
10965
10966 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010967 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010968
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010969 if (rettv->v_type == VAR_STRING)
10970 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010971#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010972}
10973
Bram Moolenaar48e697e2016-01-23 22:17:30 +010010974static void filter_map(typval_T *argvars, typval_T *rettv, int map);
10975static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010976
10977/*
10978 * Implementation of map() and filter().
10979 */
10980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010981filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010982{
10983 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010984 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010985 listitem_T *li, *nli;
10986 list_T *l = NULL;
10987 dictitem_T *di;
10988 hashtab_T *ht;
10989 hashitem_T *hi;
10990 dict_T *d = NULL;
10991 typval_T save_val;
10992 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010993 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010994 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010995 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020010996 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010997 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010998 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010999 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011000
Bram Moolenaare9a41262005-01-15 22:18:47 +000011001 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011002 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011003 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011004 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011005 return;
11006 }
11007 else if (argvars[0].v_type == VAR_DICT)
11008 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011009 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011010 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011011 return;
11012 }
11013 else
11014 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011015 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011016 return;
11017 }
11018
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011019 expr = get_tv_string_buf_chk(&argvars[1], buf);
11020 /* On type errors, the preceding call has already displayed an error
11021 * message. Avoid a misleading error message for an empty string that
11022 * was not passed as argument. */
11023 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011024 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011025 prepare_vimvar(VV_VAL, &save_val);
11026 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011027
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011028 /* We reset "did_emsg" to be able to detect whether an error
11029 * occurred during evaluation of the expression. */
11030 save_did_emsg = did_emsg;
11031 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011032
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011033 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011034 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011035 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011036 vimvars[VV_KEY].vv_type = VAR_STRING;
11037
11038 ht = &d->dv_hashtab;
11039 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011040 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011041 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011042 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011043 if (!HASHITEM_EMPTY(hi))
11044 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011045 int r;
11046
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011047 --todo;
11048 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011049 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011050 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11051 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011052 break;
11053 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011054 r = filter_map_one(&di->di_tv, expr, map, &rem);
11055 clear_tv(&vimvars[VV_KEY].vv_tv);
11056 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011057 break;
11058 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011059 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011060 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11061 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011062 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011063 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011064 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011065 }
11066 }
11067 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011068 }
11069 else
11070 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011071 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11072
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011073 for (li = l->lv_first; li != NULL; li = nli)
11074 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011075 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011076 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011077 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011078 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011079 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011080 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011081 break;
11082 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011083 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011084 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011085 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011086 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011087
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011088 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011089 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011090
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011091 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011092 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011093
11094 copy_tv(&argvars[0], rettv);
11095}
11096
11097 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011098filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011099{
Bram Moolenaar33570922005-01-25 22:26:29 +000011100 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011101 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011102 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011103
Bram Moolenaar33570922005-01-25 22:26:29 +000011104 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011105 s = expr;
11106 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011107 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011108 if (*s != NUL) /* check for trailing chars after expr */
11109 {
11110 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011111 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011112 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011113 }
11114 if (map)
11115 {
11116 /* map(): replace the list item value */
11117 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011118 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011119 *tv = rettv;
11120 }
11121 else
11122 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011123 int error = FALSE;
11124
Bram Moolenaare9a41262005-01-15 22:18:47 +000011125 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011126 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011127 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011128 /* On type error, nothing has been removed; return FAIL to stop the
11129 * loop. The error message was given by get_tv_number_chk(). */
11130 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011131 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011132 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011133 retval = OK;
11134theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011135 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011136 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011137}
11138
11139/*
11140 * "filter()" function
11141 */
11142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011143f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011144{
11145 filter_map(argvars, rettv, FALSE);
11146}
11147
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011148/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011149 * "finddir({fname}[, {path}[, {count}]])" function
11150 */
11151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011152f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011153{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011154 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011155}
11156
11157/*
11158 * "findfile({fname}[, {path}[, {count}]])" function
11159 */
11160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011161f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011162{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011163 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011164}
11165
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011166#ifdef FEAT_FLOAT
11167/*
11168 * "float2nr({float})" function
11169 */
11170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011171f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011172{
11173 float_T f;
11174
11175 if (get_float_arg(argvars, &f) == OK)
11176 {
11177 if (f < -0x7fffffff)
11178 rettv->vval.v_number = -0x7fffffff;
11179 else if (f > 0x7fffffff)
11180 rettv->vval.v_number = 0x7fffffff;
11181 else
11182 rettv->vval.v_number = (varnumber_T)f;
11183 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011184}
11185
11186/*
11187 * "floor({float})" function
11188 */
11189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011190f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011191{
11192 float_T f;
11193
11194 rettv->v_type = VAR_FLOAT;
11195 if (get_float_arg(argvars, &f) == OK)
11196 rettv->vval.v_float = floor(f);
11197 else
11198 rettv->vval.v_float = 0.0;
11199}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011200
11201/*
11202 * "fmod()" function
11203 */
11204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011205f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011206{
11207 float_T fx, fy;
11208
11209 rettv->v_type = VAR_FLOAT;
11210 if (get_float_arg(argvars, &fx) == OK
11211 && get_float_arg(&argvars[1], &fy) == OK)
11212 rettv->vval.v_float = fmod(fx, fy);
11213 else
11214 rettv->vval.v_float = 0.0;
11215}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011216#endif
11217
Bram Moolenaar0d660222005-01-07 21:51:51 +000011218/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011219 * "fnameescape({string})" function
11220 */
11221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011222f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011223{
11224 rettv->vval.v_string = vim_strsave_fnameescape(
11225 get_tv_string(&argvars[0]), FALSE);
11226 rettv->v_type = VAR_STRING;
11227}
11228
11229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230 * "fnamemodify({fname}, {mods})" function
11231 */
11232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011233f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234{
11235 char_u *fname;
11236 char_u *mods;
11237 int usedlen = 0;
11238 int len;
11239 char_u *fbuf = NULL;
11240 char_u buf[NUMBUFLEN];
11241
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011242 fname = get_tv_string_chk(&argvars[0]);
11243 mods = get_tv_string_buf_chk(&argvars[1], buf);
11244 if (fname == NULL || mods == NULL)
11245 fname = NULL;
11246 else
11247 {
11248 len = (int)STRLEN(fname);
11249 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011252 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011254 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011256 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 vim_free(fbuf);
11258}
11259
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011260static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261
11262/*
11263 * "foldclosed()" function
11264 */
11265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011266foldclosed_both(
11267 typval_T *argvars UNUSED,
11268 typval_T *rettv,
11269 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270{
11271#ifdef FEAT_FOLDING
11272 linenr_T lnum;
11273 linenr_T first, last;
11274
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011275 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11277 {
11278 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11279 {
11280 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011281 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011283 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011284 return;
11285 }
11286 }
11287#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011288 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289}
11290
11291/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011292 * "foldclosed()" function
11293 */
11294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011295f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011296{
11297 foldclosed_both(argvars, rettv, FALSE);
11298}
11299
11300/*
11301 * "foldclosedend()" function
11302 */
11303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011304f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011305{
11306 foldclosed_both(argvars, rettv, TRUE);
11307}
11308
11309/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310 * "foldlevel()" function
11311 */
11312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011313f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011314{
11315#ifdef FEAT_FOLDING
11316 linenr_T lnum;
11317
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011318 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011320 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322}
11323
11324/*
11325 * "foldtext()" function
11326 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011328f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329{
11330#ifdef FEAT_FOLDING
11331 linenr_T lnum;
11332 char_u *s;
11333 char_u *r;
11334 int len;
11335 char *txt;
11336#endif
11337
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011338 rettv->v_type = VAR_STRING;
11339 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011341 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11342 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11343 <= curbuf->b_ml.ml_line_count
11344 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345 {
11346 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011347 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11348 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349 {
11350 if (!linewhite(lnum))
11351 break;
11352 ++lnum;
11353 }
11354
11355 /* Find interesting text in this line. */
11356 s = skipwhite(ml_get(lnum));
11357 /* skip C comment-start */
11358 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011359 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011361 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011362 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011363 {
11364 s = skipwhite(ml_get(lnum + 1));
11365 if (*s == '*')
11366 s = skipwhite(s + 1);
11367 }
11368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369 txt = _("+-%s%3ld lines: ");
11370 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011371 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372 + 20 /* for %3ld */
11373 + STRLEN(s))); /* concatenated */
11374 if (r != NULL)
11375 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011376 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11377 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11378 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379 len = (int)STRLEN(r);
11380 STRCAT(r, s);
11381 /* remove 'foldmarker' and 'commentstring' */
11382 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011383 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011384 }
11385 }
11386#endif
11387}
11388
11389/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011390 * "foldtextresult(lnum)" function
11391 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011393f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011394{
11395#ifdef FEAT_FOLDING
11396 linenr_T lnum;
11397 char_u *text;
11398 char_u buf[51];
11399 foldinfo_T foldinfo;
11400 int fold_count;
11401#endif
11402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011403 rettv->v_type = VAR_STRING;
11404 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011405#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011406 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011407 /* treat illegal types and illegal string values for {lnum} the same */
11408 if (lnum < 0)
11409 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011410 fold_count = foldedCount(curwin, lnum, &foldinfo);
11411 if (fold_count > 0)
11412 {
11413 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11414 &foldinfo, buf);
11415 if (text == buf)
11416 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011417 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011418 }
11419#endif
11420}
11421
11422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423 * "foreground()" function
11424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011426f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428#ifdef FEAT_GUI
11429 if (gui.in_use)
11430 gui_mch_set_foreground();
11431#else
11432# ifdef WIN32
11433 win32_set_foreground();
11434# endif
11435#endif
11436}
11437
11438/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011439 * "function()" function
11440 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011442f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011443{
11444 char_u *s;
11445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011447 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011448 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011449 /* Don't check an autoload name for existence here. */
11450 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011451 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011452 else
11453 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011454 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011455 {
11456 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011457 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011458
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011459 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11460 * also be called from another script. Using trans_function_name()
11461 * would also work, but some plugins depend on the name being
11462 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011463 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011464 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011465 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011466 if (rettv->vval.v_string != NULL)
11467 {
11468 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011469 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011470 }
11471 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011472 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011473 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011474 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011475 }
11476}
11477
11478/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011479 * "garbagecollect()" function
11480 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011482f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011483{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011484 /* This is postponed until we are back at the toplevel, because we may be
11485 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11486 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011487
11488 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11489 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011490}
11491
11492/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011493 * "get()" function
11494 */
11495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011496f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011497{
Bram Moolenaar33570922005-01-25 22:26:29 +000011498 listitem_T *li;
11499 list_T *l;
11500 dictitem_T *di;
11501 dict_T *d;
11502 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011503
Bram Moolenaare9a41262005-01-15 22:18:47 +000011504 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011505 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011506 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011507 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011508 int error = FALSE;
11509
11510 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11511 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011512 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011513 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011514 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011515 else if (argvars[0].v_type == VAR_DICT)
11516 {
11517 if ((d = argvars[0].vval.v_dict) != NULL)
11518 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011519 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011520 if (di != NULL)
11521 tv = &di->di_tv;
11522 }
11523 }
11524 else
11525 EMSG2(_(e_listdictarg), "get()");
11526
11527 if (tv == NULL)
11528 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011529 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011530 copy_tv(&argvars[2], rettv);
11531 }
11532 else
11533 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011534}
11535
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011536static 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 +000011537
11538/*
11539 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011540 * Return a range (from start to end) of lines in rettv from the specified
11541 * buffer.
11542 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011543 */
11544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011545get_buffer_lines(
11546 buf_T *buf,
11547 linenr_T start,
11548 linenr_T end,
11549 int retlist,
11550 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011551{
11552 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011553
Bram Moolenaar959a1432013-12-14 12:17:38 +010011554 rettv->v_type = VAR_STRING;
11555 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011556 if (retlist && rettv_list_alloc(rettv) == FAIL)
11557 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011558
11559 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11560 return;
11561
11562 if (!retlist)
11563 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011564 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11565 p = ml_get_buf(buf, start, FALSE);
11566 else
11567 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011568 rettv->vval.v_string = vim_strsave(p);
11569 }
11570 else
11571 {
11572 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011573 return;
11574
11575 if (start < 1)
11576 start = 1;
11577 if (end > buf->b_ml.ml_line_count)
11578 end = buf->b_ml.ml_line_count;
11579 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011580 if (list_append_string(rettv->vval.v_list,
11581 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011582 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011583 }
11584}
11585
11586/*
11587 * "getbufline()" function
11588 */
11589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011590f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011591{
11592 linenr_T lnum;
11593 linenr_T end;
11594 buf_T *buf;
11595
11596 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11597 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011598 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011599 --emsg_off;
11600
Bram Moolenaar661b1822005-07-28 22:36:45 +000011601 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011602 if (argvars[2].v_type == VAR_UNKNOWN)
11603 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011604 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011605 end = get_tv_lnum_buf(&argvars[2], buf);
11606
Bram Moolenaar342337a2005-07-21 21:11:17 +000011607 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011608}
11609
Bram Moolenaar0d660222005-01-07 21:51:51 +000011610/*
11611 * "getbufvar()" function
11612 */
11613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011614f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011615{
11616 buf_T *buf;
11617 buf_T *save_curbuf;
11618 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011619 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011620 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011621
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011622 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11623 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011624 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011625 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011626
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011627 rettv->v_type = VAR_STRING;
11628 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011629
11630 if (buf != NULL && varname != NULL)
11631 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011632 /* set curbuf to be our buf, temporarily */
11633 save_curbuf = curbuf;
11634 curbuf = buf;
11635
Bram Moolenaar0d660222005-01-07 21:51:51 +000011636 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011637 {
11638 if (get_option_tv(&varname, rettv, TRUE) == OK)
11639 done = TRUE;
11640 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011641 else if (STRCMP(varname, "changedtick") == 0)
11642 {
11643 rettv->v_type = VAR_NUMBER;
11644 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011645 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011646 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011647 else
11648 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011649 /* Look up the variable. */
11650 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11651 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11652 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011653 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011654 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011655 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011656 done = TRUE;
11657 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011658 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011659
11660 /* restore previous notion of curbuf */
11661 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011662 }
11663
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011664 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11665 /* use the default value */
11666 copy_tv(&argvars[2], rettv);
11667
Bram Moolenaar0d660222005-01-07 21:51:51 +000011668 --emsg_off;
11669}
11670
11671/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011672 * "getchar()" function
11673 */
11674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011675f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676{
11677 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011678 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011680 /* Position the cursor. Needed after a message that ends in a space. */
11681 windgoto(msg_row, msg_col);
11682
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683 ++no_mapping;
11684 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011685 for (;;)
11686 {
11687 if (argvars[0].v_type == VAR_UNKNOWN)
11688 /* getchar(): blocking wait. */
11689 n = safe_vgetc();
11690 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11691 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011692 n = vpeekc_any();
11693 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011694 /* illegal argument or getchar(0) and no char avail: return zero */
11695 n = 0;
11696 else
11697 /* getchar(0) and char avail: return char */
11698 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011699
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011700 if (n == K_IGNORE)
11701 continue;
11702 break;
11703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704 --no_mapping;
11705 --allow_keys;
11706
Bram Moolenaar219b8702006-11-01 14:32:36 +000011707 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11708 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11709 vimvars[VV_MOUSE_COL].vv_nr = 0;
11710
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011711 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712 if (IS_SPECIAL(n) || mod_mask != 0)
11713 {
11714 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11715 int i = 0;
11716
11717 /* Turn a special key into three bytes, plus modifier. */
11718 if (mod_mask != 0)
11719 {
11720 temp[i++] = K_SPECIAL;
11721 temp[i++] = KS_MODIFIER;
11722 temp[i++] = mod_mask;
11723 }
11724 if (IS_SPECIAL(n))
11725 {
11726 temp[i++] = K_SPECIAL;
11727 temp[i++] = K_SECOND(n);
11728 temp[i++] = K_THIRD(n);
11729 }
11730#ifdef FEAT_MBYTE
11731 else if (has_mbyte)
11732 i += (*mb_char2bytes)(n, temp + i);
11733#endif
11734 else
11735 temp[i++] = n;
11736 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011737 rettv->v_type = VAR_STRING;
11738 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011739
11740#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011741 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011742 {
11743 int row = mouse_row;
11744 int col = mouse_col;
11745 win_T *win;
11746 linenr_T lnum;
11747# ifdef FEAT_WINDOWS
11748 win_T *wp;
11749# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011750 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011751
11752 if (row >= 0 && col >= 0)
11753 {
11754 /* Find the window at the mouse coordinates and compute the
11755 * text position. */
11756 win = mouse_find_win(&row, &col);
11757 (void)mouse_comp_pos(win, &row, &col, &lnum);
11758# ifdef FEAT_WINDOWS
11759 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011760 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011761# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011762 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011763 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11764 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11765 }
11766 }
11767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011768 }
11769}
11770
11771/*
11772 * "getcharmod()" function
11773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011775f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011776{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011777 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011778}
11779
11780/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011781 * "getcharsearch()" function
11782 */
11783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011784f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011785{
11786 if (rettv_dict_alloc(rettv) != FAIL)
11787 {
11788 dict_T *dict = rettv->vval.v_dict;
11789
11790 dict_add_nr_str(dict, "char", 0L, last_csearch());
11791 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11792 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11793 }
11794}
11795
11796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797 * "getcmdline()" function
11798 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011800f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011802 rettv->v_type = VAR_STRING;
11803 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804}
11805
11806/*
11807 * "getcmdpos()" function
11808 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011810f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011812 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813}
11814
11815/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011816 * "getcmdtype()" function
11817 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011819f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011820{
11821 rettv->v_type = VAR_STRING;
11822 rettv->vval.v_string = alloc(2);
11823 if (rettv->vval.v_string != NULL)
11824 {
11825 rettv->vval.v_string[0] = get_cmdline_type();
11826 rettv->vval.v_string[1] = NUL;
11827 }
11828}
11829
11830/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011831 * "getcmdwintype()" function
11832 */
11833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011834f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011835{
11836 rettv->v_type = VAR_STRING;
11837 rettv->vval.v_string = NULL;
11838#ifdef FEAT_CMDWIN
11839 rettv->vval.v_string = alloc(2);
11840 if (rettv->vval.v_string != NULL)
11841 {
11842 rettv->vval.v_string[0] = cmdwin_type;
11843 rettv->vval.v_string[1] = NUL;
11844 }
11845#endif
11846}
11847
11848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849 * "getcwd()" function
11850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011852f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853{
Bram Moolenaarc9703302016-01-17 21:49:33 +010011854 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011855 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011857 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011858 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010011859
11860 wp = find_tabwin(&argvars[0], &argvars[1]);
11861 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011862 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010011863 if (wp->w_localdir != NULL)
11864 rettv->vval.v_string = vim_strsave(wp->w_localdir);
11865 else if(globaldir != NULL)
11866 rettv->vval.v_string = vim_strsave(globaldir);
11867 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020011868 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010011869 cwd = alloc(MAXPATHL);
11870 if (cwd != NULL)
11871 {
11872 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11873 rettv->vval.v_string = vim_strsave(cwd);
11874 vim_free(cwd);
11875 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020011876 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010011877#ifdef BACKSLASH_IN_FILENAME
11878 if (rettv->vval.v_string != NULL)
11879 slash_adjust(rettv->vval.v_string);
11880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011881 }
11882}
11883
11884/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011885 * "getfontname()" function
11886 */
11887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011888f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011889{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011890 rettv->v_type = VAR_STRING;
11891 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011892#ifdef FEAT_GUI
11893 if (gui.in_use)
11894 {
11895 GuiFont font;
11896 char_u *name = NULL;
11897
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011898 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011899 {
11900 /* Get the "Normal" font. Either the name saved by
11901 * hl_set_font_name() or from the font ID. */
11902 font = gui.norm_font;
11903 name = hl_get_font_name();
11904 }
11905 else
11906 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011907 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011908 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11909 return;
11910 font = gui_mch_get_font(name, FALSE);
11911 if (font == NOFONT)
11912 return; /* Invalid font name, return empty string. */
11913 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011914 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011915 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011916 gui_mch_free_font(font);
11917 }
11918#endif
11919}
11920
11921/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011922 * "getfperm({fname})" function
11923 */
11924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011925f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011926{
11927 char_u *fname;
11928 struct stat st;
11929 char_u *perm = NULL;
11930 char_u flags[] = "rwx";
11931 int i;
11932
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011933 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011935 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011936 if (mch_stat((char *)fname, &st) >= 0)
11937 {
11938 perm = vim_strsave((char_u *)"---------");
11939 if (perm != NULL)
11940 {
11941 for (i = 0; i < 9; i++)
11942 {
11943 if (st.st_mode & (1 << (8 - i)))
11944 perm[i] = flags[i % 3];
11945 }
11946 }
11947 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011948 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011949}
11950
11951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011952 * "getfsize({fname})" function
11953 */
11954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011955f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011956{
11957 char_u *fname;
11958 struct stat st;
11959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011960 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011962 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963
11964 if (mch_stat((char *)fname, &st) >= 0)
11965 {
11966 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011967 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011969 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011970 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011971
11972 /* non-perfect check for overflow */
11973 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11974 rettv->vval.v_number = -2;
11975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011976 }
11977 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011978 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979}
11980
11981/*
11982 * "getftime({fname})" function
11983 */
11984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011985f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011986{
11987 char_u *fname;
11988 struct stat st;
11989
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011990 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011991
11992 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011993 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011995 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996}
11997
11998/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011999 * "getftype({fname})" function
12000 */
12001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012002f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012003{
12004 char_u *fname;
12005 struct stat st;
12006 char_u *type = NULL;
12007 char *t;
12008
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012009 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012010
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012011 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012012 if (mch_lstat((char *)fname, &st) >= 0)
12013 {
12014#ifdef S_ISREG
12015 if (S_ISREG(st.st_mode))
12016 t = "file";
12017 else if (S_ISDIR(st.st_mode))
12018 t = "dir";
12019# ifdef S_ISLNK
12020 else if (S_ISLNK(st.st_mode))
12021 t = "link";
12022# endif
12023# ifdef S_ISBLK
12024 else if (S_ISBLK(st.st_mode))
12025 t = "bdev";
12026# endif
12027# ifdef S_ISCHR
12028 else if (S_ISCHR(st.st_mode))
12029 t = "cdev";
12030# endif
12031# ifdef S_ISFIFO
12032 else if (S_ISFIFO(st.st_mode))
12033 t = "fifo";
12034# endif
12035# ifdef S_ISSOCK
12036 else if (S_ISSOCK(st.st_mode))
12037 t = "fifo";
12038# endif
12039 else
12040 t = "other";
12041#else
12042# ifdef S_IFMT
12043 switch (st.st_mode & S_IFMT)
12044 {
12045 case S_IFREG: t = "file"; break;
12046 case S_IFDIR: t = "dir"; break;
12047# ifdef S_IFLNK
12048 case S_IFLNK: t = "link"; break;
12049# endif
12050# ifdef S_IFBLK
12051 case S_IFBLK: t = "bdev"; break;
12052# endif
12053# ifdef S_IFCHR
12054 case S_IFCHR: t = "cdev"; break;
12055# endif
12056# ifdef S_IFIFO
12057 case S_IFIFO: t = "fifo"; break;
12058# endif
12059# ifdef S_IFSOCK
12060 case S_IFSOCK: t = "socket"; break;
12061# endif
12062 default: t = "other";
12063 }
12064# else
12065 if (mch_isdir(fname))
12066 t = "dir";
12067 else
12068 t = "file";
12069# endif
12070#endif
12071 type = vim_strsave((char_u *)t);
12072 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012073 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012074}
12075
12076/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012077 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012078 */
12079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012080f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012081{
12082 linenr_T lnum;
12083 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012084 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012085
12086 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012087 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012088 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012089 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012090 retlist = FALSE;
12091 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012092 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012093 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012094 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012095 retlist = TRUE;
12096 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012097
Bram Moolenaar342337a2005-07-21 21:11:17 +000012098 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012099}
12100
12101/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012102 * "getmatches()" function
12103 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012105f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012106{
12107#ifdef FEAT_SEARCH_EXTRA
12108 dict_T *dict;
12109 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012110 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012111
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012112 if (rettv_list_alloc(rettv) == OK)
12113 {
12114 while (cur != NULL)
12115 {
12116 dict = dict_alloc();
12117 if (dict == NULL)
12118 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012119 if (cur->match.regprog == NULL)
12120 {
12121 /* match added with matchaddpos() */
12122 for (i = 0; i < MAXPOSMATCH; ++i)
12123 {
12124 llpos_T *llpos;
12125 char buf[6];
12126 list_T *l;
12127
12128 llpos = &cur->pos.pos[i];
12129 if (llpos->lnum == 0)
12130 break;
12131 l = list_alloc();
12132 if (l == NULL)
12133 break;
12134 list_append_number(l, (varnumber_T)llpos->lnum);
12135 if (llpos->col > 0)
12136 {
12137 list_append_number(l, (varnumber_T)llpos->col);
12138 list_append_number(l, (varnumber_T)llpos->len);
12139 }
12140 sprintf(buf, "pos%d", i + 1);
12141 dict_add_list(dict, buf, l);
12142 }
12143 }
12144 else
12145 {
12146 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12147 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012148 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012149 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12150 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012151# ifdef FEAT_CONCEAL
12152 if (cur->conceal_char)
12153 {
12154 char_u buf[MB_MAXBYTES + 1];
12155
12156 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12157 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12158 }
12159# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012160 list_append_dict(rettv->vval.v_list, dict);
12161 cur = cur->next;
12162 }
12163 }
12164#endif
12165}
12166
12167/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012168 * "getpid()" function
12169 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012171f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012172{
12173 rettv->vval.v_number = mch_get_pid();
12174}
12175
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012176static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012177
12178/*
12179 * "getcurpos()" function
12180 */
12181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012182f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012183{
12184 getpos_both(argvars, rettv, TRUE);
12185}
12186
Bram Moolenaar18081e32008-02-20 19:11:07 +000012187/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012188 * "getpos(string)" function
12189 */
12190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012191f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012192{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012193 getpos_both(argvars, rettv, FALSE);
12194}
12195
12196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012197getpos_both(
12198 typval_T *argvars,
12199 typval_T *rettv,
12200 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012201{
Bram Moolenaara5525202006-03-02 22:52:09 +000012202 pos_T *fp;
12203 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012204 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012205
12206 if (rettv_list_alloc(rettv) == OK)
12207 {
12208 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012209 if (getcurpos)
12210 fp = &curwin->w_cursor;
12211 else
12212 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012213 if (fnum != -1)
12214 list_append_number(l, (varnumber_T)fnum);
12215 else
12216 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012217 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12218 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012219 list_append_number(l, (fp != NULL)
12220 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012221 : (varnumber_T)0);
12222 list_append_number(l,
12223#ifdef FEAT_VIRTUALEDIT
12224 (fp != NULL) ? (varnumber_T)fp->coladd :
12225#endif
12226 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012227 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012228 list_append_number(l, curwin->w_curswant == MAXCOL ?
12229 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012230 }
12231 else
12232 rettv->vval.v_number = FALSE;
12233}
12234
12235/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012236 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012237 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012239f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012240{
12241#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012242 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012243#endif
12244
Bram Moolenaar2641f772005-03-25 21:58:17 +000012245#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012246 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012247 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012248 wp = NULL;
12249 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12250 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012251 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012252 if (wp == NULL)
12253 return;
12254 }
12255
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012256 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012257 }
12258#endif
12259}
12260
12261/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262 * "getreg()" function
12263 */
12264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012265f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266{
12267 char_u *strregname;
12268 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012269 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012270 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012271 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012273 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012275 strregname = get_tv_string_chk(&argvars[0]);
12276 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012277 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012279 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012280 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12281 return_list = get_tv_number_chk(&argvars[2], &error);
12282 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012285 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012286
12287 if (error)
12288 return;
12289
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290 regname = (strregname == NULL ? '"' : *strregname);
12291 if (regname == 0)
12292 regname = '"';
12293
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012294 if (return_list)
12295 {
12296 rettv->v_type = VAR_LIST;
12297 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12298 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012299 if (rettv->vval.v_list != NULL)
12300 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012301 }
12302 else
12303 {
12304 rettv->v_type = VAR_STRING;
12305 rettv->vval.v_string = get_reg_contents(regname,
12306 arg2 ? GREG_EXPR_SRC : 0);
12307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012308}
12309
12310/*
12311 * "getregtype()" function
12312 */
12313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012314f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315{
12316 char_u *strregname;
12317 int regname;
12318 char_u buf[NUMBUFLEN + 2];
12319 long reglen = 0;
12320
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012321 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012322 {
12323 strregname = get_tv_string_chk(&argvars[0]);
12324 if (strregname == NULL) /* type error; errmsg already given */
12325 {
12326 rettv->v_type = VAR_STRING;
12327 rettv->vval.v_string = NULL;
12328 return;
12329 }
12330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331 else
12332 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012333 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334
12335 regname = (strregname == NULL ? '"' : *strregname);
12336 if (regname == 0)
12337 regname = '"';
12338
12339 buf[0] = NUL;
12340 buf[1] = NUL;
12341 switch (get_reg_type(regname, &reglen))
12342 {
12343 case MLINE: buf[0] = 'V'; break;
12344 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345 case MBLOCK:
12346 buf[0] = Ctrl_V;
12347 sprintf((char *)buf + 1, "%ld", reglen + 1);
12348 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012350 rettv->v_type = VAR_STRING;
12351 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352}
12353
12354/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012355 * "gettabvar()" function
12356 */
12357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012358f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012359{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012360 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012361 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012362 dictitem_T *v;
12363 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012364 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012365
12366 rettv->v_type = VAR_STRING;
12367 rettv->vval.v_string = NULL;
12368
12369 varname = get_tv_string_chk(&argvars[1]);
12370 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12371 if (tp != NULL && varname != NULL)
12372 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012373 /* Set tp to be our tabpage, temporarily. Also set the window to the
12374 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012375 if (switch_win(&oldcurwin, &oldtabpage,
12376 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012377 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012378 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012379 /* look up the variable */
12380 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12381 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12382 if (v != NULL)
12383 {
12384 copy_tv(&v->di_tv, rettv);
12385 done = TRUE;
12386 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012387 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012388
12389 /* restore previous notion of curwin */
12390 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012391 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012392
12393 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012394 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012395}
12396
12397/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012398 * "gettabwinvar()" function
12399 */
12400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012401f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012402{
12403 getwinvar(argvars, rettv, 1);
12404}
12405
12406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407 * "getwinposx()" function
12408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012410f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012411{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012412 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012413#ifdef FEAT_GUI
12414 if (gui.in_use)
12415 {
12416 int x, y;
12417
12418 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012419 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420 }
12421#endif
12422}
12423
12424/*
12425 * "getwinposy()" function
12426 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012428f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012430 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012431#ifdef FEAT_GUI
12432 if (gui.in_use)
12433 {
12434 int x, y;
12435
12436 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012437 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438 }
12439#endif
12440}
12441
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012442/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012443 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012444 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012445 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012446find_win_by_nr(
12447 typval_T *vp,
12448 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012449{
12450#ifdef FEAT_WINDOWS
12451 win_T *wp;
12452#endif
12453 int nr;
12454
12455 nr = get_tv_number_chk(vp, NULL);
12456
12457#ifdef FEAT_WINDOWS
12458 if (nr < 0)
12459 return NULL;
12460 if (nr == 0)
12461 return curwin;
12462
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012463 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12464 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012465 if (--nr <= 0)
12466 break;
12467 return wp;
12468#else
12469 if (nr == 0 || nr == 1)
12470 return curwin;
12471 return NULL;
12472#endif
12473}
12474
Bram Moolenaar071d4272004-06-13 20:20:40 +000012475/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012476 * Find window specified by "wvp" in tabpage "tvp".
12477 */
12478 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012479find_tabwin(
12480 typval_T *wvp, /* VAR_UNKNOWN for current window */
12481 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012482{
12483 win_T *wp = NULL;
12484 tabpage_T *tp = NULL;
12485 long n;
12486
12487 if (wvp->v_type != VAR_UNKNOWN)
12488 {
12489 if (tvp->v_type != VAR_UNKNOWN)
12490 {
12491 n = get_tv_number(tvp);
12492 if (n >= 0)
12493 tp = find_tabpage(n);
12494 }
12495 else
12496 tp = curtab;
12497
12498 if (tp != NULL)
12499 wp = find_win_by_nr(wvp, tp);
12500 }
12501 else
12502 wp = curwin;
12503
12504 return wp;
12505}
12506
12507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012508 * "getwinvar()" function
12509 */
12510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012511f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012513 getwinvar(argvars, rettv, 0);
12514}
12515
12516/*
12517 * getwinvar() and gettabwinvar()
12518 */
12519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012520getwinvar(
12521 typval_T *argvars,
12522 typval_T *rettv,
12523 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012524{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012525 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012526 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012527 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012528 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012529 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012530#ifdef FEAT_WINDOWS
12531 win_T *oldcurwin;
12532 tabpage_T *oldtabpage;
12533 int need_switch_win;
12534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012536#ifdef FEAT_WINDOWS
12537 if (off == 1)
12538 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12539 else
12540 tp = curtab;
12541#endif
12542 win = find_win_by_nr(&argvars[off], tp);
12543 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012544 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012545
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012546 rettv->v_type = VAR_STRING;
12547 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012548
12549 if (win != NULL && varname != NULL)
12550 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012551#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012552 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012553 * otherwise the window is not valid. Only do this when needed,
12554 * autocommands get blocked. */
12555 need_switch_win = !(tp == curtab && win == curwin);
12556 if (!need_switch_win
12557 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12558#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012559 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012560 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012561 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012562 if (get_option_tv(&varname, rettv, 1) == OK)
12563 done = TRUE;
12564 }
12565 else
12566 {
12567 /* Look up the variable. */
12568 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12569 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12570 varname, FALSE);
12571 if (v != NULL)
12572 {
12573 copy_tv(&v->di_tv, rettv);
12574 done = TRUE;
12575 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012577 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012578
Bram Moolenaarba117c22015-09-29 16:53:22 +020012579#ifdef FEAT_WINDOWS
12580 if (need_switch_win)
12581 /* restore previous notion of curwin */
12582 restore_win(oldcurwin, oldtabpage, TRUE);
12583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012584 }
12585
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012586 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12587 /* use the default return value */
12588 copy_tv(&argvars[off + 2], rettv);
12589
Bram Moolenaar071d4272004-06-13 20:20:40 +000012590 --emsg_off;
12591}
12592
12593/*
12594 * "glob()" function
12595 */
12596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012597f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012598{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012599 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012601 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012602
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012603 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012604 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012605 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012606 if (argvars[1].v_type != VAR_UNKNOWN)
12607 {
12608 if (get_tv_number_chk(&argvars[1], &error))
12609 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012610 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012611 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012612 if (get_tv_number_chk(&argvars[2], &error))
12613 {
12614 rettv->v_type = VAR_LIST;
12615 rettv->vval.v_list = NULL;
12616 }
12617 if (argvars[3].v_type != VAR_UNKNOWN
12618 && get_tv_number_chk(&argvars[3], &error))
12619 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012620 }
12621 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012622 if (!error)
12623 {
12624 ExpandInit(&xpc);
12625 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012626 if (p_wic)
12627 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012628 if (rettv->v_type == VAR_STRING)
12629 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012630 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012631 else if (rettv_list_alloc(rettv) != FAIL)
12632 {
12633 int i;
12634
12635 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12636 NULL, options, WILD_ALL_KEEP);
12637 for (i = 0; i < xpc.xp_numfiles; i++)
12638 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12639
12640 ExpandCleanup(&xpc);
12641 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012642 }
12643 else
12644 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645}
12646
12647/*
12648 * "globpath()" function
12649 */
12650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012651f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012653 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012654 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012655 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012656 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012657 garray_T ga;
12658 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012659
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012660 /* When the optional second argument is non-zero, don't remove matches
12661 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012662 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012663 if (argvars[2].v_type != VAR_UNKNOWN)
12664 {
12665 if (get_tv_number_chk(&argvars[2], &error))
12666 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012667 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012668 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012669 if (get_tv_number_chk(&argvars[3], &error))
12670 {
12671 rettv->v_type = VAR_LIST;
12672 rettv->vval.v_list = NULL;
12673 }
12674 if (argvars[4].v_type != VAR_UNKNOWN
12675 && get_tv_number_chk(&argvars[4], &error))
12676 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012677 }
12678 }
12679 if (file != NULL && !error)
12680 {
12681 ga_init2(&ga, (int)sizeof(char_u *), 10);
12682 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12683 if (rettv->v_type == VAR_STRING)
12684 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12685 else if (rettv_list_alloc(rettv) != FAIL)
12686 for (i = 0; i < ga.ga_len; ++i)
12687 list_append_string(rettv->vval.v_list,
12688 ((char_u **)(ga.ga_data))[i], -1);
12689 ga_clear_strings(&ga);
12690 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012691 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012692 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012693}
12694
12695/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012696 * "glob2regpat()" function
12697 */
12698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012699f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012700{
12701 char_u *pat = get_tv_string_chk(&argvars[0]);
12702
12703 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010012704 rettv->vval.v_string = (pat == NULL)
12705 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012706}
12707
12708/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012709 * "has()" function
12710 */
12711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012712f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713{
12714 int i;
12715 char_u *name;
12716 int n = FALSE;
12717 static char *(has_list[]) =
12718 {
12719#ifdef AMIGA
12720 "amiga",
12721# ifdef FEAT_ARP
12722 "arp",
12723# endif
12724#endif
12725#ifdef __BEOS__
12726 "beos",
12727#endif
12728#ifdef MSDOS
12729# ifdef DJGPP
12730 "dos32",
12731# else
12732 "dos16",
12733# endif
12734#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012735#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736 "mac",
12737#endif
12738#if defined(MACOS_X_UNIX)
12739 "macunix",
12740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012741#ifdef __QNX__
12742 "qnx",
12743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012744#ifdef UNIX
12745 "unix",
12746#endif
12747#ifdef VMS
12748 "vms",
12749#endif
12750#ifdef WIN16
12751 "win16",
12752#endif
12753#ifdef WIN32
12754 "win32",
12755#endif
12756#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12757 "win32unix",
12758#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012759#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760 "win64",
12761#endif
12762#ifdef EBCDIC
12763 "ebcdic",
12764#endif
12765#ifndef CASE_INSENSITIVE_FILENAME
12766 "fname_case",
12767#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012768#ifdef HAVE_ACL
12769 "acl",
12770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771#ifdef FEAT_ARABIC
12772 "arabic",
12773#endif
12774#ifdef FEAT_AUTOCMD
12775 "autocmd",
12776#endif
12777#ifdef FEAT_BEVAL
12778 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012779# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12780 "balloon_multiline",
12781# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012782#endif
12783#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12784 "builtin_terms",
12785# ifdef ALL_BUILTIN_TCAPS
12786 "all_builtin_terms",
12787# endif
12788#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012789#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12790 || defined(FEAT_GUI_W32) \
12791 || defined(FEAT_GUI_MOTIF))
12792 "browsefilter",
12793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794#ifdef FEAT_BYTEOFF
12795 "byte_offset",
12796#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010012797#ifdef FEAT_CHANNEL
12798 "channel",
12799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800#ifdef FEAT_CINDENT
12801 "cindent",
12802#endif
12803#ifdef FEAT_CLIENTSERVER
12804 "clientserver",
12805#endif
12806#ifdef FEAT_CLIPBOARD
12807 "clipboard",
12808#endif
12809#ifdef FEAT_CMDL_COMPL
12810 "cmdline_compl",
12811#endif
12812#ifdef FEAT_CMDHIST
12813 "cmdline_hist",
12814#endif
12815#ifdef FEAT_COMMENTS
12816 "comments",
12817#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012818#ifdef FEAT_CONCEAL
12819 "conceal",
12820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821#ifdef FEAT_CRYPT
12822 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010012823 "crypt-blowfish",
12824 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012825#endif
12826#ifdef FEAT_CSCOPE
12827 "cscope",
12828#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012829#ifdef FEAT_CURSORBIND
12830 "cursorbind",
12831#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012832#ifdef CURSOR_SHAPE
12833 "cursorshape",
12834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835#ifdef DEBUG
12836 "debug",
12837#endif
12838#ifdef FEAT_CON_DIALOG
12839 "dialog_con",
12840#endif
12841#ifdef FEAT_GUI_DIALOG
12842 "dialog_gui",
12843#endif
12844#ifdef FEAT_DIFF
12845 "diff",
12846#endif
12847#ifdef FEAT_DIGRAPHS
12848 "digraphs",
12849#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012850#ifdef FEAT_DIRECTX
12851 "directx",
12852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012853#ifdef FEAT_DND
12854 "dnd",
12855#endif
12856#ifdef FEAT_EMACS_TAGS
12857 "emacs_tags",
12858#endif
12859 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010012860 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012861#ifdef FEAT_SEARCH_EXTRA
12862 "extra_search",
12863#endif
12864#ifdef FEAT_FKMAP
12865 "farsi",
12866#endif
12867#ifdef FEAT_SEARCHPATH
12868 "file_in_path",
12869#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012870#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012871 "filterpipe",
12872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873#ifdef FEAT_FIND_ID
12874 "find_in_path",
12875#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012876#ifdef FEAT_FLOAT
12877 "float",
12878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879#ifdef FEAT_FOLDING
12880 "folding",
12881#endif
12882#ifdef FEAT_FOOTER
12883 "footer",
12884#endif
12885#if !defined(USE_SYSTEM) && defined(UNIX)
12886 "fork",
12887#endif
12888#ifdef FEAT_GETTEXT
12889 "gettext",
12890#endif
12891#ifdef FEAT_GUI
12892 "gui",
12893#endif
12894#ifdef FEAT_GUI_ATHENA
12895# ifdef FEAT_GUI_NEXTAW
12896 "gui_neXtaw",
12897# else
12898 "gui_athena",
12899# endif
12900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012901#ifdef FEAT_GUI_GTK
12902 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012903 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012905#ifdef FEAT_GUI_GNOME
12906 "gui_gnome",
12907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908#ifdef FEAT_GUI_MAC
12909 "gui_mac",
12910#endif
12911#ifdef FEAT_GUI_MOTIF
12912 "gui_motif",
12913#endif
12914#ifdef FEAT_GUI_PHOTON
12915 "gui_photon",
12916#endif
12917#ifdef FEAT_GUI_W16
12918 "gui_win16",
12919#endif
12920#ifdef FEAT_GUI_W32
12921 "gui_win32",
12922#endif
12923#ifdef FEAT_HANGULIN
12924 "hangul_input",
12925#endif
12926#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12927 "iconv",
12928#endif
12929#ifdef FEAT_INS_EXPAND
12930 "insert_expand",
12931#endif
12932#ifdef FEAT_JUMPLIST
12933 "jumplist",
12934#endif
12935#ifdef FEAT_KEYMAP
12936 "keymap",
12937#endif
12938#ifdef FEAT_LANGMAP
12939 "langmap",
12940#endif
12941#ifdef FEAT_LIBCALL
12942 "libcall",
12943#endif
12944#ifdef FEAT_LINEBREAK
12945 "linebreak",
12946#endif
12947#ifdef FEAT_LISP
12948 "lispindent",
12949#endif
12950#ifdef FEAT_LISTCMDS
12951 "listcmds",
12952#endif
12953#ifdef FEAT_LOCALMAP
12954 "localmap",
12955#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012956#ifdef FEAT_LUA
12957# ifndef DYNAMIC_LUA
12958 "lua",
12959# endif
12960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961#ifdef FEAT_MENU
12962 "menu",
12963#endif
12964#ifdef FEAT_SESSION
12965 "mksession",
12966#endif
12967#ifdef FEAT_MODIFY_FNAME
12968 "modify_fname",
12969#endif
12970#ifdef FEAT_MOUSE
12971 "mouse",
12972#endif
12973#ifdef FEAT_MOUSESHAPE
12974 "mouseshape",
12975#endif
12976#if defined(UNIX) || defined(VMS)
12977# ifdef FEAT_MOUSE_DEC
12978 "mouse_dec",
12979# endif
12980# ifdef FEAT_MOUSE_GPM
12981 "mouse_gpm",
12982# endif
12983# ifdef FEAT_MOUSE_JSB
12984 "mouse_jsbterm",
12985# endif
12986# ifdef FEAT_MOUSE_NET
12987 "mouse_netterm",
12988# endif
12989# ifdef FEAT_MOUSE_PTERM
12990 "mouse_pterm",
12991# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012992# ifdef FEAT_MOUSE_SGR
12993 "mouse_sgr",
12994# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012995# ifdef FEAT_SYSMOUSE
12996 "mouse_sysmouse",
12997# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012998# ifdef FEAT_MOUSE_URXVT
12999 "mouse_urxvt",
13000# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013001# ifdef FEAT_MOUSE_XTERM
13002 "mouse_xterm",
13003# endif
13004#endif
13005#ifdef FEAT_MBYTE
13006 "multi_byte",
13007#endif
13008#ifdef FEAT_MBYTE_IME
13009 "multi_byte_ime",
13010#endif
13011#ifdef FEAT_MULTI_LANG
13012 "multi_lang",
13013#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013014#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013015#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013016 "mzscheme",
13017#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013019#ifdef FEAT_OLE
13020 "ole",
13021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022#ifdef FEAT_PATH_EXTRA
13023 "path_extra",
13024#endif
13025#ifdef FEAT_PERL
13026#ifndef DYNAMIC_PERL
13027 "perl",
13028#endif
13029#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013030#ifdef FEAT_PERSISTENT_UNDO
13031 "persistent_undo",
13032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033#ifdef FEAT_PYTHON
13034#ifndef DYNAMIC_PYTHON
13035 "python",
13036#endif
13037#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013038#ifdef FEAT_PYTHON3
13039#ifndef DYNAMIC_PYTHON3
13040 "python3",
13041#endif
13042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043#ifdef FEAT_POSTSCRIPT
13044 "postscript",
13045#endif
13046#ifdef FEAT_PRINTER
13047 "printer",
13048#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013049#ifdef FEAT_PROFILE
13050 "profile",
13051#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013052#ifdef FEAT_RELTIME
13053 "reltime",
13054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055#ifdef FEAT_QUICKFIX
13056 "quickfix",
13057#endif
13058#ifdef FEAT_RIGHTLEFT
13059 "rightleft",
13060#endif
13061#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13062 "ruby",
13063#endif
13064#ifdef FEAT_SCROLLBIND
13065 "scrollbind",
13066#endif
13067#ifdef FEAT_CMDL_INFO
13068 "showcmd",
13069 "cmdline_info",
13070#endif
13071#ifdef FEAT_SIGNS
13072 "signs",
13073#endif
13074#ifdef FEAT_SMARTINDENT
13075 "smartindent",
13076#endif
13077#ifdef FEAT_SNIFF
13078 "sniff",
13079#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013080#ifdef STARTUPTIME
13081 "startuptime",
13082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083#ifdef FEAT_STL_OPT
13084 "statusline",
13085#endif
13086#ifdef FEAT_SUN_WORKSHOP
13087 "sun_workshop",
13088#endif
13089#ifdef FEAT_NETBEANS_INTG
13090 "netbeans_intg",
13091#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013092#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013093 "spell",
13094#endif
13095#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013096 "syntax",
13097#endif
13098#if defined(USE_SYSTEM) || !defined(UNIX)
13099 "system",
13100#endif
13101#ifdef FEAT_TAG_BINS
13102 "tag_binary",
13103#endif
13104#ifdef FEAT_TAG_OLDSTATIC
13105 "tag_old_static",
13106#endif
13107#ifdef FEAT_TAG_ANYWHITE
13108 "tag_any_white",
13109#endif
13110#ifdef FEAT_TCL
13111# ifndef DYNAMIC_TCL
13112 "tcl",
13113# endif
13114#endif
13115#ifdef TERMINFO
13116 "terminfo",
13117#endif
13118#ifdef FEAT_TERMRESPONSE
13119 "termresponse",
13120#endif
13121#ifdef FEAT_TEXTOBJ
13122 "textobjects",
13123#endif
13124#ifdef HAVE_TGETENT
13125 "tgetent",
13126#endif
13127#ifdef FEAT_TITLE
13128 "title",
13129#endif
13130#ifdef FEAT_TOOLBAR
13131 "toolbar",
13132#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013133#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13134 "unnamedplus",
13135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136#ifdef FEAT_USR_CMDS
13137 "user-commands", /* was accidentally included in 5.4 */
13138 "user_commands",
13139#endif
13140#ifdef FEAT_VIMINFO
13141 "viminfo",
13142#endif
13143#ifdef FEAT_VERTSPLIT
13144 "vertsplit",
13145#endif
13146#ifdef FEAT_VIRTUALEDIT
13147 "virtualedit",
13148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150#ifdef FEAT_VISUALEXTRA
13151 "visualextra",
13152#endif
13153#ifdef FEAT_VREPLACE
13154 "vreplace",
13155#endif
13156#ifdef FEAT_WILDIGN
13157 "wildignore",
13158#endif
13159#ifdef FEAT_WILDMENU
13160 "wildmenu",
13161#endif
13162#ifdef FEAT_WINDOWS
13163 "windows",
13164#endif
13165#ifdef FEAT_WAK
13166 "winaltkeys",
13167#endif
13168#ifdef FEAT_WRITEBACKUP
13169 "writebackup",
13170#endif
13171#ifdef FEAT_XIM
13172 "xim",
13173#endif
13174#ifdef FEAT_XFONTSET
13175 "xfontset",
13176#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013177#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013178 "xpm",
13179 "xpm_w32", /* for backward compatibility */
13180#else
13181# if defined(HAVE_XPM)
13182 "xpm",
13183# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013185#ifdef USE_XSMP
13186 "xsmp",
13187#endif
13188#ifdef USE_XSMP_INTERACT
13189 "xsmp_interact",
13190#endif
13191#ifdef FEAT_XCLIPBOARD
13192 "xterm_clipboard",
13193#endif
13194#ifdef FEAT_XTERM_SAVE
13195 "xterm_save",
13196#endif
13197#if defined(UNIX) && defined(FEAT_X11)
13198 "X11",
13199#endif
13200 NULL
13201 };
13202
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013203 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204 for (i = 0; has_list[i] != NULL; ++i)
13205 if (STRICMP(name, has_list[i]) == 0)
13206 {
13207 n = TRUE;
13208 break;
13209 }
13210
13211 if (n == FALSE)
13212 {
13213 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013214 {
13215 if (name[5] == '-'
13216 && STRLEN(name) > 11
13217 && vim_isdigit(name[6])
13218 && vim_isdigit(name[8])
13219 && vim_isdigit(name[10]))
13220 {
13221 int major = atoi((char *)name + 6);
13222 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013223
13224 /* Expect "patch-9.9.01234". */
13225 n = (major < VIM_VERSION_MAJOR
13226 || (major == VIM_VERSION_MAJOR
13227 && (minor < VIM_VERSION_MINOR
13228 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013229 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013230 }
13231 else
13232 n = has_patch(atoi((char *)name + 5));
13233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013234 else if (STRICMP(name, "vim_starting") == 0)
13235 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013236#ifdef FEAT_MBYTE
13237 else if (STRICMP(name, "multi_byte_encoding") == 0)
13238 n = has_mbyte;
13239#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013240#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13241 else if (STRICMP(name, "balloon_multiline") == 0)
13242 n = multiline_balloon_available();
13243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244#ifdef DYNAMIC_TCL
13245 else if (STRICMP(name, "tcl") == 0)
13246 n = tcl_enabled(FALSE);
13247#endif
13248#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13249 else if (STRICMP(name, "iconv") == 0)
13250 n = iconv_enabled(FALSE);
13251#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013252#ifdef DYNAMIC_LUA
13253 else if (STRICMP(name, "lua") == 0)
13254 n = lua_enabled(FALSE);
13255#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013256#ifdef DYNAMIC_MZSCHEME
13257 else if (STRICMP(name, "mzscheme") == 0)
13258 n = mzscheme_enabled(FALSE);
13259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260#ifdef DYNAMIC_RUBY
13261 else if (STRICMP(name, "ruby") == 0)
13262 n = ruby_enabled(FALSE);
13263#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013264#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265#ifdef DYNAMIC_PYTHON
13266 else if (STRICMP(name, "python") == 0)
13267 n = python_enabled(FALSE);
13268#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013269#endif
13270#ifdef FEAT_PYTHON3
13271#ifdef DYNAMIC_PYTHON3
13272 else if (STRICMP(name, "python3") == 0)
13273 n = python3_enabled(FALSE);
13274#endif
13275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013276#ifdef DYNAMIC_PERL
13277 else if (STRICMP(name, "perl") == 0)
13278 n = perl_enabled(FALSE);
13279#endif
13280#ifdef FEAT_GUI
13281 else if (STRICMP(name, "gui_running") == 0)
13282 n = (gui.in_use || gui.starting);
13283# ifdef FEAT_GUI_W32
13284 else if (STRICMP(name, "gui_win32s") == 0)
13285 n = gui_is_win32s();
13286# endif
13287# ifdef FEAT_BROWSE
13288 else if (STRICMP(name, "browse") == 0)
13289 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13290# endif
13291#endif
13292#ifdef FEAT_SYN_HL
13293 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013294 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295#endif
13296#if defined(WIN3264)
13297 else if (STRICMP(name, "win95") == 0)
13298 n = mch_windows95();
13299#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013300#ifdef FEAT_NETBEANS_INTG
13301 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013302 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304 }
13305
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013306 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013307}
13308
13309/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013310 * "has_key()" function
13311 */
13312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013313f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013314{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013315 if (argvars[0].v_type != VAR_DICT)
13316 {
13317 EMSG(_(e_dictreq));
13318 return;
13319 }
13320 if (argvars[0].vval.v_dict == NULL)
13321 return;
13322
13323 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013324 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013325}
13326
13327/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013328 * "haslocaldir()" function
13329 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013331f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013332{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013333 win_T *wp = NULL;
13334
13335 wp = find_tabwin(&argvars[0], &argvars[1]);
13336 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013337}
13338
13339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340 * "hasmapto()" function
13341 */
13342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013343f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013344{
13345 char_u *name;
13346 char_u *mode;
13347 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013348 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013350 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013351 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352 mode = (char_u *)"nvo";
13353 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013354 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013355 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013356 if (argvars[2].v_type != VAR_UNKNOWN)
13357 abbr = get_tv_number(&argvars[2]);
13358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013359
Bram Moolenaar2c932302006-03-18 21:42:09 +000013360 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013361 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013363 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364}
13365
13366/*
13367 * "histadd()" function
13368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013370f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013371{
13372#ifdef FEAT_CMDHIST
13373 int histype;
13374 char_u *str;
13375 char_u buf[NUMBUFLEN];
13376#endif
13377
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013378 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379 if (check_restricted() || check_secure())
13380 return;
13381#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013382 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13383 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384 if (histype >= 0)
13385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013386 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387 if (*str != NUL)
13388 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013389 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013391 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013392 return;
13393 }
13394 }
13395#endif
13396}
13397
13398/*
13399 * "histdel()" function
13400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013402f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403{
13404#ifdef FEAT_CMDHIST
13405 int n;
13406 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013407 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013408
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013409 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13410 if (str == NULL)
13411 n = 0;
13412 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013414 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013415 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013417 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013418 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013419 else
13420 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013421 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013422 get_tv_string_buf(&argvars[1], buf));
13423 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013424#endif
13425}
13426
13427/*
13428 * "histget()" function
13429 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013431f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432{
13433#ifdef FEAT_CMDHIST
13434 int type;
13435 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013436 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013437
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013438 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13439 if (str == NULL)
13440 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013442 {
13443 type = get_histtype(str);
13444 if (argvars[1].v_type == VAR_UNKNOWN)
13445 idx = get_history_idx(type);
13446 else
13447 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13448 /* -1 on type error */
13449 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013451#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013452 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013453#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013454 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013455}
13456
13457/*
13458 * "histnr()" function
13459 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013461f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462{
13463 int i;
13464
13465#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013466 char_u *history = get_tv_string_chk(&argvars[0]);
13467
13468 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013469 if (i >= HIST_CMD && i < HIST_COUNT)
13470 i = get_history_idx(i);
13471 else
13472#endif
13473 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013474 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475}
13476
13477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478 * "highlightID(name)" function
13479 */
13480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013481f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013483 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484}
13485
13486/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013487 * "highlight_exists()" function
13488 */
13489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013490f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013491{
13492 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13493}
13494
13495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496 * "hostname()" function
13497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013499f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500{
13501 char_u hostname[256];
13502
13503 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013504 rettv->v_type = VAR_STRING;
13505 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506}
13507
13508/*
13509 * iconv() function
13510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013512f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513{
13514#ifdef FEAT_MBYTE
13515 char_u buf1[NUMBUFLEN];
13516 char_u buf2[NUMBUFLEN];
13517 char_u *from, *to, *str;
13518 vimconv_T vimconv;
13519#endif
13520
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013521 rettv->v_type = VAR_STRING;
13522 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523
13524#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013525 str = get_tv_string(&argvars[0]);
13526 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13527 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528 vimconv.vc_type = CONV_NONE;
13529 convert_setup(&vimconv, from, to);
13530
13531 /* If the encodings are equal, no conversion needed. */
13532 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013533 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013535 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536
13537 convert_setup(&vimconv, NULL, NULL);
13538 vim_free(from);
13539 vim_free(to);
13540#endif
13541}
13542
13543/*
13544 * "indent()" function
13545 */
13546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013547f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548{
13549 linenr_T lnum;
13550
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013551 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013552 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013553 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013555 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013556}
13557
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013558/*
13559 * "index()" function
13560 */
13561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013562f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013563{
Bram Moolenaar33570922005-01-25 22:26:29 +000013564 list_T *l;
13565 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013566 long idx = 0;
13567 int ic = FALSE;
13568
13569 rettv->vval.v_number = -1;
13570 if (argvars[0].v_type != VAR_LIST)
13571 {
13572 EMSG(_(e_listreq));
13573 return;
13574 }
13575 l = argvars[0].vval.v_list;
13576 if (l != NULL)
13577 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013578 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013579 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013580 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013581 int error = FALSE;
13582
Bram Moolenaar758711c2005-02-02 23:11:38 +000013583 /* Start at specified item. Use the cached index that list_find()
13584 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013585 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013586 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013587 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013588 ic = get_tv_number_chk(&argvars[3], &error);
13589 if (error)
13590 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013591 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013592
Bram Moolenaar758711c2005-02-02 23:11:38 +000013593 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013594 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013595 {
13596 rettv->vval.v_number = idx;
13597 break;
13598 }
13599 }
13600}
13601
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602static int inputsecret_flag = 0;
13603
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013604static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013605
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013607 * This function is used by f_input() and f_inputdialog() functions. The third
13608 * argument to f_input() specifies the type of completion to use at the
13609 * prompt. The third argument to f_inputdialog() specifies the value to return
13610 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013611 */
13612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013613get_user_input(
13614 typval_T *argvars,
13615 typval_T *rettv,
13616 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013618 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619 char_u *p = NULL;
13620 int c;
13621 char_u buf[NUMBUFLEN];
13622 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013623 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013624 int xp_type = EXPAND_NOTHING;
13625 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013627 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013628 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629
13630#ifdef NO_CONSOLE_INPUT
13631 /* While starting up, there is no place to enter text. */
13632 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634#endif
13635
13636 cmd_silent = FALSE; /* Want to see the prompt. */
13637 if (prompt != NULL)
13638 {
13639 /* Only the part of the message after the last NL is considered as
13640 * prompt for the command line */
13641 p = vim_strrchr(prompt, '\n');
13642 if (p == NULL)
13643 p = prompt;
13644 else
13645 {
13646 ++p;
13647 c = *p;
13648 *p = NUL;
13649 msg_start();
13650 msg_clr_eos();
13651 msg_puts_attr(prompt, echo_attr);
13652 msg_didout = FALSE;
13653 msg_starthere();
13654 *p = c;
13655 }
13656 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013658 if (argvars[1].v_type != VAR_UNKNOWN)
13659 {
13660 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13661 if (defstr != NULL)
13662 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013664 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013665 {
13666 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013667 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013668 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013669
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013670 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013671 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013672
Bram Moolenaar4463f292005-09-25 22:20:24 +000013673 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13674 if (xp_name == NULL)
13675 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013676
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013677 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013678
Bram Moolenaar4463f292005-09-25 22:20:24 +000013679 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13680 &xp_arg) == FAIL)
13681 return;
13682 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013683 }
13684
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013685 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013686 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013687 int save_ex_normal_busy = ex_normal_busy;
13688 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013689 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013690 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13691 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013692 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013693 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013694 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013695 && argvars[1].v_type != VAR_UNKNOWN
13696 && argvars[2].v_type != VAR_UNKNOWN)
13697 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13698 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013699
13700 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013702 /* since the user typed this, no need to wait for return */
13703 need_wait_return = FALSE;
13704 msg_didout = FALSE;
13705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706 cmd_silent = cmd_silent_save;
13707}
13708
13709/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013710 * "input()" function
13711 * Also handles inputsecret() when inputsecret is set.
13712 */
13713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013714f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013715{
13716 get_user_input(argvars, rettv, FALSE);
13717}
13718
13719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720 * "inputdialog()" function
13721 */
13722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013723f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724{
13725#if defined(FEAT_GUI_TEXTDIALOG)
13726 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13727 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13728 {
13729 char_u *message;
13730 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013731 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013733 message = get_tv_string_chk(&argvars[0]);
13734 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013735 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013736 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737 else
13738 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013739 if (message != NULL && defstr != NULL
13740 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013741 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013742 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013743 else
13744 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013745 if (message != NULL && defstr != NULL
13746 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013747 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013748 rettv->vval.v_string = vim_strsave(
13749 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013750 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013751 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013753 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754 }
13755 else
13756#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013757 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758}
13759
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013760/*
13761 * "inputlist()" function
13762 */
13763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013764f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013765{
13766 listitem_T *li;
13767 int selected;
13768 int mouse_used;
13769
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013770#ifdef NO_CONSOLE_INPUT
13771 /* While starting up, there is no place to enter text. */
13772 if (no_console_input())
13773 return;
13774#endif
13775 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13776 {
13777 EMSG2(_(e_listarg), "inputlist()");
13778 return;
13779 }
13780
13781 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013782 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013783 lines_left = Rows; /* avoid more prompt */
13784 msg_scroll = TRUE;
13785 msg_clr_eos();
13786
13787 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13788 {
13789 msg_puts(get_tv_string(&li->li_tv));
13790 msg_putchar('\n');
13791 }
13792
13793 /* Ask for choice. */
13794 selected = prompt_for_number(&mouse_used);
13795 if (mouse_used)
13796 selected -= lines_left;
13797
13798 rettv->vval.v_number = selected;
13799}
13800
13801
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13803
13804/*
13805 * "inputrestore()" function
13806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013808f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809{
13810 if (ga_userinput.ga_len > 0)
13811 {
13812 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13814 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013815 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816 }
13817 else if (p_verbose > 1)
13818 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013819 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013820 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821 }
13822}
13823
13824/*
13825 * "inputsave()" function
13826 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013828f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013830 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831 if (ga_grow(&ga_userinput, 1) == OK)
13832 {
13833 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13834 + ga_userinput.ga_len);
13835 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013836 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837 }
13838 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013839 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840}
13841
13842/*
13843 * "inputsecret()" function
13844 */
13845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013846f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013847{
13848 ++cmdline_star;
13849 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013850 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013851 --cmdline_star;
13852 --inputsecret_flag;
13853}
13854
13855/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013856 * "insert()" function
13857 */
13858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013859f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013860{
13861 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013862 listitem_T *item;
13863 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013864 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013865
13866 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013867 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013868 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013869 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013870 {
13871 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013872 before = get_tv_number_chk(&argvars[2], &error);
13873 if (error)
13874 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013875
Bram Moolenaar758711c2005-02-02 23:11:38 +000013876 if (before == l->lv_len)
13877 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013878 else
13879 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013880 item = list_find(l, before);
13881 if (item == NULL)
13882 {
13883 EMSGN(_(e_listidx), before);
13884 l = NULL;
13885 }
13886 }
13887 if (l != NULL)
13888 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013889 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013890 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013891 }
13892 }
13893}
13894
13895/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013896 * "invert(expr)" function
13897 */
13898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013899f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013900{
13901 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13902}
13903
13904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905 * "isdirectory()" function
13906 */
13907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013908f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013910 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911}
13912
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013913/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013914 * "islocked()" function
13915 */
13916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013917f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013918{
13919 lval_T lv;
13920 char_u *end;
13921 dictitem_T *di;
13922
13923 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013924 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13925 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013926 if (end != NULL && lv.ll_name != NULL)
13927 {
13928 if (*end != NUL)
13929 EMSG(_(e_trailing));
13930 else
13931 {
13932 if (lv.ll_tv == NULL)
13933 {
13934 if (check_changedtick(lv.ll_name))
13935 rettv->vval.v_number = 1; /* always locked */
13936 else
13937 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013938 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013939 if (di != NULL)
13940 {
13941 /* Consider a variable locked when:
13942 * 1. the variable itself is locked
13943 * 2. the value of the variable is locked.
13944 * 3. the List or Dict value is locked.
13945 */
13946 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13947 || tv_islocked(&di->di_tv));
13948 }
13949 }
13950 }
13951 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013952 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013953 else if (lv.ll_newkey != NULL)
13954 EMSG2(_(e_dictkey), lv.ll_newkey);
13955 else if (lv.ll_list != NULL)
13956 /* List item. */
13957 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13958 else
13959 /* Dictionary item. */
13960 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13961 }
13962 }
13963
13964 clear_lval(&lv);
13965}
13966
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013967static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013968
13969/*
13970 * Turn a dict into a list:
13971 * "what" == 0: list of keys
13972 * "what" == 1: list of values
13973 * "what" == 2: list of items
13974 */
13975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013976dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013977{
Bram Moolenaar33570922005-01-25 22:26:29 +000013978 list_T *l2;
13979 dictitem_T *di;
13980 hashitem_T *hi;
13981 listitem_T *li;
13982 listitem_T *li2;
13983 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013984 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013985
Bram Moolenaar8c711452005-01-14 21:53:12 +000013986 if (argvars[0].v_type != VAR_DICT)
13987 {
13988 EMSG(_(e_dictreq));
13989 return;
13990 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013991 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013992 return;
13993
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013994 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013995 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013996
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013997 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013998 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013999 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014000 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014001 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014002 --todo;
14003 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014004
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014005 li = listitem_alloc();
14006 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014007 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014008 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014009
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014010 if (what == 0)
14011 {
14012 /* keys() */
14013 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014014 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014015 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14016 }
14017 else if (what == 1)
14018 {
14019 /* values() */
14020 copy_tv(&di->di_tv, &li->li_tv);
14021 }
14022 else
14023 {
14024 /* items() */
14025 l2 = list_alloc();
14026 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014027 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014028 li->li_tv.vval.v_list = l2;
14029 if (l2 == NULL)
14030 break;
14031 ++l2->lv_refcount;
14032
14033 li2 = listitem_alloc();
14034 if (li2 == NULL)
14035 break;
14036 list_append(l2, li2);
14037 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014038 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014039 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14040
14041 li2 = listitem_alloc();
14042 if (li2 == NULL)
14043 break;
14044 list_append(l2, li2);
14045 copy_tv(&di->di_tv, &li2->li_tv);
14046 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014047 }
14048 }
14049}
14050
14051/*
14052 * "items(dict)" function
14053 */
14054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014055f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014056{
14057 dict_list(argvars, rettv, 2);
14058}
14059
Bram Moolenaar071d4272004-06-13 20:20:40 +000014060/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014061 * "join()" function
14062 */
14063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014064f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014065{
14066 garray_T ga;
14067 char_u *sep;
14068
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014069 if (argvars[0].v_type != VAR_LIST)
14070 {
14071 EMSG(_(e_listreq));
14072 return;
14073 }
14074 if (argvars[0].vval.v_list == NULL)
14075 return;
14076 if (argvars[1].v_type == VAR_UNKNOWN)
14077 sep = (char_u *)" ";
14078 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014079 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014080
14081 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014082
14083 if (sep != NULL)
14084 {
14085 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014086 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014087 ga_append(&ga, NUL);
14088 rettv->vval.v_string = (char_u *)ga.ga_data;
14089 }
14090 else
14091 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014092}
14093
14094/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014095 * "jsondecode()" function
14096 */
14097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014098f_jsondecode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014099{
14100 js_read_T reader;
14101
14102 reader.js_buf = get_tv_string(&argvars[0]);
14103 reader.js_eof = TRUE;
14104 reader.js_used = 0;
14105 json_decode(&reader, rettv);
14106}
14107
14108/*
14109 * "jsonencode()" function
14110 */
14111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014112f_jsonencode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014113{
14114 rettv->v_type = VAR_STRING;
14115 rettv->vval.v_string = json_encode(&argvars[0]);
14116}
14117
14118/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014119 * "keys()" function
14120 */
14121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014122f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014123{
14124 dict_list(argvars, rettv, 0);
14125}
14126
14127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128 * "last_buffer_nr()" function.
14129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014131f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132{
14133 int n = 0;
14134 buf_T *buf;
14135
14136 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14137 if (n < buf->b_fnum)
14138 n = buf->b_fnum;
14139
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014140 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014141}
14142
14143/*
14144 * "len()" function
14145 */
14146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014147f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014148{
14149 switch (argvars[0].v_type)
14150 {
14151 case VAR_STRING:
14152 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014153 rettv->vval.v_number = (varnumber_T)STRLEN(
14154 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014155 break;
14156 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014157 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014158 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014159 case VAR_DICT:
14160 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14161 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014162 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014163 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014164 break;
14165 }
14166}
14167
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014168static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014169
14170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014171libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014172{
14173#ifdef FEAT_LIBCALL
14174 char_u *string_in;
14175 char_u **string_result;
14176 int nr_result;
14177#endif
14178
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014179 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014180 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014181 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014182
14183 if (check_restricted() || check_secure())
14184 return;
14185
14186#ifdef FEAT_LIBCALL
14187 /* The first two args must be strings, otherwise its meaningless */
14188 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14189 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014190 string_in = NULL;
14191 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014192 string_in = argvars[2].vval.v_string;
14193 if (type == VAR_NUMBER)
14194 string_result = NULL;
14195 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014196 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014197 if (mch_libcall(argvars[0].vval.v_string,
14198 argvars[1].vval.v_string,
14199 string_in,
14200 argvars[2].vval.v_number,
14201 string_result,
14202 &nr_result) == OK
14203 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014204 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014205 }
14206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014207}
14208
14209/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014210 * "libcall()" function
14211 */
14212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014213f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014214{
14215 libcall_common(argvars, rettv, VAR_STRING);
14216}
14217
14218/*
14219 * "libcallnr()" function
14220 */
14221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014222f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014223{
14224 libcall_common(argvars, rettv, VAR_NUMBER);
14225}
14226
14227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228 * "line(string)" function
14229 */
14230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014231f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232{
14233 linenr_T lnum = 0;
14234 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014235 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014237 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238 if (fp != NULL)
14239 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014240 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241}
14242
14243/*
14244 * "line2byte(lnum)" function
14245 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014247f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248{
14249#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014250 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014251#else
14252 linenr_T lnum;
14253
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014254 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014256 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014257 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014258 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14259 if (rettv->vval.v_number >= 0)
14260 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261#endif
14262}
14263
14264/*
14265 * "lispindent(lnum)" function
14266 */
14267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014268f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269{
14270#ifdef FEAT_LISP
14271 pos_T pos;
14272 linenr_T lnum;
14273
14274 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014275 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014276 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14277 {
14278 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014279 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280 curwin->w_cursor = pos;
14281 }
14282 else
14283#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014284 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285}
14286
14287/*
14288 * "localtime()" function
14289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014291f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014292{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014293 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014294}
14295
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014296static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014297
14298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014299get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014300{
14301 char_u *keys;
14302 char_u *which;
14303 char_u buf[NUMBUFLEN];
14304 char_u *keys_buf = NULL;
14305 char_u *rhs;
14306 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014307 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014308 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014309 mapblock_T *mp;
14310 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014311
14312 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014313 rettv->v_type = VAR_STRING;
14314 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014316 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014317 if (*keys == NUL)
14318 return;
14319
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014320 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014321 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014322 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014323 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014324 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014325 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014326 if (argvars[3].v_type != VAR_UNKNOWN)
14327 get_dict = get_tv_number(&argvars[3]);
14328 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330 else
14331 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014332 if (which == NULL)
14333 return;
14334
Bram Moolenaar071d4272004-06-13 20:20:40 +000014335 mode = get_map_mode(&which, 0);
14336
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014337 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014338 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014340
14341 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014343 /* Return a string. */
14344 if (rhs != NULL)
14345 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346
Bram Moolenaarbd743252010-10-20 21:23:33 +020014347 }
14348 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14349 {
14350 /* Return a dictionary. */
14351 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14352 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14353 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014354
Bram Moolenaarbd743252010-10-20 21:23:33 +020014355 dict_add_nr_str(dict, "lhs", 0L, lhs);
14356 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14357 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14358 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14359 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14360 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14361 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014362 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014363 dict_add_nr_str(dict, "mode", 0L, mapmode);
14364
14365 vim_free(lhs);
14366 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367 }
14368}
14369
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014370#ifdef FEAT_FLOAT
14371/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014372 * "log()" function
14373 */
14374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014375f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014376{
14377 float_T f;
14378
14379 rettv->v_type = VAR_FLOAT;
14380 if (get_float_arg(argvars, &f) == OK)
14381 rettv->vval.v_float = log(f);
14382 else
14383 rettv->vval.v_float = 0.0;
14384}
14385
14386/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014387 * "log10()" function
14388 */
14389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014390f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014391{
14392 float_T f;
14393
14394 rettv->v_type = VAR_FLOAT;
14395 if (get_float_arg(argvars, &f) == OK)
14396 rettv->vval.v_float = log10(f);
14397 else
14398 rettv->vval.v_float = 0.0;
14399}
14400#endif
14401
Bram Moolenaar1dced572012-04-05 16:54:08 +020014402#ifdef FEAT_LUA
14403/*
14404 * "luaeval()" function
14405 */
14406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014407f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020014408{
14409 char_u *str;
14410 char_u buf[NUMBUFLEN];
14411
14412 str = get_tv_string_buf(&argvars[0], buf);
14413 do_luaeval(str, argvars + 1, rettv);
14414}
14415#endif
14416
Bram Moolenaar071d4272004-06-13 20:20:40 +000014417/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014418 * "map()" function
14419 */
14420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014421f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014422{
14423 filter_map(argvars, rettv, TRUE);
14424}
14425
14426/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014427 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014428 */
14429 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014430f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014431{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014432 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433}
14434
14435/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014436 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437 */
14438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014439f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014441 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014442}
14443
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014444static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445
14446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014447find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014448{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014449 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014450 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014451 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014452 char_u *pat;
14453 regmatch_T regmatch;
14454 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014455 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456 char_u *save_cpo;
14457 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014458 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014459 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014460 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014461 list_T *l = NULL;
14462 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014463 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014464 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465
14466 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14467 save_cpo = p_cpo;
14468 p_cpo = (char_u *)"";
14469
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014470 rettv->vval.v_number = -1;
14471 if (type == 3)
14472 {
14473 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014474 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014475 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014476 }
14477 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014479 rettv->v_type = VAR_STRING;
14480 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014483 if (argvars[0].v_type == VAR_LIST)
14484 {
14485 if ((l = argvars[0].vval.v_list) == NULL)
14486 goto theend;
14487 li = l->lv_first;
14488 }
14489 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014490 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014491 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014492 len = (long)STRLEN(str);
14493 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014494
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014495 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14496 if (pat == NULL)
14497 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014498
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014499 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014500 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014501 int error = FALSE;
14502
14503 start = get_tv_number_chk(&argvars[2], &error);
14504 if (error)
14505 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014506 if (l != NULL)
14507 {
14508 li = list_find(l, start);
14509 if (li == NULL)
14510 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014511 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014512 }
14513 else
14514 {
14515 if (start < 0)
14516 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014517 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014518 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014519 /* When "count" argument is there ignore matches before "start",
14520 * otherwise skip part of the string. Differs when pattern is "^"
14521 * or "\<". */
14522 if (argvars[3].v_type != VAR_UNKNOWN)
14523 startcol = start;
14524 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014525 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014526 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014527 len -= start;
14528 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014529 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014530
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014531 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014532 nth = get_tv_number_chk(&argvars[3], &error);
14533 if (error)
14534 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535 }
14536
14537 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14538 if (regmatch.regprog != NULL)
14539 {
14540 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014541
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014542 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014543 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014544 if (l != NULL)
14545 {
14546 if (li == NULL)
14547 {
14548 match = FALSE;
14549 break;
14550 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014551 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014552 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014553 if (str == NULL)
14554 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014555 }
14556
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014557 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014558
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014559 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014560 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014561 if (l == NULL && !match)
14562 break;
14563
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014564 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014565 if (l != NULL)
14566 {
14567 li = li->li_next;
14568 ++idx;
14569 }
14570 else
14571 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014572#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014573 startcol = (colnr_T)(regmatch.startp[0]
14574 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014575#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014576 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014577#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014578 if (startcol > (colnr_T)len
14579 || str + startcol <= regmatch.startp[0])
14580 {
14581 match = FALSE;
14582 break;
14583 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014584 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014585 }
14586
14587 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014589 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014590 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014591 int i;
14592
14593 /* return list with matched string and submatches */
14594 for (i = 0; i < NSUBEXP; ++i)
14595 {
14596 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014597 {
14598 if (list_append_string(rettv->vval.v_list,
14599 (char_u *)"", 0) == FAIL)
14600 break;
14601 }
14602 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014603 regmatch.startp[i],
14604 (int)(regmatch.endp[i] - regmatch.startp[i]))
14605 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014606 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014607 }
14608 }
14609 else if (type == 2)
14610 {
14611 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014612 if (l != NULL)
14613 copy_tv(&li->li_tv, rettv);
14614 else
14615 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014616 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014617 }
14618 else if (l != NULL)
14619 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014620 else
14621 {
14622 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014623 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624 (varnumber_T)(regmatch.startp[0] - str);
14625 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014626 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014627 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014628 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014629 }
14630 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014631 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632 }
14633
14634theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014635 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636 p_cpo = save_cpo;
14637}
14638
14639/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014640 * "match()" function
14641 */
14642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014643f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014644{
14645 find_some_match(argvars, rettv, 1);
14646}
14647
14648/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014649 * "matchadd()" function
14650 */
14651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014652f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014653{
14654#ifdef FEAT_SEARCH_EXTRA
14655 char_u buf[NUMBUFLEN];
14656 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14657 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14658 int prio = 10; /* default priority */
14659 int id = -1;
14660 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014661 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014662
14663 rettv->vval.v_number = -1;
14664
14665 if (grp == NULL || pat == NULL)
14666 return;
14667 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014668 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014669 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014670 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014671 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014672 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014673 if (argvars[4].v_type != VAR_UNKNOWN)
14674 {
14675 if (argvars[4].v_type != VAR_DICT)
14676 {
14677 EMSG(_(e_dictreq));
14678 return;
14679 }
14680 if (dict_find(argvars[4].vval.v_dict,
14681 (char_u *)"conceal", -1) != NULL)
14682 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14683 (char_u *)"conceal", FALSE);
14684 }
14685 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014686 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014687 if (error == TRUE)
14688 return;
14689 if (id >= 1 && id <= 3)
14690 {
14691 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14692 return;
14693 }
14694
Bram Moolenaar6561d522015-07-21 15:48:27 +020014695 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14696 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014697#endif
14698}
14699
14700/*
14701 * "matchaddpos()" function
14702 */
14703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014704f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020014705{
14706#ifdef FEAT_SEARCH_EXTRA
14707 char_u buf[NUMBUFLEN];
14708 char_u *group;
14709 int prio = 10;
14710 int id = -1;
14711 int error = FALSE;
14712 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014713 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014714
14715 rettv->vval.v_number = -1;
14716
14717 group = get_tv_string_buf_chk(&argvars[0], buf);
14718 if (group == NULL)
14719 return;
14720
14721 if (argvars[1].v_type != VAR_LIST)
14722 {
14723 EMSG2(_(e_listarg), "matchaddpos()");
14724 return;
14725 }
14726 l = argvars[1].vval.v_list;
14727 if (l == NULL)
14728 return;
14729
14730 if (argvars[2].v_type != VAR_UNKNOWN)
14731 {
14732 prio = get_tv_number_chk(&argvars[2], &error);
14733 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014734 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014735 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014736 if (argvars[4].v_type != VAR_UNKNOWN)
14737 {
14738 if (argvars[4].v_type != VAR_DICT)
14739 {
14740 EMSG(_(e_dictreq));
14741 return;
14742 }
14743 if (dict_find(argvars[4].vval.v_dict,
14744 (char_u *)"conceal", -1) != NULL)
14745 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14746 (char_u *)"conceal", FALSE);
14747 }
14748 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014749 }
14750 if (error == TRUE)
14751 return;
14752
14753 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14754 if (id == 1 || id == 2)
14755 {
14756 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14757 return;
14758 }
14759
Bram Moolenaar6561d522015-07-21 15:48:27 +020014760 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14761 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014762#endif
14763}
14764
14765/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014766 * "matcharg()" function
14767 */
14768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014769f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014770{
14771 if (rettv_list_alloc(rettv) == OK)
14772 {
14773#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014774 int id = get_tv_number(&argvars[0]);
14775 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014776
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014777 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014778 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014779 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14780 {
14781 list_append_string(rettv->vval.v_list,
14782 syn_id2name(m->hlg_id), -1);
14783 list_append_string(rettv->vval.v_list, m->pattern, -1);
14784 }
14785 else
14786 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014787 list_append_string(rettv->vval.v_list, NULL, -1);
14788 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014789 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014790 }
14791#endif
14792 }
14793}
14794
14795/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014796 * "matchdelete()" function
14797 */
14798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014799f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014800{
14801#ifdef FEAT_SEARCH_EXTRA
14802 rettv->vval.v_number = match_delete(curwin,
14803 (int)get_tv_number(&argvars[0]), TRUE);
14804#endif
14805}
14806
14807/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014808 * "matchend()" function
14809 */
14810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014811f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014812{
14813 find_some_match(argvars, rettv, 0);
14814}
14815
14816/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014817 * "matchlist()" function
14818 */
14819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014820f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014821{
14822 find_some_match(argvars, rettv, 3);
14823}
14824
14825/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014826 * "matchstr()" function
14827 */
14828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014829f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014830{
14831 find_some_match(argvars, rettv, 2);
14832}
14833
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014834static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014835
14836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014837max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014838{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014839 long n = 0;
14840 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014841 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014842
14843 if (argvars[0].v_type == VAR_LIST)
14844 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014845 list_T *l;
14846 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014847
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014848 l = argvars[0].vval.v_list;
14849 if (l != NULL)
14850 {
14851 li = l->lv_first;
14852 if (li != NULL)
14853 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014854 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014855 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014856 {
14857 li = li->li_next;
14858 if (li == NULL)
14859 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014860 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014861 if (domax ? i > n : i < n)
14862 n = i;
14863 }
14864 }
14865 }
14866 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014867 else if (argvars[0].v_type == VAR_DICT)
14868 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014869 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014870 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014871 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014872 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014873
14874 d = argvars[0].vval.v_dict;
14875 if (d != NULL)
14876 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014877 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014878 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014879 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014880 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014881 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014882 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014883 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014884 if (first)
14885 {
14886 n = i;
14887 first = FALSE;
14888 }
14889 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014890 n = i;
14891 }
14892 }
14893 }
14894 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014895 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014896 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014897 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014898}
14899
14900/*
14901 * "max()" function
14902 */
14903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014904f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014905{
14906 max_min(argvars, rettv, TRUE);
14907}
14908
14909/*
14910 * "min()" function
14911 */
14912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014913f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014914{
14915 max_min(argvars, rettv, FALSE);
14916}
14917
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014918static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014919
14920/*
14921 * Create the directory in which "dir" is located, and higher levels when
14922 * needed.
14923 */
14924 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010014925mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014926{
14927 char_u *p;
14928 char_u *updir;
14929 int r = FAIL;
14930
14931 /* Get end of directory name in "dir".
14932 * We're done when it's "/" or "c:/". */
14933 p = gettail_sep(dir);
14934 if (p <= get_past_head(dir))
14935 return OK;
14936
14937 /* If the directory exists we're done. Otherwise: create it.*/
14938 updir = vim_strnsave(dir, (int)(p - dir));
14939 if (updir == NULL)
14940 return FAIL;
14941 if (mch_isdir(updir))
14942 r = OK;
14943 else if (mkdir_recurse(updir, prot) == OK)
14944 r = vim_mkdir_emsg(updir, prot);
14945 vim_free(updir);
14946 return r;
14947}
14948
14949#ifdef vim_mkdir
14950/*
14951 * "mkdir()" function
14952 */
14953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014954f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014955{
14956 char_u *dir;
14957 char_u buf[NUMBUFLEN];
14958 int prot = 0755;
14959
14960 rettv->vval.v_number = FAIL;
14961 if (check_restricted() || check_secure())
14962 return;
14963
14964 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014965 if (*dir == NUL)
14966 rettv->vval.v_number = FAIL;
14967 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014968 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014969 if (*gettail(dir) == NUL)
14970 /* remove trailing slashes */
14971 *gettail_sep(dir) = NUL;
14972
14973 if (argvars[1].v_type != VAR_UNKNOWN)
14974 {
14975 if (argvars[2].v_type != VAR_UNKNOWN)
14976 prot = get_tv_number_chk(&argvars[2], NULL);
14977 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14978 mkdir_recurse(dir, prot);
14979 }
14980 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014981 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014982}
14983#endif
14984
Bram Moolenaar0d660222005-01-07 21:51:51 +000014985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014986 * "mode()" function
14987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014989f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014990{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014991 char_u buf[3];
14992
14993 buf[1] = NUL;
14994 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014995
Bram Moolenaar071d4272004-06-13 20:20:40 +000014996 if (VIsual_active)
14997 {
14998 if (VIsual_select)
14999 buf[0] = VIsual_mode + 's' - 'v';
15000 else
15001 buf[0] = VIsual_mode;
15002 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015003 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015004 || State == CONFIRM)
15005 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015006 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015007 if (State == ASKMORE)
15008 buf[1] = 'm';
15009 else if (State == CONFIRM)
15010 buf[1] = '?';
15011 }
15012 else if (State == EXTERNCMD)
15013 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015014 else if (State & INSERT)
15015 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015016#ifdef FEAT_VREPLACE
15017 if (State & VREPLACE_FLAG)
15018 {
15019 buf[0] = 'R';
15020 buf[1] = 'v';
15021 }
15022 else
15023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015024 if (State & REPLACE_FLAG)
15025 buf[0] = 'R';
15026 else
15027 buf[0] = 'i';
15028 }
15029 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015030 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015031 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015032 if (exmode_active)
15033 buf[1] = 'v';
15034 }
15035 else if (exmode_active)
15036 {
15037 buf[0] = 'c';
15038 buf[1] = 'e';
15039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015040 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015041 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015042 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015043 if (finish_op)
15044 buf[1] = 'o';
15045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015046
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015047 /* Clear out the minor mode when the argument is not a non-zero number or
15048 * non-empty string. */
15049 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015050 buf[1] = NUL;
15051
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015052 rettv->vval.v_string = vim_strsave(buf);
15053 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054}
15055
Bram Moolenaar429fa852013-04-15 12:27:36 +020015056#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015057/*
15058 * "mzeval()" function
15059 */
15060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015061f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015062{
15063 char_u *str;
15064 char_u buf[NUMBUFLEN];
15065
15066 str = get_tv_string_buf(&argvars[0], buf);
15067 do_mzeval(str, rettv);
15068}
Bram Moolenaar75676462013-01-30 14:55:42 +010015069
15070 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015071mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015072{
15073 typval_T argvars[3];
15074
15075 argvars[0].v_type = VAR_STRING;
15076 argvars[0].vval.v_string = name;
15077 copy_tv(args, &argvars[1]);
15078 argvars[2].v_type = VAR_UNKNOWN;
15079 f_call(argvars, rettv);
15080 clear_tv(&argvars[1]);
15081}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015082#endif
15083
Bram Moolenaar071d4272004-06-13 20:20:40 +000015084/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015085 * "nextnonblank()" function
15086 */
15087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015088f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015089{
15090 linenr_T lnum;
15091
15092 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15093 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015094 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015095 {
15096 lnum = 0;
15097 break;
15098 }
15099 if (*skipwhite(ml_get(lnum)) != NUL)
15100 break;
15101 }
15102 rettv->vval.v_number = lnum;
15103}
15104
15105/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015106 * "nr2char()" function
15107 */
15108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015109f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015110{
15111 char_u buf[NUMBUFLEN];
15112
15113#ifdef FEAT_MBYTE
15114 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015115 {
15116 int utf8 = 0;
15117
15118 if (argvars[1].v_type != VAR_UNKNOWN)
15119 utf8 = get_tv_number_chk(&argvars[1], NULL);
15120 if (utf8)
15121 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15122 else
15123 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015125 else
15126#endif
15127 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015128 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015129 buf[1] = NUL;
15130 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015131 rettv->v_type = VAR_STRING;
15132 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015133}
15134
15135/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015136 * "or(expr, expr)" function
15137 */
15138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015139f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015140{
15141 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15142 | get_tv_number_chk(&argvars[1], NULL);
15143}
15144
15145/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015146 * "pathshorten()" function
15147 */
15148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015149f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015150{
15151 char_u *p;
15152
15153 rettv->v_type = VAR_STRING;
15154 p = get_tv_string_chk(&argvars[0]);
15155 if (p == NULL)
15156 rettv->vval.v_string = NULL;
15157 else
15158 {
15159 p = vim_strsave(p);
15160 rettv->vval.v_string = p;
15161 if (p != NULL)
15162 shorten_dir(p);
15163 }
15164}
15165
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015166#ifdef FEAT_PERL
15167/*
15168 * "perleval()" function
15169 */
15170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015171f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015172{
15173 char_u *str;
15174 char_u buf[NUMBUFLEN];
15175
15176 str = get_tv_string_buf(&argvars[0], buf);
15177 do_perleval(str, rettv);
15178}
15179#endif
15180
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015181#ifdef FEAT_FLOAT
15182/*
15183 * "pow()" function
15184 */
15185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015186f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015187{
15188 float_T fx, fy;
15189
15190 rettv->v_type = VAR_FLOAT;
15191 if (get_float_arg(argvars, &fx) == OK
15192 && get_float_arg(&argvars[1], &fy) == OK)
15193 rettv->vval.v_float = pow(fx, fy);
15194 else
15195 rettv->vval.v_float = 0.0;
15196}
15197#endif
15198
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015199/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015200 * "prevnonblank()" function
15201 */
15202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015203f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015204{
15205 linenr_T lnum;
15206
15207 lnum = get_tv_lnum(argvars);
15208 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15209 lnum = 0;
15210 else
15211 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15212 --lnum;
15213 rettv->vval.v_number = lnum;
15214}
15215
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015216/* This dummy va_list is here because:
15217 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15218 * - locally in the function results in a "used before set" warning
15219 * - using va_start() to initialize it gives "function with fixed args" error */
15220static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015221
Bram Moolenaar8c711452005-01-14 21:53:12 +000015222/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015223 * "printf()" function
15224 */
15225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015226f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015227{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015228 char_u buf[NUMBUFLEN];
15229 int len;
15230 char_u *s;
15231 int saved_did_emsg = did_emsg;
15232 char *fmt;
15233
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015234 rettv->v_type = VAR_STRING;
15235 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015236
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015237 /* Get the required length, allocate the buffer and do it for real. */
15238 did_emsg = FALSE;
15239 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15240 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15241 if (!did_emsg)
15242 {
15243 s = alloc(len + 1);
15244 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015245 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015246 rettv->vval.v_string = s;
15247 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015248 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015249 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015250 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015251}
15252
15253/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015254 * "pumvisible()" function
15255 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015257f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015258{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015259#ifdef FEAT_INS_EXPAND
15260 if (pum_visible())
15261 rettv->vval.v_number = 1;
15262#endif
15263}
15264
Bram Moolenaardb913952012-06-29 12:54:53 +020015265#ifdef FEAT_PYTHON3
15266/*
15267 * "py3eval()" function
15268 */
15269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015270f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015271{
15272 char_u *str;
15273 char_u buf[NUMBUFLEN];
15274
15275 str = get_tv_string_buf(&argvars[0], buf);
15276 do_py3eval(str, rettv);
15277}
15278#endif
15279
15280#ifdef FEAT_PYTHON
15281/*
15282 * "pyeval()" function
15283 */
15284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015285f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015286{
15287 char_u *str;
15288 char_u buf[NUMBUFLEN];
15289
15290 str = get_tv_string_buf(&argvars[0], buf);
15291 do_pyeval(str, rettv);
15292}
15293#endif
15294
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015295/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015296 * "range()" function
15297 */
15298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015299f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015300{
15301 long start;
15302 long end;
15303 long stride = 1;
15304 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015305 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015306
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015307 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015308 if (argvars[1].v_type == VAR_UNKNOWN)
15309 {
15310 end = start - 1;
15311 start = 0;
15312 }
15313 else
15314 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015315 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015316 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015317 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015318 }
15319
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015320 if (error)
15321 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015322 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015323 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015324 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015325 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015326 else
15327 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015328 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015329 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015330 if (list_append_number(rettv->vval.v_list,
15331 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015332 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015333 }
15334}
15335
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015336/*
15337 * "readfile()" function
15338 */
15339 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015340f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015341{
15342 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015343 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015344 char_u *fname;
15345 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015346 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15347 int io_size = sizeof(buf);
15348 int readlen; /* size of last fread() */
15349 char_u *prev = NULL; /* previously read bytes, if any */
15350 long prevlen = 0; /* length of data in prev */
15351 long prevsize = 0; /* size of prev buffer */
15352 long maxline = MAXLNUM;
15353 long cnt = 0;
15354 char_u *p; /* position in buf */
15355 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015356
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015357 if (argvars[1].v_type != VAR_UNKNOWN)
15358 {
15359 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15360 binary = TRUE;
15361 if (argvars[2].v_type != VAR_UNKNOWN)
15362 maxline = get_tv_number(&argvars[2]);
15363 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015364
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015365 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015366 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015367
15368 /* Always open the file in binary mode, library functions have a mind of
15369 * their own about CR-LF conversion. */
15370 fname = get_tv_string(&argvars[0]);
15371 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15372 {
15373 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15374 return;
15375 }
15376
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015377 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015378 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015379 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015380
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015381 /* This for loop processes what was read, but is also entered at end
15382 * of file so that either:
15383 * - an incomplete line gets written
15384 * - a "binary" file gets an empty line at the end if it ends in a
15385 * newline. */
15386 for (p = buf, start = buf;
15387 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15388 ++p)
15389 {
15390 if (*p == '\n' || readlen <= 0)
15391 {
15392 listitem_T *li;
15393 char_u *s = NULL;
15394 long_u len = p - start;
15395
15396 /* Finished a line. Remove CRs before NL. */
15397 if (readlen > 0 && !binary)
15398 {
15399 while (len > 0 && start[len - 1] == '\r')
15400 --len;
15401 /* removal may cross back to the "prev" string */
15402 if (len == 0)
15403 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15404 --prevlen;
15405 }
15406 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015407 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015408 else
15409 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015410 /* Change "prev" buffer to be the right size. This way
15411 * the bytes are only copied once, and very long lines are
15412 * allocated only once. */
15413 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015414 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015415 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015416 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015417 prev = NULL; /* the list will own the string */
15418 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015419 }
15420 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015421 if (s == NULL)
15422 {
15423 do_outofmem_msg((long_u) prevlen + len + 1);
15424 failed = TRUE;
15425 break;
15426 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015427
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015428 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015429 {
15430 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015431 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015432 break;
15433 }
15434 li->li_tv.v_type = VAR_STRING;
15435 li->li_tv.v_lock = 0;
15436 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015437 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015438
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015439 start = p + 1; /* step over newline */
15440 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015441 break;
15442 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015443 else if (*p == NUL)
15444 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015445#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015446 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15447 * when finding the BF and check the previous two bytes. */
15448 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015449 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015450 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15451 * + 1, these may be in the "prev" string. */
15452 char_u back1 = p >= buf + 1 ? p[-1]
15453 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15454 char_u back2 = p >= buf + 2 ? p[-2]
15455 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15456 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015457
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015458 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015459 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015460 char_u *dest = p - 2;
15461
15462 /* Usually a BOM is at the beginning of a file, and so at
15463 * the beginning of a line; then we can just step over it.
15464 */
15465 if (start == dest)
15466 start = p + 1;
15467 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015468 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015469 /* have to shuffle buf to close gap */
15470 int adjust_prevlen = 0;
15471
15472 if (dest < buf)
15473 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015474 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015475 dest = buf;
15476 }
15477 if (readlen > p - buf + 1)
15478 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15479 readlen -= 3 - adjust_prevlen;
15480 prevlen -= adjust_prevlen;
15481 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015482 }
15483 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015484 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015485#endif
15486 } /* for */
15487
15488 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15489 break;
15490 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015491 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015492 /* There's part of a line in buf, store it in "prev". */
15493 if (p - start + prevlen >= prevsize)
15494 {
15495 /* need bigger "prev" buffer */
15496 char_u *newprev;
15497
15498 /* A common use case is ordinary text files and "prev" gets a
15499 * fragment of a line, so the first allocation is made
15500 * small, to avoid repeatedly 'allocing' large and
15501 * 'reallocing' small. */
15502 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015503 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015504 else
15505 {
15506 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015507 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015508 prevsize = grow50pc > growmin ? grow50pc : growmin;
15509 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015510 newprev = prev == NULL ? alloc(prevsize)
15511 : vim_realloc(prev, prevsize);
15512 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015513 {
15514 do_outofmem_msg((long_u)prevsize);
15515 failed = TRUE;
15516 break;
15517 }
15518 prev = newprev;
15519 }
15520 /* Add the line part to end of "prev". */
15521 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015522 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015523 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015524 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015525
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015526 /*
15527 * For a negative line count use only the lines at the end of the file,
15528 * free the rest.
15529 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015530 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015531 while (cnt > -maxline)
15532 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015533 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015534 --cnt;
15535 }
15536
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015537 if (failed)
15538 {
15539 list_free(rettv->vval.v_list, TRUE);
15540 /* readfile doc says an empty list is returned on error */
15541 rettv->vval.v_list = list_alloc();
15542 }
15543
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015544 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015545 fclose(fd);
15546}
15547
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015548#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015549static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015550
15551/*
15552 * Convert a List to proftime_T.
15553 * Return FAIL when there is something wrong.
15554 */
15555 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015556list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015557{
15558 long n1, n2;
15559 int error = FALSE;
15560
15561 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15562 || arg->vval.v_list->lv_len != 2)
15563 return FAIL;
15564 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15565 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15566# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015567 tm->HighPart = n1;
15568 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015569# else
15570 tm->tv_sec = n1;
15571 tm->tv_usec = n2;
15572# endif
15573 return error ? FAIL : OK;
15574}
15575#endif /* FEAT_RELTIME */
15576
15577/*
15578 * "reltime()" function
15579 */
15580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015581f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015582{
15583#ifdef FEAT_RELTIME
15584 proftime_T res;
15585 proftime_T start;
15586
15587 if (argvars[0].v_type == VAR_UNKNOWN)
15588 {
15589 /* No arguments: get current time. */
15590 profile_start(&res);
15591 }
15592 else if (argvars[1].v_type == VAR_UNKNOWN)
15593 {
15594 if (list2proftime(&argvars[0], &res) == FAIL)
15595 return;
15596 profile_end(&res);
15597 }
15598 else
15599 {
15600 /* Two arguments: compute the difference. */
15601 if (list2proftime(&argvars[0], &start) == FAIL
15602 || list2proftime(&argvars[1], &res) == FAIL)
15603 return;
15604 profile_sub(&res, &start);
15605 }
15606
15607 if (rettv_list_alloc(rettv) == OK)
15608 {
15609 long n1, n2;
15610
15611# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015612 n1 = res.HighPart;
15613 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015614# else
15615 n1 = res.tv_sec;
15616 n2 = res.tv_usec;
15617# endif
15618 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15619 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15620 }
15621#endif
15622}
15623
15624/*
15625 * "reltimestr()" function
15626 */
15627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015628f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015629{
15630#ifdef FEAT_RELTIME
15631 proftime_T tm;
15632#endif
15633
15634 rettv->v_type = VAR_STRING;
15635 rettv->vval.v_string = NULL;
15636#ifdef FEAT_RELTIME
15637 if (list2proftime(&argvars[0], &tm) == OK)
15638 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15639#endif
15640}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015641
Bram Moolenaar0d660222005-01-07 21:51:51 +000015642#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015643static void make_connection(void);
15644static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015645
15646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015647make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015648{
15649 if (X_DISPLAY == NULL
15650# ifdef FEAT_GUI
15651 && !gui.in_use
15652# endif
15653 )
15654 {
15655 x_force_connect = TRUE;
15656 setup_term_clip();
15657 x_force_connect = FALSE;
15658 }
15659}
15660
15661 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015662check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015663{
15664 make_connection();
15665 if (X_DISPLAY == NULL)
15666 {
15667 EMSG(_("E240: No connection to Vim server"));
15668 return FAIL;
15669 }
15670 return OK;
15671}
15672#endif
15673
15674#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015675static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015676
15677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015678remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015679{
15680 char_u *server_name;
15681 char_u *keys;
15682 char_u *r = NULL;
15683 char_u buf[NUMBUFLEN];
15684# ifdef WIN32
15685 HWND w;
15686# else
15687 Window w;
15688# endif
15689
15690 if (check_restricted() || check_secure())
15691 return;
15692
15693# ifdef FEAT_X11
15694 if (check_connection() == FAIL)
15695 return;
15696# endif
15697
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015698 server_name = get_tv_string_chk(&argvars[0]);
15699 if (server_name == NULL)
15700 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015701 keys = get_tv_string_buf(&argvars[1], buf);
15702# ifdef WIN32
15703 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15704# else
15705 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15706 < 0)
15707# endif
15708 {
15709 if (r != NULL)
15710 EMSG(r); /* sending worked but evaluation failed */
15711 else
15712 EMSG2(_("E241: Unable to send to %s"), server_name);
15713 return;
15714 }
15715
15716 rettv->vval.v_string = r;
15717
15718 if (argvars[2].v_type != VAR_UNKNOWN)
15719 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015720 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015721 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015722 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015723
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015724 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015725 v.di_tv.v_type = VAR_STRING;
15726 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015727 idvar = get_tv_string_chk(&argvars[2]);
15728 if (idvar != NULL)
15729 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015730 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015731 }
15732}
15733#endif
15734
15735/*
15736 * "remote_expr()" function
15737 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015739f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015740{
15741 rettv->v_type = VAR_STRING;
15742 rettv->vval.v_string = NULL;
15743#ifdef FEAT_CLIENTSERVER
15744 remote_common(argvars, rettv, TRUE);
15745#endif
15746}
15747
15748/*
15749 * "remote_foreground()" function
15750 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015752f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015753{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015754#ifdef FEAT_CLIENTSERVER
15755# ifdef WIN32
15756 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015757 {
15758 char_u *server_name = get_tv_string_chk(&argvars[0]);
15759
15760 if (server_name != NULL)
15761 serverForeground(server_name);
15762 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015763# else
15764 /* Send a foreground() expression to the server. */
15765 argvars[1].v_type = VAR_STRING;
15766 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15767 argvars[2].v_type = VAR_UNKNOWN;
15768 remote_common(argvars, rettv, TRUE);
15769 vim_free(argvars[1].vval.v_string);
15770# endif
15771#endif
15772}
15773
Bram Moolenaar0d660222005-01-07 21:51:51 +000015774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015775f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015776{
15777#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015778 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015779 char_u *s = NULL;
15780# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015781 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015782# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015783 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015784
15785 if (check_restricted() || check_secure())
15786 {
15787 rettv->vval.v_number = -1;
15788 return;
15789 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015790 serverid = get_tv_string_chk(&argvars[0]);
15791 if (serverid == NULL)
15792 {
15793 rettv->vval.v_number = -1;
15794 return; /* type error; errmsg already given */
15795 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015796# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015797 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015798 if (n == 0)
15799 rettv->vval.v_number = -1;
15800 else
15801 {
15802 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15803 rettv->vval.v_number = (s != NULL);
15804 }
15805# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015806 if (check_connection() == FAIL)
15807 return;
15808
15809 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015810 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015811# endif
15812
15813 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15814 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015815 char_u *retvar;
15816
Bram Moolenaar33570922005-01-25 22:26:29 +000015817 v.di_tv.v_type = VAR_STRING;
15818 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015819 retvar = get_tv_string_chk(&argvars[1]);
15820 if (retvar != NULL)
15821 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015822 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015823 }
15824#else
15825 rettv->vval.v_number = -1;
15826#endif
15827}
15828
Bram Moolenaar0d660222005-01-07 21:51:51 +000015829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015830f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015831{
15832 char_u *r = NULL;
15833
15834#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015835 char_u *serverid = get_tv_string_chk(&argvars[0]);
15836
15837 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015838 {
15839# ifdef WIN32
15840 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015841 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015842
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015843 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015844 if (n != 0)
15845 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15846 if (r == NULL)
15847# else
15848 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015849 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015850# endif
15851 EMSG(_("E277: Unable to read a server reply"));
15852 }
15853#endif
15854 rettv->v_type = VAR_STRING;
15855 rettv->vval.v_string = r;
15856}
15857
15858/*
15859 * "remote_send()" function
15860 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015862f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015863{
15864 rettv->v_type = VAR_STRING;
15865 rettv->vval.v_string = NULL;
15866#ifdef FEAT_CLIENTSERVER
15867 remote_common(argvars, rettv, FALSE);
15868#endif
15869}
15870
15871/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015872 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015873 */
15874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015875f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015876{
Bram Moolenaar33570922005-01-25 22:26:29 +000015877 list_T *l;
15878 listitem_T *item, *item2;
15879 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015880 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015881 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015882 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015883 dict_T *d;
15884 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020015885 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015886
Bram Moolenaar8c711452005-01-14 21:53:12 +000015887 if (argvars[0].v_type == VAR_DICT)
15888 {
15889 if (argvars[2].v_type != VAR_UNKNOWN)
15890 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015891 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015892 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015893 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015894 key = get_tv_string_chk(&argvars[1]);
15895 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015896 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015897 di = dict_find(d, key, -1);
15898 if (di == NULL)
15899 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020015900 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
15901 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015902 {
15903 *rettv = di->di_tv;
15904 init_tv(&di->di_tv);
15905 dictitem_remove(d, di);
15906 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015907 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015908 }
15909 }
15910 else if (argvars[0].v_type != VAR_LIST)
15911 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015912 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015913 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015914 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015915 int error = FALSE;
15916
15917 idx = get_tv_number_chk(&argvars[1], &error);
15918 if (error)
15919 ; /* type error: do nothing, errmsg already given */
15920 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015921 EMSGN(_(e_listidx), idx);
15922 else
15923 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015924 if (argvars[2].v_type == VAR_UNKNOWN)
15925 {
15926 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015927 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015928 *rettv = item->li_tv;
15929 vim_free(item);
15930 }
15931 else
15932 {
15933 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015934 end = get_tv_number_chk(&argvars[2], &error);
15935 if (error)
15936 ; /* type error: do nothing */
15937 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015938 EMSGN(_(e_listidx), end);
15939 else
15940 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015941 int cnt = 0;
15942
15943 for (li = item; li != NULL; li = li->li_next)
15944 {
15945 ++cnt;
15946 if (li == item2)
15947 break;
15948 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015949 if (li == NULL) /* didn't find "item2" after "item" */
15950 EMSG(_(e_invrange));
15951 else
15952 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015953 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015954 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015955 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015956 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015957 l->lv_first = item;
15958 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015959 item->li_prev = NULL;
15960 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015961 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015962 }
15963 }
15964 }
15965 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015966 }
15967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015968}
15969
15970/*
15971 * "rename({from}, {to})" function
15972 */
15973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015974f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015975{
15976 char_u buf[NUMBUFLEN];
15977
15978 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015979 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015980 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015981 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15982 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015983}
15984
15985/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015986 * "repeat()" function
15987 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015989f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015990{
15991 char_u *p;
15992 int n;
15993 int slen;
15994 int len;
15995 char_u *r;
15996 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015997
15998 n = get_tv_number(&argvars[1]);
15999 if (argvars[0].v_type == VAR_LIST)
16000 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016001 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016002 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016003 if (list_extend(rettv->vval.v_list,
16004 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016005 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016006 }
16007 else
16008 {
16009 p = get_tv_string(&argvars[0]);
16010 rettv->v_type = VAR_STRING;
16011 rettv->vval.v_string = NULL;
16012
16013 slen = (int)STRLEN(p);
16014 len = slen * n;
16015 if (len <= 0)
16016 return;
16017
16018 r = alloc(len + 1);
16019 if (r != NULL)
16020 {
16021 for (i = 0; i < n; i++)
16022 mch_memmove(r + i * slen, p, (size_t)slen);
16023 r[len] = NUL;
16024 }
16025
16026 rettv->vval.v_string = r;
16027 }
16028}
16029
16030/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016031 * "resolve()" function
16032 */
16033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016034f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016035{
16036 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016037#ifdef HAVE_READLINK
16038 char_u *buf = NULL;
16039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016040
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016041 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016042#ifdef FEAT_SHORTCUT
16043 {
16044 char_u *v = NULL;
16045
16046 v = mch_resolve_shortcut(p);
16047 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016048 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016049 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016050 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016051 }
16052#else
16053# ifdef HAVE_READLINK
16054 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016055 char_u *cpy;
16056 int len;
16057 char_u *remain = NULL;
16058 char_u *q;
16059 int is_relative_to_current = FALSE;
16060 int has_trailing_pathsep = FALSE;
16061 int limit = 100;
16062
16063 p = vim_strsave(p);
16064
16065 if (p[0] == '.' && (vim_ispathsep(p[1])
16066 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16067 is_relative_to_current = TRUE;
16068
16069 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016070 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016071 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016072 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016073 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016075
16076 q = getnextcomp(p);
16077 if (*q != NUL)
16078 {
16079 /* Separate the first path component in "p", and keep the
16080 * remainder (beginning with the path separator). */
16081 remain = vim_strsave(q - 1);
16082 q[-1] = NUL;
16083 }
16084
Bram Moolenaard9462e32011-04-11 21:35:11 +020016085 buf = alloc(MAXPATHL + 1);
16086 if (buf == NULL)
16087 goto fail;
16088
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089 for (;;)
16090 {
16091 for (;;)
16092 {
16093 len = readlink((char *)p, (char *)buf, MAXPATHL);
16094 if (len <= 0)
16095 break;
16096 buf[len] = NUL;
16097
16098 if (limit-- == 0)
16099 {
16100 vim_free(p);
16101 vim_free(remain);
16102 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016103 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016104 goto fail;
16105 }
16106
16107 /* Ensure that the result will have a trailing path separator
16108 * if the argument has one. */
16109 if (remain == NULL && has_trailing_pathsep)
16110 add_pathsep(buf);
16111
16112 /* Separate the first path component in the link value and
16113 * concatenate the remainders. */
16114 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16115 if (*q != NUL)
16116 {
16117 if (remain == NULL)
16118 remain = vim_strsave(q - 1);
16119 else
16120 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016121 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016122 if (cpy != NULL)
16123 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016124 vim_free(remain);
16125 remain = cpy;
16126 }
16127 }
16128 q[-1] = NUL;
16129 }
16130
16131 q = gettail(p);
16132 if (q > p && *q == NUL)
16133 {
16134 /* Ignore trailing path separator. */
16135 q[-1] = NUL;
16136 q = gettail(p);
16137 }
16138 if (q > p && !mch_isFullName(buf))
16139 {
16140 /* symlink is relative to directory of argument */
16141 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16142 if (cpy != NULL)
16143 {
16144 STRCPY(cpy, p);
16145 STRCPY(gettail(cpy), buf);
16146 vim_free(p);
16147 p = cpy;
16148 }
16149 }
16150 else
16151 {
16152 vim_free(p);
16153 p = vim_strsave(buf);
16154 }
16155 }
16156
16157 if (remain == NULL)
16158 break;
16159
16160 /* Append the first path component of "remain" to "p". */
16161 q = getnextcomp(remain + 1);
16162 len = q - remain - (*q != NUL);
16163 cpy = vim_strnsave(p, STRLEN(p) + len);
16164 if (cpy != NULL)
16165 {
16166 STRNCAT(cpy, remain, len);
16167 vim_free(p);
16168 p = cpy;
16169 }
16170 /* Shorten "remain". */
16171 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016172 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173 else
16174 {
16175 vim_free(remain);
16176 remain = NULL;
16177 }
16178 }
16179
16180 /* If the result is a relative path name, make it explicitly relative to
16181 * the current directory if and only if the argument had this form. */
16182 if (!vim_ispathsep(*p))
16183 {
16184 if (is_relative_to_current
16185 && *p != NUL
16186 && !(p[0] == '.'
16187 && (p[1] == NUL
16188 || vim_ispathsep(p[1])
16189 || (p[1] == '.'
16190 && (p[2] == NUL
16191 || vim_ispathsep(p[2]))))))
16192 {
16193 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016194 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016195 if (cpy != NULL)
16196 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016197 vim_free(p);
16198 p = cpy;
16199 }
16200 }
16201 else if (!is_relative_to_current)
16202 {
16203 /* Strip leading "./". */
16204 q = p;
16205 while (q[0] == '.' && vim_ispathsep(q[1]))
16206 q += 2;
16207 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016208 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016209 }
16210 }
16211
16212 /* Ensure that the result will have no trailing path separator
16213 * if the argument had none. But keep "/" or "//". */
16214 if (!has_trailing_pathsep)
16215 {
16216 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016217 if (after_pathsep(p, q))
16218 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016219 }
16220
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016221 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016222 }
16223# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016224 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016225# endif
16226#endif
16227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016228 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016229
16230#ifdef HAVE_READLINK
16231fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016232 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016234 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016235}
16236
16237/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016238 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016239 */
16240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016241f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016242{
Bram Moolenaar33570922005-01-25 22:26:29 +000016243 list_T *l;
16244 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016245
Bram Moolenaar0d660222005-01-07 21:51:51 +000016246 if (argvars[0].v_type != VAR_LIST)
16247 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016248 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016249 && !tv_check_lock(l->lv_lock,
16250 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016251 {
16252 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016253 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016254 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016255 while (li != NULL)
16256 {
16257 ni = li->li_prev;
16258 list_append(l, li);
16259 li = ni;
16260 }
16261 rettv->vval.v_list = l;
16262 rettv->v_type = VAR_LIST;
16263 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016264 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016266}
16267
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016268#define SP_NOMOVE 0x01 /* don't move cursor */
16269#define SP_REPEAT 0x02 /* repeat to find outer pair */
16270#define SP_RETCOUNT 0x04 /* return matchcount */
16271#define SP_SETPCMARK 0x08 /* set previous context mark */
16272#define SP_START 0x10 /* accept match at start position */
16273#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16274#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016275#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016276
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016277static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016278
16279/*
16280 * Get flags for a search function.
16281 * Possibly sets "p_ws".
16282 * Returns BACKWARD, FORWARD or zero (for an error).
16283 */
16284 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016285get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016286{
16287 int dir = FORWARD;
16288 char_u *flags;
16289 char_u nbuf[NUMBUFLEN];
16290 int mask;
16291
16292 if (varp->v_type != VAR_UNKNOWN)
16293 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016294 flags = get_tv_string_buf_chk(varp, nbuf);
16295 if (flags == NULL)
16296 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016297 while (*flags != NUL)
16298 {
16299 switch (*flags)
16300 {
16301 case 'b': dir = BACKWARD; break;
16302 case 'w': p_ws = TRUE; break;
16303 case 'W': p_ws = FALSE; break;
16304 default: mask = 0;
16305 if (flagsp != NULL)
16306 switch (*flags)
16307 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016308 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016309 case 'e': mask = SP_END; break;
16310 case 'm': mask = SP_RETCOUNT; break;
16311 case 'n': mask = SP_NOMOVE; break;
16312 case 'p': mask = SP_SUBPAT; break;
16313 case 'r': mask = SP_REPEAT; break;
16314 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016315 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016316 }
16317 if (mask == 0)
16318 {
16319 EMSG2(_(e_invarg2), flags);
16320 dir = 0;
16321 }
16322 else
16323 *flagsp |= mask;
16324 }
16325 if (dir == 0)
16326 break;
16327 ++flags;
16328 }
16329 }
16330 return dir;
16331}
16332
Bram Moolenaar071d4272004-06-13 20:20:40 +000016333/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016334 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016335 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016336 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016337search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016338{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016339 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016340 char_u *pat;
16341 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016342 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016343 int save_p_ws = p_ws;
16344 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016345 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016346 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016347 proftime_T tm;
16348#ifdef FEAT_RELTIME
16349 long time_limit = 0;
16350#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016351 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016352 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016353
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016354 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016355 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016356 if (dir == 0)
16357 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016358 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016359 if (flags & SP_START)
16360 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016361 if (flags & SP_END)
16362 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016363 if (flags & SP_COLUMN)
16364 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016365
Bram Moolenaar76929292008-01-06 19:07:36 +000016366 /* Optional arguments: line number to stop searching and timeout. */
16367 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016368 {
16369 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16370 if (lnum_stop < 0)
16371 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016372#ifdef FEAT_RELTIME
16373 if (argvars[3].v_type != VAR_UNKNOWN)
16374 {
16375 time_limit = get_tv_number_chk(&argvars[3], NULL);
16376 if (time_limit < 0)
16377 goto theend;
16378 }
16379#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016380 }
16381
Bram Moolenaar76929292008-01-06 19:07:36 +000016382#ifdef FEAT_RELTIME
16383 /* Set the time limit, if there is one. */
16384 profile_setlimit(time_limit, &tm);
16385#endif
16386
Bram Moolenaar231334e2005-07-25 20:46:57 +000016387 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016388 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016389 * Check to make sure only those flags are set.
16390 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16391 * flags cannot be set. Check for that condition also.
16392 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016393 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016394 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016395 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016396 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016397 goto theend;
16398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016399
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016400 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016401 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016402 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016403 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016404 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016405 if (flags & SP_SUBPAT)
16406 retval = subpatnum;
16407 else
16408 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016409 if (flags & SP_SETPCMARK)
16410 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016411 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016412 if (match_pos != NULL)
16413 {
16414 /* Store the match cursor position */
16415 match_pos->lnum = pos.lnum;
16416 match_pos->col = pos.col + 1;
16417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016418 /* "/$" will put the cursor after the end of the line, may need to
16419 * correct that here */
16420 check_cursor();
16421 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016422
16423 /* If 'n' flag is used: restore cursor position. */
16424 if (flags & SP_NOMOVE)
16425 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016426 else
16427 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016428theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016429 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016430
16431 return retval;
16432}
16433
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016434#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016435
16436/*
16437 * round() is not in C90, use ceil() or floor() instead.
16438 */
16439 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010016440vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016441{
16442 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16443}
16444
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016445/*
16446 * "round({float})" function
16447 */
16448 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016449f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016450{
16451 float_T f;
16452
16453 rettv->v_type = VAR_FLOAT;
16454 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016455 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016456 else
16457 rettv->vval.v_float = 0.0;
16458}
16459#endif
16460
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016461/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016462 * "screenattr()" function
16463 */
16464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016465f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016466{
16467 int row;
16468 int col;
16469 int c;
16470
16471 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16472 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16473 if (row < 0 || row >= screen_Rows
16474 || col < 0 || col >= screen_Columns)
16475 c = -1;
16476 else
16477 c = ScreenAttrs[LineOffset[row] + col];
16478 rettv->vval.v_number = c;
16479}
16480
16481/*
16482 * "screenchar()" function
16483 */
16484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016485f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016486{
16487 int row;
16488 int col;
16489 int off;
16490 int c;
16491
16492 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16493 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16494 if (row < 0 || row >= screen_Rows
16495 || col < 0 || col >= screen_Columns)
16496 c = -1;
16497 else
16498 {
16499 off = LineOffset[row] + col;
16500#ifdef FEAT_MBYTE
16501 if (enc_utf8 && ScreenLinesUC[off] != 0)
16502 c = ScreenLinesUC[off];
16503 else
16504#endif
16505 c = ScreenLines[off];
16506 }
16507 rettv->vval.v_number = c;
16508}
16509
16510/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016511 * "screencol()" function
16512 *
16513 * First column is 1 to be consistent with virtcol().
16514 */
16515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016516f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016517{
16518 rettv->vval.v_number = screen_screencol() + 1;
16519}
16520
16521/*
16522 * "screenrow()" function
16523 */
16524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016525f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016526{
16527 rettv->vval.v_number = screen_screenrow() + 1;
16528}
16529
16530/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016531 * "search()" function
16532 */
16533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016534f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016535{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016536 int flags = 0;
16537
16538 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016539}
16540
Bram Moolenaar071d4272004-06-13 20:20:40 +000016541/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016542 * "searchdecl()" function
16543 */
16544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016545f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016546{
16547 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016548 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016549 int error = FALSE;
16550 char_u *name;
16551
16552 rettv->vval.v_number = 1; /* default: FAIL */
16553
16554 name = get_tv_string_chk(&argvars[0]);
16555 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016556 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016557 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016558 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16559 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16560 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016561 if (!error && name != NULL)
16562 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016563 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016564}
16565
16566/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016567 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016568 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016569 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016570searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016571{
16572 char_u *spat, *mpat, *epat;
16573 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016574 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016575 int dir;
16576 int flags = 0;
16577 char_u nbuf1[NUMBUFLEN];
16578 char_u nbuf2[NUMBUFLEN];
16579 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016580 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016581 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016582 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016583
Bram Moolenaar071d4272004-06-13 20:20:40 +000016584 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016585 spat = get_tv_string_chk(&argvars[0]);
16586 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16587 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16588 if (spat == NULL || mpat == NULL || epat == NULL)
16589 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016590
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591 /* Handle the optional fourth argument: flags */
16592 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016593 if (dir == 0)
16594 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016595
16596 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016597 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16598 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016599 if ((flags & (SP_END | SP_SUBPAT)) != 0
16600 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016601 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016602 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016603 goto theend;
16604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016605
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016606 /* Using 'r' implies 'W', otherwise it doesn't work. */
16607 if (flags & SP_REPEAT)
16608 p_ws = FALSE;
16609
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016610 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016611 if (argvars[3].v_type == VAR_UNKNOWN
16612 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016613 skip = (char_u *)"";
16614 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016615 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016616 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016617 if (argvars[5].v_type != VAR_UNKNOWN)
16618 {
16619 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16620 if (lnum_stop < 0)
16621 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016622#ifdef FEAT_RELTIME
16623 if (argvars[6].v_type != VAR_UNKNOWN)
16624 {
16625 time_limit = get_tv_number_chk(&argvars[6], NULL);
16626 if (time_limit < 0)
16627 goto theend;
16628 }
16629#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016630 }
16631 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016632 if (skip == NULL)
16633 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016634
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016635 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016636 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016637
16638theend:
16639 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016640
16641 return retval;
16642}
16643
16644/*
16645 * "searchpair()" function
16646 */
16647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016648f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016649{
16650 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16651}
16652
16653/*
16654 * "searchpairpos()" function
16655 */
16656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016657f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016658{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016659 pos_T match_pos;
16660 int lnum = 0;
16661 int col = 0;
16662
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016663 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016664 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016665
16666 if (searchpair_cmn(argvars, &match_pos) > 0)
16667 {
16668 lnum = match_pos.lnum;
16669 col = match_pos.col;
16670 }
16671
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016672 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16673 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016674}
16675
16676/*
16677 * Search for a start/middle/end thing.
16678 * Used by searchpair(), see its documentation for the details.
16679 * Returns 0 or -1 for no match,
16680 */
16681 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010016682do_searchpair(
16683 char_u *spat, /* start pattern */
16684 char_u *mpat, /* middle pattern */
16685 char_u *epat, /* end pattern */
16686 int dir, /* BACKWARD or FORWARD */
16687 char_u *skip, /* skip expression */
16688 int flags, /* SP_SETPCMARK and other SP_ values */
16689 pos_T *match_pos,
16690 linenr_T lnum_stop, /* stop at this line if not zero */
16691 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016692{
16693 char_u *save_cpo;
16694 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16695 long retval = 0;
16696 pos_T pos;
16697 pos_T firstpos;
16698 pos_T foundpos;
16699 pos_T save_cursor;
16700 pos_T save_pos;
16701 int n;
16702 int r;
16703 int nest = 1;
16704 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016705 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016706 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016707
16708 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16709 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016710 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016711
Bram Moolenaar76929292008-01-06 19:07:36 +000016712#ifdef FEAT_RELTIME
16713 /* Set the time limit, if there is one. */
16714 profile_setlimit(time_limit, &tm);
16715#endif
16716
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016717 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16718 * start/middle/end (pat3, for the top pair). */
16719 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16720 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16721 if (pat2 == NULL || pat3 == NULL)
16722 goto theend;
16723 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16724 if (*mpat == NUL)
16725 STRCPY(pat3, pat2);
16726 else
16727 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16728 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016729 if (flags & SP_START)
16730 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016731
Bram Moolenaar071d4272004-06-13 20:20:40 +000016732 save_cursor = curwin->w_cursor;
16733 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016734 clearpos(&firstpos);
16735 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016736 pat = pat3;
16737 for (;;)
16738 {
16739 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016740 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016741 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16742 /* didn't find it or found the first match again: FAIL */
16743 break;
16744
16745 if (firstpos.lnum == 0)
16746 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016747 if (equalpos(pos, foundpos))
16748 {
16749 /* Found the same position again. Can happen with a pattern that
16750 * has "\zs" at the end and searching backwards. Advance one
16751 * character and try again. */
16752 if (dir == BACKWARD)
16753 decl(&pos);
16754 else
16755 incl(&pos);
16756 }
16757 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016758
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016759 /* clear the start flag to avoid getting stuck here */
16760 options &= ~SEARCH_START;
16761
Bram Moolenaar071d4272004-06-13 20:20:40 +000016762 /* If the skip pattern matches, ignore this match. */
16763 if (*skip != NUL)
16764 {
16765 save_pos = curwin->w_cursor;
16766 curwin->w_cursor = pos;
16767 r = eval_to_bool(skip, &err, NULL, FALSE);
16768 curwin->w_cursor = save_pos;
16769 if (err)
16770 {
16771 /* Evaluating {skip} caused an error, break here. */
16772 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016773 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016774 break;
16775 }
16776 if (r)
16777 continue;
16778 }
16779
16780 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16781 {
16782 /* Found end when searching backwards or start when searching
16783 * forward: nested pair. */
16784 ++nest;
16785 pat = pat2; /* nested, don't search for middle */
16786 }
16787 else
16788 {
16789 /* Found end when searching forward or start when searching
16790 * backward: end of (nested) pair; or found middle in outer pair. */
16791 if (--nest == 1)
16792 pat = pat3; /* outer level, search for middle */
16793 }
16794
16795 if (nest == 0)
16796 {
16797 /* Found the match: return matchcount or line number. */
16798 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016799 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016800 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016801 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016802 if (flags & SP_SETPCMARK)
16803 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016804 curwin->w_cursor = pos;
16805 if (!(flags & SP_REPEAT))
16806 break;
16807 nest = 1; /* search for next unmatched */
16808 }
16809 }
16810
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016811 if (match_pos != NULL)
16812 {
16813 /* Store the match cursor position */
16814 match_pos->lnum = curwin->w_cursor.lnum;
16815 match_pos->col = curwin->w_cursor.col + 1;
16816 }
16817
Bram Moolenaar071d4272004-06-13 20:20:40 +000016818 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016819 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016820 curwin->w_cursor = save_cursor;
16821
16822theend:
16823 vim_free(pat2);
16824 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016825 if (p_cpo == empty_option)
16826 p_cpo = save_cpo;
16827 else
16828 /* Darn, evaluating the {skip} expression changed the value. */
16829 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016830
16831 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016832}
16833
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016834/*
16835 * "searchpos()" function
16836 */
16837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016838f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016839{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016840 pos_T match_pos;
16841 int lnum = 0;
16842 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016843 int n;
16844 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016845
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016846 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016847 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016848
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016849 n = search_cmn(argvars, &match_pos, &flags);
16850 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016851 {
16852 lnum = match_pos.lnum;
16853 col = match_pos.col;
16854 }
16855
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016856 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16857 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016858 if (flags & SP_SUBPAT)
16859 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016860}
16861
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016862#ifdef FEAT_CHANNEL
16863/*
16864 * common for "sendexpr()" and "sendraw()"
16865 * Returns the channel index if the caller should read the response.
16866 * Otherwise returns -1.
16867 */
16868 static int
16869send_common(typval_T *argvars, char_u *text, char *fun)
16870{
16871 int ch_idx;
16872 char_u *callback = NULL;
16873
16874 ch_idx = get_channel_arg(&argvars[0]);
16875 if (ch_idx < 0)
16876 return -1;
16877
16878 if (argvars[2].v_type != VAR_UNKNOWN)
16879 {
16880 callback = get_callback(&argvars[2]);
16881 if (callback == NULL)
16882 return -1;
16883 }
16884 /* Set the callback or clear it. An empty callback means no callback and
16885 * not reading the response. */
16886 channel_set_req_callback(ch_idx,
16887 callback != NULL && *callback == NUL ? NULL : callback);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016888
16889 if (channel_send(ch_idx, text, fun) == OK && callback == NULL)
16890 return ch_idx;
16891 return -1;
16892}
16893
16894/*
16895 * "sendexpr()" function
16896 */
16897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016898f_sendexpr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016899{
16900 char_u *text;
16901 char_u *resp;
16902 typval_T nrtv;
16903 typval_T listtv;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010016904 typval_T typetv;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016905 int ch_idx;
16906
16907 /* return an empty string by default */
16908 rettv->v_type = VAR_STRING;
16909 rettv->vval.v_string = NULL;
16910
16911 nrtv.v_type = VAR_NUMBER;
16912 nrtv.vval.v_number = channel_get_id();
16913 if (rettv_list_alloc(&listtv) == FAIL)
16914 return;
16915 if (list_append_tv(listtv.vval.v_list, &nrtv) == FAIL
16916 || list_append_tv(listtv.vval.v_list, &argvars[1]) == FAIL)
16917 {
16918 list_unref(listtv.vval.v_list);
16919 return;
16920 }
16921
16922 text = json_encode(&listtv);
16923 list_unref(listtv.vval.v_list);
16924
16925 ch_idx = send_common(argvars, text, "sendexpr");
16926 if (ch_idx >= 0)
16927 {
16928 /* TODO: read until the whole JSON message is received */
16929 /* TODO: only use the message with the right message ID */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010016930 /* TODO: check sequence number */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016931 resp = channel_read_block(ch_idx);
16932 if (resp != NULL)
16933 {
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010016934 channel_decode_json(resp, &typetv, rettv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016935 vim_free(resp);
16936 }
16937 }
16938}
16939
16940/*
16941 * "sendraw()" function
16942 */
16943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016944f_sendraw(typval_T *argvars, typval_T *rettv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016945{
16946 char_u buf[NUMBUFLEN];
16947 char_u *text;
16948 int ch_idx;
16949
16950 /* return an empty string by default */
16951 rettv->v_type = VAR_STRING;
16952 rettv->vval.v_string = NULL;
16953
16954 text = get_tv_string_buf(&argvars[1], buf);
16955 ch_idx = send_common(argvars, text, "sendraw");
16956 if (ch_idx >= 0)
16957 rettv->vval.v_string = channel_read_block(ch_idx);
16958}
16959#endif
16960
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016961
Bram Moolenaar0d660222005-01-07 21:51:51 +000016962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016963f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016964{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016965#ifdef FEAT_CLIENTSERVER
16966 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016967 char_u *server = get_tv_string_chk(&argvars[0]);
16968 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016969
Bram Moolenaar0d660222005-01-07 21:51:51 +000016970 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016971 if (server == NULL || reply == NULL)
16972 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016973 if (check_restricted() || check_secure())
16974 return;
16975# ifdef FEAT_X11
16976 if (check_connection() == FAIL)
16977 return;
16978# endif
16979
16980 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016981 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016982 EMSG(_("E258: Unable to send to client"));
16983 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016985 rettv->vval.v_number = 0;
16986#else
16987 rettv->vval.v_number = -1;
16988#endif
16989}
16990
Bram Moolenaar0d660222005-01-07 21:51:51 +000016991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016992f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016993{
16994 char_u *r = NULL;
16995
16996#ifdef FEAT_CLIENTSERVER
16997# ifdef WIN32
16998 r = serverGetVimNames();
16999# else
17000 make_connection();
17001 if (X_DISPLAY != NULL)
17002 r = serverGetVimNames(X_DISPLAY);
17003# endif
17004#endif
17005 rettv->v_type = VAR_STRING;
17006 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017007}
17008
17009/*
17010 * "setbufvar()" function
17011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017013f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014{
17015 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017017 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017018 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017019 char_u nbuf[NUMBUFLEN];
17020
17021 if (check_restricted() || check_secure())
17022 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017023 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17024 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017025 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017026 varp = &argvars[2];
17027
17028 if (buf != NULL && varname != NULL && varp != NULL)
17029 {
17030 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017031 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017032
17033 if (*varname == '&')
17034 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017035 long numval;
17036 char_u *strval;
17037 int error = FALSE;
17038
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017040 numval = get_tv_number_chk(varp, &error);
17041 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017042 if (!error && strval != NULL)
17043 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044 }
17045 else
17046 {
17047 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17048 if (bufvarname != NULL)
17049 {
17050 STRCPY(bufvarname, "b:");
17051 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017052 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017053 vim_free(bufvarname);
17054 }
17055 }
17056
17057 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017060}
17061
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017063f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017064{
17065 dict_T *d;
17066 dictitem_T *di;
17067 char_u *csearch;
17068
17069 if (argvars[0].v_type != VAR_DICT)
17070 {
17071 EMSG(_(e_dictreq));
17072 return;
17073 }
17074
17075 if ((d = argvars[0].vval.v_dict) != NULL)
17076 {
17077 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17078 if (csearch != NULL)
17079 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017080#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017081 if (enc_utf8)
17082 {
17083 int pcc[MAX_MCO];
17084 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017085
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017086 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17087 }
17088 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017089#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017090 set_last_csearch(PTR2CHAR(csearch),
17091 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017092 }
17093
17094 di = dict_find(d, (char_u *)"forward", -1);
17095 if (di != NULL)
17096 set_csearch_direction(get_tv_number(&di->di_tv)
17097 ? FORWARD : BACKWARD);
17098
17099 di = dict_find(d, (char_u *)"until", -1);
17100 if (di != NULL)
17101 set_csearch_until(!!get_tv_number(&di->di_tv));
17102 }
17103}
17104
Bram Moolenaar071d4272004-06-13 20:20:40 +000017105/*
17106 * "setcmdpos()" function
17107 */
17108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017109f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017111 int pos = (int)get_tv_number(&argvars[0]) - 1;
17112
17113 if (pos >= 0)
17114 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115}
17116
17117/*
17118 * "setline()" function
17119 */
17120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017121f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017122{
17123 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017124 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017125 list_T *l = NULL;
17126 listitem_T *li = NULL;
17127 long added = 0;
17128 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017129
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017130 lnum = get_tv_lnum(&argvars[0]);
17131 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017132 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017133 l = argvars[1].vval.v_list;
17134 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017135 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017136 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017137 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017138
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017139 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017140 for (;;)
17141 {
17142 if (l != NULL)
17143 {
17144 /* list argument, get next string */
17145 if (li == NULL)
17146 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017147 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017148 li = li->li_next;
17149 }
17150
17151 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017152 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017153 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017154
17155 /* When coming here from Insert mode, sync undo, so that this can be
17156 * undone separately from what was previously inserted. */
17157 if (u_sync_once == 2)
17158 {
17159 u_sync_once = 1; /* notify that u_sync() was called */
17160 u_sync(TRUE);
17161 }
17162
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017163 if (lnum <= curbuf->b_ml.ml_line_count)
17164 {
17165 /* existing line, replace it */
17166 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17167 {
17168 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017169 if (lnum == curwin->w_cursor.lnum)
17170 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017171 rettv->vval.v_number = 0; /* OK */
17172 }
17173 }
17174 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17175 {
17176 /* lnum is one past the last line, append the line */
17177 ++added;
17178 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17179 rettv->vval.v_number = 0; /* OK */
17180 }
17181
17182 if (l == NULL) /* only one string argument */
17183 break;
17184 ++lnum;
17185 }
17186
17187 if (added > 0)
17188 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189}
17190
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017191static 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 +000017192
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017194 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017195 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017197set_qf_ll_list(
17198 win_T *wp UNUSED,
17199 typval_T *list_arg UNUSED,
17200 typval_T *action_arg UNUSED,
17201 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017202{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017203#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017204 char_u *act;
17205 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017206#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017207
Bram Moolenaar2641f772005-03-25 21:58:17 +000017208 rettv->vval.v_number = -1;
17209
17210#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017211 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017212 EMSG(_(e_listreq));
17213 else
17214 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017215 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017216
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017217 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017218 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017219 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017220 if (act == NULL)
17221 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017222 if (*act == 'a' || *act == 'r')
17223 action = *act;
17224 }
17225
Bram Moolenaar81484f42012-12-05 15:16:47 +010017226 if (l != NULL && set_errorlist(wp, l, action,
17227 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017228 rettv->vval.v_number = 0;
17229 }
17230#endif
17231}
17232
17233/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017234 * "setloclist()" function
17235 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017237f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017238{
17239 win_T *win;
17240
17241 rettv->vval.v_number = -1;
17242
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017243 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017244 if (win != NULL)
17245 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17246}
17247
17248/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017249 * "setmatches()" function
17250 */
17251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017252f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017253{
17254#ifdef FEAT_SEARCH_EXTRA
17255 list_T *l;
17256 listitem_T *li;
17257 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017258 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017259
17260 rettv->vval.v_number = -1;
17261 if (argvars[0].v_type != VAR_LIST)
17262 {
17263 EMSG(_(e_listreq));
17264 return;
17265 }
17266 if ((l = argvars[0].vval.v_list) != NULL)
17267 {
17268
17269 /* To some extent make sure that we are dealing with a list from
17270 * "getmatches()". */
17271 li = l->lv_first;
17272 while (li != NULL)
17273 {
17274 if (li->li_tv.v_type != VAR_DICT
17275 || (d = li->li_tv.vval.v_dict) == NULL)
17276 {
17277 EMSG(_(e_invarg));
17278 return;
17279 }
17280 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017281 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17282 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017283 && dict_find(d, (char_u *)"priority", -1) != NULL
17284 && dict_find(d, (char_u *)"id", -1) != NULL))
17285 {
17286 EMSG(_(e_invarg));
17287 return;
17288 }
17289 li = li->li_next;
17290 }
17291
17292 clear_matches(curwin);
17293 li = l->lv_first;
17294 while (li != NULL)
17295 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017296 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017297 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017298 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017299 char_u *group;
17300 int priority;
17301 int id;
17302 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017303
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017304 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017305 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17306 {
17307 if (s == NULL)
17308 {
17309 s = list_alloc();
17310 if (s == NULL)
17311 return;
17312 }
17313
17314 /* match from matchaddpos() */
17315 for (i = 1; i < 9; i++)
17316 {
17317 sprintf((char *)buf, (char *)"pos%d", i);
17318 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17319 {
17320 if (di->di_tv.v_type != VAR_LIST)
17321 return;
17322
17323 list_append_tv(s, &di->di_tv);
17324 s->lv_refcount++;
17325 }
17326 else
17327 break;
17328 }
17329 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017330
17331 group = get_dict_string(d, (char_u *)"group", FALSE);
17332 priority = (int)get_dict_number(d, (char_u *)"priority");
17333 id = (int)get_dict_number(d, (char_u *)"id");
17334 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17335 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17336 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017337 if (i == 0)
17338 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017339 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017340 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017341 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017342 }
17343 else
17344 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017345 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017346 list_unref(s);
17347 s = NULL;
17348 }
17349
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017350 li = li->li_next;
17351 }
17352 rettv->vval.v_number = 0;
17353 }
17354#endif
17355}
17356
17357/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017358 * "setpos()" function
17359 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017361f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017362{
17363 pos_T pos;
17364 int fnum;
17365 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017366 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017367
Bram Moolenaar08250432008-02-13 11:42:46 +000017368 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017369 name = get_tv_string_chk(argvars);
17370 if (name != NULL)
17371 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017372 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017373 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017374 if (--pos.col < 0)
17375 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017376 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017377 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017378 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017379 if (fnum == curbuf->b_fnum)
17380 {
17381 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017382 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017383 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017384 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017385 curwin->w_set_curswant = FALSE;
17386 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017387 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017388 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017389 }
17390 else
17391 EMSG(_(e_invarg));
17392 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017393 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17394 {
17395 /* set mark */
17396 if (setmark_pos(name[1], &pos, fnum) == OK)
17397 rettv->vval.v_number = 0;
17398 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017399 else
17400 EMSG(_(e_invarg));
17401 }
17402 }
17403}
17404
17405/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017406 * "setqflist()" function
17407 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017408 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017409f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017410{
17411 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17412}
17413
17414/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017415 * "setreg()" function
17416 */
17417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017418f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419{
17420 int regname;
17421 char_u *strregname;
17422 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017423 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424 int append;
17425 char_u yank_type;
17426 long block_len;
17427
17428 block_len = -1;
17429 yank_type = MAUTO;
17430 append = FALSE;
17431
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017432 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017433 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017434
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017435 if (strregname == NULL)
17436 return; /* type error; errmsg already given */
17437 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017438 if (regname == 0 || regname == '@')
17439 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017440
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017441 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017442 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017443 stropt = get_tv_string_chk(&argvars[2]);
17444 if (stropt == NULL)
17445 return; /* type error */
17446 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017447 switch (*stropt)
17448 {
17449 case 'a': case 'A': /* append */
17450 append = TRUE;
17451 break;
17452 case 'v': case 'c': /* character-wise selection */
17453 yank_type = MCHAR;
17454 break;
17455 case 'V': case 'l': /* line-wise selection */
17456 yank_type = MLINE;
17457 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017458 case 'b': case Ctrl_V: /* block-wise selection */
17459 yank_type = MBLOCK;
17460 if (VIM_ISDIGIT(stropt[1]))
17461 {
17462 ++stropt;
17463 block_len = getdigits(&stropt) - 1;
17464 --stropt;
17465 }
17466 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017467 }
17468 }
17469
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017470 if (argvars[1].v_type == VAR_LIST)
17471 {
17472 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017473 char_u **allocval;
17474 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017475 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017476 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017477 int len = argvars[1].vval.v_list->lv_len;
17478 listitem_T *li;
17479
Bram Moolenaar7d647822014-04-05 21:28:56 +020017480 /* First half: use for pointers to result lines; second half: use for
17481 * pointers to allocated copies. */
17482 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017483 if (lstval == NULL)
17484 return;
17485 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017486 allocval = lstval + len + 2;
17487 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017488
17489 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17490 li = li->li_next)
17491 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017492 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017493 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017494 goto free_lstval;
17495 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017496 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017497 /* Need to make a copy, next get_tv_string_buf_chk() will
17498 * overwrite the string. */
17499 strval = vim_strsave(buf);
17500 if (strval == NULL)
17501 goto free_lstval;
17502 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017503 }
17504 *curval++ = strval;
17505 }
17506 *curval++ = NULL;
17507
17508 write_reg_contents_lst(regname, lstval, -1,
17509 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017510free_lstval:
17511 while (curallocval > allocval)
17512 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017513 vim_free(lstval);
17514 }
17515 else
17516 {
17517 strval = get_tv_string_chk(&argvars[1]);
17518 if (strval == NULL)
17519 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017520 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017521 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017522 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017523 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017524}
17525
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017526/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017527 * "settabvar()" function
17528 */
17529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017530f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017531{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017532#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017533 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017534 tabpage_T *tp;
17535#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017536 char_u *varname, *tabvarname;
17537 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017538
17539 rettv->vval.v_number = 0;
17540
17541 if (check_restricted() || check_secure())
17542 return;
17543
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017544#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017545 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017546#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017547 varname = get_tv_string_chk(&argvars[1]);
17548 varp = &argvars[2];
17549
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017550 if (varname != NULL && varp != NULL
17551#ifdef FEAT_WINDOWS
17552 && tp != NULL
17553#endif
17554 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017555 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017556#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017557 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017558 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017559#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017560
17561 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17562 if (tabvarname != NULL)
17563 {
17564 STRCPY(tabvarname, "t:");
17565 STRCPY(tabvarname + 2, varname);
17566 set_var(tabvarname, varp, TRUE);
17567 vim_free(tabvarname);
17568 }
17569
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017570#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017571 /* Restore current tabpage */
17572 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017573 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017574#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017575 }
17576}
17577
17578/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017579 * "settabwinvar()" function
17580 */
17581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017582f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017583{
17584 setwinvar(argvars, rettv, 1);
17585}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017586
17587/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017588 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017591f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017592{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017593 setwinvar(argvars, rettv, 0);
17594}
17595
17596/*
17597 * "setwinvar()" and "settabwinvar()" functions
17598 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017599
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017601setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017602{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017603 win_T *win;
17604#ifdef FEAT_WINDOWS
17605 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017606 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017607 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017608#endif
17609 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017610 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017611 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017612 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017613
17614 if (check_restricted() || check_secure())
17615 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017616
17617#ifdef FEAT_WINDOWS
17618 if (off == 1)
17619 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17620 else
17621 tp = curtab;
17622#endif
17623 win = find_win_by_nr(&argvars[off], tp);
17624 varname = get_tv_string_chk(&argvars[off + 1]);
17625 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626
17627 if (win != NULL && varname != NULL && varp != NULL)
17628 {
17629#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017630 need_switch_win = !(tp == curtab && win == curwin);
17631 if (!need_switch_win
17632 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017634 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017635 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017636 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017637 long numval;
17638 char_u *strval;
17639 int error = FALSE;
17640
17641 ++varname;
17642 numval = get_tv_number_chk(varp, &error);
17643 strval = get_tv_string_buf_chk(varp, nbuf);
17644 if (!error && strval != NULL)
17645 set_option_value(varname, numval, strval, OPT_LOCAL);
17646 }
17647 else
17648 {
17649 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17650 if (winvarname != NULL)
17651 {
17652 STRCPY(winvarname, "w:");
17653 STRCPY(winvarname + 2, varname);
17654 set_var(winvarname, varp, TRUE);
17655 vim_free(winvarname);
17656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657 }
17658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017659#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017660 if (need_switch_win)
17661 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017662#endif
17663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664}
17665
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017666#ifdef FEAT_CRYPT
17667/*
17668 * "sha256({string})" function
17669 */
17670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017671f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017672{
17673 char_u *p;
17674
17675 p = get_tv_string(&argvars[0]);
17676 rettv->vval.v_string = vim_strsave(
17677 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17678 rettv->v_type = VAR_STRING;
17679}
17680#endif /* FEAT_CRYPT */
17681
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017683 * "shellescape({string})" function
17684 */
17685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017686f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017687{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017688 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017689 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017690 rettv->v_type = VAR_STRING;
17691}
17692
17693/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017694 * shiftwidth() function
17695 */
17696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017697f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017698{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017699 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017700}
17701
17702/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017703 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 */
17705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017706f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017708 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017709
Bram Moolenaar0d660222005-01-07 21:51:51 +000017710 p = get_tv_string(&argvars[0]);
17711 rettv->vval.v_string = vim_strsave(p);
17712 simplify_filename(rettv->vval.v_string); /* simplify in place */
17713 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714}
17715
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017716#ifdef FEAT_FLOAT
17717/*
17718 * "sin()" function
17719 */
17720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017721f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017722{
17723 float_T f;
17724
17725 rettv->v_type = VAR_FLOAT;
17726 if (get_float_arg(argvars, &f) == OK)
17727 rettv->vval.v_float = sin(f);
17728 else
17729 rettv->vval.v_float = 0.0;
17730}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017731
17732/*
17733 * "sinh()" function
17734 */
17735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017736f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017737{
17738 float_T f;
17739
17740 rettv->v_type = VAR_FLOAT;
17741 if (get_float_arg(argvars, &f) == OK)
17742 rettv->vval.v_float = sinh(f);
17743 else
17744 rettv->vval.v_float = 0.0;
17745}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017746#endif
17747
Bram Moolenaar0d660222005-01-07 21:51:51 +000017748static int
17749#ifdef __BORLANDC__
17750 _RTLENTRYF
17751#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017752 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017753static int
17754#ifdef __BORLANDC__
17755 _RTLENTRYF
17756#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017757 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017758
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017759/* struct used in the array that's given to qsort() */
17760typedef struct
17761{
17762 listitem_T *item;
17763 int idx;
17764} sortItem_T;
17765
Bram Moolenaar0d660222005-01-07 21:51:51 +000017766static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017767static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017768static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017769#ifdef FEAT_FLOAT
17770static int item_compare_float;
17771#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017772static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017773static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017774static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017775static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017776static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017777#define ITEM_COMPARE_FAIL 999
17778
Bram Moolenaar071d4272004-06-13 20:20:40 +000017779/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017780 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017781 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017782 static int
17783#ifdef __BORLANDC__
17784_RTLENTRYF
17785#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010017786item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017787{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017788 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017789 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017790 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017791 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017792 int res;
17793 char_u numbuf1[NUMBUFLEN];
17794 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017795
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017796 si1 = (sortItem_T *)s1;
17797 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017798 tv1 = &si1->item->li_tv;
17799 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017800
17801 if (item_compare_numbers)
17802 {
17803 long v1 = get_tv_number(tv1);
17804 long v2 = get_tv_number(tv2);
17805
17806 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17807 }
17808
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017809#ifdef FEAT_FLOAT
17810 if (item_compare_float)
17811 {
17812 float_T v1 = get_tv_float(tv1);
17813 float_T v2 = get_tv_float(tv2);
17814
17815 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17816 }
17817#endif
17818
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017819 /* tv2string() puts quotes around a string and allocates memory. Don't do
17820 * that for string variables. Use a single quote when comparing with a
17821 * non-string to do what the docs promise. */
17822 if (tv1->v_type == VAR_STRING)
17823 {
17824 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17825 p1 = (char_u *)"'";
17826 else
17827 p1 = tv1->vval.v_string;
17828 }
17829 else
17830 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17831 if (tv2->v_type == VAR_STRING)
17832 {
17833 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17834 p2 = (char_u *)"'";
17835 else
17836 p2 = tv2->vval.v_string;
17837 }
17838 else
17839 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017840 if (p1 == NULL)
17841 p1 = (char_u *)"";
17842 if (p2 == NULL)
17843 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017844 if (!item_compare_numeric)
17845 {
17846 if (item_compare_ic)
17847 res = STRICMP(p1, p2);
17848 else
17849 res = STRCMP(p1, p2);
17850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017851 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017852 {
17853 double n1, n2;
17854 n1 = strtod((char *)p1, (char **)&p1);
17855 n2 = strtod((char *)p2, (char **)&p2);
17856 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17857 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017858
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017859 /* When the result would be zero, compare the item indexes. Makes the
17860 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017861 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017862 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017863
Bram Moolenaar0d660222005-01-07 21:51:51 +000017864 vim_free(tofree1);
17865 vim_free(tofree2);
17866 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017867}
17868
17869 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017870#ifdef __BORLANDC__
17871_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010017873item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017874{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017875 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017876 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017877 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017878 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017879 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017880
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017881 /* shortcut after failure in previous call; compare all items equal */
17882 if (item_compare_func_err)
17883 return 0;
17884
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017885 si1 = (sortItem_T *)s1;
17886 si2 = (sortItem_T *)s2;
17887
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017888 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017889 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017890 copy_tv(&si1->item->li_tv, &argv[0]);
17891 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017892
17893 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017894 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017895 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17896 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017897 clear_tv(&argv[0]);
17898 clear_tv(&argv[1]);
17899
17900 if (res == FAIL)
17901 res = ITEM_COMPARE_FAIL;
17902 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017903 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17904 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017905 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017906 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017907
17908 /* When the result would be zero, compare the pointers themselves. Makes
17909 * the sort stable. */
17910 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017911 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017912
Bram Moolenaar0d660222005-01-07 21:51:51 +000017913 return res;
17914}
17915
17916/*
17917 * "sort({list})" function
17918 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017920do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017921{
Bram Moolenaar33570922005-01-25 22:26:29 +000017922 list_T *l;
17923 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017924 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017925 long len;
17926 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017927
Bram Moolenaar0d660222005-01-07 21:51:51 +000017928 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017929 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017930 else
17931 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017932 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017933 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020017934 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
17935 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017936 return;
17937 rettv->vval.v_list = l;
17938 rettv->v_type = VAR_LIST;
17939 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017940
Bram Moolenaar0d660222005-01-07 21:51:51 +000017941 len = list_len(l);
17942 if (len <= 1)
17943 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017944
Bram Moolenaar0d660222005-01-07 21:51:51 +000017945 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017946 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017947 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017948#ifdef FEAT_FLOAT
17949 item_compare_float = FALSE;
17950#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017951 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017952 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017953 if (argvars[1].v_type != VAR_UNKNOWN)
17954 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017955 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017956 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017957 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017958 else
17959 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017960 int error = FALSE;
17961
17962 i = get_tv_number_chk(&argvars[1], &error);
17963 if (error)
17964 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017965 if (i == 1)
17966 item_compare_ic = TRUE;
17967 else
17968 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017969 if (item_compare_func != NULL)
17970 {
17971 if (STRCMP(item_compare_func, "n") == 0)
17972 {
17973 item_compare_func = NULL;
17974 item_compare_numeric = TRUE;
17975 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017976 else if (STRCMP(item_compare_func, "N") == 0)
17977 {
17978 item_compare_func = NULL;
17979 item_compare_numbers = TRUE;
17980 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017981#ifdef FEAT_FLOAT
17982 else if (STRCMP(item_compare_func, "f") == 0)
17983 {
17984 item_compare_func = NULL;
17985 item_compare_float = TRUE;
17986 }
17987#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020017988 else if (STRCMP(item_compare_func, "i") == 0)
17989 {
17990 item_compare_func = NULL;
17991 item_compare_ic = TRUE;
17992 }
17993 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017994 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017995
17996 if (argvars[2].v_type != VAR_UNKNOWN)
17997 {
17998 /* optional third argument: {dict} */
17999 if (argvars[2].v_type != VAR_DICT)
18000 {
18001 EMSG(_(e_dictreq));
18002 return;
18003 }
18004 item_compare_selfdict = argvars[2].vval.v_dict;
18005 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018007
Bram Moolenaar0d660222005-01-07 21:51:51 +000018008 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018009 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018010 if (ptrs == NULL)
18011 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018012
Bram Moolenaar327aa022014-03-25 18:24:23 +010018013 i = 0;
18014 if (sort)
18015 {
18016 /* sort(): ptrs will be the list to sort */
18017 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018018 {
18019 ptrs[i].item = li;
18020 ptrs[i].idx = i;
18021 ++i;
18022 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018023
18024 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018025 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018026 /* test the compare function */
18027 if (item_compare_func != NULL
18028 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018029 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018030 EMSG(_("E702: Sort compare function failed"));
18031 else
18032 {
18033 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018034 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018035 item_compare_func == NULL ? item_compare : item_compare2);
18036
18037 if (!item_compare_func_err)
18038 {
18039 /* Clear the List and append the items in sorted order. */
18040 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18041 l->lv_len = 0;
18042 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018043 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018044 }
18045 }
18046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018047 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018048 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018049 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018050
18051 /* f_uniq(): ptrs will be a stack of items to remove */
18052 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018053 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018054 item_compare_func_ptr = item_compare_func
18055 ? item_compare2 : item_compare;
18056
18057 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18058 li = li->li_next)
18059 {
18060 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18061 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018062 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018063 if (item_compare_func_err)
18064 {
18065 EMSG(_("E882: Uniq compare function failed"));
18066 break;
18067 }
18068 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018069
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018070 if (!item_compare_func_err)
18071 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018072 while (--i >= 0)
18073 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018074 li = ptrs[i].item->li_next;
18075 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018076 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018077 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018078 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018079 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018080 list_fix_watch(l, li);
18081 listitem_free(li);
18082 l->lv_len--;
18083 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018084 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018085 }
18086
18087 vim_free(ptrs);
18088 }
18089}
18090
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018091/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018092 * "sort({list})" function
18093 */
18094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018095f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018096{
18097 do_sort_uniq(argvars, rettv, TRUE);
18098}
18099
18100/*
18101 * "uniq({list})" function
18102 */
18103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018104f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018105{
18106 do_sort_uniq(argvars, rettv, FALSE);
18107}
18108
18109/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018110 * "soundfold({word})" function
18111 */
18112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018113f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018114{
18115 char_u *s;
18116
18117 rettv->v_type = VAR_STRING;
18118 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018119#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018120 rettv->vval.v_string = eval_soundfold(s);
18121#else
18122 rettv->vval.v_string = vim_strsave(s);
18123#endif
18124}
18125
18126/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018127 * "spellbadword()" function
18128 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018130f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018131{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018132 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018133 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018134 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018135
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018136 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018137 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018138
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018139#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018140 if (argvars[0].v_type == VAR_UNKNOWN)
18141 {
18142 /* Find the start and length of the badly spelled word. */
18143 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18144 if (len != 0)
18145 word = ml_get_cursor();
18146 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018147 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018148 {
18149 char_u *str = get_tv_string_chk(&argvars[0]);
18150 int capcol = -1;
18151
18152 if (str != NULL)
18153 {
18154 /* Check the argument for spelling. */
18155 while (*str != NUL)
18156 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018157 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018158 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018159 {
18160 word = str;
18161 break;
18162 }
18163 str += len;
18164 }
18165 }
18166 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018167#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018168
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018169 list_append_string(rettv->vval.v_list, word, len);
18170 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018171 attr == HLF_SPB ? "bad" :
18172 attr == HLF_SPR ? "rare" :
18173 attr == HLF_SPL ? "local" :
18174 attr == HLF_SPC ? "caps" :
18175 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018176}
18177
18178/*
18179 * "spellsuggest()" function
18180 */
18181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018182f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018183{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018184#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018185 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018186 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018187 int maxcount;
18188 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018189 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018190 listitem_T *li;
18191 int need_capital = FALSE;
18192#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018193
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018194 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018195 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018196
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018197#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018198 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018199 {
18200 str = get_tv_string(&argvars[0]);
18201 if (argvars[1].v_type != VAR_UNKNOWN)
18202 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018203 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018204 if (maxcount <= 0)
18205 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018206 if (argvars[2].v_type != VAR_UNKNOWN)
18207 {
18208 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18209 if (typeerr)
18210 return;
18211 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018212 }
18213 else
18214 maxcount = 25;
18215
Bram Moolenaar4770d092006-01-12 23:22:24 +000018216 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018217
18218 for (i = 0; i < ga.ga_len; ++i)
18219 {
18220 str = ((char_u **)ga.ga_data)[i];
18221
18222 li = listitem_alloc();
18223 if (li == NULL)
18224 vim_free(str);
18225 else
18226 {
18227 li->li_tv.v_type = VAR_STRING;
18228 li->li_tv.v_lock = 0;
18229 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018230 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018231 }
18232 }
18233 ga_clear(&ga);
18234 }
18235#endif
18236}
18237
Bram Moolenaar0d660222005-01-07 21:51:51 +000018238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018239f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018240{
18241 char_u *str;
18242 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018243 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018244 regmatch_T regmatch;
18245 char_u patbuf[NUMBUFLEN];
18246 char_u *save_cpo;
18247 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018248 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018249 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018250 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018251
18252 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18253 save_cpo = p_cpo;
18254 p_cpo = (char_u *)"";
18255
18256 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018257 if (argvars[1].v_type != VAR_UNKNOWN)
18258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018259 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18260 if (pat == NULL)
18261 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018262 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018263 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018264 }
18265 if (pat == NULL || *pat == NUL)
18266 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018267
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018268 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018269 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018270 if (typeerr)
18271 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018272
Bram Moolenaar0d660222005-01-07 21:51:51 +000018273 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18274 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018275 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018276 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018277 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018278 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018279 if (*str == NUL)
18280 match = FALSE; /* empty item at the end */
18281 else
18282 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018283 if (match)
18284 end = regmatch.startp[0];
18285 else
18286 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018287 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18288 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018289 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018290 if (list_append_string(rettv->vval.v_list, str,
18291 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018292 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018293 }
18294 if (!match)
18295 break;
18296 /* Advance to just after the match. */
18297 if (regmatch.endp[0] > str)
18298 col = 0;
18299 else
18300 {
18301 /* Don't get stuck at the same match. */
18302#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018303 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018304#else
18305 col = 1;
18306#endif
18307 }
18308 str = regmatch.endp[0];
18309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018310
Bram Moolenaar473de612013-06-08 18:19:48 +020018311 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018313
Bram Moolenaar0d660222005-01-07 21:51:51 +000018314 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315}
18316
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018317#ifdef FEAT_FLOAT
18318/*
18319 * "sqrt()" function
18320 */
18321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018322f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018323{
18324 float_T f;
18325
18326 rettv->v_type = VAR_FLOAT;
18327 if (get_float_arg(argvars, &f) == OK)
18328 rettv->vval.v_float = sqrt(f);
18329 else
18330 rettv->vval.v_float = 0.0;
18331}
18332
18333/*
18334 * "str2float()" function
18335 */
18336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018337f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018338{
18339 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18340
18341 if (*p == '+')
18342 p = skipwhite(p + 1);
18343 (void)string2float(p, &rettv->vval.v_float);
18344 rettv->v_type = VAR_FLOAT;
18345}
18346#endif
18347
Bram Moolenaar2c932302006-03-18 21:42:09 +000018348/*
18349 * "str2nr()" function
18350 */
18351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018352f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018353{
18354 int base = 10;
18355 char_u *p;
18356 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018357 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018358
18359 if (argvars[1].v_type != VAR_UNKNOWN)
18360 {
18361 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018362 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018363 {
18364 EMSG(_(e_invarg));
18365 return;
18366 }
18367 }
18368
18369 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018370 if (*p == '+')
18371 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018372 switch (base)
18373 {
18374 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18375 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18376 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18377 default: what = 0;
18378 }
18379 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018380 rettv->vval.v_number = n;
18381}
18382
Bram Moolenaar071d4272004-06-13 20:20:40 +000018383#ifdef HAVE_STRFTIME
18384/*
18385 * "strftime({format}[, {time}])" function
18386 */
18387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018388f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018389{
18390 char_u result_buf[256];
18391 struct tm *curtime;
18392 time_t seconds;
18393 char_u *p;
18394
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018395 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018396
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018397 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018398 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018399 seconds = time(NULL);
18400 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018401 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018402 curtime = localtime(&seconds);
18403 /* MSVC returns NULL for an invalid value of seconds. */
18404 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018405 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018406 else
18407 {
18408# ifdef FEAT_MBYTE
18409 vimconv_T conv;
18410 char_u *enc;
18411
18412 conv.vc_type = CONV_NONE;
18413 enc = enc_locale();
18414 convert_setup(&conv, p_enc, enc);
18415 if (conv.vc_type != CONV_NONE)
18416 p = string_convert(&conv, p, NULL);
18417# endif
18418 if (p != NULL)
18419 (void)strftime((char *)result_buf, sizeof(result_buf),
18420 (char *)p, curtime);
18421 else
18422 result_buf[0] = NUL;
18423
18424# ifdef FEAT_MBYTE
18425 if (conv.vc_type != CONV_NONE)
18426 vim_free(p);
18427 convert_setup(&conv, enc, p_enc);
18428 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018429 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018430 else
18431# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018432 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018433
18434# ifdef FEAT_MBYTE
18435 /* Release conversion descriptors */
18436 convert_setup(&conv, NULL, NULL);
18437 vim_free(enc);
18438# endif
18439 }
18440}
18441#endif
18442
18443/*
18444 * "stridx()" function
18445 */
18446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018447f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018448{
18449 char_u buf[NUMBUFLEN];
18450 char_u *needle;
18451 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018452 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018453 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018454 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018455
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018456 needle = get_tv_string_chk(&argvars[1]);
18457 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018458 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018459 if (needle == NULL || haystack == NULL)
18460 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018461
Bram Moolenaar33570922005-01-25 22:26:29 +000018462 if (argvars[2].v_type != VAR_UNKNOWN)
18463 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018464 int error = FALSE;
18465
18466 start_idx = get_tv_number_chk(&argvars[2], &error);
18467 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018468 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018469 if (start_idx >= 0)
18470 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018471 }
18472
18473 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18474 if (pos != NULL)
18475 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018476}
18477
18478/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018479 * "string()" function
18480 */
18481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018482f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018483{
18484 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018485 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018486
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018487 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018488 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018489 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018490 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018491 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018492}
18493
18494/*
18495 * "strlen()" function
18496 */
18497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018498f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018499{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018500 rettv->vval.v_number = (varnumber_T)(STRLEN(
18501 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018502}
18503
18504/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018505 * "strchars()" function
18506 */
18507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018508f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018509{
18510 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018511 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018512#ifdef FEAT_MBYTE
18513 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018514 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018515#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018516
18517 if (argvars[1].v_type != VAR_UNKNOWN)
18518 skipcc = get_tv_number_chk(&argvars[1], NULL);
18519 if (skipcc < 0 || skipcc > 1)
18520 EMSG(_(e_invarg));
18521 else
18522 {
18523#ifdef FEAT_MBYTE
18524 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18525 while (*s != NUL)
18526 {
18527 func_mb_ptr2char_adv(&s);
18528 ++len;
18529 }
18530 rettv->vval.v_number = len;
18531#else
18532 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18533#endif
18534 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018535}
18536
18537/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018538 * "strdisplaywidth()" function
18539 */
18540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018541f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020018542{
18543 char_u *s = get_tv_string(&argvars[0]);
18544 int col = 0;
18545
18546 if (argvars[1].v_type != VAR_UNKNOWN)
18547 col = get_tv_number(&argvars[1]);
18548
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018549 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018550}
18551
18552/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018553 * "strwidth()" function
18554 */
18555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018556f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018557{
18558 char_u *s = get_tv_string(&argvars[0]);
18559
18560 rettv->vval.v_number = (varnumber_T)(
18561#ifdef FEAT_MBYTE
18562 mb_string2cells(s, -1)
18563#else
18564 STRLEN(s)
18565#endif
18566 );
18567}
18568
18569/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570 * "strpart()" function
18571 */
18572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018573f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018574{
18575 char_u *p;
18576 int n;
18577 int len;
18578 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018579 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018581 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582 slen = (int)STRLEN(p);
18583
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018584 n = get_tv_number_chk(&argvars[1], &error);
18585 if (error)
18586 len = 0;
18587 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018588 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018589 else
18590 len = slen - n; /* default len: all bytes that are available. */
18591
18592 /*
18593 * Only return the overlap between the specified part and the actual
18594 * string.
18595 */
18596 if (n < 0)
18597 {
18598 len += n;
18599 n = 0;
18600 }
18601 else if (n > slen)
18602 n = slen;
18603 if (len < 0)
18604 len = 0;
18605 else if (n + len > slen)
18606 len = slen - n;
18607
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018608 rettv->v_type = VAR_STRING;
18609 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018610}
18611
18612/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018613 * "strridx()" function
18614 */
18615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018616f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018617{
18618 char_u buf[NUMBUFLEN];
18619 char_u *needle;
18620 char_u *haystack;
18621 char_u *rest;
18622 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018623 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018624
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018625 needle = get_tv_string_chk(&argvars[1]);
18626 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018627
18628 rettv->vval.v_number = -1;
18629 if (needle == NULL || haystack == NULL)
18630 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018631
18632 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018633 if (argvars[2].v_type != VAR_UNKNOWN)
18634 {
18635 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018636 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018637 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018638 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018639 }
18640 else
18641 end_idx = haystack_len;
18642
Bram Moolenaar0d660222005-01-07 21:51:51 +000018643 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018644 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018645 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018646 lastmatch = haystack + end_idx;
18647 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018648 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018649 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018650 for (rest = haystack; *rest != '\0'; ++rest)
18651 {
18652 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018653 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018654 break;
18655 lastmatch = rest;
18656 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018657 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018658
18659 if (lastmatch == NULL)
18660 rettv->vval.v_number = -1;
18661 else
18662 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18663}
18664
18665/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018666 * "strtrans()" function
18667 */
18668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018669f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018671 rettv->v_type = VAR_STRING;
18672 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018673}
18674
18675/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018676 * "submatch()" function
18677 */
18678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018679f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018680{
Bram Moolenaar41571762014-04-02 19:00:58 +020018681 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018682 int no;
18683 int retList = 0;
18684
18685 no = (int)get_tv_number_chk(&argvars[0], &error);
18686 if (error)
18687 return;
18688 error = FALSE;
18689 if (argvars[1].v_type != VAR_UNKNOWN)
18690 retList = get_tv_number_chk(&argvars[1], &error);
18691 if (error)
18692 return;
18693
18694 if (retList == 0)
18695 {
18696 rettv->v_type = VAR_STRING;
18697 rettv->vval.v_string = reg_submatch(no);
18698 }
18699 else
18700 {
18701 rettv->v_type = VAR_LIST;
18702 rettv->vval.v_list = reg_submatch_list(no);
18703 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018704}
18705
18706/*
18707 * "substitute()" function
18708 */
18709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018710f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018711{
18712 char_u patbuf[NUMBUFLEN];
18713 char_u subbuf[NUMBUFLEN];
18714 char_u flagsbuf[NUMBUFLEN];
18715
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018716 char_u *str = get_tv_string_chk(&argvars[0]);
18717 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18718 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18719 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18720
Bram Moolenaar0d660222005-01-07 21:51:51 +000018721 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018722 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18723 rettv->vval.v_string = NULL;
18724 else
18725 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018726}
18727
18728/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018729 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018730 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018732f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018733{
18734 int id = 0;
18735#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018736 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737 long col;
18738 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018739 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018740
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018741 lnum = get_tv_lnum(argvars); /* -1 on type error */
18742 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18743 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018745 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018746 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018747 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018748#endif
18749
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018750 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018751}
18752
18753/*
18754 * "synIDattr(id, what [, mode])" function
18755 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018757f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758{
18759 char_u *p = NULL;
18760#ifdef FEAT_SYN_HL
18761 int id;
18762 char_u *what;
18763 char_u *mode;
18764 char_u modebuf[NUMBUFLEN];
18765 int modec;
18766
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018767 id = get_tv_number(&argvars[0]);
18768 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018769 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018770 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018771 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018772 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018773 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018774 modec = 0; /* replace invalid with current */
18775 }
18776 else
18777 {
18778#ifdef FEAT_GUI
18779 if (gui.in_use)
18780 modec = 'g';
18781 else
18782#endif
18783 if (t_colors > 1)
18784 modec = 'c';
18785 else
18786 modec = 't';
18787 }
18788
18789
18790 switch (TOLOWER_ASC(what[0]))
18791 {
18792 case 'b':
18793 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18794 p = highlight_color(id, what, modec);
18795 else /* bold */
18796 p = highlight_has_attr(id, HL_BOLD, modec);
18797 break;
18798
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018799 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018800 p = highlight_color(id, what, modec);
18801 break;
18802
18803 case 'i':
18804 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18805 p = highlight_has_attr(id, HL_INVERSE, modec);
18806 else /* italic */
18807 p = highlight_has_attr(id, HL_ITALIC, modec);
18808 break;
18809
18810 case 'n': /* name */
18811 p = get_highlight_name(NULL, id - 1);
18812 break;
18813
18814 case 'r': /* reverse */
18815 p = highlight_has_attr(id, HL_INVERSE, modec);
18816 break;
18817
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018818 case 's':
18819 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18820 p = highlight_color(id, what, modec);
18821 else /* standout */
18822 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823 break;
18824
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018825 case 'u':
18826 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18827 /* underline */
18828 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18829 else
18830 /* undercurl */
18831 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018832 break;
18833 }
18834
18835 if (p != NULL)
18836 p = vim_strsave(p);
18837#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018838 rettv->v_type = VAR_STRING;
18839 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018840}
18841
18842/*
18843 * "synIDtrans(id)" function
18844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018846f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018847{
18848 int id;
18849
18850#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018851 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018852
18853 if (id > 0)
18854 id = syn_get_final_id(id);
18855 else
18856#endif
18857 id = 0;
18858
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018859 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018860}
18861
18862/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018863 * "synconcealed(lnum, col)" function
18864 */
18865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018866f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018867{
18868#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18869 long lnum;
18870 long col;
18871 int syntax_flags = 0;
18872 int cchar;
18873 int matchid = 0;
18874 char_u str[NUMBUFLEN];
18875#endif
18876
18877 rettv->v_type = VAR_LIST;
18878 rettv->vval.v_list = NULL;
18879
18880#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18881 lnum = get_tv_lnum(argvars); /* -1 on type error */
18882 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18883
18884 vim_memset(str, NUL, sizeof(str));
18885
18886 if (rettv_list_alloc(rettv) != FAIL)
18887 {
18888 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18889 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18890 && curwin->w_p_cole > 0)
18891 {
18892 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18893 syntax_flags = get_syntax_info(&matchid);
18894
18895 /* get the conceal character */
18896 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18897 {
18898 cchar = syn_get_sub_char();
18899 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18900 cchar = lcs_conceal;
18901 if (cchar != NUL)
18902 {
18903# ifdef FEAT_MBYTE
18904 if (has_mbyte)
18905 (*mb_char2bytes)(cchar, str);
18906 else
18907# endif
18908 str[0] = cchar;
18909 }
18910 }
18911 }
18912
18913 list_append_number(rettv->vval.v_list,
18914 (syntax_flags & HL_CONCEAL) != 0);
18915 /* -1 to auto-determine strlen */
18916 list_append_string(rettv->vval.v_list, str, -1);
18917 list_append_number(rettv->vval.v_list, matchid);
18918 }
18919#endif
18920}
18921
18922/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018923 * "synstack(lnum, col)" function
18924 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018926f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018927{
18928#ifdef FEAT_SYN_HL
18929 long lnum;
18930 long col;
18931 int i;
18932 int id;
18933#endif
18934
18935 rettv->v_type = VAR_LIST;
18936 rettv->vval.v_list = NULL;
18937
18938#ifdef FEAT_SYN_HL
18939 lnum = get_tv_lnum(argvars); /* -1 on type error */
18940 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18941
18942 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018943 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018944 && rettv_list_alloc(rettv) != FAIL)
18945 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018946 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018947 for (i = 0; ; ++i)
18948 {
18949 id = syn_get_stack_item(i);
18950 if (id < 0)
18951 break;
18952 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18953 break;
18954 }
18955 }
18956#endif
18957}
18958
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018960get_cmd_output_as_rettv(
18961 typval_T *argvars,
18962 typval_T *rettv,
18963 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018964{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018965 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018966 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018967 char_u *infile = NULL;
18968 char_u buf[NUMBUFLEN];
18969 int err = FALSE;
18970 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018971 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020018972 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018973
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018974 rettv->v_type = VAR_STRING;
18975 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018976 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018977 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018978
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018979 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018980 {
18981 /*
18982 * Write the string to a temp file, to be used for input of the shell
18983 * command.
18984 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020018985 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018986 {
18987 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018988 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018989 }
18990
18991 fd = mch_fopen((char *)infile, WRITEBIN);
18992 if (fd == NULL)
18993 {
18994 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018995 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018996 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018997 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018998 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018999 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19000 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019001 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019002 else
19003 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019004 size_t len;
19005
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019006 p = get_tv_string_buf_chk(&argvars[1], buf);
19007 if (p == NULL)
19008 {
19009 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019010 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019011 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019012 len = STRLEN(p);
19013 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019014 err = TRUE;
19015 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019016 if (fclose(fd) != 0)
19017 err = TRUE;
19018 if (err)
19019 {
19020 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019021 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019022 }
19023 }
19024
Bram Moolenaar52a72462014-08-29 15:53:52 +020019025 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19026 * echoes typeahead, that messes up the display. */
19027 if (!msg_silent)
19028 flags += SHELL_COOKED;
19029
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019030 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019032 int len;
19033 listitem_T *li;
19034 char_u *s = NULL;
19035 char_u *start;
19036 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019037 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019038
Bram Moolenaar52a72462014-08-29 15:53:52 +020019039 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019040 if (res == NULL)
19041 goto errret;
19042
19043 list = list_alloc();
19044 if (list == NULL)
19045 goto errret;
19046
19047 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019049 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019050 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019051 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019052 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019053
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019054 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019055 if (s == NULL)
19056 goto errret;
19057
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019058 for (p = s; start < end; ++p, ++start)
19059 *p = *start == NUL ? NL : *start;
19060 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019061
19062 li = listitem_alloc();
19063 if (li == NULL)
19064 {
19065 vim_free(s);
19066 goto errret;
19067 }
19068 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019069 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019070 li->li_tv.vval.v_string = s;
19071 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019072 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019073
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019074 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019075 rettv->v_type = VAR_LIST;
19076 rettv->vval.v_list = list;
19077 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019078 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019079 else
19080 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019081 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019082#ifdef USE_CR
19083 /* translate <CR> into <NL> */
19084 if (res != NULL)
19085 {
19086 char_u *s;
19087
19088 for (s = res; *s; ++s)
19089 {
19090 if (*s == CAR)
19091 *s = NL;
19092 }
19093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019094#else
19095# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019096 /* translate <CR><NL> into <NL> */
19097 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019099 char_u *s, *d;
19100
19101 d = res;
19102 for (s = res; *s; ++s)
19103 {
19104 if (s[0] == CAR && s[1] == NL)
19105 ++s;
19106 *d++ = *s;
19107 }
19108 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019110# endif
19111#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019112 rettv->vval.v_string = res;
19113 res = NULL;
19114 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019115
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019116errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019117 if (infile != NULL)
19118 {
19119 mch_remove(infile);
19120 vim_free(infile);
19121 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019122 if (res != NULL)
19123 vim_free(res);
19124 if (list != NULL)
19125 list_free(list, TRUE);
19126}
19127
19128/*
19129 * "system()" function
19130 */
19131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019132f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019133{
19134 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19135}
19136
19137/*
19138 * "systemlist()" function
19139 */
19140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019141f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019142{
19143 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019144}
19145
19146/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019147 * "tabpagebuflist()" function
19148 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019150f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019151{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019152#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019153 tabpage_T *tp;
19154 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019155
19156 if (argvars[0].v_type == VAR_UNKNOWN)
19157 wp = firstwin;
19158 else
19159 {
19160 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19161 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019162 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019163 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019164 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019165 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019166 for (; wp != NULL; wp = wp->w_next)
19167 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019168 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019169 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019170 }
19171#endif
19172}
19173
19174
19175/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019176 * "tabpagenr()" function
19177 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019179f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019180{
19181 int nr = 1;
19182#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019183 char_u *arg;
19184
19185 if (argvars[0].v_type != VAR_UNKNOWN)
19186 {
19187 arg = get_tv_string_chk(&argvars[0]);
19188 nr = 0;
19189 if (arg != NULL)
19190 {
19191 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019192 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019193 else
19194 EMSG2(_(e_invexpr2), arg);
19195 }
19196 }
19197 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019198 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019199#endif
19200 rettv->vval.v_number = nr;
19201}
19202
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019203
19204#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019205static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019206
19207/*
19208 * Common code for tabpagewinnr() and winnr().
19209 */
19210 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019211get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019212{
19213 win_T *twin;
19214 int nr = 1;
19215 win_T *wp;
19216 char_u *arg;
19217
19218 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19219 if (argvar->v_type != VAR_UNKNOWN)
19220 {
19221 arg = get_tv_string_chk(argvar);
19222 if (arg == NULL)
19223 nr = 0; /* type error; errmsg already given */
19224 else if (STRCMP(arg, "$") == 0)
19225 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19226 else if (STRCMP(arg, "#") == 0)
19227 {
19228 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19229 if (twin == NULL)
19230 nr = 0;
19231 }
19232 else
19233 {
19234 EMSG2(_(e_invexpr2), arg);
19235 nr = 0;
19236 }
19237 }
19238
19239 if (nr > 0)
19240 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19241 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019242 {
19243 if (wp == NULL)
19244 {
19245 /* didn't find it in this tabpage */
19246 nr = 0;
19247 break;
19248 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019249 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019250 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019251 return nr;
19252}
19253#endif
19254
19255/*
19256 * "tabpagewinnr()" function
19257 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019259f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019260{
19261 int nr = 1;
19262#ifdef FEAT_WINDOWS
19263 tabpage_T *tp;
19264
19265 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19266 if (tp == NULL)
19267 nr = 0;
19268 else
19269 nr = get_winnr(tp, &argvars[1]);
19270#endif
19271 rettv->vval.v_number = nr;
19272}
19273
19274
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019275/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019276 * "tagfiles()" function
19277 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019279f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019280{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019281 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019282 tagname_T tn;
19283 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019284
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019285 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019286 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019287 fname = alloc(MAXPATHL);
19288 if (fname == NULL)
19289 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019290
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019291 for (first = TRUE; ; first = FALSE)
19292 if (get_tagfname(&tn, first, fname) == FAIL
19293 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019294 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019295 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019296 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019297}
19298
19299/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019300 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019301 */
19302 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019303f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019304{
19305 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019306
19307 tag_pattern = get_tv_string(&argvars[0]);
19308
19309 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019310 if (*tag_pattern == NUL)
19311 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019312
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019313 if (rettv_list_alloc(rettv) == OK)
19314 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019315}
19316
19317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019318 * "tempname()" function
19319 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019321f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019322{
19323 static int x = 'A';
19324
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019325 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019326 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019327
19328 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19329 * names. Skip 'I' and 'O', they are used for shell redirection. */
19330 do
19331 {
19332 if (x == 'Z')
19333 x = '0';
19334 else if (x == '9')
19335 x = 'A';
19336 else
19337 {
19338#ifdef EBCDIC
19339 if (x == 'I')
19340 x = 'J';
19341 else if (x == 'R')
19342 x = 'S';
19343 else
19344#endif
19345 ++x;
19346 }
19347 } while (x == 'I' || x == 'O');
19348}
19349
19350/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019351 * "test(list)" function: Just checking the walls...
19352 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019354f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019355{
19356 /* Used for unit testing. Change the code below to your liking. */
19357#if 0
19358 listitem_T *li;
19359 list_T *l;
19360 char_u *bad, *good;
19361
19362 if (argvars[0].v_type != VAR_LIST)
19363 return;
19364 l = argvars[0].vval.v_list;
19365 if (l == NULL)
19366 return;
19367 li = l->lv_first;
19368 if (li == NULL)
19369 return;
19370 bad = get_tv_string(&li->li_tv);
19371 li = li->li_next;
19372 if (li == NULL)
19373 return;
19374 good = get_tv_string(&li->li_tv);
19375 rettv->vval.v_number = test_edit_score(bad, good);
19376#endif
19377}
19378
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019379#ifdef FEAT_FLOAT
19380/*
19381 * "tan()" function
19382 */
19383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019384f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019385{
19386 float_T f;
19387
19388 rettv->v_type = VAR_FLOAT;
19389 if (get_float_arg(argvars, &f) == OK)
19390 rettv->vval.v_float = tan(f);
19391 else
19392 rettv->vval.v_float = 0.0;
19393}
19394
19395/*
19396 * "tanh()" function
19397 */
19398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019399f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019400{
19401 float_T f;
19402
19403 rettv->v_type = VAR_FLOAT;
19404 if (get_float_arg(argvars, &f) == OK)
19405 rettv->vval.v_float = tanh(f);
19406 else
19407 rettv->vval.v_float = 0.0;
19408}
19409#endif
19410
Bram Moolenaard52d9742005-08-21 22:20:28 +000019411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019412 * "tolower(string)" function
19413 */
19414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019415f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019416{
19417 char_u *p;
19418
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019419 p = vim_strsave(get_tv_string(&argvars[0]));
19420 rettv->v_type = VAR_STRING;
19421 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019422
19423 if (p != NULL)
19424 while (*p != NUL)
19425 {
19426#ifdef FEAT_MBYTE
19427 int l;
19428
19429 if (enc_utf8)
19430 {
19431 int c, lc;
19432
19433 c = utf_ptr2char(p);
19434 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019435 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019436 /* TODO: reallocate string when byte count changes. */
19437 if (utf_char2len(lc) == l)
19438 utf_char2bytes(lc, p);
19439 p += l;
19440 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019441 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019442 p += l; /* skip multi-byte character */
19443 else
19444#endif
19445 {
19446 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19447 ++p;
19448 }
19449 }
19450}
19451
19452/*
19453 * "toupper(string)" function
19454 */
19455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019456f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019458 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019459 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019460}
19461
19462/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019463 * "tr(string, fromstr, tostr)" function
19464 */
19465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019466f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019467{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019468 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019469 char_u *fromstr;
19470 char_u *tostr;
19471 char_u *p;
19472#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019473 int inlen;
19474 int fromlen;
19475 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019476 int idx;
19477 char_u *cpstr;
19478 int cplen;
19479 int first = TRUE;
19480#endif
19481 char_u buf[NUMBUFLEN];
19482 char_u buf2[NUMBUFLEN];
19483 garray_T ga;
19484
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019485 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019486 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19487 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019488
19489 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019490 rettv->v_type = VAR_STRING;
19491 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019492 if (fromstr == NULL || tostr == NULL)
19493 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019494 ga_init2(&ga, (int)sizeof(char), 80);
19495
19496#ifdef FEAT_MBYTE
19497 if (!has_mbyte)
19498#endif
19499 /* not multi-byte: fromstr and tostr must be the same length */
19500 if (STRLEN(fromstr) != STRLEN(tostr))
19501 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019502#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019503error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019504#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019505 EMSG2(_(e_invarg2), fromstr);
19506 ga_clear(&ga);
19507 return;
19508 }
19509
19510 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019511 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019512 {
19513#ifdef FEAT_MBYTE
19514 if (has_mbyte)
19515 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019516 inlen = (*mb_ptr2len)(in_str);
19517 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019518 cplen = inlen;
19519 idx = 0;
19520 for (p = fromstr; *p != NUL; p += fromlen)
19521 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019522 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019523 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019524 {
19525 for (p = tostr; *p != NUL; p += tolen)
19526 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019527 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019528 if (idx-- == 0)
19529 {
19530 cplen = tolen;
19531 cpstr = p;
19532 break;
19533 }
19534 }
19535 if (*p == NUL) /* tostr is shorter than fromstr */
19536 goto error;
19537 break;
19538 }
19539 ++idx;
19540 }
19541
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019542 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019543 {
19544 /* Check that fromstr and tostr have the same number of
19545 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019546 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019547 first = FALSE;
19548 for (p = tostr; *p != NUL; p += tolen)
19549 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019550 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019551 --idx;
19552 }
19553 if (idx != 0)
19554 goto error;
19555 }
19556
Bram Moolenaarcde88542015-08-11 19:14:00 +020019557 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019558 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019559 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019560
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019561 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019562 }
19563 else
19564#endif
19565 {
19566 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019567 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019568 if (p != NULL)
19569 ga_append(&ga, tostr[p - fromstr]);
19570 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019571 ga_append(&ga, *in_str);
19572 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019573 }
19574 }
19575
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019576 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019577 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019578 ga_append(&ga, NUL);
19579
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019580 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019581}
19582
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019583#ifdef FEAT_FLOAT
19584/*
19585 * "trunc({float})" function
19586 */
19587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019588f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019589{
19590 float_T f;
19591
19592 rettv->v_type = VAR_FLOAT;
19593 if (get_float_arg(argvars, &f) == OK)
19594 /* trunc() is not in C90, use floor() or ceil() instead. */
19595 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19596 else
19597 rettv->vval.v_float = 0.0;
19598}
19599#endif
19600
Bram Moolenaar8299df92004-07-10 09:47:34 +000019601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019602 * "type(expr)" function
19603 */
19604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019605f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019606{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019607 int n;
19608
19609 switch (argvars[0].v_type)
19610 {
19611 case VAR_NUMBER: n = 0; break;
19612 case VAR_STRING: n = 1; break;
19613 case VAR_FUNC: n = 2; break;
19614 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019615 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019616#ifdef FEAT_FLOAT
19617 case VAR_FLOAT: n = 5; break;
19618#endif
Bram Moolenaarf95534c2016-01-23 21:59:52 +010019619 case VAR_SPECIAL:
19620 if (argvars[0].vval.v_number == VVAL_FALSE
19621 || argvars[0].vval.v_number == VVAL_TRUE)
19622 n = 6;
19623 else
19624 n = 7;
19625 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019626 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19627 }
19628 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629}
19630
19631/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019632 * "undofile(name)" function
19633 */
19634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019635f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019636{
19637 rettv->v_type = VAR_STRING;
19638#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019639 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019640 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019641
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019642 if (*fname == NUL)
19643 {
19644 /* If there is no file name there will be no undo file. */
19645 rettv->vval.v_string = NULL;
19646 }
19647 else
19648 {
19649 char_u *ffname = FullName_save(fname, FALSE);
19650
19651 if (ffname != NULL)
19652 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19653 vim_free(ffname);
19654 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019655 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019656#else
19657 rettv->vval.v_string = NULL;
19658#endif
19659}
19660
19661/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019662 * "undotree()" function
19663 */
19664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019665f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020019666{
19667 if (rettv_dict_alloc(rettv) == OK)
19668 {
19669 dict_T *dict = rettv->vval.v_dict;
19670 list_T *list;
19671
Bram Moolenaar730cde92010-06-27 05:18:54 +020019672 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019673 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019674 dict_add_nr_str(dict, "save_last",
19675 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019676 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19677 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019678 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019679
19680 list = list_alloc();
19681 if (list != NULL)
19682 {
19683 u_eval_tree(curbuf->b_u_oldhead, list);
19684 dict_add_list(dict, "entries", list);
19685 }
19686 }
19687}
19688
19689/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019690 * "values(dict)" function
19691 */
19692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019693f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000019694{
19695 dict_list(argvars, rettv, 1);
19696}
19697
19698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019699 * "virtcol(string)" function
19700 */
19701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019702f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019703{
19704 colnr_T vcol = 0;
19705 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019706 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019707
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019708 fp = var2fpos(&argvars[0], FALSE, &fnum);
19709 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19710 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019711 {
19712 getvvcol(curwin, fp, NULL, NULL, &vcol);
19713 ++vcol;
19714 }
19715
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019716 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019717}
19718
19719/*
19720 * "visualmode()" function
19721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019723f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019724{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019725 char_u str[2];
19726
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019727 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019728 str[0] = curbuf->b_visual_mode_eval;
19729 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019730 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731
19732 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019733 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019734 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019735}
19736
19737/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019738 * "wildmenumode()" function
19739 */
19740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019741f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019742{
19743#ifdef FEAT_WILDMENU
19744 if (wild_menu_showing)
19745 rettv->vval.v_number = 1;
19746#endif
19747}
19748
19749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019750 * "winbufnr(nr)" function
19751 */
19752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019753f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019754{
19755 win_T *wp;
19756
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019757 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019759 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019760 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019761 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019762}
19763
19764/*
19765 * "wincol()" function
19766 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019768f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769{
19770 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019771 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019772}
19773
19774/*
19775 * "winheight(nr)" function
19776 */
19777 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019778f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779{
19780 win_T *wp;
19781
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019782 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019784 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019785 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019786 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019787}
19788
19789/*
19790 * "winline()" function
19791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019793f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019794{
19795 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019796 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019797}
19798
19799/*
19800 * "winnr()" function
19801 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019803f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804{
19805 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019806
Bram Moolenaar071d4272004-06-13 20:20:40 +000019807#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019808 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019810 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019811}
19812
19813/*
19814 * "winrestcmd()" function
19815 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019817f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818{
19819#ifdef FEAT_WINDOWS
19820 win_T *wp;
19821 int winnr = 1;
19822 garray_T ga;
19823 char_u buf[50];
19824
19825 ga_init2(&ga, (int)sizeof(char), 70);
19826 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19827 {
19828 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19829 ga_concat(&ga, buf);
19830# ifdef FEAT_VERTSPLIT
19831 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19832 ga_concat(&ga, buf);
19833# endif
19834 ++winnr;
19835 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019836 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019837
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019838 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019839#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019840 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019841#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019842 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019843}
19844
19845/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019846 * "winrestview()" function
19847 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019849f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019850{
19851 dict_T *dict;
19852
19853 if (argvars[0].v_type != VAR_DICT
19854 || (dict = argvars[0].vval.v_dict) == NULL)
19855 EMSG(_(e_invarg));
19856 else
19857 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019858 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19859 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19860 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19861 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019862#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019863 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19864 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019865#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019866 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19867 {
19868 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19869 curwin->w_set_curswant = FALSE;
19870 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019871
Bram Moolenaar82c25852014-05-28 16:47:16 +020019872 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19873 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019874#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019875 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19876 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019877#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019878 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19879 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19880 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19881 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019882
19883 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019884 win_new_height(curwin, curwin->w_height);
19885# ifdef FEAT_VERTSPLIT
19886 win_new_width(curwin, W_WIDTH(curwin));
19887# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019888 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019889
Bram Moolenaarb851a962014-10-31 15:45:52 +010019890 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019891 curwin->w_topline = 1;
19892 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19893 curwin->w_topline = curbuf->b_ml.ml_line_count;
19894#ifdef FEAT_DIFF
19895 check_topfill(curwin, TRUE);
19896#endif
19897 }
19898}
19899
19900/*
19901 * "winsaveview()" function
19902 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019904f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019905{
19906 dict_T *dict;
19907
Bram Moolenaara800b422010-06-27 01:15:55 +020019908 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019909 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019910 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019911
19912 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19913 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19914#ifdef FEAT_VIRTUALEDIT
19915 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19916#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019917 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019918 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19919
19920 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19921#ifdef FEAT_DIFF
19922 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19923#endif
19924 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19925 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19926}
19927
19928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019929 * "winwidth(nr)" function
19930 */
19931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019932f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019933{
19934 win_T *wp;
19935
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019936 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019938 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019939 else
19940#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019941 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019942#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019943 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019944#endif
19945}
19946
Bram Moolenaar071d4272004-06-13 20:20:40 +000019947/*
Bram Moolenaared767a22016-01-03 22:49:16 +010019948 * "wordcount()" function
19949 */
19950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019951f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010019952{
19953 if (rettv_dict_alloc(rettv) == FAIL)
19954 return;
19955 cursor_pos_info(rettv->vval.v_dict);
19956}
19957
19958/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019959 * Write list of strings to file
19960 */
19961 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019962write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019963{
19964 listitem_T *li;
19965 int c;
19966 int ret = OK;
19967 char_u *s;
19968
19969 for (li = list->lv_first; li != NULL; li = li->li_next)
19970 {
19971 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19972 {
19973 if (*s == '\n')
19974 c = putc(NUL, fd);
19975 else
19976 c = putc(*s, fd);
19977 if (c == EOF)
19978 {
19979 ret = FAIL;
19980 break;
19981 }
19982 }
19983 if (!binary || li->li_next != NULL)
19984 if (putc('\n', fd) == EOF)
19985 {
19986 ret = FAIL;
19987 break;
19988 }
19989 if (ret == FAIL)
19990 {
19991 EMSG(_(e_write));
19992 break;
19993 }
19994 }
19995 return ret;
19996}
19997
19998/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019999 * "writefile()" function
20000 */
20001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020002f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020003{
20004 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020005 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020006 char_u *fname;
20007 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020008 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020009
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020010 if (check_restricted() || check_secure())
20011 return;
20012
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020013 if (argvars[0].v_type != VAR_LIST)
20014 {
20015 EMSG2(_(e_listarg), "writefile()");
20016 return;
20017 }
20018 if (argvars[0].vval.v_list == NULL)
20019 return;
20020
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020021 if (argvars[2].v_type != VAR_UNKNOWN)
20022 {
20023 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20024 binary = TRUE;
20025 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20026 append = TRUE;
20027 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020028
20029 /* Always open the file in binary mode, library functions have a mind of
20030 * their own about CR-LF conversion. */
20031 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020032 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20033 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020034 {
20035 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20036 ret = -1;
20037 }
20038 else
20039 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020040 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20041 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020042 fclose(fd);
20043 }
20044
20045 rettv->vval.v_number = ret;
20046}
20047
20048/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020049 * "xor(expr, expr)" function
20050 */
20051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020052f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020053{
20054 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20055 ^ get_tv_number_chk(&argvars[1], NULL);
20056}
20057
20058
20059/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020061 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062 */
20063 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020064var2fpos(
20065 typval_T *varp,
20066 int dollar_lnum, /* TRUE when $ is last line */
20067 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020068{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020069 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020070 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020071 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072
Bram Moolenaara5525202006-03-02 22:52:09 +000020073 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020074 if (varp->v_type == VAR_LIST)
20075 {
20076 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020077 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020078 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020079 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020080
20081 l = varp->vval.v_list;
20082 if (l == NULL)
20083 return NULL;
20084
20085 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020086 pos.lnum = list_find_nr(l, 0L, &error);
20087 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020088 return NULL; /* invalid line number */
20089
20090 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020091 pos.col = list_find_nr(l, 1L, &error);
20092 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020093 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020094 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020095
20096 /* We accept "$" for the column number: last column. */
20097 li = list_find(l, 1L);
20098 if (li != NULL && li->li_tv.v_type == VAR_STRING
20099 && li->li_tv.vval.v_string != NULL
20100 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20101 pos.col = len + 1;
20102
Bram Moolenaara5525202006-03-02 22:52:09 +000020103 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020104 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020105 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020106 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020107
Bram Moolenaara5525202006-03-02 22:52:09 +000020108#ifdef FEAT_VIRTUALEDIT
20109 /* Get the virtual offset. Defaults to zero. */
20110 pos.coladd = list_find_nr(l, 2L, &error);
20111 if (error)
20112 pos.coladd = 0;
20113#endif
20114
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020115 return &pos;
20116 }
20117
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020118 name = get_tv_string_chk(varp);
20119 if (name == NULL)
20120 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020121 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020122 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020123 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20124 {
20125 if (VIsual_active)
20126 return &VIsual;
20127 return &curwin->w_cursor;
20128 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020129 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020131 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20133 return NULL;
20134 return pp;
20135 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020136
20137#ifdef FEAT_VIRTUALEDIT
20138 pos.coladd = 0;
20139#endif
20140
Bram Moolenaar477933c2007-07-17 14:32:23 +000020141 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020142 {
20143 pos.col = 0;
20144 if (name[1] == '0') /* "w0": first visible line */
20145 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020146 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020147 pos.lnum = curwin->w_topline;
20148 return &pos;
20149 }
20150 else if (name[1] == '$') /* "w$": last visible line */
20151 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020152 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020153 pos.lnum = curwin->w_botline - 1;
20154 return &pos;
20155 }
20156 }
20157 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020158 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020159 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020160 {
20161 pos.lnum = curbuf->b_ml.ml_line_count;
20162 pos.col = 0;
20163 }
20164 else
20165 {
20166 pos.lnum = curwin->w_cursor.lnum;
20167 pos.col = (colnr_T)STRLEN(ml_get_curline());
20168 }
20169 return &pos;
20170 }
20171 return NULL;
20172}
20173
20174/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020175 * Convert list in "arg" into a position and optional file number.
20176 * When "fnump" is NULL there is no file number, only 3 items.
20177 * Note that the column is passed on as-is, the caller may want to decrement
20178 * it to use 1 for the first column.
20179 * Return FAIL when conversion is not possible, doesn't check the position for
20180 * validity.
20181 */
20182 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020183list2fpos(
20184 typval_T *arg,
20185 pos_T *posp,
20186 int *fnump,
20187 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020188{
20189 list_T *l = arg->vval.v_list;
20190 long i = 0;
20191 long n;
20192
Bram Moolenaar493c1782014-05-28 14:34:46 +020020193 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20194 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020195 if (arg->v_type != VAR_LIST
20196 || l == NULL
20197 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020198 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020199 return FAIL;
20200
20201 if (fnump != NULL)
20202 {
20203 n = list_find_nr(l, i++, NULL); /* fnum */
20204 if (n < 0)
20205 return FAIL;
20206 if (n == 0)
20207 n = curbuf->b_fnum; /* current buffer */
20208 *fnump = n;
20209 }
20210
20211 n = list_find_nr(l, i++, NULL); /* lnum */
20212 if (n < 0)
20213 return FAIL;
20214 posp->lnum = n;
20215
20216 n = list_find_nr(l, i++, NULL); /* col */
20217 if (n < 0)
20218 return FAIL;
20219 posp->col = n;
20220
20221#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020222 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020223 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020224 posp->coladd = 0;
20225 else
20226 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020227#endif
20228
Bram Moolenaar493c1782014-05-28 14:34:46 +020020229 if (curswantp != NULL)
20230 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20231
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020232 return OK;
20233}
20234
20235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020236 * Get the length of an environment variable name.
20237 * Advance "arg" to the first character after the name.
20238 * Return 0 for error.
20239 */
20240 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020241get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242{
20243 char_u *p;
20244 int len;
20245
20246 for (p = *arg; vim_isIDc(*p); ++p)
20247 ;
20248 if (p == *arg) /* no name found */
20249 return 0;
20250
20251 len = (int)(p - *arg);
20252 *arg = p;
20253 return len;
20254}
20255
20256/*
20257 * Get the length of the name of a function or internal variable.
20258 * "arg" is advanced to the first non-white character after the name.
20259 * Return 0 if something is wrong.
20260 */
20261 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020262get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020263{
20264 char_u *p;
20265 int len;
20266
20267 /* Find the end of the name. */
20268 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020269 {
20270 if (*p == ':')
20271 {
20272 /* "s:" is start of "s:var", but "n:" is not and can be used in
20273 * slice "[n:]". Also "xx:" is not a namespace. */
20274 len = (int)(p - *arg);
20275 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20276 || len > 1)
20277 break;
20278 }
20279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020280 if (p == *arg) /* no name found */
20281 return 0;
20282
20283 len = (int)(p - *arg);
20284 *arg = skipwhite(p);
20285
20286 return len;
20287}
20288
20289/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020290 * Get the length of the name of a variable or function.
20291 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020293 * Return -1 if curly braces expansion failed.
20294 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295 * If the name contains 'magic' {}'s, expand them and return the
20296 * expanded name in an allocated string via 'alias' - caller must free.
20297 */
20298 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020299get_name_len(
20300 char_u **arg,
20301 char_u **alias,
20302 int evaluate,
20303 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020304{
20305 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306 char_u *p;
20307 char_u *expr_start;
20308 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020309
20310 *alias = NULL; /* default to no alias */
20311
20312 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20313 && (*arg)[2] == (int)KE_SNR)
20314 {
20315 /* hard coded <SNR>, already translated */
20316 *arg += 3;
20317 return get_id_len(arg) + 3;
20318 }
20319 len = eval_fname_script(*arg);
20320 if (len > 0)
20321 {
20322 /* literal "<SID>", "s:" or "<SNR>" */
20323 *arg += len;
20324 }
20325
Bram Moolenaar071d4272004-06-13 20:20:40 +000020326 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020327 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020328 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020329 p = find_name_end(*arg, &expr_start, &expr_end,
20330 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020331 if (expr_start != NULL)
20332 {
20333 char_u *temp_string;
20334
20335 if (!evaluate)
20336 {
20337 len += (int)(p - *arg);
20338 *arg = skipwhite(p);
20339 return len;
20340 }
20341
20342 /*
20343 * Include any <SID> etc in the expanded string:
20344 * Thus the -len here.
20345 */
20346 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20347 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020348 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020349 *alias = temp_string;
20350 *arg = skipwhite(p);
20351 return (int)STRLEN(temp_string);
20352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020353
20354 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020355 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020356 EMSG2(_(e_invexpr2), *arg);
20357
20358 return len;
20359}
20360
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020361/*
20362 * Find the end of a variable or function name, taking care of magic braces.
20363 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20364 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020365 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020366 * Return a pointer to just after the name. Equal to "arg" if there is no
20367 * valid name.
20368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020370find_name_end(
20371 char_u *arg,
20372 char_u **expr_start,
20373 char_u **expr_end,
20374 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020375{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020376 int mb_nest = 0;
20377 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020378 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020379 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020381 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020383 *expr_start = NULL;
20384 *expr_end = NULL;
20385 }
20386
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020387 /* Quick check for valid starting character. */
20388 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20389 return arg;
20390
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020391 for (p = arg; *p != NUL
20392 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020393 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020394 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020395 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020396 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020397 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020398 if (*p == '\'')
20399 {
20400 /* skip over 'string' to avoid counting [ and ] inside it. */
20401 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20402 ;
20403 if (*p == NUL)
20404 break;
20405 }
20406 else if (*p == '"')
20407 {
20408 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20409 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20410 if (*p == '\\' && p[1] != NUL)
20411 ++p;
20412 if (*p == NUL)
20413 break;
20414 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020415 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20416 {
20417 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020418 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020419 len = (int)(p - arg);
20420 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020421 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020422 break;
20423 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020424
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020425 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020426 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020427 if (*p == '[')
20428 ++br_nest;
20429 else if (*p == ']')
20430 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020431 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020432
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020433 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020434 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020435 if (*p == '{')
20436 {
20437 mb_nest++;
20438 if (expr_start != NULL && *expr_start == NULL)
20439 *expr_start = p;
20440 }
20441 else if (*p == '}')
20442 {
20443 mb_nest--;
20444 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20445 *expr_end = p;
20446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020448 }
20449
20450 return p;
20451}
20452
20453/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020454 * Expands out the 'magic' {}'s in a variable/function name.
20455 * Note that this can call itself recursively, to deal with
20456 * constructs like foo{bar}{baz}{bam}
20457 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20458 * "in_start" ^
20459 * "expr_start" ^
20460 * "expr_end" ^
20461 * "in_end" ^
20462 *
20463 * Returns a new allocated string, which the caller must free.
20464 * Returns NULL for failure.
20465 */
20466 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020467make_expanded_name(
20468 char_u *in_start,
20469 char_u *expr_start,
20470 char_u *expr_end,
20471 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020472{
20473 char_u c1;
20474 char_u *retval = NULL;
20475 char_u *temp_result;
20476 char_u *nextcmd = NULL;
20477
20478 if (expr_end == NULL || in_end == NULL)
20479 return NULL;
20480 *expr_start = NUL;
20481 *expr_end = NUL;
20482 c1 = *in_end;
20483 *in_end = NUL;
20484
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020485 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020486 if (temp_result != NULL && nextcmd == NULL)
20487 {
20488 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20489 + (in_end - expr_end) + 1));
20490 if (retval != NULL)
20491 {
20492 STRCPY(retval, in_start);
20493 STRCAT(retval, temp_result);
20494 STRCAT(retval, expr_end + 1);
20495 }
20496 }
20497 vim_free(temp_result);
20498
20499 *in_end = c1; /* put char back for error messages */
20500 *expr_start = '{';
20501 *expr_end = '}';
20502
20503 if (retval != NULL)
20504 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020505 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020506 if (expr_start != NULL)
20507 {
20508 /* Further expansion! */
20509 temp_result = make_expanded_name(retval, expr_start,
20510 expr_end, temp_result);
20511 vim_free(retval);
20512 retval = temp_result;
20513 }
20514 }
20515
20516 return retval;
20517}
20518
20519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020521 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020522 */
20523 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020524eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020525{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020526 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20527}
20528
20529/*
20530 * Return TRUE if character "c" can be used as the first character in a
20531 * variable or function name (excluding '{' and '}').
20532 */
20533 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020534eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020535{
20536 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020537}
20538
20539/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020540 * Set number v: variable to "val".
20541 */
20542 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020543set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020544{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020545 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546}
20547
20548/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020549 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020550 */
20551 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010020552get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020553{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020554 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020555}
20556
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020557/*
20558 * Get string v: variable value. Uses a static buffer, can only be used once.
20559 */
20560 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020561get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020562{
20563 return get_tv_string(&vimvars[idx].vv_tv);
20564}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020565
Bram Moolenaar071d4272004-06-13 20:20:40 +000020566/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020567 * Get List v: variable value. Caller must take care of reference count when
20568 * needed.
20569 */
20570 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020571get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000020572{
20573 return vimvars[idx].vv_list;
20574}
20575
20576/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020577 * Set v:char to character "c".
20578 */
20579 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020580set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020581{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020582 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020583
20584#ifdef FEAT_MBYTE
20585 if (has_mbyte)
20586 buf[(*mb_char2bytes)(c, buf)] = NUL;
20587 else
20588#endif
20589 {
20590 buf[0] = c;
20591 buf[1] = NUL;
20592 }
20593 set_vim_var_string(VV_CHAR, buf, -1);
20594}
20595
20596/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020597 * Set v:count to "count" and v:count1 to "count1".
20598 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020599 */
20600 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020601set_vcount(
20602 long count,
20603 long count1,
20604 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020606 if (set_prevcount)
20607 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020608 vimvars[VV_COUNT].vv_nr = count;
20609 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020610}
20611
20612/*
20613 * Set string v: variable to a copy of "val".
20614 */
20615 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020616set_vim_var_string(
20617 int idx,
20618 char_u *val,
20619 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020620{
Bram Moolenaara542c682016-01-31 16:28:04 +010020621 clear_tv(&vimvars[idx].vv_di.di_tv);
20622 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020623 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020624 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020625 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020626 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020627 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020628 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020629}
20630
20631/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020632 * Set List v: variable to "val".
20633 */
20634 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020635set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000020636{
Bram Moolenaara542c682016-01-31 16:28:04 +010020637 clear_tv(&vimvars[idx].vv_di.di_tv);
20638 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000020639 vimvars[idx].vv_list = val;
20640 if (val != NULL)
20641 ++val->lv_refcount;
20642}
20643
20644/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020645 * Set Dictionary v: variable to "val".
20646 */
20647 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020648set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020020649{
20650 int todo;
20651 hashitem_T *hi;
20652
Bram Moolenaara542c682016-01-31 16:28:04 +010020653 clear_tv(&vimvars[idx].vv_di.di_tv);
20654 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020020655 vimvars[idx].vv_dict = val;
20656 if (val != NULL)
20657 {
20658 ++val->dv_refcount;
20659
20660 /* Set readonly */
20661 todo = (int)val->dv_hashtab.ht_used;
20662 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20663 {
20664 if (HASHITEM_EMPTY(hi))
20665 continue;
20666 --todo;
20667 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20668 }
20669 }
20670}
20671
20672/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020673 * Set v:register if needed.
20674 */
20675 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020676set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020677{
20678 char_u regname;
20679
20680 if (c == 0 || c == ' ')
20681 regname = '"';
20682 else
20683 regname = c;
20684 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020685 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020686 set_vim_var_string(VV_REG, &regname, 1);
20687}
20688
20689/*
20690 * Get or set v:exception. If "oldval" == NULL, return the current value.
20691 * Otherwise, restore the value to "oldval" and return NULL.
20692 * Must always be called in pairs to save and restore v:exception! Does not
20693 * take care of memory allocations.
20694 */
20695 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020696v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020697{
20698 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020699 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020700
Bram Moolenaare9a41262005-01-15 22:18:47 +000020701 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702 return NULL;
20703}
20704
20705/*
20706 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20707 * Otherwise, restore the value to "oldval" and return NULL.
20708 * Must always be called in pairs to save and restore v:throwpoint! Does not
20709 * take care of memory allocations.
20710 */
20711 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020712v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020713{
20714 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020715 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020716
Bram Moolenaare9a41262005-01-15 22:18:47 +000020717 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020718 return NULL;
20719}
20720
20721#if defined(FEAT_AUTOCMD) || defined(PROTO)
20722/*
20723 * Set v:cmdarg.
20724 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20725 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20726 * Must always be called in pairs!
20727 */
20728 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020729set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020730{
20731 char_u *oldval;
20732 char_u *newval;
20733 unsigned len;
20734
Bram Moolenaare9a41262005-01-15 22:18:47 +000020735 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020736 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020737 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020738 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020739 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020740 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020741 }
20742
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020743 if (eap->force_bin == FORCE_BIN)
20744 len = 6;
20745 else if (eap->force_bin == FORCE_NOBIN)
20746 len = 8;
20747 else
20748 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020749
20750 if (eap->read_edit)
20751 len += 7;
20752
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020753 if (eap->force_ff != 0)
20754 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20755# ifdef FEAT_MBYTE
20756 if (eap->force_enc != 0)
20757 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020758 if (eap->bad_char != 0)
20759 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020760# endif
20761
20762 newval = alloc(len + 1);
20763 if (newval == NULL)
20764 return NULL;
20765
20766 if (eap->force_bin == FORCE_BIN)
20767 sprintf((char *)newval, " ++bin");
20768 else if (eap->force_bin == FORCE_NOBIN)
20769 sprintf((char *)newval, " ++nobin");
20770 else
20771 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020772
20773 if (eap->read_edit)
20774 STRCAT(newval, " ++edit");
20775
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020776 if (eap->force_ff != 0)
20777 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20778 eap->cmd + eap->force_ff);
20779# ifdef FEAT_MBYTE
20780 if (eap->force_enc != 0)
20781 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20782 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020783 if (eap->bad_char == BAD_KEEP)
20784 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20785 else if (eap->bad_char == BAD_DROP)
20786 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20787 else if (eap->bad_char != 0)
20788 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020789# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020790 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020791 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020792}
20793#endif
20794
20795/*
20796 * Get the value of internal variable "name".
20797 * Return OK or FAIL.
20798 */
20799 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020800get_var_tv(
20801 char_u *name,
20802 int len, /* length of "name" */
20803 typval_T *rettv, /* NULL when only checking existence */
20804 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
20805 int verbose, /* may give error message */
20806 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807{
20808 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020809 typval_T *tv = NULL;
20810 typval_T atv;
20811 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020812 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020813
20814 /* truncate the name, so that we can use strcmp() */
20815 cc = name[len];
20816 name[len] = NUL;
20817
20818 /*
20819 * Check for "b:changedtick".
20820 */
20821 if (STRCMP(name, "b:changedtick") == 0)
20822 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020823 atv.v_type = VAR_NUMBER;
20824 atv.vval.v_number = curbuf->b_changedtick;
20825 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020826 }
20827
20828 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020829 * Check for user-defined variables.
20830 */
20831 else
20832 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020833 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020834 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020835 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020836 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020837 if (dip != NULL)
20838 *dip = v;
20839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020840 }
20841
Bram Moolenaare9a41262005-01-15 22:18:47 +000020842 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020843 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020844 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020845 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020846 ret = FAIL;
20847 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020848 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020849 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020850
20851 name[len] = cc;
20852
20853 return ret;
20854}
20855
20856/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020857 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20858 * Also handle function call with Funcref variable: func(expr)
20859 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20860 */
20861 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020862handle_subscript(
20863 char_u **arg,
20864 typval_T *rettv,
20865 int evaluate, /* do more than finding the end */
20866 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020867{
20868 int ret = OK;
20869 dict_T *selfdict = NULL;
20870 char_u *s;
20871 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020872 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020873
20874 while (ret == OK
20875 && (**arg == '['
20876 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020877 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020878 && !vim_iswhite(*(*arg - 1)))
20879 {
20880 if (**arg == '(')
20881 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020882 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020883 if (evaluate)
20884 {
20885 functv = *rettv;
20886 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020887
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020888 /* Invoke the function. Recursive! */
20889 s = functv.vval.v_string;
20890 }
20891 else
20892 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020893 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020894 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20895 &len, evaluate, selfdict);
20896
20897 /* Clear the funcref afterwards, so that deleting it while
20898 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020899 if (evaluate)
20900 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020901
20902 /* Stop the expression evaluation when immediately aborting on
20903 * error, or when an interrupt occurred or an exception was thrown
20904 * but not caught. */
20905 if (aborting())
20906 {
20907 if (ret == OK)
20908 clear_tv(rettv);
20909 ret = FAIL;
20910 }
20911 dict_unref(selfdict);
20912 selfdict = NULL;
20913 }
20914 else /* **arg == '[' || **arg == '.' */
20915 {
20916 dict_unref(selfdict);
20917 if (rettv->v_type == VAR_DICT)
20918 {
20919 selfdict = rettv->vval.v_dict;
20920 if (selfdict != NULL)
20921 ++selfdict->dv_refcount;
20922 }
20923 else
20924 selfdict = NULL;
20925 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20926 {
20927 clear_tv(rettv);
20928 ret = FAIL;
20929 }
20930 }
20931 }
20932 dict_unref(selfdict);
20933 return ret;
20934}
20935
20936/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020937 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020938 * value).
20939 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020940 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020941alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020942{
Bram Moolenaar33570922005-01-25 22:26:29 +000020943 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020944}
20945
20946/*
20947 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020948 * The string "s" must have been allocated, it is consumed.
20949 * Return NULL for out of memory, the variable otherwise.
20950 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020951 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020952alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020953{
Bram Moolenaar33570922005-01-25 22:26:29 +000020954 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020955
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020956 rettv = alloc_tv();
20957 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020959 rettv->v_type = VAR_STRING;
20960 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020961 }
20962 else
20963 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020964 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020965}
20966
20967/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020968 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020969 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020970 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020971free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020972{
20973 if (varp != NULL)
20974 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020975 switch (varp->v_type)
20976 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020977 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020978 func_unref(varp->vval.v_string);
20979 /*FALLTHROUGH*/
20980 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020981 vim_free(varp->vval.v_string);
20982 break;
20983 case VAR_LIST:
20984 list_unref(varp->vval.v_list);
20985 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020986 case VAR_DICT:
20987 dict_unref(varp->vval.v_dict);
20988 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020989 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020990#ifdef FEAT_FLOAT
20991 case VAR_FLOAT:
20992#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020993 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010020994 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020995 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020996 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020997 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020998 break;
20999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021000 vim_free(varp);
21001 }
21002}
21003
21004/*
21005 * Free the memory for a variable value and set the value to NULL or 0.
21006 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021007 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021008clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021009{
21010 if (varp != NULL)
21011 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021012 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021013 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021014 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021015 func_unref(varp->vval.v_string);
21016 /*FALLTHROUGH*/
21017 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021018 vim_free(varp->vval.v_string);
21019 varp->vval.v_string = NULL;
21020 break;
21021 case VAR_LIST:
21022 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021023 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021024 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021025 case VAR_DICT:
21026 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021027 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021028 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021029 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021030 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021031 varp->vval.v_number = 0;
21032 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021033#ifdef FEAT_FLOAT
21034 case VAR_FLOAT:
21035 varp->vval.v_float = 0.0;
21036 break;
21037#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021038 case VAR_UNKNOWN:
21039 break;
21040 default:
21041 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021042 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021043 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021044 }
21045}
21046
21047/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021048 * Set the value of a variable to NULL without freeing items.
21049 */
21050 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021051init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021052{
21053 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021054 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021055}
21056
21057/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021058 * Get the number value of a variable.
21059 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021060 * For incompatible types, return 0.
21061 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21062 * caller of incompatible types: it sets *denote to TRUE if "denote"
21063 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021064 */
21065 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021066get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021067{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021068 int error = FALSE;
21069
21070 return get_tv_number_chk(varp, &error); /* return 0L on error */
21071}
21072
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021073 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021074get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021075{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021076 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021078 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021079 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021080 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021081 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021082#ifdef FEAT_FLOAT
21083 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021084 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021085 break;
21086#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021087 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021088 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021089 break;
21090 case VAR_STRING:
21091 if (varp->vval.v_string != NULL)
21092 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021093 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021094 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021095 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021096 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021097 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021098 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021099 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021100 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021101 case VAR_SPECIAL:
21102 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21103 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021104 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021105 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021106 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021108 if (denote == NULL) /* useful for values that must be unsigned */
21109 n = -1;
21110 else
21111 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021112 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021113}
21114
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021115#ifdef FEAT_FLOAT
21116 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021117get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021118{
21119 switch (varp->v_type)
21120 {
21121 case VAR_NUMBER:
21122 return (float_T)(varp->vval.v_number);
21123#ifdef FEAT_FLOAT
21124 case VAR_FLOAT:
21125 return varp->vval.v_float;
21126 break;
21127#endif
21128 case VAR_FUNC:
21129 EMSG(_("E891: Using a Funcref as a Float"));
21130 break;
21131 case VAR_STRING:
21132 EMSG(_("E892: Using a String as a Float"));
21133 break;
21134 case VAR_LIST:
21135 EMSG(_("E893: Using a List as a Float"));
21136 break;
21137 case VAR_DICT:
21138 EMSG(_("E894: Using a Dictionary as a Float"));
21139 break;
21140 default:
21141 EMSG2(_(e_intern2), "get_tv_float()");
21142 break;
21143 }
21144 return 0;
21145}
21146#endif
21147
Bram Moolenaar071d4272004-06-13 20:20:40 +000021148/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021149 * Get the lnum from the first argument.
21150 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021151 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021152 */
21153 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021154get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021155{
Bram Moolenaar33570922005-01-25 22:26:29 +000021156 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021157 linenr_T lnum;
21158
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021159 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021160 if (lnum == 0) /* no valid number, try using line() */
21161 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021162 rettv.v_type = VAR_NUMBER;
21163 f_line(argvars, &rettv);
21164 lnum = rettv.vval.v_number;
21165 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021166 }
21167 return lnum;
21168}
21169
21170/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021171 * Get the lnum from the first argument.
21172 * Also accepts "$", then "buf" is used.
21173 * Returns 0 on error.
21174 */
21175 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021176get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021177{
21178 if (argvars[0].v_type == VAR_STRING
21179 && argvars[0].vval.v_string != NULL
21180 && argvars[0].vval.v_string[0] == '$'
21181 && buf != NULL)
21182 return buf->b_ml.ml_line_count;
21183 return get_tv_number_chk(&argvars[0], NULL);
21184}
21185
21186/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021187 * Get the string value of a variable.
21188 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021189 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21190 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021191 * If the String variable has never been set, return an empty string.
21192 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021193 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21194 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195 */
21196 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021197get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021198{
21199 static char_u mybuf[NUMBUFLEN];
21200
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021201 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021202}
21203
21204 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021205get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021206{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021207 char_u *res = get_tv_string_buf_chk(varp, buf);
21208
21209 return res != NULL ? res : (char_u *)"";
21210}
21211
Bram Moolenaar7d647822014-04-05 21:28:56 +020021212/*
21213 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21214 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021215 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021216get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021217{
21218 static char_u mybuf[NUMBUFLEN];
21219
21220 return get_tv_string_buf_chk(varp, mybuf);
21221}
21222
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021223 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021224get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021225{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021226 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021227 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021228 case VAR_NUMBER:
21229 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21230 return buf;
21231 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021232 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021233 break;
21234 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021235 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021236 break;
21237 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021238 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021239 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021240#ifdef FEAT_FLOAT
21241 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021242 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021243 break;
21244#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021245 case VAR_STRING:
21246 if (varp->vval.v_string != NULL)
21247 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021248 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021249 case VAR_SPECIAL:
21250 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21251 return buf;
21252
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021253 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021254 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021255 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021256 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021257 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021258}
21259
21260/*
21261 * Find variable "name" in the list of variables.
21262 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021263 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021264 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021265 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021266 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021267 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021268find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021269{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021270 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021271 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021272
Bram Moolenaara7043832005-01-21 11:56:39 +000021273 ht = find_var_ht(name, &varname);
21274 if (htp != NULL)
21275 *htp = ht;
21276 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021277 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021278 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021279}
21280
21281/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021282 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021283 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021284 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021285 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021286find_var_in_ht(
21287 hashtab_T *ht,
21288 int htname,
21289 char_u *varname,
21290 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021291{
Bram Moolenaar33570922005-01-25 22:26:29 +000021292 hashitem_T *hi;
21293
21294 if (*varname == NUL)
21295 {
21296 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021297 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021298 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021299 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021300 case 'g': return &globvars_var;
21301 case 'v': return &vimvars_var;
21302 case 'b': return &curbuf->b_bufvar;
21303 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021304#ifdef FEAT_WINDOWS
21305 case 't': return &curtab->tp_winvar;
21306#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021307 case 'l': return current_funccal == NULL
21308 ? NULL : &current_funccal->l_vars_var;
21309 case 'a': return current_funccal == NULL
21310 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021311 }
21312 return NULL;
21313 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021314
21315 hi = hash_find(ht, varname);
21316 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021317 {
21318 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021319 * worked find the variable again. Don't auto-load a script if it was
21320 * loaded already, otherwise it would be loaded every time when
21321 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021322 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021323 {
21324 /* Note: script_autoload() may make "hi" invalid. It must either
21325 * be obtained again or not used. */
21326 if (!script_autoload(varname, FALSE) || aborting())
21327 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021328 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021329 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021330 if (HASHITEM_EMPTY(hi))
21331 return NULL;
21332 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021333 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021334}
21335
21336/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021337 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021338 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021339 * Set "varname" to the start of name without ':'.
21340 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021341 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021342find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021343{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021344 hashitem_T *hi;
21345
Bram Moolenaar73627d02015-08-11 15:46:09 +020021346 if (name[0] == NUL)
21347 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021348 if (name[1] != ':')
21349 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021350 /* The name must not start with a colon or #. */
21351 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021352 return NULL;
21353 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021354
21355 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021356 hi = hash_find(&compat_hashtab, name);
21357 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021358 return &compat_hashtab;
21359
Bram Moolenaar071d4272004-06-13 20:20:40 +000021360 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021361 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021362 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363 }
21364 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021365 if (*name == 'g') /* global variable */
21366 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021367 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21368 */
21369 if (vim_strchr(name + 2, ':') != NULL
21370 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021371 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021372 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021373 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021374 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021375 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021376#ifdef FEAT_WINDOWS
21377 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021378 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021379#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021380 if (*name == 'v') /* v: variable */
21381 return &vimvarht;
21382 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021383 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021384 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021385 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386 if (*name == 's' /* script variable */
21387 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21388 return &SCRIPT_VARS(current_SID);
21389 return NULL;
21390}
21391
21392/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021393 * Get function call environment based on bactrace debug level
21394 */
21395 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021396get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021397{
21398 int i;
21399 funccall_T *funccal;
21400 funccall_T *temp_funccal;
21401
21402 funccal = current_funccal;
21403 if (debug_backtrace_level > 0)
21404 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021405 for (i = 0; i < debug_backtrace_level; i++)
21406 {
21407 temp_funccal = funccal->caller;
21408 if (temp_funccal)
21409 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021410 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021411 /* backtrace level overflow. reset to max */
21412 debug_backtrace_level = i;
21413 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021414 }
21415 return funccal;
21416}
21417
21418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021419 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021420 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021421 * Returns NULL when it doesn't exist.
21422 */
21423 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021424get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021425{
Bram Moolenaar33570922005-01-25 22:26:29 +000021426 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021427
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021428 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021429 if (v == NULL)
21430 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021431 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021432}
21433
21434/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021435 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021436 * sourcing this script and when executing functions defined in the script.
21437 */
21438 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021439new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021440{
Bram Moolenaara7043832005-01-21 11:56:39 +000021441 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021442 hashtab_T *ht;
21443 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021444
Bram Moolenaar071d4272004-06-13 20:20:40 +000021445 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21446 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021447 /* Re-allocating ga_data means that an ht_array pointing to
21448 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021449 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021450 for (i = 1; i <= ga_scripts.ga_len; ++i)
21451 {
21452 ht = &SCRIPT_VARS(i);
21453 if (ht->ht_mask == HT_INIT_SIZE - 1)
21454 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021455 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021456 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021457 }
21458
Bram Moolenaar071d4272004-06-13 20:20:40 +000021459 while (ga_scripts.ga_len < id)
21460 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021461 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021462 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021463 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021464 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021465 }
21466 }
21467}
21468
21469/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021470 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21471 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021472 */
21473 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021474init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021475{
Bram Moolenaar33570922005-01-25 22:26:29 +000021476 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021477 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021478 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021479 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021480 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021481 dict_var->di_tv.vval.v_dict = dict;
21482 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021483 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021484 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21485 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486}
21487
21488/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021489 * Unreference a dictionary initialized by init_var_dict().
21490 */
21491 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021492unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020021493{
21494 /* Now the dict needs to be freed if no one else is using it, go back to
21495 * normal reference counting. */
21496 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21497 dict_unref(dict);
21498}
21499
21500/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021501 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021502 * Frees all allocated variables and the value they contain.
21503 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021504 */
21505 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021506vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021507{
21508 vars_clear_ext(ht, TRUE);
21509}
21510
21511/*
21512 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21513 */
21514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021515vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021516{
Bram Moolenaara7043832005-01-21 11:56:39 +000021517 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021518 hashitem_T *hi;
21519 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021520
Bram Moolenaar33570922005-01-25 22:26:29 +000021521 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021522 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021523 for (hi = ht->ht_array; todo > 0; ++hi)
21524 {
21525 if (!HASHITEM_EMPTY(hi))
21526 {
21527 --todo;
21528
Bram Moolenaar33570922005-01-25 22:26:29 +000021529 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021530 * ht_array might change then. hash_clear() takes care of it
21531 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021532 v = HI2DI(hi);
21533 if (free_val)
21534 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021535 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021536 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021537 }
21538 }
21539 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021540 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021541}
21542
Bram Moolenaara7043832005-01-21 11:56:39 +000021543/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021544 * Delete a variable from hashtab "ht" at item "hi".
21545 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021548delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021549{
Bram Moolenaar33570922005-01-25 22:26:29 +000021550 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021551
21552 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021553 clear_tv(&di->di_tv);
21554 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021555}
21556
21557/*
21558 * List the value of one internal variable.
21559 */
21560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021561list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021562{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021563 char_u *tofree;
21564 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021565 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021566
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021567 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000021568 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021569 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021570 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021571}
21572
Bram Moolenaar071d4272004-06-13 20:20:40 +000021573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021574list_one_var_a(
21575 char_u *prefix,
21576 char_u *name,
21577 int type,
21578 char_u *string,
21579 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021580{
Bram Moolenaar31859182007-08-14 20:41:13 +000021581 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21582 msg_start();
21583 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021584 if (name != NULL) /* "a:" vars don't have a name stored */
21585 msg_puts(name);
21586 msg_putchar(' ');
21587 msg_advance(22);
21588 if (type == VAR_NUMBER)
21589 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021590 else if (type == VAR_FUNC)
21591 msg_putchar('*');
21592 else if (type == VAR_LIST)
21593 {
21594 msg_putchar('[');
21595 if (*string == '[')
21596 ++string;
21597 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021598 else if (type == VAR_DICT)
21599 {
21600 msg_putchar('{');
21601 if (*string == '{')
21602 ++string;
21603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604 else
21605 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021606
Bram Moolenaar071d4272004-06-13 20:20:40 +000021607 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021608
21609 if (type == VAR_FUNC)
21610 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021611 if (*first)
21612 {
21613 msg_clr_eos();
21614 *first = FALSE;
21615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616}
21617
21618/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021619 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620 * If the variable already exists, the value is updated.
21621 * Otherwise the variable is created.
21622 */
21623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021624set_var(
21625 char_u *name,
21626 typval_T *tv,
21627 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021628{
Bram Moolenaar33570922005-01-25 22:26:29 +000021629 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021631 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021633 ht = find_var_ht(name, &varname);
21634 if (ht == NULL || *varname == NUL)
21635 {
21636 EMSG2(_(e_illvar), name);
21637 return;
21638 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021639 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021640
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021641 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21642 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021643
Bram Moolenaar33570922005-01-25 22:26:29 +000021644 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021645 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021646 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021647 if (var_check_ro(v->di_flags, name, FALSE)
21648 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021649 return;
21650 if (v->di_tv.v_type != tv->v_type
21651 && !((v->di_tv.v_type == VAR_STRING
21652 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021653 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021654 || tv->v_type == VAR_NUMBER))
21655#ifdef FEAT_FLOAT
21656 && !((v->di_tv.v_type == VAR_NUMBER
21657 || v->di_tv.v_type == VAR_FLOAT)
21658 && (tv->v_type == VAR_NUMBER
21659 || tv->v_type == VAR_FLOAT))
21660#endif
21661 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021662 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021663 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021664 return;
21665 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021666
21667 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021668 * Handle setting internal v: variables separately where needed to
21669 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021670 */
21671 if (ht == &vimvarht)
21672 {
21673 if (v->di_tv.v_type == VAR_STRING)
21674 {
21675 vim_free(v->di_tv.vval.v_string);
21676 if (copy || tv->v_type != VAR_STRING)
21677 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21678 else
21679 {
21680 /* Take over the string to avoid an extra alloc/free. */
21681 v->di_tv.vval.v_string = tv->vval.v_string;
21682 tv->vval.v_string = NULL;
21683 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021684 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021685 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021686 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021687 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021688 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021689 if (STRCMP(varname, "searchforward") == 0)
21690 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021691#ifdef FEAT_SEARCH_EXTRA
21692 else if (STRCMP(varname, "hlsearch") == 0)
21693 {
21694 no_hlsearch = !v->di_tv.vval.v_number;
21695 redraw_all_later(SOME_VALID);
21696 }
21697#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021698 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021699 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021700 else if (v->di_tv.v_type != tv->v_type)
21701 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021702 }
21703
21704 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021705 }
21706 else /* add a new variable */
21707 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021708 /* Can't add "v:" variable. */
21709 if (ht == &vimvarht)
21710 {
21711 EMSG2(_(e_illvar), name);
21712 return;
21713 }
21714
Bram Moolenaar92124a32005-06-17 22:03:40 +000021715 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021716 if (!valid_varname(varname))
21717 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021718
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021719 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21720 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021721 if (v == NULL)
21722 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021723 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021724 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021725 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021726 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021727 return;
21728 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021729 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021731
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021732 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021733 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021734 else
21735 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021736 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021737 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021738 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740}
21741
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021742/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021743 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021744 * Also give an error message.
21745 */
21746 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021747var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000021748{
21749 if (flags & DI_FLAGS_RO)
21750 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021751 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021752 return TRUE;
21753 }
21754 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21755 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021756 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021757 return TRUE;
21758 }
21759 return FALSE;
21760}
21761
21762/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021763 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21764 * Also give an error message.
21765 */
21766 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021767var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021768{
21769 if (flags & DI_FLAGS_FIX)
21770 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021771 EMSG2(_("E795: Cannot delete variable %s"),
21772 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021773 return TRUE;
21774 }
21775 return FALSE;
21776}
21777
21778/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021779 * Check if a funcref is assigned to a valid variable name.
21780 * Return TRUE and give an error if not.
21781 */
21782 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021783var_check_func_name(
21784 char_u *name, /* points to start of variable name */
21785 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021786{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021787 /* Allow for w: b: s: and t:. */
21788 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021789 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21790 ? name[2] : name[0]))
21791 {
21792 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21793 name);
21794 return TRUE;
21795 }
21796 /* Don't allow hiding a function. When "v" is not NULL we might be
21797 * assigning another function to the same var, the type is checked
21798 * below. */
21799 if (new_var && function_exists(name))
21800 {
21801 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21802 name);
21803 return TRUE;
21804 }
21805 return FALSE;
21806}
21807
21808/*
21809 * Check if a variable name is valid.
21810 * Return FALSE and give an error if not.
21811 */
21812 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021813valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021814{
21815 char_u *p;
21816
21817 for (p = varname; *p != NUL; ++p)
21818 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21819 && *p != AUTOLOAD_CHAR)
21820 {
21821 EMSG2(_(e_illvar), varname);
21822 return FALSE;
21823 }
21824 return TRUE;
21825}
21826
21827/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021828 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020021829 * Also give an error message, using "name" or _("name") when use_gettext is
21830 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021831 */
21832 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021833tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021834{
21835 if (lock & VAR_LOCKED)
21836 {
21837 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021838 name == NULL ? (char_u *)_("Unknown")
21839 : use_gettext ? (char_u *)_(name)
21840 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021841 return TRUE;
21842 }
21843 if (lock & VAR_FIXED)
21844 {
21845 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021846 name == NULL ? (char_u *)_("Unknown")
21847 : use_gettext ? (char_u *)_(name)
21848 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021849 return TRUE;
21850 }
21851 return FALSE;
21852}
21853
21854/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021855 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021856 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021857 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021858 * It is OK for "from" and "to" to point to the same item. This is used to
21859 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021860 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021861 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021862copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021863{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021864 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021865 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021866 switch (from->v_type)
21867 {
21868 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021869 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021870 to->vval.v_number = from->vval.v_number;
21871 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021872#ifdef FEAT_FLOAT
21873 case VAR_FLOAT:
21874 to->vval.v_float = from->vval.v_float;
21875 break;
21876#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021877 case VAR_STRING:
21878 case VAR_FUNC:
21879 if (from->vval.v_string == NULL)
21880 to->vval.v_string = NULL;
21881 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021882 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021883 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021884 if (from->v_type == VAR_FUNC)
21885 func_ref(to->vval.v_string);
21886 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021887 break;
21888 case VAR_LIST:
21889 if (from->vval.v_list == NULL)
21890 to->vval.v_list = NULL;
21891 else
21892 {
21893 to->vval.v_list = from->vval.v_list;
21894 ++to->vval.v_list->lv_refcount;
21895 }
21896 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021897 case VAR_DICT:
21898 if (from->vval.v_dict == NULL)
21899 to->vval.v_dict = NULL;
21900 else
21901 {
21902 to->vval.v_dict = from->vval.v_dict;
21903 ++to->vval.v_dict->dv_refcount;
21904 }
21905 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021906 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021907 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021908 break;
21909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021910}
21911
21912/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021913 * Make a copy of an item.
21914 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021915 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21916 * reference to an already copied list/dict can be used.
21917 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021918 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021919 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021920item_copy(
21921 typval_T *from,
21922 typval_T *to,
21923 int deep,
21924 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021925{
21926 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021927 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021928
Bram Moolenaar33570922005-01-25 22:26:29 +000021929 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021930 {
21931 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021932 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021933 }
21934 ++recurse;
21935
21936 switch (from->v_type)
21937 {
21938 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021939#ifdef FEAT_FLOAT
21940 case VAR_FLOAT:
21941#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021942 case VAR_STRING:
21943 case VAR_FUNC:
21944 copy_tv(from, to);
21945 break;
21946 case VAR_LIST:
21947 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021948 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021949 if (from->vval.v_list == NULL)
21950 to->vval.v_list = NULL;
21951 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21952 {
21953 /* use the copy made earlier */
21954 to->vval.v_list = from->vval.v_list->lv_copylist;
21955 ++to->vval.v_list->lv_refcount;
21956 }
21957 else
21958 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21959 if (to->vval.v_list == NULL)
21960 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021961 break;
21962 case VAR_DICT:
21963 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021964 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021965 if (from->vval.v_dict == NULL)
21966 to->vval.v_dict = NULL;
21967 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21968 {
21969 /* use the copy made earlier */
21970 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21971 ++to->vval.v_dict->dv_refcount;
21972 }
21973 else
21974 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21975 if (to->vval.v_dict == NULL)
21976 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021977 break;
21978 default:
21979 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021980 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021981 }
21982 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021983 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021984}
21985
21986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021987 * ":echo expr1 ..." print each argument separated with a space, add a
21988 * newline at the end.
21989 * ":echon expr1 ..." print each argument plain.
21990 */
21991 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021992ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021993{
21994 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021995 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021996 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021997 char_u *p;
21998 int needclr = TRUE;
21999 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022000 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022001
22002 if (eap->skip)
22003 ++emsg_skip;
22004 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22005 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022006 /* If eval1() causes an error message the text from the command may
22007 * still need to be cleared. E.g., "echo 22,44". */
22008 need_clr_eos = needclr;
22009
Bram Moolenaar071d4272004-06-13 20:20:40 +000022010 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022011 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022012 {
22013 /*
22014 * Report the invalid expression unless the expression evaluation
22015 * has been cancelled due to an aborting error, an interrupt, or an
22016 * exception.
22017 */
22018 if (!aborting())
22019 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022020 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022021 break;
22022 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022023 need_clr_eos = FALSE;
22024
Bram Moolenaar071d4272004-06-13 20:20:40 +000022025 if (!eap->skip)
22026 {
22027 if (atstart)
22028 {
22029 atstart = FALSE;
22030 /* Call msg_start() after eval1(), evaluating the expression
22031 * may cause a message to appear. */
22032 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022033 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022034 /* Mark the saved text as finishing the line, so that what
22035 * follows is displayed on a new line when scrolling back
22036 * at the more prompt. */
22037 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022038 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022040 }
22041 else if (eap->cmdidx == CMD_echo)
22042 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022043 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022044 if (p != NULL)
22045 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022046 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022047 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022049 if (*p != TAB && needclr)
22050 {
22051 /* remove any text still there from the command */
22052 msg_clr_eos();
22053 needclr = FALSE;
22054 }
22055 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022056 }
22057 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022058 {
22059#ifdef FEAT_MBYTE
22060 if (has_mbyte)
22061 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022062 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022063
22064 (void)msg_outtrans_len_attr(p, i, echo_attr);
22065 p += i - 1;
22066 }
22067 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022068#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022069 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022072 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022073 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022074 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022075 arg = skipwhite(arg);
22076 }
22077 eap->nextcmd = check_nextcmd(arg);
22078
22079 if (eap->skip)
22080 --emsg_skip;
22081 else
22082 {
22083 /* remove text that may still be there from the command */
22084 if (needclr)
22085 msg_clr_eos();
22086 if (eap->cmdidx == CMD_echo)
22087 msg_end();
22088 }
22089}
22090
22091/*
22092 * ":echohl {name}".
22093 */
22094 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022095ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022096{
22097 int id;
22098
22099 id = syn_name2id(eap->arg);
22100 if (id == 0)
22101 echo_attr = 0;
22102 else
22103 echo_attr = syn_id2attr(id);
22104}
22105
22106/*
22107 * ":execute expr1 ..." execute the result of an expression.
22108 * ":echomsg expr1 ..." Print a message
22109 * ":echoerr expr1 ..." Print an error
22110 * Each gets spaces around each argument and a newline at the end for
22111 * echo commands
22112 */
22113 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022114ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022115{
22116 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022117 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022118 int ret = OK;
22119 char_u *p;
22120 garray_T ga;
22121 int len;
22122 int save_did_emsg;
22123
22124 ga_init2(&ga, 1, 80);
22125
22126 if (eap->skip)
22127 ++emsg_skip;
22128 while (*arg != NUL && *arg != '|' && *arg != '\n')
22129 {
22130 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022131 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022132 {
22133 /*
22134 * Report the invalid expression unless the expression evaluation
22135 * has been cancelled due to an aborting error, an interrupt, or an
22136 * exception.
22137 */
22138 if (!aborting())
22139 EMSG2(_(e_invexpr2), p);
22140 ret = FAIL;
22141 break;
22142 }
22143
22144 if (!eap->skip)
22145 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022146 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022147 len = (int)STRLEN(p);
22148 if (ga_grow(&ga, len + 2) == FAIL)
22149 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022150 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022151 ret = FAIL;
22152 break;
22153 }
22154 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022156 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022157 ga.ga_len += len;
22158 }
22159
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022160 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022161 arg = skipwhite(arg);
22162 }
22163
22164 if (ret != FAIL && ga.ga_data != NULL)
22165 {
22166 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022167 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022169 out_flush();
22170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022171 else if (eap->cmdidx == CMD_echoerr)
22172 {
22173 /* We don't want to abort following commands, restore did_emsg. */
22174 save_did_emsg = did_emsg;
22175 EMSG((char_u *)ga.ga_data);
22176 if (!force_abort)
22177 did_emsg = save_did_emsg;
22178 }
22179 else if (eap->cmdidx == CMD_execute)
22180 do_cmdline((char_u *)ga.ga_data,
22181 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22182 }
22183
22184 ga_clear(&ga);
22185
22186 if (eap->skip)
22187 --emsg_skip;
22188
22189 eap->nextcmd = check_nextcmd(arg);
22190}
22191
22192/*
22193 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22194 * "arg" points to the "&" or '+' when called, to "option" when returning.
22195 * Returns NULL when no option name found. Otherwise pointer to the char
22196 * after the option name.
22197 */
22198 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022199find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022200{
22201 char_u *p = *arg;
22202
22203 ++p;
22204 if (*p == 'g' && p[1] == ':')
22205 {
22206 *opt_flags = OPT_GLOBAL;
22207 p += 2;
22208 }
22209 else if (*p == 'l' && p[1] == ':')
22210 {
22211 *opt_flags = OPT_LOCAL;
22212 p += 2;
22213 }
22214 else
22215 *opt_flags = 0;
22216
22217 if (!ASCII_ISALPHA(*p))
22218 return NULL;
22219 *arg = p;
22220
22221 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22222 p += 4; /* termcap option */
22223 else
22224 while (ASCII_ISALPHA(*p))
22225 ++p;
22226 return p;
22227}
22228
22229/*
22230 * ":function"
22231 */
22232 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022233ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022234{
22235 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022236 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022237 int j;
22238 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022239 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022240 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022241 char_u *name = NULL;
22242 char_u *p;
22243 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022244 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022245 garray_T newargs;
22246 garray_T newlines;
22247 int varargs = FALSE;
22248 int mustend = FALSE;
22249 int flags = 0;
22250 ufunc_T *fp;
22251 int indent;
22252 int nesting;
22253 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022254 dictitem_T *v;
22255 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022256 static int func_nr = 0; /* number for nameless function */
22257 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022258 hashtab_T *ht;
22259 int todo;
22260 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022261 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262
22263 /*
22264 * ":function" without argument: list functions.
22265 */
22266 if (ends_excmd(*eap->arg))
22267 {
22268 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022269 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022270 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022271 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022272 {
22273 if (!HASHITEM_EMPTY(hi))
22274 {
22275 --todo;
22276 fp = HI2UF(hi);
22277 if (!isdigit(*fp->uf_name))
22278 list_func_head(fp, FALSE);
22279 }
22280 }
22281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022282 eap->nextcmd = check_nextcmd(eap->arg);
22283 return;
22284 }
22285
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022286 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022287 * ":function /pat": list functions matching pattern.
22288 */
22289 if (*eap->arg == '/')
22290 {
22291 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22292 if (!eap->skip)
22293 {
22294 regmatch_T regmatch;
22295
22296 c = *p;
22297 *p = NUL;
22298 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22299 *p = c;
22300 if (regmatch.regprog != NULL)
22301 {
22302 regmatch.rm_ic = p_ic;
22303
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022304 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022305 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22306 {
22307 if (!HASHITEM_EMPTY(hi))
22308 {
22309 --todo;
22310 fp = HI2UF(hi);
22311 if (!isdigit(*fp->uf_name)
22312 && vim_regexec(&regmatch, fp->uf_name, 0))
22313 list_func_head(fp, FALSE);
22314 }
22315 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022316 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022317 }
22318 }
22319 if (*p == '/')
22320 ++p;
22321 eap->nextcmd = check_nextcmd(p);
22322 return;
22323 }
22324
22325 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022326 * Get the function name. There are these situations:
22327 * func normal function name
22328 * "name" == func, "fudi.fd_dict" == NULL
22329 * dict.func new dictionary entry
22330 * "name" == NULL, "fudi.fd_dict" set,
22331 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22332 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022333 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022334 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22335 * dict.func existing dict entry that's not a Funcref
22336 * "name" == NULL, "fudi.fd_dict" set,
22337 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022338 * s:func script-local function name
22339 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022340 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022341 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022342 name = trans_function_name(&p, eap->skip, 0, &fudi);
22343 paren = (vim_strchr(p, '(') != NULL);
22344 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022345 {
22346 /*
22347 * Return on an invalid expression in braces, unless the expression
22348 * evaluation has been cancelled due to an aborting error, an
22349 * interrupt, or an exception.
22350 */
22351 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022352 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022353 if (!eap->skip && fudi.fd_newkey != NULL)
22354 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022355 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022358 else
22359 eap->skip = TRUE;
22360 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022361
Bram Moolenaar071d4272004-06-13 20:20:40 +000022362 /* An error in a function call during evaluation of an expression in magic
22363 * braces should not cause the function not to be defined. */
22364 saved_did_emsg = did_emsg;
22365 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022366
22367 /*
22368 * ":function func" with only function name: list function.
22369 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022370 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022371 {
22372 if (!ends_excmd(*skipwhite(p)))
22373 {
22374 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022375 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022376 }
22377 eap->nextcmd = check_nextcmd(p);
22378 if (eap->nextcmd != NULL)
22379 *p = NUL;
22380 if (!eap->skip && !got_int)
22381 {
22382 fp = find_func(name);
22383 if (fp != NULL)
22384 {
22385 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022386 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022387 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022388 if (FUNCLINE(fp, j) == NULL)
22389 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022390 msg_putchar('\n');
22391 msg_outnum((long)(j + 1));
22392 if (j < 9)
22393 msg_putchar(' ');
22394 if (j < 99)
22395 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022396 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022397 out_flush(); /* show a line at a time */
22398 ui_breakcheck();
22399 }
22400 if (!got_int)
22401 {
22402 msg_putchar('\n');
22403 msg_puts((char_u *)" endfunction");
22404 }
22405 }
22406 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022407 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022408 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022409 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022410 }
22411
22412 /*
22413 * ":function name(arg1, arg2)" Define function.
22414 */
22415 p = skipwhite(p);
22416 if (*p != '(')
22417 {
22418 if (!eap->skip)
22419 {
22420 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022421 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022422 }
22423 /* attempt to continue by skipping some text */
22424 if (vim_strchr(p, '(') != NULL)
22425 p = vim_strchr(p, '(');
22426 }
22427 p = skipwhite(p + 1);
22428
22429 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22430 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22431
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022432 if (!eap->skip)
22433 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022434 /* Check the name of the function. Unless it's a dictionary function
22435 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022436 if (name != NULL)
22437 arg = name;
22438 else
22439 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022440 if (arg != NULL && (fudi.fd_di == NULL
22441 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022442 {
22443 if (*arg == K_SPECIAL)
22444 j = 3;
22445 else
22446 j = 0;
22447 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22448 : eval_isnamec(arg[j])))
22449 ++j;
22450 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022451 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022452 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022453 /* Disallow using the g: dict. */
22454 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22455 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022456 }
22457
Bram Moolenaar071d4272004-06-13 20:20:40 +000022458 /*
22459 * Isolate the arguments: "arg1, arg2, ...)"
22460 */
22461 while (*p != ')')
22462 {
22463 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22464 {
22465 varargs = TRUE;
22466 p += 3;
22467 mustend = TRUE;
22468 }
22469 else
22470 {
22471 arg = p;
22472 while (ASCII_ISALNUM(*p) || *p == '_')
22473 ++p;
22474 if (arg == p || isdigit(*arg)
22475 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22476 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22477 {
22478 if (!eap->skip)
22479 EMSG2(_("E125: Illegal argument: %s"), arg);
22480 break;
22481 }
22482 if (ga_grow(&newargs, 1) == FAIL)
22483 goto erret;
22484 c = *p;
22485 *p = NUL;
22486 arg = vim_strsave(arg);
22487 if (arg == NULL)
22488 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022489
22490 /* Check for duplicate argument name. */
22491 for (i = 0; i < newargs.ga_len; ++i)
22492 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22493 {
22494 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022495 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022496 goto erret;
22497 }
22498
Bram Moolenaar071d4272004-06-13 20:20:40 +000022499 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22500 *p = c;
22501 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022502 if (*p == ',')
22503 ++p;
22504 else
22505 mustend = TRUE;
22506 }
22507 p = skipwhite(p);
22508 if (mustend && *p != ')')
22509 {
22510 if (!eap->skip)
22511 EMSG2(_(e_invarg2), eap->arg);
22512 break;
22513 }
22514 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022515 if (*p != ')')
22516 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022517 ++p; /* skip the ')' */
22518
Bram Moolenaare9a41262005-01-15 22:18:47 +000022519 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022520 for (;;)
22521 {
22522 p = skipwhite(p);
22523 if (STRNCMP(p, "range", 5) == 0)
22524 {
22525 flags |= FC_RANGE;
22526 p += 5;
22527 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022528 else if (STRNCMP(p, "dict", 4) == 0)
22529 {
22530 flags |= FC_DICT;
22531 p += 4;
22532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022533 else if (STRNCMP(p, "abort", 5) == 0)
22534 {
22535 flags |= FC_ABORT;
22536 p += 5;
22537 }
22538 else
22539 break;
22540 }
22541
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022542 /* When there is a line break use what follows for the function body.
22543 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22544 if (*p == '\n')
22545 line_arg = p + 1;
22546 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022547 EMSG(_(e_trailing));
22548
22549 /*
22550 * Read the body of the function, until ":endfunction" is found.
22551 */
22552 if (KeyTyped)
22553 {
22554 /* Check if the function already exists, don't let the user type the
22555 * whole function before telling him it doesn't work! For a script we
22556 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022557 if (!eap->skip && !eap->forceit)
22558 {
22559 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22560 EMSG(_(e_funcdict));
22561 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022562 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022564
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022565 if (!eap->skip && did_emsg)
22566 goto erret;
22567
Bram Moolenaar071d4272004-06-13 20:20:40 +000022568 msg_putchar('\n'); /* don't overwrite the function name */
22569 cmdline_row = msg_row;
22570 }
22571
22572 indent = 2;
22573 nesting = 0;
22574 for (;;)
22575 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022576 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022577 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022578 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022579 saved_wait_return = FALSE;
22580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022581 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022582 sourcing_lnum_off = sourcing_lnum;
22583
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022584 if (line_arg != NULL)
22585 {
22586 /* Use eap->arg, split up in parts by line breaks. */
22587 theline = line_arg;
22588 p = vim_strchr(theline, '\n');
22589 if (p == NULL)
22590 line_arg += STRLEN(line_arg);
22591 else
22592 {
22593 *p = NUL;
22594 line_arg = p + 1;
22595 }
22596 }
22597 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022598 theline = getcmdline(':', 0L, indent);
22599 else
22600 theline = eap->getline(':', eap->cookie, indent);
22601 if (KeyTyped)
22602 lines_left = Rows - 1;
22603 if (theline == NULL)
22604 {
22605 EMSG(_("E126: Missing :endfunction"));
22606 goto erret;
22607 }
22608
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022609 /* Detect line continuation: sourcing_lnum increased more than one. */
22610 if (sourcing_lnum > sourcing_lnum_off + 1)
22611 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22612 else
22613 sourcing_lnum_off = 0;
22614
Bram Moolenaar071d4272004-06-13 20:20:40 +000022615 if (skip_until != NULL)
22616 {
22617 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22618 * don't check for ":endfunc". */
22619 if (STRCMP(theline, skip_until) == 0)
22620 {
22621 vim_free(skip_until);
22622 skip_until = NULL;
22623 }
22624 }
22625 else
22626 {
22627 /* skip ':' and blanks*/
22628 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22629 ;
22630
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022631 /* Check for "endfunction". */
22632 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022633 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022634 if (line_arg == NULL)
22635 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022636 break;
22637 }
22638
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022639 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022640 * at "end". */
22641 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22642 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022643 else if (STRNCMP(p, "if", 2) == 0
22644 || STRNCMP(p, "wh", 2) == 0
22645 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022646 || STRNCMP(p, "try", 3) == 0)
22647 indent += 2;
22648
22649 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022650 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022651 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022652 if (*p == '!')
22653 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022654 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022655 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22656 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022657 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022658 ++nesting;
22659 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022660 }
22661 }
22662
22663 /* Check for ":append" or ":insert". */
22664 p = skip_range(p, NULL);
22665 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22666 || (p[0] == 'i'
22667 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22668 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22669 skip_until = vim_strsave((char_u *)".");
22670
22671 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22672 arg = skipwhite(skiptowhite(p));
22673 if (arg[0] == '<' && arg[1] =='<'
22674 && ((p[0] == 'p' && p[1] == 'y'
22675 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22676 || (p[0] == 'p' && p[1] == 'e'
22677 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22678 || (p[0] == 't' && p[1] == 'c'
22679 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022680 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22681 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022682 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22683 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022684 || (p[0] == 'm' && p[1] == 'z'
22685 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686 ))
22687 {
22688 /* ":python <<" continues until a dot, like ":append" */
22689 p = skipwhite(arg + 2);
22690 if (*p == NUL)
22691 skip_until = vim_strsave((char_u *)".");
22692 else
22693 skip_until = vim_strsave(p);
22694 }
22695 }
22696
22697 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022698 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022699 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022700 if (line_arg == NULL)
22701 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022702 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022703 }
22704
22705 /* Copy the line to newly allocated memory. get_one_sourceline()
22706 * allocates 250 bytes per line, this saves 80% on average. The cost
22707 * is an extra alloc/free. */
22708 p = vim_strsave(theline);
22709 if (p != NULL)
22710 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022711 if (line_arg == NULL)
22712 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022713 theline = p;
22714 }
22715
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022716 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22717
22718 /* Add NULL lines for continuation lines, so that the line count is
22719 * equal to the index in the growarray. */
22720 while (sourcing_lnum_off-- > 0)
22721 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022722
22723 /* Check for end of eap->arg. */
22724 if (line_arg != NULL && *line_arg == NUL)
22725 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726 }
22727
22728 /* Don't define the function when skipping commands or when an error was
22729 * detected. */
22730 if (eap->skip || did_emsg)
22731 goto erret;
22732
22733 /*
22734 * If there are no errors, add the function
22735 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022736 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022737 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022738 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022739 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022740 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022741 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022742 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022743 goto erret;
22744 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022745
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022746 fp = find_func(name);
22747 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022748 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022749 if (!eap->forceit)
22750 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022751 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022752 goto erret;
22753 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022754 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022755 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022756 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022757 name);
22758 goto erret;
22759 }
22760 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022761 ga_clear_strings(&(fp->uf_args));
22762 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022763 vim_free(name);
22764 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022766 }
22767 else
22768 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022769 char numbuf[20];
22770
22771 fp = NULL;
22772 if (fudi.fd_newkey == NULL && !eap->forceit)
22773 {
22774 EMSG(_(e_funcdict));
22775 goto erret;
22776 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022777 if (fudi.fd_di == NULL)
22778 {
22779 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022780 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022781 goto erret;
22782 }
22783 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022784 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022785 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022786
22787 /* Give the function a sequential number. Can only be used with a
22788 * Funcref! */
22789 vim_free(name);
22790 sprintf(numbuf, "%d", ++func_nr);
22791 name = vim_strsave((char_u *)numbuf);
22792 if (name == NULL)
22793 goto erret;
22794 }
22795
22796 if (fp == NULL)
22797 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022798 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022799 {
22800 int slen, plen;
22801 char_u *scriptname;
22802
22803 /* Check that the autoload name matches the script name. */
22804 j = FAIL;
22805 if (sourcing_name != NULL)
22806 {
22807 scriptname = autoload_name(name);
22808 if (scriptname != NULL)
22809 {
22810 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022811 plen = (int)STRLEN(p);
22812 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022813 if (slen > plen && fnamecmp(p,
22814 sourcing_name + slen - plen) == 0)
22815 j = OK;
22816 vim_free(scriptname);
22817 }
22818 }
22819 if (j == FAIL)
22820 {
22821 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22822 goto erret;
22823 }
22824 }
22825
22826 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022827 if (fp == NULL)
22828 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022829
22830 if (fudi.fd_dict != NULL)
22831 {
22832 if (fudi.fd_di == NULL)
22833 {
22834 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022835 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022836 if (fudi.fd_di == NULL)
22837 {
22838 vim_free(fp);
22839 goto erret;
22840 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022841 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22842 {
22843 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022844 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022845 goto erret;
22846 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022847 }
22848 else
22849 /* overwrite existing dict entry */
22850 clear_tv(&fudi.fd_di->di_tv);
22851 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022852 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022853 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022854 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022855
22856 /* behave like "dict" was used */
22857 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022858 }
22859
Bram Moolenaar071d4272004-06-13 20:20:40 +000022860 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022861 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010022862 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
22863 {
22864 vim_free(fp);
22865 goto erret;
22866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022867 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022868 fp->uf_args = newargs;
22869 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022870#ifdef FEAT_PROFILE
22871 fp->uf_tml_count = NULL;
22872 fp->uf_tml_total = NULL;
22873 fp->uf_tml_self = NULL;
22874 fp->uf_profiling = FALSE;
22875 if (prof_def_func())
22876 func_do_profile(fp);
22877#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022878 fp->uf_varargs = varargs;
22879 fp->uf_flags = flags;
22880 fp->uf_calls = 0;
22881 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022882 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022883
22884erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022885 ga_clear_strings(&newargs);
22886 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022887ret_free:
22888 vim_free(skip_until);
22889 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022890 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022891 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022892 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893}
22894
22895/*
22896 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022897 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022898 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022899 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010022900 * TFN_INT: internal function name OK
22901 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022902 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903 * Advances "pp" to just after the function name (if no error).
22904 */
22905 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022906trans_function_name(
22907 char_u **pp,
22908 int skip, /* only find the end, don't evaluate */
22909 int flags,
22910 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022911{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022912 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022913 char_u *start;
22914 char_u *end;
22915 int lead;
22916 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022917 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022918 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022919
22920 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022921 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022922 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022923
22924 /* Check for hard coded <SNR>: already translated function ID (from a user
22925 * command). */
22926 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22927 && (*pp)[2] == (int)KE_SNR)
22928 {
22929 *pp += 3;
22930 len = get_id_len(pp) + 3;
22931 return vim_strnsave(start, len);
22932 }
22933
22934 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22935 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022936 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022937 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022938 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022939
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022940 /* Note that TFN_ flags use the same values as GLV_ flags. */
22941 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022942 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022943 if (end == start)
22944 {
22945 if (!skip)
22946 EMSG(_("E129: Function name required"));
22947 goto theend;
22948 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022949 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022950 {
22951 /*
22952 * Report an invalid expression in braces, unless the expression
22953 * evaluation has been cancelled due to an aborting error, an
22954 * interrupt, or an exception.
22955 */
22956 if (!aborting())
22957 {
22958 if (end != NULL)
22959 EMSG2(_(e_invarg2), start);
22960 }
22961 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022962 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022963 goto theend;
22964 }
22965
22966 if (lv.ll_tv != NULL)
22967 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022968 if (fdp != NULL)
22969 {
22970 fdp->fd_dict = lv.ll_dict;
22971 fdp->fd_newkey = lv.ll_newkey;
22972 lv.ll_newkey = NULL;
22973 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022974 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022975 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22976 {
22977 name = vim_strsave(lv.ll_tv->vval.v_string);
22978 *pp = end;
22979 }
22980 else
22981 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022982 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22983 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022984 EMSG(_(e_funcref));
22985 else
22986 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022987 name = NULL;
22988 }
22989 goto theend;
22990 }
22991
22992 if (lv.ll_name == NULL)
22993 {
22994 /* Error found, but continue after the function name. */
22995 *pp = end;
22996 goto theend;
22997 }
22998
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022999 /* Check if the name is a Funcref. If so, use the value. */
23000 if (lv.ll_exp_name != NULL)
23001 {
23002 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023003 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023004 if (name == lv.ll_exp_name)
23005 name = NULL;
23006 }
23007 else
23008 {
23009 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023010 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023011 if (name == *pp)
23012 name = NULL;
23013 }
23014 if (name != NULL)
23015 {
23016 name = vim_strsave(name);
23017 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023018 if (STRNCMP(name, "<SNR>", 5) == 0)
23019 {
23020 /* Change "<SNR>" to the byte sequence. */
23021 name[0] = K_SPECIAL;
23022 name[1] = KS_EXTRA;
23023 name[2] = (int)KE_SNR;
23024 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23025 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023026 goto theend;
23027 }
23028
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023029 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023030 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023031 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023032 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23033 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23034 {
23035 /* When there was "s:" already or the name expanded to get a
23036 * leading "s:" then remove it. */
23037 lv.ll_name += 2;
23038 len -= 2;
23039 lead = 2;
23040 }
23041 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023042 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023043 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023044 /* skip over "s:" and "g:" */
23045 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023046 lv.ll_name += 2;
23047 len = (int)(end - lv.ll_name);
23048 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023049
23050 /*
23051 * Copy the function name to allocated memory.
23052 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23053 * Accept <SNR>123_name() outside a script.
23054 */
23055 if (skip)
23056 lead = 0; /* do nothing */
23057 else if (lead > 0)
23058 {
23059 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023060 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23061 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023062 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023063 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023064 if (current_SID <= 0)
23065 {
23066 EMSG(_(e_usingsid));
23067 goto theend;
23068 }
23069 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23070 lead += (int)STRLEN(sid_buf);
23071 }
23072 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023073 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023074 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023075 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023076 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023077 goto theend;
23078 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023079 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023080 {
23081 char_u *cp = vim_strchr(lv.ll_name, ':');
23082
23083 if (cp != NULL && cp < end)
23084 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023085 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023086 goto theend;
23087 }
23088 }
23089
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023090 name = alloc((unsigned)(len + lead + 1));
23091 if (name != NULL)
23092 {
23093 if (lead > 0)
23094 {
23095 name[0] = K_SPECIAL;
23096 name[1] = KS_EXTRA;
23097 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023098 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023099 STRCPY(name + 3, sid_buf);
23100 }
23101 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023102 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023103 }
23104 *pp = end;
23105
23106theend:
23107 clear_lval(&lv);
23108 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023109}
23110
23111/*
23112 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23113 * Return 2 if "p" starts with "s:".
23114 * Return 0 otherwise.
23115 */
23116 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023117eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023118{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023119 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23120 * the standard library function. */
23121 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23122 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023123 return 5;
23124 if (p[0] == 's' && p[1] == ':')
23125 return 2;
23126 return 0;
23127}
23128
23129/*
23130 * Return TRUE if "p" starts with "<SID>" or "s:".
23131 * Only works if eval_fname_script() returned non-zero for "p"!
23132 */
23133 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023134eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023135{
23136 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23137}
23138
23139/*
23140 * List the head of the function: "name(arg1, arg2)".
23141 */
23142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023143list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023144{
23145 int j;
23146
23147 msg_start();
23148 if (indent)
23149 MSG_PUTS(" ");
23150 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023151 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023152 {
23153 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023154 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023155 }
23156 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023157 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023158 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023159 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023160 {
23161 if (j)
23162 MSG_PUTS(", ");
23163 msg_puts(FUNCARG(fp, j));
23164 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023165 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023166 {
23167 if (j)
23168 MSG_PUTS(", ");
23169 MSG_PUTS("...");
23170 }
23171 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023172 if (fp->uf_flags & FC_ABORT)
23173 MSG_PUTS(" abort");
23174 if (fp->uf_flags & FC_RANGE)
23175 MSG_PUTS(" range");
23176 if (fp->uf_flags & FC_DICT)
23177 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023178 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023179 if (p_verbose > 0)
23180 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023181}
23182
23183/*
23184 * Find a function by name, return pointer to it in ufuncs.
23185 * Return NULL for unknown function.
23186 */
23187 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023188find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023190 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023191
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023192 hi = hash_find(&func_hashtab, name);
23193 if (!HASHITEM_EMPTY(hi))
23194 return HI2UF(hi);
23195 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023196}
23197
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023198#if defined(EXITFREE) || defined(PROTO)
23199 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023200free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023201{
23202 hashitem_T *hi;
23203
23204 /* Need to start all over every time, because func_free() may change the
23205 * hash table. */
23206 while (func_hashtab.ht_used > 0)
23207 for (hi = func_hashtab.ht_array; ; ++hi)
23208 if (!HASHITEM_EMPTY(hi))
23209 {
23210 func_free(HI2UF(hi));
23211 break;
23212 }
23213}
23214#endif
23215
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023216 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023217translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023218{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023219 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023220 return find_internal_func(name) >= 0;
23221 return find_func(name) != NULL;
23222}
23223
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023224/*
23225 * Return TRUE if a function "name" exists.
23226 */
23227 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023228function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023229{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023230 char_u *nm = name;
23231 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023232 int n = FALSE;
23233
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023234 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23235 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023236 nm = skipwhite(nm);
23237
23238 /* Only accept "funcname", "funcname ", "funcname (..." and
23239 * "funcname(...", not "funcname!...". */
23240 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023241 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023242 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023243 return n;
23244}
23245
Bram Moolenaara1544c02013-05-30 12:35:52 +020023246 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023247get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023248{
23249 char_u *nm = name;
23250 char_u *p;
23251
23252 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23253
23254 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023255 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023256 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023257
Bram Moolenaara1544c02013-05-30 12:35:52 +020023258 vim_free(p);
23259 return NULL;
23260}
23261
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023262/*
23263 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023264 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23265 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023266 */
23267 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023268builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023269{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023270 char_u *p;
23271
23272 if (!ASCII_ISLOWER(name[0]))
23273 return FALSE;
23274 p = vim_strchr(name, AUTOLOAD_CHAR);
23275 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023276}
23277
Bram Moolenaar05159a02005-02-26 23:04:13 +000023278#if defined(FEAT_PROFILE) || defined(PROTO)
23279/*
23280 * Start profiling function "fp".
23281 */
23282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023283func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023284{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023285 int len = fp->uf_lines.ga_len;
23286
23287 if (len == 0)
23288 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023289 fp->uf_tm_count = 0;
23290 profile_zero(&fp->uf_tm_self);
23291 profile_zero(&fp->uf_tm_total);
23292 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023293 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023294 if (fp->uf_tml_total == NULL)
23295 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023296 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023297 if (fp->uf_tml_self == NULL)
23298 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023299 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023300 fp->uf_tml_idx = -1;
23301 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23302 || fp->uf_tml_self == NULL)
23303 return; /* out of memory */
23304
23305 fp->uf_profiling = TRUE;
23306}
23307
23308/*
23309 * Dump the profiling results for all functions in file "fd".
23310 */
23311 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023312func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023313{
23314 hashitem_T *hi;
23315 int todo;
23316 ufunc_T *fp;
23317 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023318 ufunc_T **sorttab;
23319 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023320
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023321 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023322 if (todo == 0)
23323 return; /* nothing to dump */
23324
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023325 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023326
Bram Moolenaar05159a02005-02-26 23:04:13 +000023327 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23328 {
23329 if (!HASHITEM_EMPTY(hi))
23330 {
23331 --todo;
23332 fp = HI2UF(hi);
23333 if (fp->uf_profiling)
23334 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023335 if (sorttab != NULL)
23336 sorttab[st_len++] = fp;
23337
Bram Moolenaar05159a02005-02-26 23:04:13 +000023338 if (fp->uf_name[0] == K_SPECIAL)
23339 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23340 else
23341 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23342 if (fp->uf_tm_count == 1)
23343 fprintf(fd, "Called 1 time\n");
23344 else
23345 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23346 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23347 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23348 fprintf(fd, "\n");
23349 fprintf(fd, "count total (s) self (s)\n");
23350
23351 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23352 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023353 if (FUNCLINE(fp, i) == NULL)
23354 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023355 prof_func_line(fd, fp->uf_tml_count[i],
23356 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023357 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23358 }
23359 fprintf(fd, "\n");
23360 }
23361 }
23362 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023363
23364 if (sorttab != NULL && st_len > 0)
23365 {
23366 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23367 prof_total_cmp);
23368 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23369 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23370 prof_self_cmp);
23371 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23372 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023373
23374 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023375}
Bram Moolenaar73830342005-02-28 22:48:19 +000023376
23377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023378prof_sort_list(
23379 FILE *fd,
23380 ufunc_T **sorttab,
23381 int st_len,
23382 char *title,
23383 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023384{
23385 int i;
23386 ufunc_T *fp;
23387
23388 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23389 fprintf(fd, "count total (s) self (s) function\n");
23390 for (i = 0; i < 20 && i < st_len; ++i)
23391 {
23392 fp = sorttab[i];
23393 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23394 prefer_self);
23395 if (fp->uf_name[0] == K_SPECIAL)
23396 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23397 else
23398 fprintf(fd, " %s()\n", fp->uf_name);
23399 }
23400 fprintf(fd, "\n");
23401}
23402
23403/*
23404 * Print the count and times for one function or function line.
23405 */
23406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023407prof_func_line(
23408 FILE *fd,
23409 int count,
23410 proftime_T *total,
23411 proftime_T *self,
23412 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023413{
23414 if (count > 0)
23415 {
23416 fprintf(fd, "%5d ", count);
23417 if (prefer_self && profile_equal(total, self))
23418 fprintf(fd, " ");
23419 else
23420 fprintf(fd, "%s ", profile_msg(total));
23421 if (!prefer_self && profile_equal(total, self))
23422 fprintf(fd, " ");
23423 else
23424 fprintf(fd, "%s ", profile_msg(self));
23425 }
23426 else
23427 fprintf(fd, " ");
23428}
23429
23430/*
23431 * Compare function for total time sorting.
23432 */
23433 static int
23434#ifdef __BORLANDC__
23435_RTLENTRYF
23436#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023437prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023438{
23439 ufunc_T *p1, *p2;
23440
23441 p1 = *(ufunc_T **)s1;
23442 p2 = *(ufunc_T **)s2;
23443 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23444}
23445
23446/*
23447 * Compare function for self time sorting.
23448 */
23449 static int
23450#ifdef __BORLANDC__
23451_RTLENTRYF
23452#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023453prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023454{
23455 ufunc_T *p1, *p2;
23456
23457 p1 = *(ufunc_T **)s1;
23458 p2 = *(ufunc_T **)s2;
23459 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23460}
23461
Bram Moolenaar05159a02005-02-26 23:04:13 +000023462#endif
23463
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023464/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023465 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023466 * Return TRUE if a package was loaded.
23467 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023468 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023469script_autoload(
23470 char_u *name,
23471 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023472{
23473 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023474 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023475 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023476 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023477
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023478 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023479 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023480 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023481 return FALSE;
23482
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023483 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023484
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023485 /* Find the name in the list of previously loaded package names. Skip
23486 * "autoload/", it's always the same. */
23487 for (i = 0; i < ga_loaded.ga_len; ++i)
23488 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23489 break;
23490 if (!reload && i < ga_loaded.ga_len)
23491 ret = FALSE; /* was loaded already */
23492 else
23493 {
23494 /* Remember the name if it wasn't loaded already. */
23495 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23496 {
23497 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23498 tofree = NULL;
23499 }
23500
23501 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023502 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023503 ret = TRUE;
23504 }
23505
23506 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023507 return ret;
23508}
23509
23510/*
23511 * Return the autoload script name for a function or variable name.
23512 * Returns NULL when out of memory.
23513 */
23514 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023515autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023516{
23517 char_u *p;
23518 char_u *scriptname;
23519
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023520 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023521 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23522 if (scriptname == NULL)
23523 return FALSE;
23524 STRCPY(scriptname, "autoload/");
23525 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023526 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023527 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023528 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023529 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023530 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023531}
23532
Bram Moolenaar071d4272004-06-13 20:20:40 +000023533#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23534
23535/*
23536 * Function given to ExpandGeneric() to obtain the list of user defined
23537 * function names.
23538 */
23539 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023540get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023541{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023542 static long_u done;
23543 static hashitem_T *hi;
23544 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023545
23546 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023547 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023548 done = 0;
23549 hi = func_hashtab.ht_array;
23550 }
23551 if (done < func_hashtab.ht_used)
23552 {
23553 if (done++ > 0)
23554 ++hi;
23555 while (HASHITEM_EMPTY(hi))
23556 ++hi;
23557 fp = HI2UF(hi);
23558
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023559 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023560 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023561
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023562 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23563 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023564
23565 cat_func_name(IObuff, fp);
23566 if (xp->xp_context != EXPAND_USER_FUNC)
23567 {
23568 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023569 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023570 STRCAT(IObuff, ")");
23571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023572 return IObuff;
23573 }
23574 return NULL;
23575}
23576
23577#endif /* FEAT_CMDL_COMPL */
23578
23579/*
23580 * Copy the function name of "fp" to buffer "buf".
23581 * "buf" must be able to hold the function name plus three bytes.
23582 * Takes care of script-local function names.
23583 */
23584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023585cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023586{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023587 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023588 {
23589 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023590 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023591 }
23592 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023593 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023594}
23595
23596/*
23597 * ":delfunction {name}"
23598 */
23599 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023600ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023601{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023602 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023603 char_u *p;
23604 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023605 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023606
23607 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023608 name = trans_function_name(&p, eap->skip, 0, &fudi);
23609 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023610 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023611 {
23612 if (fudi.fd_dict != NULL && !eap->skip)
23613 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023614 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023616 if (!ends_excmd(*skipwhite(p)))
23617 {
23618 vim_free(name);
23619 EMSG(_(e_trailing));
23620 return;
23621 }
23622 eap->nextcmd = check_nextcmd(p);
23623 if (eap->nextcmd != NULL)
23624 *p = NUL;
23625
23626 if (!eap->skip)
23627 fp = find_func(name);
23628 vim_free(name);
23629
23630 if (!eap->skip)
23631 {
23632 if (fp == NULL)
23633 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023634 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023635 return;
23636 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023637 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023638 {
23639 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23640 return;
23641 }
23642
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023643 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023644 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023645 /* Delete the dict item that refers to the function, it will
23646 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023647 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023648 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023649 else
23650 func_free(fp);
23651 }
23652}
23653
23654/*
23655 * Free a function and remove it from the list of functions.
23656 */
23657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023658func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023659{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023660 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023661
23662 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023663 ga_clear_strings(&(fp->uf_args));
23664 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023665#ifdef FEAT_PROFILE
23666 vim_free(fp->uf_tml_count);
23667 vim_free(fp->uf_tml_total);
23668 vim_free(fp->uf_tml_self);
23669#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023670
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023671 /* remove the function from the function hashtable */
23672 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23673 if (HASHITEM_EMPTY(hi))
23674 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023675 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023676 hash_remove(&func_hashtab, hi);
23677
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023678 vim_free(fp);
23679}
23680
23681/*
23682 * Unreference a Function: decrement the reference count and free it when it
23683 * becomes zero. Only for numbered functions.
23684 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023685 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023686func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023687{
23688 ufunc_T *fp;
23689
23690 if (name != NULL && isdigit(*name))
23691 {
23692 fp = find_func(name);
23693 if (fp == NULL)
23694 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023695 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023696 {
23697 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023698 * when "uf_calls" becomes zero. */
23699 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023700 func_free(fp);
23701 }
23702 }
23703}
23704
23705/*
23706 * Count a reference to a Function.
23707 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023708 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023709func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023710{
23711 ufunc_T *fp;
23712
23713 if (name != NULL && isdigit(*name))
23714 {
23715 fp = find_func(name);
23716 if (fp == NULL)
23717 EMSG2(_(e_intern2), "func_ref()");
23718 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023719 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023720 }
23721}
23722
23723/*
23724 * Call a user function.
23725 */
23726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023727call_user_func(
23728 ufunc_T *fp, /* pointer to function */
23729 int argcount, /* nr of args */
23730 typval_T *argvars, /* arguments */
23731 typval_T *rettv, /* return value */
23732 linenr_T firstline, /* first line of range */
23733 linenr_T lastline, /* last line of range */
23734 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023735{
Bram Moolenaar33570922005-01-25 22:26:29 +000023736 char_u *save_sourcing_name;
23737 linenr_T save_sourcing_lnum;
23738 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023739 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023740 int save_did_emsg;
23741 static int depth = 0;
23742 dictitem_T *v;
23743 int fixvar_idx = 0; /* index in fixvar[] */
23744 int i;
23745 int ai;
23746 char_u numbuf[NUMBUFLEN];
23747 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023748 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023749#ifdef FEAT_PROFILE
23750 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023751 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023753
23754 /* If depth of calling is getting too high, don't execute the function */
23755 if (depth >= p_mfd)
23756 {
23757 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023758 rettv->v_type = VAR_NUMBER;
23759 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023760 return;
23761 }
23762 ++depth;
23763
23764 line_breakcheck(); /* check for CTRL-C hit */
23765
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023766 fc = (funccall_T *)alloc(sizeof(funccall_T));
23767 fc->caller = current_funccal;
23768 current_funccal = fc;
23769 fc->func = fp;
23770 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023771 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023772 fc->linenr = 0;
23773 fc->returned = FALSE;
23774 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023775 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023776 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23777 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023778
Bram Moolenaar33570922005-01-25 22:26:29 +000023779 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023780 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023781 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23782 * each argument variable and saves a lot of time.
23783 */
23784 /*
23785 * Init l: variables.
23786 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023787 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023788 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023789 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023790 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23791 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023792 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023793 name = v->di_key;
23794 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023795 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023796 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023797 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023798 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023799 v->di_tv.vval.v_dict = selfdict;
23800 ++selfdict->dv_refcount;
23801 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023802
Bram Moolenaar33570922005-01-25 22:26:29 +000023803 /*
23804 * Init a: variables.
23805 * Set a:0 to "argcount".
23806 * Set a:000 to a list with room for the "..." arguments.
23807 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023808 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023809 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023810 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023811 /* Use "name" to avoid a warning from some compiler that checks the
23812 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023813 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023814 name = v->di_key;
23815 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023816 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023817 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023818 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023819 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023820 v->di_tv.vval.v_list = &fc->l_varlist;
23821 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23822 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23823 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023824
23825 /*
23826 * Set a:firstline to "firstline" and a:lastline to "lastline".
23827 * Set a:name to named arguments.
23828 * Set a:N to the "..." arguments.
23829 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023830 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023831 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023832 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023833 (varnumber_T)lastline);
23834 for (i = 0; i < argcount; ++i)
23835 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023836 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023837 if (ai < 0)
23838 /* named argument a:name */
23839 name = FUNCARG(fp, i);
23840 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023841 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023842 /* "..." argument a:1, a:2, etc. */
23843 sprintf((char *)numbuf, "%d", ai + 1);
23844 name = numbuf;
23845 }
23846 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23847 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023848 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023849 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23850 }
23851 else
23852 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023853 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23854 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023855 if (v == NULL)
23856 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023857 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000023858 }
23859 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023860 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023861
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023862 /* Note: the values are copied directly to avoid alloc/free.
23863 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023864 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023865 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023866
23867 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23868 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023869 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23870 fc->l_listitems[ai].li_tv = argvars[i];
23871 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023872 }
23873 }
23874
Bram Moolenaar071d4272004-06-13 20:20:40 +000023875 /* Don't redraw while executing the function. */
23876 ++RedrawingDisabled;
23877 save_sourcing_name = sourcing_name;
23878 save_sourcing_lnum = sourcing_lnum;
23879 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023880 /* need space for function name + ("function " + 3) or "[number]" */
23881 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
23882 + STRLEN(fp->uf_name) + 20;
23883 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023884 if (sourcing_name != NULL)
23885 {
23886 if (save_sourcing_name != NULL
23887 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023888 sprintf((char *)sourcing_name, "%s[%d]..",
23889 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023890 else
23891 STRCPY(sourcing_name, "function ");
23892 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23893
23894 if (p_verbose >= 12)
23895 {
23896 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023897 verbose_enter_scroll();
23898
Bram Moolenaar555b2802005-05-19 21:08:39 +000023899 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023900 if (p_verbose >= 14)
23901 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023902 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023903 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023904 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023905 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023906
23907 msg_puts((char_u *)"(");
23908 for (i = 0; i < argcount; ++i)
23909 {
23910 if (i > 0)
23911 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023912 if (argvars[i].v_type == VAR_NUMBER)
23913 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023914 else
23915 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023916 /* Do not want errors such as E724 here. */
23917 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023918 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023919 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023920 if (s != NULL)
23921 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023922 if (vim_strsize(s) > MSG_BUF_CLEN)
23923 {
23924 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23925 s = buf;
23926 }
23927 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023928 vim_free(tofree);
23929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023930 }
23931 }
23932 msg_puts((char_u *)")");
23933 }
23934 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023935
23936 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023937 --no_wait_return;
23938 }
23939 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023940#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023941 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023942 {
23943 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23944 func_do_profile(fp);
23945 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023946 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023947 {
23948 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023949 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023950 profile_zero(&fp->uf_tm_children);
23951 }
23952 script_prof_save(&wait_start);
23953 }
23954#endif
23955
Bram Moolenaar071d4272004-06-13 20:20:40 +000023956 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023957 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023958 save_did_emsg = did_emsg;
23959 did_emsg = FALSE;
23960
23961 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023962 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023963 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23964
23965 --RedrawingDisabled;
23966
23967 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023968 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023969 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023970 clear_tv(rettv);
23971 rettv->v_type = VAR_NUMBER;
23972 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023973 }
23974
Bram Moolenaar05159a02005-02-26 23:04:13 +000023975#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023976 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023977 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023978 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023979 profile_end(&call_start);
23980 profile_sub_wait(&wait_start, &call_start);
23981 profile_add(&fp->uf_tm_total, &call_start);
23982 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023983 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023984 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023985 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23986 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023987 }
23988 }
23989#endif
23990
Bram Moolenaar071d4272004-06-13 20:20:40 +000023991 /* when being verbose, mention the return value */
23992 if (p_verbose >= 12)
23993 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023994 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023995 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023996
Bram Moolenaar071d4272004-06-13 20:20:40 +000023997 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023998 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023999 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024000 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024001 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024002 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024003 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024004 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024005 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024006 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024007 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024008
Bram Moolenaar555b2802005-05-19 21:08:39 +000024009 /* The value may be very long. Skip the middle part, so that we
24010 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024011 * truncate it at the end. Don't want errors such as E724 here. */
24012 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024013 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024014 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024015 if (s != NULL)
24016 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024017 if (vim_strsize(s) > MSG_BUF_CLEN)
24018 {
24019 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24020 s = buf;
24021 }
24022 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024023 vim_free(tofree);
24024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024025 }
24026 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024027
24028 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024029 --no_wait_return;
24030 }
24031
24032 vim_free(sourcing_name);
24033 sourcing_name = save_sourcing_name;
24034 sourcing_lnum = save_sourcing_lnum;
24035 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024036#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024037 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024038 script_prof_restore(&wait_start);
24039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024040
24041 if (p_verbose >= 12 && sourcing_name != NULL)
24042 {
24043 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024044 verbose_enter_scroll();
24045
Bram Moolenaar555b2802005-05-19 21:08:39 +000024046 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024047 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024048
24049 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024050 --no_wait_return;
24051 }
24052
24053 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024054 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024055 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024056
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024057 /* If the a:000 list and the l: and a: dicts are not referenced we can
24058 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024059 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24060 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24061 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24062 {
24063 free_funccal(fc, FALSE);
24064 }
24065 else
24066 {
24067 hashitem_T *hi;
24068 listitem_T *li;
24069 int todo;
24070
24071 /* "fc" is still in use. This can happen when returning "a:000" or
24072 * assigning "l:" to a global variable.
24073 * Link "fc" in the list for garbage collection later. */
24074 fc->caller = previous_funccal;
24075 previous_funccal = fc;
24076
24077 /* Make a copy of the a: variables, since we didn't do that above. */
24078 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24079 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24080 {
24081 if (!HASHITEM_EMPTY(hi))
24082 {
24083 --todo;
24084 v = HI2DI(hi);
24085 copy_tv(&v->di_tv, &v->di_tv);
24086 }
24087 }
24088
24089 /* Make a copy of the a:000 items, since we didn't do that above. */
24090 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24091 copy_tv(&li->li_tv, &li->li_tv);
24092 }
24093}
24094
24095/*
24096 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024097 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024098 */
24099 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024100can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024101{
24102 return (fc->l_varlist.lv_copyID != copyID
24103 && fc->l_vars.dv_copyID != copyID
24104 && fc->l_avars.dv_copyID != copyID);
24105}
24106
24107/*
24108 * Free "fc" and what it contains.
24109 */
24110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024111free_funccal(
24112 funccall_T *fc,
24113 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024114{
24115 listitem_T *li;
24116
24117 /* The a: variables typevals may not have been allocated, only free the
24118 * allocated variables. */
24119 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24120
24121 /* free all l: variables */
24122 vars_clear(&fc->l_vars.dv_hashtab);
24123
24124 /* Free the a:000 variables if they were allocated. */
24125 if (free_val)
24126 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24127 clear_tv(&li->li_tv);
24128
24129 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024130}
24131
24132/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024133 * Add a number variable "name" to dict "dp" with value "nr".
24134 */
24135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024136add_nr_var(
24137 dict_T *dp,
24138 dictitem_T *v,
24139 char *name,
24140 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024141{
24142 STRCPY(v->di_key, name);
24143 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24144 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24145 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024146 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024147 v->di_tv.vval.v_number = nr;
24148}
24149
24150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024151 * ":return [expr]"
24152 */
24153 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024154ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024155{
24156 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024157 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024158 int returning = FALSE;
24159
24160 if (current_funccal == NULL)
24161 {
24162 EMSG(_("E133: :return not inside a function"));
24163 return;
24164 }
24165
24166 if (eap->skip)
24167 ++emsg_skip;
24168
24169 eap->nextcmd = NULL;
24170 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024171 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024172 {
24173 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024174 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024175 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024176 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024177 }
24178 /* It's safer to return also on error. */
24179 else if (!eap->skip)
24180 {
24181 /*
24182 * Return unless the expression evaluation has been cancelled due to an
24183 * aborting error, an interrupt, or an exception.
24184 */
24185 if (!aborting())
24186 returning = do_return(eap, FALSE, TRUE, NULL);
24187 }
24188
24189 /* When skipping or the return gets pending, advance to the next command
24190 * in this line (!returning). Otherwise, ignore the rest of the line.
24191 * Following lines will be ignored by get_func_line(). */
24192 if (returning)
24193 eap->nextcmd = NULL;
24194 else if (eap->nextcmd == NULL) /* no argument */
24195 eap->nextcmd = check_nextcmd(arg);
24196
24197 if (eap->skip)
24198 --emsg_skip;
24199}
24200
24201/*
24202 * Return from a function. Possibly makes the return pending. Also called
24203 * for a pending return at the ":endtry" or after returning from an extra
24204 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024205 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024206 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024207 * FALSE when the return gets pending.
24208 */
24209 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024210do_return(
24211 exarg_T *eap,
24212 int reanimate,
24213 int is_cmd,
24214 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024215{
24216 int idx;
24217 struct condstack *cstack = eap->cstack;
24218
24219 if (reanimate)
24220 /* Undo the return. */
24221 current_funccal->returned = FALSE;
24222
24223 /*
24224 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24225 * not in its finally clause (which then is to be executed next) is found.
24226 * In this case, make the ":return" pending for execution at the ":endtry".
24227 * Otherwise, return normally.
24228 */
24229 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24230 if (idx >= 0)
24231 {
24232 cstack->cs_pending[idx] = CSTP_RETURN;
24233
24234 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024235 /* A pending return again gets pending. "rettv" points to an
24236 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024237 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024238 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024239 else
24240 {
24241 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024242 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024243 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024244 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024245
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024246 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024247 {
24248 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024249 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024250 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024251 else
24252 EMSG(_(e_outofmem));
24253 }
24254 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024255 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024256
24257 if (reanimate)
24258 {
24259 /* The pending return value could be overwritten by a ":return"
24260 * without argument in a finally clause; reset the default
24261 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024262 current_funccal->rettv->v_type = VAR_NUMBER;
24263 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024264 }
24265 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024266 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024267 }
24268 else
24269 {
24270 current_funccal->returned = TRUE;
24271
24272 /* If the return is carried out now, store the return value. For
24273 * a return immediately after reanimation, the value is already
24274 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024275 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024276 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024277 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024278 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024279 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024280 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024281 }
24282 }
24283
24284 return idx < 0;
24285}
24286
24287/*
24288 * Free the variable with a pending return value.
24289 */
24290 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024291discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024292{
Bram Moolenaar33570922005-01-25 22:26:29 +000024293 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024294}
24295
24296/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024297 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024298 * is an allocated string. Used by report_pending() for verbose messages.
24299 */
24300 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024301get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024302{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024303 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024304 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024305 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024306
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024307 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024308 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024309 if (s == NULL)
24310 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024311
24312 STRCPY(IObuff, ":return ");
24313 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24314 if (STRLEN(s) + 8 >= IOSIZE)
24315 STRCPY(IObuff + IOSIZE - 4, "...");
24316 vim_free(tofree);
24317 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024318}
24319
24320/*
24321 * Get next function line.
24322 * Called by do_cmdline() to get the next line.
24323 * Returns allocated string, or NULL for end of function.
24324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024325 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024326get_func_line(
24327 int c UNUSED,
24328 void *cookie,
24329 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024330{
Bram Moolenaar33570922005-01-25 22:26:29 +000024331 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024332 ufunc_T *fp = fcp->func;
24333 char_u *retval;
24334 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024335
24336 /* If breakpoints have been added/deleted need to check for it. */
24337 if (fcp->dbg_tick != debug_tick)
24338 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024339 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024340 sourcing_lnum);
24341 fcp->dbg_tick = debug_tick;
24342 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024343#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024344 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024345 func_line_end(cookie);
24346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024347
Bram Moolenaar05159a02005-02-26 23:04:13 +000024348 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024349 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24350 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024351 retval = NULL;
24352 else
24353 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024354 /* Skip NULL lines (continuation lines). */
24355 while (fcp->linenr < gap->ga_len
24356 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24357 ++fcp->linenr;
24358 if (fcp->linenr >= gap->ga_len)
24359 retval = NULL;
24360 else
24361 {
24362 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24363 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024364#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024365 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024366 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024367#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024369 }
24370
24371 /* Did we encounter a breakpoint? */
24372 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24373 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024374 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024375 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024376 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024377 sourcing_lnum);
24378 fcp->dbg_tick = debug_tick;
24379 }
24380
24381 return retval;
24382}
24383
Bram Moolenaar05159a02005-02-26 23:04:13 +000024384#if defined(FEAT_PROFILE) || defined(PROTO)
24385/*
24386 * Called when starting to read a function line.
24387 * "sourcing_lnum" must be correct!
24388 * When skipping lines it may not actually be executed, but we won't find out
24389 * until later and we need to store the time now.
24390 */
24391 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024392func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024393{
24394 funccall_T *fcp = (funccall_T *)cookie;
24395 ufunc_T *fp = fcp->func;
24396
24397 if (fp->uf_profiling && sourcing_lnum >= 1
24398 && sourcing_lnum <= fp->uf_lines.ga_len)
24399 {
24400 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024401 /* Skip continuation lines. */
24402 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24403 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024404 fp->uf_tml_execed = FALSE;
24405 profile_start(&fp->uf_tml_start);
24406 profile_zero(&fp->uf_tml_children);
24407 profile_get_wait(&fp->uf_tml_wait);
24408 }
24409}
24410
24411/*
24412 * Called when actually executing a function line.
24413 */
24414 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024415func_line_exec(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 fp->uf_tml_execed = TRUE;
24422}
24423
24424/*
24425 * Called when done with a function line.
24426 */
24427 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024428func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024429{
24430 funccall_T *fcp = (funccall_T *)cookie;
24431 ufunc_T *fp = fcp->func;
24432
24433 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24434 {
24435 if (fp->uf_tml_execed)
24436 {
24437 ++fp->uf_tml_count[fp->uf_tml_idx];
24438 profile_end(&fp->uf_tml_start);
24439 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024440 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024441 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24442 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024443 }
24444 fp->uf_tml_idx = -1;
24445 }
24446}
24447#endif
24448
Bram Moolenaar071d4272004-06-13 20:20:40 +000024449/*
24450 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024451 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024452 */
24453 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024454func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024455{
Bram Moolenaar33570922005-01-25 22:26:29 +000024456 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024457
24458 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24459 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024460 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024461 || fcp->returned);
24462}
24463
24464/*
24465 * return TRUE if cookie indicates a function which "abort"s on errors.
24466 */
24467 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024468func_has_abort(
24469 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024470{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024471 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024472}
24473
24474#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24475typedef enum
24476{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024477 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24478 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24479 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024480} var_flavour_T;
24481
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024482static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024483
24484 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010024485var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024486{
24487 char_u *p = varname;
24488
24489 if (ASCII_ISUPPER(*p))
24490 {
24491 while (*(++p))
24492 if (ASCII_ISLOWER(*p))
24493 return VAR_FLAVOUR_SESSION;
24494 return VAR_FLAVOUR_VIMINFO;
24495 }
24496 else
24497 return VAR_FLAVOUR_DEFAULT;
24498}
24499#endif
24500
24501#if defined(FEAT_VIMINFO) || defined(PROTO)
24502/*
24503 * Restore global vars that start with a capital from the viminfo file
24504 */
24505 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024506read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024507{
24508 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024509 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024510 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024511 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024512
24513 if (!writing && (find_viminfo_parameter('!') != NULL))
24514 {
24515 tab = vim_strchr(virp->vir_line + 1, '\t');
24516 if (tab != NULL)
24517 {
24518 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024519 switch (*tab)
24520 {
24521 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024522#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024523 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024524#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024525 case 'D': type = VAR_DICT; break;
24526 case 'L': type = VAR_LIST; break;
24527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024528
24529 tab = vim_strchr(tab, '\t');
24530 if (tab != NULL)
24531 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024532 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024533 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024534 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024535 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024536#ifdef FEAT_FLOAT
24537 else if (type == VAR_FLOAT)
24538 (void)string2float(tab + 1, &tv.vval.v_float);
24539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024540 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024541 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024542 if (type == VAR_DICT || type == VAR_LIST)
24543 {
24544 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24545
24546 if (etv == NULL)
24547 /* Failed to parse back the dict or list, use it as a
24548 * string. */
24549 tv.v_type = VAR_STRING;
24550 else
24551 {
24552 vim_free(tv.vval.v_string);
24553 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024554 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024555 }
24556 }
24557
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024558 /* when in a function use global variables */
24559 save_funccal = current_funccal;
24560 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024561 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024562 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024563
24564 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024565 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024566 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24567 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024568 }
24569 }
24570 }
24571
24572 return viminfo_readline(virp);
24573}
24574
24575/*
24576 * Write global vars that start with a capital to the viminfo file
24577 */
24578 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024579write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024580{
Bram Moolenaar33570922005-01-25 22:26:29 +000024581 hashitem_T *hi;
24582 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024583 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024584 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024585 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024586 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024587 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024588
24589 if (find_viminfo_parameter('!') == NULL)
24590 return;
24591
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024592 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024593
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024594 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024595 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024596 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024597 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024598 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024599 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024600 this_var = HI2DI(hi);
24601 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024602 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024603 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024604 {
24605 case VAR_STRING: s = "STR"; break;
24606 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024607#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024608 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024609#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024610 case VAR_DICT: s = "DIC"; break;
24611 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024612 default: continue;
24613 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024614 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024615 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024616 if (p != NULL)
24617 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024618 vim_free(tofree);
24619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024620 }
24621 }
24622}
24623#endif
24624
24625#if defined(FEAT_SESSION) || defined(PROTO)
24626 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024627store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024628{
Bram Moolenaar33570922005-01-25 22:26:29 +000024629 hashitem_T *hi;
24630 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024631 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024632 char_u *p, *t;
24633
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024634 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024635 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024636 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024637 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024638 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024639 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024640 this_var = HI2DI(hi);
24641 if ((this_var->di_tv.v_type == VAR_NUMBER
24642 || this_var->di_tv.v_type == VAR_STRING)
24643 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024644 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024645 /* Escape special characters with a backslash. Turn a LF and
24646 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024647 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024648 (char_u *)"\\\"\n\r");
24649 if (p == NULL) /* out of memory */
24650 break;
24651 for (t = p; *t != NUL; ++t)
24652 if (*t == '\n')
24653 *t = 'n';
24654 else if (*t == '\r')
24655 *t = 'r';
24656 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024657 this_var->di_key,
24658 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24659 : ' ',
24660 p,
24661 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24662 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024663 || put_eol(fd) == FAIL)
24664 {
24665 vim_free(p);
24666 return FAIL;
24667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024668 vim_free(p);
24669 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024670#ifdef FEAT_FLOAT
24671 else if (this_var->di_tv.v_type == VAR_FLOAT
24672 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24673 {
24674 float_T f = this_var->di_tv.vval.v_float;
24675 int sign = ' ';
24676
24677 if (f < 0)
24678 {
24679 f = -f;
24680 sign = '-';
24681 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024682 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024683 this_var->di_key, sign, f) < 0)
24684 || put_eol(fd) == FAIL)
24685 return FAIL;
24686 }
24687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024688 }
24689 }
24690 return OK;
24691}
24692#endif
24693
Bram Moolenaar661b1822005-07-28 22:36:45 +000024694/*
24695 * Display script name where an item was last set.
24696 * Should only be invoked when 'verbose' is non-zero.
24697 */
24698 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024699last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000024700{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024701 char_u *p;
24702
Bram Moolenaar661b1822005-07-28 22:36:45 +000024703 if (scriptID != 0)
24704 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024705 p = home_replace_save(NULL, get_scriptname(scriptID));
24706 if (p != NULL)
24707 {
24708 verbose_enter();
24709 MSG_PUTS(_("\n\tLast set from "));
24710 MSG_PUTS(p);
24711 vim_free(p);
24712 verbose_leave();
24713 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024714 }
24715}
24716
Bram Moolenaard812df62008-11-09 12:46:09 +000024717/*
24718 * List v:oldfiles in a nice way.
24719 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024720 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024721ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000024722{
24723 list_T *l = vimvars[VV_OLDFILES].vv_list;
24724 listitem_T *li;
24725 int nr = 0;
24726
24727 if (l == NULL)
24728 msg((char_u *)_("No old files"));
24729 else
24730 {
24731 msg_start();
24732 msg_scroll = TRUE;
24733 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24734 {
24735 msg_outnum((long)++nr);
24736 MSG_PUTS(": ");
24737 msg_outtrans(get_tv_string(&li->li_tv));
24738 msg_putchar('\n');
24739 out_flush(); /* output one line at a time */
24740 ui_breakcheck();
24741 }
24742 /* Assume "got_int" was set to truncate the listing. */
24743 got_int = FALSE;
24744
24745#ifdef FEAT_BROWSE_CMD
24746 if (cmdmod.browse)
24747 {
24748 quit_more = FALSE;
24749 nr = prompt_for_number(FALSE);
24750 msg_starthere();
24751 if (nr > 0)
24752 {
24753 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24754 (long)nr);
24755
24756 if (p != NULL)
24757 {
24758 p = expand_env_save(p);
24759 eap->arg = p;
24760 eap->cmdidx = CMD_edit;
24761 cmdmod.browse = FALSE;
24762 do_exedit(eap, NULL);
24763 vim_free(p);
24764 }
24765 }
24766 }
24767#endif
24768 }
24769}
24770
Bram Moolenaar53744302015-07-17 17:38:22 +020024771/* reset v:option_new, v:option_old and v:option_type */
24772 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024773reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020024774{
24775 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
24776 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
24777 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
24778}
24779
24780
Bram Moolenaar071d4272004-06-13 20:20:40 +000024781#endif /* FEAT_EVAL */
24782
Bram Moolenaar071d4272004-06-13 20:20:40 +000024783
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024784#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024785
24786#ifdef WIN3264
24787/*
24788 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24789 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024790static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
24791static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
24792static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024793
24794/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024795 * Get the short path (8.3) for the filename in "fnamep".
24796 * Only works for a valid file name.
24797 * When the path gets longer "fnamep" is changed and the allocated buffer
24798 * is put in "bufp".
24799 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24800 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024801 */
24802 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024803get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024804{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024805 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024806 char_u *newbuf;
24807
24808 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024809 l = GetShortPathName(*fnamep, *fnamep, len);
24810 if (l > len - 1)
24811 {
24812 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024813 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024814 newbuf = vim_strnsave(*fnamep, l);
24815 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024816 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024817
24818 vim_free(*bufp);
24819 *fnamep = *bufp = newbuf;
24820
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024821 /* Really should always succeed, as the buffer is big enough. */
24822 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024823 }
24824
24825 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024826 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024827}
24828
24829/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024830 * Get the short path (8.3) for the filename in "fname". The converted
24831 * path is returned in "bufp".
24832 *
24833 * Some of the directories specified in "fname" may not exist. This function
24834 * will shorten the existing directories at the beginning of the path and then
24835 * append the remaining non-existing path.
24836 *
24837 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024838 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024839 * bufp - Pointer to an allocated buffer for the filename.
24840 * fnamelen - Length of the filename pointed to by fname
24841 *
24842 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024843 */
24844 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024845shortpath_for_invalid_fname(
24846 char_u **fname,
24847 char_u **bufp,
24848 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024849{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024850 char_u *short_fname, *save_fname, *pbuf_unused;
24851 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024852 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024853 int old_len, len;
24854 int new_len, sfx_len;
24855 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024856
24857 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024858 old_len = *fnamelen;
24859 save_fname = vim_strnsave(*fname, old_len);
24860 pbuf_unused = NULL;
24861 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024862
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024863 endp = save_fname + old_len - 1; /* Find the end of the copy */
24864 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024865
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024866 /*
24867 * Try shortening the supplied path till it succeeds by removing one
24868 * directory at a time from the tail of the path.
24869 */
24870 len = 0;
24871 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024872 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024873 /* go back one path-separator */
24874 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24875 --endp;
24876 if (endp <= save_fname)
24877 break; /* processed the complete path */
24878
24879 /*
24880 * Replace the path separator with a NUL and try to shorten the
24881 * resulting path.
24882 */
24883 ch = *endp;
24884 *endp = 0;
24885 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024886 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024887 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24888 {
24889 retval = FAIL;
24890 goto theend;
24891 }
24892 *endp = ch; /* preserve the string */
24893
24894 if (len > 0)
24895 break; /* successfully shortened the path */
24896
24897 /* failed to shorten the path. Skip the path separator */
24898 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024899 }
24900
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024901 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024902 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024903 /*
24904 * Succeeded in shortening the path. Now concatenate the shortened
24905 * path with the remaining path at the tail.
24906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024907
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024908 /* Compute the length of the new path. */
24909 sfx_len = (int)(save_endp - endp) + 1;
24910 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024911
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024912 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024913 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024914 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024915 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024916 /* There is not enough space in the currently allocated string,
24917 * copy it to a buffer big enough. */
24918 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024919 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024920 {
24921 retval = FAIL;
24922 goto theend;
24923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024924 }
24925 else
24926 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024927 /* Transfer short_fname to the main buffer (it's big enough),
24928 * unless get_short_pathname() did its work in-place. */
24929 *fname = *bufp = save_fname;
24930 if (short_fname != save_fname)
24931 vim_strncpy(save_fname, short_fname, len);
24932 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024933 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024934
24935 /* concat the not-shortened part of the path */
24936 vim_strncpy(*fname + len, endp, sfx_len);
24937 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024938 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024939
24940theend:
24941 vim_free(pbuf_unused);
24942 vim_free(save_fname);
24943
24944 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024945}
24946
24947/*
24948 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024949 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024950 */
24951 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024952shortpath_for_partial(
24953 char_u **fnamep,
24954 char_u **bufp,
24955 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024956{
24957 int sepcount, len, tflen;
24958 char_u *p;
24959 char_u *pbuf, *tfname;
24960 int hasTilde;
24961
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024962 /* Count up the path separators from the RHS.. so we know which part
24963 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024964 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024965 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024966 if (vim_ispathsep(*p))
24967 ++sepcount;
24968
24969 /* Need full path first (use expand_env() to remove a "~/") */
24970 hasTilde = (**fnamep == '~');
24971 if (hasTilde)
24972 pbuf = tfname = expand_env_save(*fnamep);
24973 else
24974 pbuf = tfname = FullName_save(*fnamep, FALSE);
24975
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024976 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024977
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024978 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24979 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024980
24981 if (len == 0)
24982 {
24983 /* Don't have a valid filename, so shorten the rest of the
24984 * path if we can. This CAN give us invalid 8.3 filenames, but
24985 * there's not a lot of point in guessing what it might be.
24986 */
24987 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024988 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24989 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024990 }
24991
24992 /* Count the paths backward to find the beginning of the desired string. */
24993 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024994 {
24995#ifdef FEAT_MBYTE
24996 if (has_mbyte)
24997 p -= mb_head_off(tfname, p);
24998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024999 if (vim_ispathsep(*p))
25000 {
25001 if (sepcount == 0 || (hasTilde && sepcount == 1))
25002 break;
25003 else
25004 sepcount --;
25005 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025007 if (hasTilde)
25008 {
25009 --p;
25010 if (p >= tfname)
25011 *p = '~';
25012 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025013 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025014 }
25015 else
25016 ++p;
25017
25018 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25019 vim_free(*bufp);
25020 *fnamelen = (int)STRLEN(p);
25021 *bufp = pbuf;
25022 *fnamep = p;
25023
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025024 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025025}
25026#endif /* WIN3264 */
25027
25028/*
25029 * Adjust a filename, according to a string of modifiers.
25030 * *fnamep must be NUL terminated when called. When returning, the length is
25031 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025032 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025033 * When there is an error, *fnamep is set to NULL.
25034 */
25035 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025036modify_fname(
25037 char_u *src, /* string with modifiers */
25038 int *usedlen, /* characters after src that are used */
25039 char_u **fnamep, /* file name so far */
25040 char_u **bufp, /* buffer for allocated file name or NULL */
25041 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025042{
25043 int valid = 0;
25044 char_u *tail;
25045 char_u *s, *p, *pbuf;
25046 char_u dirname[MAXPATHL];
25047 int c;
25048 int has_fullname = 0;
25049#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025050 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025051 int has_shortname = 0;
25052#endif
25053
25054repeat:
25055 /* ":p" - full path/file_name */
25056 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25057 {
25058 has_fullname = 1;
25059
25060 valid |= VALID_PATH;
25061 *usedlen += 2;
25062
25063 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25064 if ((*fnamep)[0] == '~'
25065#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25066 && ((*fnamep)[1] == '/'
25067# ifdef BACKSLASH_IN_FILENAME
25068 || (*fnamep)[1] == '\\'
25069# endif
25070 || (*fnamep)[1] == NUL)
25071
25072#endif
25073 )
25074 {
25075 *fnamep = expand_env_save(*fnamep);
25076 vim_free(*bufp); /* free any allocated file name */
25077 *bufp = *fnamep;
25078 if (*fnamep == NULL)
25079 return -1;
25080 }
25081
25082 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025083 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025084 {
25085 if (vim_ispathsep(*p)
25086 && p[1] == '.'
25087 && (p[2] == NUL
25088 || vim_ispathsep(p[2])
25089 || (p[2] == '.'
25090 && (p[3] == NUL || vim_ispathsep(p[3])))))
25091 break;
25092 }
25093
25094 /* FullName_save() is slow, don't use it when not needed. */
25095 if (*p != NUL || !vim_isAbsName(*fnamep))
25096 {
25097 *fnamep = FullName_save(*fnamep, *p != NUL);
25098 vim_free(*bufp); /* free any allocated file name */
25099 *bufp = *fnamep;
25100 if (*fnamep == NULL)
25101 return -1;
25102 }
25103
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025104#ifdef WIN3264
25105# if _WIN32_WINNT >= 0x0500
25106 if (vim_strchr(*fnamep, '~') != NULL)
25107 {
25108 /* Expand 8.3 filename to full path. Needed to make sure the same
25109 * file does not have two different names.
25110 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25111 p = alloc(_MAX_PATH + 1);
25112 if (p != NULL)
25113 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025114 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025115 {
25116 vim_free(*bufp);
25117 *bufp = *fnamep = p;
25118 }
25119 else
25120 vim_free(p);
25121 }
25122 }
25123# endif
25124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025125 /* Append a path separator to a directory. */
25126 if (mch_isdir(*fnamep))
25127 {
25128 /* Make room for one or two extra characters. */
25129 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25130 vim_free(*bufp); /* free any allocated file name */
25131 *bufp = *fnamep;
25132 if (*fnamep == NULL)
25133 return -1;
25134 add_pathsep(*fnamep);
25135 }
25136 }
25137
25138 /* ":." - path relative to the current directory */
25139 /* ":~" - path relative to the home directory */
25140 /* ":8" - shortname path - postponed till after */
25141 while (src[*usedlen] == ':'
25142 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25143 {
25144 *usedlen += 2;
25145 if (c == '8')
25146 {
25147#ifdef WIN3264
25148 has_shortname = 1; /* Postpone this. */
25149#endif
25150 continue;
25151 }
25152 pbuf = NULL;
25153 /* Need full path first (use expand_env() to remove a "~/") */
25154 if (!has_fullname)
25155 {
25156 if (c == '.' && **fnamep == '~')
25157 p = pbuf = expand_env_save(*fnamep);
25158 else
25159 p = pbuf = FullName_save(*fnamep, FALSE);
25160 }
25161 else
25162 p = *fnamep;
25163
25164 has_fullname = 0;
25165
25166 if (p != NULL)
25167 {
25168 if (c == '.')
25169 {
25170 mch_dirname(dirname, MAXPATHL);
25171 s = shorten_fname(p, dirname);
25172 if (s != NULL)
25173 {
25174 *fnamep = s;
25175 if (pbuf != NULL)
25176 {
25177 vim_free(*bufp); /* free any allocated file name */
25178 *bufp = pbuf;
25179 pbuf = NULL;
25180 }
25181 }
25182 }
25183 else
25184 {
25185 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25186 /* Only replace it when it starts with '~' */
25187 if (*dirname == '~')
25188 {
25189 s = vim_strsave(dirname);
25190 if (s != NULL)
25191 {
25192 *fnamep = s;
25193 vim_free(*bufp);
25194 *bufp = s;
25195 }
25196 }
25197 }
25198 vim_free(pbuf);
25199 }
25200 }
25201
25202 tail = gettail(*fnamep);
25203 *fnamelen = (int)STRLEN(*fnamep);
25204
25205 /* ":h" - head, remove "/file_name", can be repeated */
25206 /* Don't remove the first "/" or "c:\" */
25207 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25208 {
25209 valid |= VALID_HEAD;
25210 *usedlen += 2;
25211 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025212 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025213 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025214 *fnamelen = (int)(tail - *fnamep);
25215#ifdef VMS
25216 if (*fnamelen > 0)
25217 *fnamelen += 1; /* the path separator is part of the path */
25218#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025219 if (*fnamelen == 0)
25220 {
25221 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25222 p = vim_strsave((char_u *)".");
25223 if (p == NULL)
25224 return -1;
25225 vim_free(*bufp);
25226 *bufp = *fnamep = tail = p;
25227 *fnamelen = 1;
25228 }
25229 else
25230 {
25231 while (tail > s && !after_pathsep(s, tail))
25232 mb_ptr_back(*fnamep, tail);
25233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025234 }
25235
25236 /* ":8" - shortname */
25237 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25238 {
25239 *usedlen += 2;
25240#ifdef WIN3264
25241 has_shortname = 1;
25242#endif
25243 }
25244
25245#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025246 /*
25247 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025248 */
25249 if (has_shortname)
25250 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025251 /* Copy the string if it is shortened by :h and when it wasn't copied
25252 * yet, because we are going to change it in place. Avoids changing
25253 * the buffer name for "%:8". */
25254 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025255 {
25256 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025257 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025258 return -1;
25259 vim_free(*bufp);
25260 *bufp = *fnamep = p;
25261 }
25262
25263 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025264 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025265 if (!has_fullname && !vim_isAbsName(*fnamep))
25266 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025267 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025268 return -1;
25269 }
25270 else
25271 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025272 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025273
Bram Moolenaardc935552011-08-17 15:23:23 +020025274 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025275 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025276 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025277 return -1;
25278
25279 if (l == 0)
25280 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025281 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025282 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025283 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025284 return -1;
25285 }
25286 *fnamelen = l;
25287 }
25288 }
25289#endif /* WIN3264 */
25290
25291 /* ":t" - tail, just the basename */
25292 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25293 {
25294 *usedlen += 2;
25295 *fnamelen -= (int)(tail - *fnamep);
25296 *fnamep = tail;
25297 }
25298
25299 /* ":e" - extension, can be repeated */
25300 /* ":r" - root, without extension, can be repeated */
25301 while (src[*usedlen] == ':'
25302 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25303 {
25304 /* find a '.' in the tail:
25305 * - for second :e: before the current fname
25306 * - otherwise: The last '.'
25307 */
25308 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25309 s = *fnamep - 2;
25310 else
25311 s = *fnamep + *fnamelen - 1;
25312 for ( ; s > tail; --s)
25313 if (s[0] == '.')
25314 break;
25315 if (src[*usedlen + 1] == 'e') /* :e */
25316 {
25317 if (s > tail)
25318 {
25319 *fnamelen += (int)(*fnamep - (s + 1));
25320 *fnamep = s + 1;
25321#ifdef VMS
25322 /* cut version from the extension */
25323 s = *fnamep + *fnamelen - 1;
25324 for ( ; s > *fnamep; --s)
25325 if (s[0] == ';')
25326 break;
25327 if (s > *fnamep)
25328 *fnamelen = s - *fnamep;
25329#endif
25330 }
25331 else if (*fnamep <= tail)
25332 *fnamelen = 0;
25333 }
25334 else /* :r */
25335 {
25336 if (s > tail) /* remove one extension */
25337 *fnamelen = (int)(s - *fnamep);
25338 }
25339 *usedlen += 2;
25340 }
25341
25342 /* ":s?pat?foo?" - substitute */
25343 /* ":gs?pat?foo?" - global substitute */
25344 if (src[*usedlen] == ':'
25345 && (src[*usedlen + 1] == 's'
25346 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25347 {
25348 char_u *str;
25349 char_u *pat;
25350 char_u *sub;
25351 int sep;
25352 char_u *flags;
25353 int didit = FALSE;
25354
25355 flags = (char_u *)"";
25356 s = src + *usedlen + 2;
25357 if (src[*usedlen + 1] == 'g')
25358 {
25359 flags = (char_u *)"g";
25360 ++s;
25361 }
25362
25363 sep = *s++;
25364 if (sep)
25365 {
25366 /* find end of pattern */
25367 p = vim_strchr(s, sep);
25368 if (p != NULL)
25369 {
25370 pat = vim_strnsave(s, (int)(p - s));
25371 if (pat != NULL)
25372 {
25373 s = p + 1;
25374 /* find end of substitution */
25375 p = vim_strchr(s, sep);
25376 if (p != NULL)
25377 {
25378 sub = vim_strnsave(s, (int)(p - s));
25379 str = vim_strnsave(*fnamep, *fnamelen);
25380 if (sub != NULL && str != NULL)
25381 {
25382 *usedlen = (int)(p + 1 - src);
25383 s = do_string_sub(str, pat, sub, flags);
25384 if (s != NULL)
25385 {
25386 *fnamep = s;
25387 *fnamelen = (int)STRLEN(s);
25388 vim_free(*bufp);
25389 *bufp = s;
25390 didit = TRUE;
25391 }
25392 }
25393 vim_free(sub);
25394 vim_free(str);
25395 }
25396 vim_free(pat);
25397 }
25398 }
25399 /* after using ":s", repeat all the modifiers */
25400 if (didit)
25401 goto repeat;
25402 }
25403 }
25404
Bram Moolenaar26df0922014-02-23 23:39:13 +010025405 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25406 {
25407 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25408 if (p == NULL)
25409 return -1;
25410 vim_free(*bufp);
25411 *bufp = *fnamep = p;
25412 *fnamelen = (int)STRLEN(p);
25413 *usedlen += 2;
25414 }
25415
Bram Moolenaar071d4272004-06-13 20:20:40 +000025416 return valid;
25417}
25418
25419/*
25420 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25421 * "flags" can be "g" to do a global substitute.
25422 * Returns an allocated string, NULL for error.
25423 */
25424 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025425do_string_sub(
25426 char_u *str,
25427 char_u *pat,
25428 char_u *sub,
25429 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025430{
25431 int sublen;
25432 regmatch_T regmatch;
25433 int i;
25434 int do_all;
25435 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025436 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025437 garray_T ga;
25438 char_u *ret;
25439 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025440 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025441
25442 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25443 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025444 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025445
25446 ga_init2(&ga, 1, 200);
25447
25448 do_all = (flags[0] == 'g');
25449
25450 regmatch.rm_ic = p_ic;
25451 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25452 if (regmatch.regprog != NULL)
25453 {
25454 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025455 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025456 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25457 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025458 /* Skip empty match except for first match. */
25459 if (regmatch.startp[0] == regmatch.endp[0])
25460 {
25461 if (zero_width == regmatch.startp[0])
25462 {
25463 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025464 i = MB_PTR2LEN(tail);
25465 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25466 (size_t)i);
25467 ga.ga_len += i;
25468 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025469 continue;
25470 }
25471 zero_width = regmatch.startp[0];
25472 }
25473
Bram Moolenaar071d4272004-06-13 20:20:40 +000025474 /*
25475 * Get some space for a temporary buffer to do the substitution
25476 * into. It will contain:
25477 * - The text up to where the match is.
25478 * - The substituted text.
25479 * - The text after the match.
25480 */
25481 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025482 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025483 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25484 {
25485 ga_clear(&ga);
25486 break;
25487 }
25488
25489 /* copy the text up to where the match is */
25490 i = (int)(regmatch.startp[0] - tail);
25491 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25492 /* add the substituted text */
25493 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25494 + ga.ga_len + i, TRUE, TRUE, FALSE);
25495 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025496 tail = regmatch.endp[0];
25497 if (*tail == NUL)
25498 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025499 if (!do_all)
25500 break;
25501 }
25502
25503 if (ga.ga_data != NULL)
25504 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25505
Bram Moolenaar473de612013-06-08 18:19:48 +020025506 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025507 }
25508
25509 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25510 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025511 if (p_cpo == empty_option)
25512 p_cpo = save_cpo;
25513 else
25514 /* Darn, evaluating {sub} expression changed the value. */
25515 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025516
25517 return ret;
25518}
25519
25520#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */