blob: 892e14adac6ab4801e1a957d83b909e70fe4b80b [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000102static char *e_listreq = N_("E714: List required");
103static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000104static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000105static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
106static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
107static char *e_funcdict = N_("E717: Dictionary entry already exists");
108static char *e_funcref = N_("E718: Funcref required");
109static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
110static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000111static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000112static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200113#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200114static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000116
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100117#define NAMESPACE_CHAR (char_u *)"abglstvw"
118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
Bram Moolenaar8502c702014-06-17 12:51:16 +0200137/* Abort conversion to string after a recursion error. */
138static int did_echo_string_emsg = FALSE;
139
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100159#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
160
161/* Values for get_lval() flags argument: */
162#define GLV_QUIET TFN_QUIET /* no error messages */
163#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000215/* list heads for garbage collection */
216static dict_T *first_dict = NULL; /* list of all dicts */
217static list_T *first_list = NULL; /* list of all lists */
218
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000219/* From user function to hashitem and back. */
220static ufunc_T dumuf;
221#define UF2HIKEY(fp) ((fp)->uf_name)
222#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
223#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
224
225#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
226#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
Bram Moolenaar33570922005-01-25 22:26:29 +0000228#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
229#define VAR_SHORT_LEN 20 /* short variable name length */
230#define FIXVAR_CNT 12 /* number of fixed variables */
231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000233typedef struct funccall_S funccall_T;
234
235struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
237 ufunc_T *func; /* function being called */
238 int linenr; /* next line to be executed */
239 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000240 struct /* fixed variables for arguments */
241 {
242 dictitem_T var; /* variable (without room for name) */
243 char_u room[VAR_SHORT_LEN]; /* room for the name */
244 } fixvar[FIXVAR_CNT];
245 dict_T l_vars; /* l: local function variables */
246 dictitem_T l_vars_var; /* variable for l: scope */
247 dict_T l_avars; /* a: argument variables */
248 dictitem_T l_avars_var; /* variable for a: scope */
249 list_T l_varlist; /* list for a:000 */
250 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
251 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 linenr_T breakpoint; /* next line with breakpoint or zero */
253 int dbg_tick; /* debug_tick when breakpoint was set */
254 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000255#ifdef FEAT_PROFILE
256 proftime_T prof_child; /* time spent in a child */
257#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000258 funccall_T *caller; /* calling function or NULL */
259};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
261/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262 * Info used by a ":for" loop.
263 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000264typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000265{
266 int fi_semicolon; /* TRUE if ending in '; var]' */
267 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000268 listwatch_T fi_lw; /* keep an eye on the item used. */
269 list_T *fi_list; /* list being used */
270} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000272/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000273 * Struct used by trans_function_name()
274 */
275typedef struct
276{
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000278 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dictitem_T *fd_di; /* Dictionary item used */
280} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000281
Bram Moolenaara7043832005-01-21 11:56:39 +0000282
283/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000284 * Array to hold the value of v: variables.
285 * The value is in a dictitem, so that it can also be used in the v: scope.
286 * The reason to use this table anyway is for very quick access to the
287 * variables with the VV_ defines.
288 */
289#include "version.h"
290
291/* values for vv_flags: */
292#define VV_COMPAT 1 /* compatible, also used without "v:" */
293#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000294#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000295
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000296#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
298static struct vimvar
299{
300 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301 dictitem_T vv_di; /* value and name for key */
302 char vv_filler[16]; /* space for LONGEST name below!!! */
303 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
304} vimvars[VV_LEN] =
305{
306 /*
307 * The order here must match the VV_ defines in vim.h!
308 * Initializing a union does not work, leave tv.vval empty to get zero's.
309 */
310 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
311 {VV_NAME("count1", VAR_NUMBER), VV_RO},
312 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
313 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
314 {VV_NAME("warningmsg", VAR_STRING), 0},
315 {VV_NAME("statusmsg", VAR_STRING), 0},
316 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
318 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
319 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("termresponse", VAR_STRING), VV_RO},
321 {VV_NAME("fname", VAR_STRING), VV_RO},
322 {VV_NAME("lang", VAR_STRING), VV_RO},
323 {VV_NAME("lc_time", VAR_STRING), VV_RO},
324 {VV_NAME("ctype", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
327 {VV_NAME("fname_in", VAR_STRING), VV_RO},
328 {VV_NAME("fname_out", VAR_STRING), VV_RO},
329 {VV_NAME("fname_new", VAR_STRING), VV_RO},
330 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
331 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
332 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
335 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
336 {VV_NAME("progname", VAR_STRING), VV_RO},
337 {VV_NAME("servername", VAR_STRING), VV_RO},
338 {VV_NAME("dying", VAR_NUMBER), VV_RO},
339 {VV_NAME("exception", VAR_STRING), VV_RO},
340 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
341 {VV_NAME("register", VAR_STRING), VV_RO},
342 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
343 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000344 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
345 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000346 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000347 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
348 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000349 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000354 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000355 {VV_NAME("swapname", VAR_STRING), VV_RO},
356 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000357 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200358 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000359 {VV_NAME("mouse_win", VAR_NUMBER), 0},
360 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
361 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000362 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000363 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100364 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000365 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200366 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200367 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200368 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200369 {VV_NAME("option_new", VAR_STRING), VV_RO},
370 {VV_NAME("option_old", VAR_STRING), VV_RO},
371 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100372 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100373 {VV_NAME("false", VAR_SPECIAL), VV_RO},
374 {VV_NAME("true", VAR_SPECIAL), VV_RO},
375 {VV_NAME("null", VAR_SPECIAL), VV_RO},
376 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000377};
378
379/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000380#define vv_type vv_di.di_tv.v_type
381#define vv_nr vv_di.di_tv.vval.v_number
382#define vv_float vv_di.di_tv.vval.v_float
383#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000384#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200385#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000386#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000387
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200388static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000389#define vimvarht vimvardict.dv_hashtab
390
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100391static void prepare_vimvar(int idx, typval_T *save_tv);
392static void restore_vimvar(int idx, typval_T *save_tv);
393static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
394static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
395static char_u *skip_var_one(char_u *arg);
396static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
397static void list_glob_vars(int *first);
398static void list_buf_vars(int *first);
399static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000400#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100401static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000402#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100403static void list_vim_vars(int *first);
404static void list_script_vars(int *first);
405static void list_func_vars(int *first);
406static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
407static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
408static int check_changedtick(char_u *arg);
409static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
410static void clear_lval(lval_T *lp);
411static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
412static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
413static void list_fix_watch(list_T *l, listitem_T *item);
414static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
415static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
416static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
417static void item_lock(typval_T *tv, int deep, int lock);
418static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000419
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100420static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
421static int eval1(char_u **arg, typval_T *rettv, int evaluate);
422static int eval2(char_u **arg, typval_T *rettv, int evaluate);
423static int eval3(char_u **arg, typval_T *rettv, int evaluate);
424static int eval4(char_u **arg, typval_T *rettv, int evaluate);
425static int eval5(char_u **arg, typval_T *rettv, int evaluate);
426static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
427static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000428
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100429static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
430static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
431static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
432static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
433static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
434static long list_len(list_T *l);
435static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
436static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
437static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
438static long list_find_nr(list_T *l, long idx, int *errorp);
439static long list_idx_of_item(list_T *l, listitem_T *item);
440static int list_append_number(list_T *l, varnumber_T n);
441static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
442static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
443static list_T *list_copy(list_T *orig, int deep, int copyID);
444static char_u *list2string(typval_T *tv, int copyID);
445static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
446static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
447static int free_unref_items(int copyID);
448static dictitem_T *dictitem_copy(dictitem_T *org);
449static void dictitem_remove(dict_T *dict, dictitem_T *item);
450static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
451static long dict_len(dict_T *d);
452static char_u *dict2string(typval_T *tv, int copyID);
453static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
454static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
455static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
456static char_u *string_quote(char_u *str, int function);
457static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
458static int find_internal_func(char_u *name);
459static char_u *deref_func_name(char_u *name, int *lenp, int no_autoload);
460static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100461static void emsg_funcname(char *ermsg, char_u *name);
462static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000463
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100465static void f_abs(typval_T *argvars, typval_T *rettv);
466static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100468static void f_add(typval_T *argvars, typval_T *rettv);
469static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
470static void f_and(typval_T *argvars, typval_T *rettv);
471static void f_append(typval_T *argvars, typval_T *rettv);
472static void f_argc(typval_T *argvars, typval_T *rettv);
473static void f_argidx(typval_T *argvars, typval_T *rettv);
474static void f_arglistid(typval_T *argvars, typval_T *rettv);
475static void f_argv(typval_T *argvars, typval_T *rettv);
476static void f_assert_equal(typval_T *argvars, typval_T *rettv);
477static void f_assert_exception(typval_T *argvars, typval_T *rettv);
478static void f_assert_fails(typval_T *argvars, typval_T *rettv);
479static void f_assert_false(typval_T *argvars, typval_T *rettv);
480static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000481#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100482static void f_asin(typval_T *argvars, typval_T *rettv);
483static void f_atan(typval_T *argvars, typval_T *rettv);
484static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100486static void f_browse(typval_T *argvars, typval_T *rettv);
487static void f_browsedir(typval_T *argvars, typval_T *rettv);
488static void f_bufexists(typval_T *argvars, typval_T *rettv);
489static void f_buflisted(typval_T *argvars, typval_T *rettv);
490static void f_bufloaded(typval_T *argvars, typval_T *rettv);
491static void f_bufname(typval_T *argvars, typval_T *rettv);
492static void f_bufnr(typval_T *argvars, typval_T *rettv);
493static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
494static void f_byte2line(typval_T *argvars, typval_T *rettv);
495static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
496static void f_byteidx(typval_T *argvars, typval_T *rettv);
497static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
498static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000499#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100500static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000501#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100502#ifdef FEAT_CHANNEL
503static void f_ch_open(typval_T *argvars, typval_T *rettv);
504static void f_ch_close(typval_T *argvars, typval_T *rettv);
505static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
506static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
507#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100508static void f_changenr(typval_T *argvars, typval_T *rettv);
509static void f_char2nr(typval_T *argvars, typval_T *rettv);
510static void f_cindent(typval_T *argvars, typval_T *rettv);
511static void f_clearmatches(typval_T *argvars, typval_T *rettv);
512static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000513#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100514static void f_complete(typval_T *argvars, typval_T *rettv);
515static void f_complete_add(typval_T *argvars, typval_T *rettv);
516static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000517#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100518static void f_confirm(typval_T *argvars, typval_T *rettv);
519static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000520#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100521static void f_cos(typval_T *argvars, typval_T *rettv);
522static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000523#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100524static void f_count(typval_T *argvars, typval_T *rettv);
525static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
526static void f_cursor(typval_T *argsvars, typval_T *rettv);
527static void f_deepcopy(typval_T *argvars, typval_T *rettv);
528static void f_delete(typval_T *argvars, typval_T *rettv);
529static void f_did_filetype(typval_T *argvars, typval_T *rettv);
530static void f_diff_filler(typval_T *argvars, typval_T *rettv);
531static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
532static void f_empty(typval_T *argvars, typval_T *rettv);
533static void f_escape(typval_T *argvars, typval_T *rettv);
534static void f_eval(typval_T *argvars, typval_T *rettv);
535static void f_eventhandler(typval_T *argvars, typval_T *rettv);
536static void f_executable(typval_T *argvars, typval_T *rettv);
537static void f_exepath(typval_T *argvars, typval_T *rettv);
538static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200539#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100540static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200541#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100542static void f_expand(typval_T *argvars, typval_T *rettv);
543static void f_extend(typval_T *argvars, typval_T *rettv);
544static void f_feedkeys(typval_T *argvars, typval_T *rettv);
545static void f_filereadable(typval_T *argvars, typval_T *rettv);
546static void f_filewritable(typval_T *argvars, typval_T *rettv);
547static void f_filter(typval_T *argvars, typval_T *rettv);
548static void f_finddir(typval_T *argvars, typval_T *rettv);
549static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000550#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100551static void f_float2nr(typval_T *argvars, typval_T *rettv);
552static void f_floor(typval_T *argvars, typval_T *rettv);
553static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000554#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100555static void f_fnameescape(typval_T *argvars, typval_T *rettv);
556static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
557static void f_foldclosed(typval_T *argvars, typval_T *rettv);
558static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
559static void f_foldlevel(typval_T *argvars, typval_T *rettv);
560static void f_foldtext(typval_T *argvars, typval_T *rettv);
561static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
562static void f_foreground(typval_T *argvars, typval_T *rettv);
563static void f_function(typval_T *argvars, typval_T *rettv);
564static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
565static void f_get(typval_T *argvars, typval_T *rettv);
566static void f_getbufline(typval_T *argvars, typval_T *rettv);
567static void f_getbufvar(typval_T *argvars, typval_T *rettv);
568static void f_getchar(typval_T *argvars, typval_T *rettv);
569static void f_getcharmod(typval_T *argvars, typval_T *rettv);
570static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
571static void f_getcmdline(typval_T *argvars, typval_T *rettv);
572static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
573static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
574static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
575static void f_getcwd(typval_T *argvars, typval_T *rettv);
576static void f_getfontname(typval_T *argvars, typval_T *rettv);
577static void f_getfperm(typval_T *argvars, typval_T *rettv);
578static void f_getfsize(typval_T *argvars, typval_T *rettv);
579static void f_getftime(typval_T *argvars, typval_T *rettv);
580static void f_getftype(typval_T *argvars, typval_T *rettv);
581static void f_getline(typval_T *argvars, typval_T *rettv);
582static void f_getmatches(typval_T *argvars, typval_T *rettv);
583static void f_getpid(typval_T *argvars, typval_T *rettv);
584static void f_getcurpos(typval_T *argvars, typval_T *rettv);
585static void f_getpos(typval_T *argvars, typval_T *rettv);
586static void f_getqflist(typval_T *argvars, typval_T *rettv);
587static void f_getreg(typval_T *argvars, typval_T *rettv);
588static void f_getregtype(typval_T *argvars, typval_T *rettv);
589static void f_gettabvar(typval_T *argvars, typval_T *rettv);
590static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
591static void f_getwinposx(typval_T *argvars, typval_T *rettv);
592static void f_getwinposy(typval_T *argvars, typval_T *rettv);
593static void f_getwinvar(typval_T *argvars, typval_T *rettv);
594static void f_glob(typval_T *argvars, typval_T *rettv);
595static void f_globpath(typval_T *argvars, typval_T *rettv);
596static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
597static void f_has(typval_T *argvars, typval_T *rettv);
598static void f_has_key(typval_T *argvars, typval_T *rettv);
599static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
600static void f_hasmapto(typval_T *argvars, typval_T *rettv);
601static void f_histadd(typval_T *argvars, typval_T *rettv);
602static void f_histdel(typval_T *argvars, typval_T *rettv);
603static void f_histget(typval_T *argvars, typval_T *rettv);
604static void f_histnr(typval_T *argvars, typval_T *rettv);
605static void f_hlID(typval_T *argvars, typval_T *rettv);
606static void f_hlexists(typval_T *argvars, typval_T *rettv);
607static void f_hostname(typval_T *argvars, typval_T *rettv);
608static void f_iconv(typval_T *argvars, typval_T *rettv);
609static void f_indent(typval_T *argvars, typval_T *rettv);
610static void f_index(typval_T *argvars, typval_T *rettv);
611static void f_input(typval_T *argvars, typval_T *rettv);
612static void f_inputdialog(typval_T *argvars, typval_T *rettv);
613static void f_inputlist(typval_T *argvars, typval_T *rettv);
614static void f_inputrestore(typval_T *argvars, typval_T *rettv);
615static void f_inputsave(typval_T *argvars, typval_T *rettv);
616static void f_inputsecret(typval_T *argvars, typval_T *rettv);
617static void f_insert(typval_T *argvars, typval_T *rettv);
618static void f_invert(typval_T *argvars, typval_T *rettv);
619static void f_isdirectory(typval_T *argvars, typval_T *rettv);
620static void f_islocked(typval_T *argvars, typval_T *rettv);
621static void f_items(typval_T *argvars, typval_T *rettv);
622static void f_join(typval_T *argvars, typval_T *rettv);
623static void f_jsondecode(typval_T *argvars, typval_T *rettv);
624static void f_jsonencode(typval_T *argvars, typval_T *rettv);
625static void f_keys(typval_T *argvars, typval_T *rettv);
626static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
627static void f_len(typval_T *argvars, typval_T *rettv);
628static void f_libcall(typval_T *argvars, typval_T *rettv);
629static void f_libcallnr(typval_T *argvars, typval_T *rettv);
630static void f_line(typval_T *argvars, typval_T *rettv);
631static void f_line2byte(typval_T *argvars, typval_T *rettv);
632static void f_lispindent(typval_T *argvars, typval_T *rettv);
633static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000634#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100635static void f_log(typval_T *argvars, typval_T *rettv);
636static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000637#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200638#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100639static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200640#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100641static void f_map(typval_T *argvars, typval_T *rettv);
642static void f_maparg(typval_T *argvars, typval_T *rettv);
643static void f_mapcheck(typval_T *argvars, typval_T *rettv);
644static void f_match(typval_T *argvars, typval_T *rettv);
645static void f_matchadd(typval_T *argvars, typval_T *rettv);
646static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
647static void f_matcharg(typval_T *argvars, typval_T *rettv);
648static void f_matchdelete(typval_T *argvars, typval_T *rettv);
649static void f_matchend(typval_T *argvars, typval_T *rettv);
650static void f_matchlist(typval_T *argvars, typval_T *rettv);
651static void f_matchstr(typval_T *argvars, typval_T *rettv);
652static void f_max(typval_T *argvars, typval_T *rettv);
653static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000654#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100655static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000656#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100657static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100658#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100659static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100660#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100661static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
662static void f_nr2char(typval_T *argvars, typval_T *rettv);
663static void f_or(typval_T *argvars, typval_T *rettv);
664static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100665#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100666static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100667#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000668#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100669static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000670#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100671static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
672static void f_printf(typval_T *argvars, typval_T *rettv);
673static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200674#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100675static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200676#endif
677#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100678static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200679#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100680static void f_range(typval_T *argvars, typval_T *rettv);
681static void f_readfile(typval_T *argvars, typval_T *rettv);
682static void f_reltime(typval_T *argvars, typval_T *rettv);
683static void f_reltimestr(typval_T *argvars, typval_T *rettv);
684static void f_remote_expr(typval_T *argvars, typval_T *rettv);
685static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
686static void f_remote_peek(typval_T *argvars, typval_T *rettv);
687static void f_remote_read(typval_T *argvars, typval_T *rettv);
688static void f_remote_send(typval_T *argvars, typval_T *rettv);
689static void f_remove(typval_T *argvars, typval_T *rettv);
690static void f_rename(typval_T *argvars, typval_T *rettv);
691static void f_repeat(typval_T *argvars, typval_T *rettv);
692static void f_resolve(typval_T *argvars, typval_T *rettv);
693static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000696#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100697static void f_screenattr(typval_T *argvars, typval_T *rettv);
698static void f_screenchar(typval_T *argvars, typval_T *rettv);
699static void f_screencol(typval_T *argvars, typval_T *rettv);
700static void f_screenrow(typval_T *argvars, typval_T *rettv);
701static void f_search(typval_T *argvars, typval_T *rettv);
702static void f_searchdecl(typval_T *argvars, typval_T *rettv);
703static void f_searchpair(typval_T *argvars, typval_T *rettv);
704static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
705static void f_searchpos(typval_T *argvars, typval_T *rettv);
706static void f_server2client(typval_T *argvars, typval_T *rettv);
707static void f_serverlist(typval_T *argvars, typval_T *rettv);
708static void f_setbufvar(typval_T *argvars, typval_T *rettv);
709static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
710static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
711static void f_setline(typval_T *argvars, typval_T *rettv);
712static void f_setloclist(typval_T *argvars, typval_T *rettv);
713static void f_setmatches(typval_T *argvars, typval_T *rettv);
714static void f_setpos(typval_T *argvars, typval_T *rettv);
715static void f_setqflist(typval_T *argvars, typval_T *rettv);
716static void f_setreg(typval_T *argvars, typval_T *rettv);
717static void f_settabvar(typval_T *argvars, typval_T *rettv);
718static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
719static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100720#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100721static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100722#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100723static void f_shellescape(typval_T *argvars, typval_T *rettv);
724static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
725static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000726#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100727static void f_sin(typval_T *argvars, typval_T *rettv);
728static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000729#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100730static void f_sort(typval_T *argvars, typval_T *rettv);
731static void f_soundfold(typval_T *argvars, typval_T *rettv);
732static void f_spellbadword(typval_T *argvars, typval_T *rettv);
733static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
734static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000735#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100736static void f_sqrt(typval_T *argvars, typval_T *rettv);
737static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000738#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100739static void f_str2nr(typval_T *argvars, typval_T *rettv);
740static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000741#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100742static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000743#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100744static void f_stridx(typval_T *argvars, typval_T *rettv);
745static void f_string(typval_T *argvars, typval_T *rettv);
746static void f_strlen(typval_T *argvars, typval_T *rettv);
747static void f_strpart(typval_T *argvars, typval_T *rettv);
748static void f_strridx(typval_T *argvars, typval_T *rettv);
749static void f_strtrans(typval_T *argvars, typval_T *rettv);
750static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
751static void f_strwidth(typval_T *argvars, typval_T *rettv);
752static void f_submatch(typval_T *argvars, typval_T *rettv);
753static void f_substitute(typval_T *argvars, typval_T *rettv);
754static void f_synID(typval_T *argvars, typval_T *rettv);
755static void f_synIDattr(typval_T *argvars, typval_T *rettv);
756static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
757static void f_synstack(typval_T *argvars, typval_T *rettv);
758static void f_synconcealed(typval_T *argvars, typval_T *rettv);
759static void f_system(typval_T *argvars, typval_T *rettv);
760static void f_systemlist(typval_T *argvars, typval_T *rettv);
761static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
762static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
763static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
764static void f_taglist(typval_T *argvars, typval_T *rettv);
765static void f_tagfiles(typval_T *argvars, typval_T *rettv);
766static void f_tempname(typval_T *argvars, typval_T *rettv);
767static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200768#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100769static void f_tan(typval_T *argvars, typval_T *rettv);
770static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200771#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100772static void f_tolower(typval_T *argvars, typval_T *rettv);
773static void f_toupper(typval_T *argvars, typval_T *rettv);
774static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000775#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100776static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000777#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100778static void f_type(typval_T *argvars, typval_T *rettv);
779static void f_undofile(typval_T *argvars, typval_T *rettv);
780static void f_undotree(typval_T *argvars, typval_T *rettv);
781static void f_uniq(typval_T *argvars, typval_T *rettv);
782static void f_values(typval_T *argvars, typval_T *rettv);
783static void f_virtcol(typval_T *argvars, typval_T *rettv);
784static void f_visualmode(typval_T *argvars, typval_T *rettv);
785static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
786static void f_winbufnr(typval_T *argvars, typval_T *rettv);
787static void f_wincol(typval_T *argvars, typval_T *rettv);
788static void f_winheight(typval_T *argvars, typval_T *rettv);
789static void f_winline(typval_T *argvars, typval_T *rettv);
790static void f_winnr(typval_T *argvars, typval_T *rettv);
791static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
792static void f_winrestview(typval_T *argvars, typval_T *rettv);
793static void f_winsaveview(typval_T *argvars, typval_T *rettv);
794static void f_winwidth(typval_T *argvars, typval_T *rettv);
795static void f_writefile(typval_T *argvars, typval_T *rettv);
796static void f_wordcount(typval_T *argvars, typval_T *rettv);
797static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000798
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100799static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
800static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
801static int get_env_len(char_u **arg);
802static int get_id_len(char_u **arg);
803static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
804static char_u *find_name_end(char_u *arg, char_u **expr_start, char_u **expr_end, int flags);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000805#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
806#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
807 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100808static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
809static int eval_isnamec(int c);
810static int eval_isnamec1(int c);
811static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
812static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100813static typval_T *alloc_string_tv(char_u *string);
814static void init_tv(typval_T *varp);
815static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100816#ifdef FEAT_FLOAT
817static float_T get_tv_float(typval_T *varp);
818#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100819static linenr_T get_tv_lnum(typval_T *argvars);
820static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
821static char_u *get_tv_string(typval_T *varp);
822static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
823static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
824static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
825static hashtab_T *find_var_ht(char_u *name, char_u **varname);
826static funccall_T *get_funccal(void);
827static void vars_clear_ext(hashtab_T *ht, int free_val);
828static void delete_var(hashtab_T *ht, hashitem_T *hi);
829static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
830static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
831static void set_var(char_u *name, typval_T *varp, int copy);
832static int var_check_ro(int flags, char_u *name, int use_gettext);
833static int var_check_fixed(int flags, char_u *name, int use_gettext);
834static int var_check_func_name(char_u *name, int new_var);
835static int valid_varname(char_u *varname);
836static int tv_check_lock(int lock, char_u *name, int use_gettext);
837static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
838static char_u *find_option_end(char_u **arg, int *opt_flags);
839static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
840static int eval_fname_script(char_u *p);
841static int eval_fname_sid(char_u *p);
842static void list_func_head(ufunc_T *fp, int indent);
843static ufunc_T *find_func(char_u *name);
844static int function_exists(char_u *name);
845static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000846#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100847static void func_do_profile(ufunc_T *fp);
848static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
849static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000850static int
851# ifdef __BORLANDC__
852 _RTLENTRYF
853# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100854 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000855static int
856# ifdef __BORLANDC__
857 _RTLENTRYF
858# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100859 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000860#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100861static int script_autoload(char_u *name, int reload);
862static char_u *autoload_name(char_u *name);
863static void cat_func_name(char_u *buf, ufunc_T *fp);
864static void func_free(ufunc_T *fp);
865static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
866static int can_free_funccal(funccall_T *fc, int copyID) ;
867static void free_funccal(funccall_T *fc, int free_val);
868static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
869static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
870static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
871static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
872static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
873static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
874static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
875static int write_list(FILE *fd, list_T *list, int binary);
876static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000877
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200878
879#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100880static int compare_func_name(const void *s1, const void *s2);
881static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200882#endif
883
Bram Moolenaar33570922005-01-25 22:26:29 +0000884/*
885 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000886 */
887 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100888eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000889{
Bram Moolenaar33570922005-01-25 22:26:29 +0000890 int i;
891 struct vimvar *p;
892
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200893 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
894 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200895 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000896 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000897 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000898
899 for (i = 0; i < VV_LEN; ++i)
900 {
901 p = &vimvars[i];
902 STRCPY(p->vv_di.di_key, p->vv_name);
903 if (p->vv_flags & VV_RO)
904 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
905 else if (p->vv_flags & VV_RO_SBX)
906 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
907 else
908 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000909
910 /* add to v: scope dict, unless the value is not always available */
911 if (p->vv_type != VAR_UNKNOWN)
912 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000913 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000914 /* add to compat scope dict */
915 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000916 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100917 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
918
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000919 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100920 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200921 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100922 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100923
924 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
925 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
926 set_vim_var_nr(VV_NONE, VVAL_NONE);
927 set_vim_var_nr(VV_NULL, VVAL_NULL);
928
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200929 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200930
931#ifdef EBCDIC
932 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100933 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200934 */
935 sortFunctions();
936#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000937}
938
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000939#if defined(EXITFREE) || defined(PROTO)
940 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100941eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000942{
943 int i;
944 struct vimvar *p;
945
946 for (i = 0; i < VV_LEN; ++i)
947 {
948 p = &vimvars[i];
949 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000950 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000951 vim_free(p->vv_str);
952 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000953 }
954 else if (p->vv_di.di_tv.v_type == VAR_LIST)
955 {
956 list_unref(p->vv_list);
957 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000958 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000959 }
960 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000961 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000962 hash_clear(&compat_hashtab);
963
Bram Moolenaard9fba312005-06-26 22:34:35 +0000964 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100965# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200966 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100967# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000968
969 /* global variables */
970 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000971
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000972 /* autoloaded script names */
973 ga_clear_strings(&ga_loaded);
974
Bram Moolenaarcca74132013-09-25 21:00:28 +0200975 /* Script-local variables. First clear all the variables and in a second
976 * loop free the scriptvar_T, because a variable in one script might hold
977 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200978 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200979 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200980 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200981 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200982 ga_clear(&ga_scripts);
983
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000984 /* unreferenced lists and dicts */
985 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000986
987 /* functions */
988 free_all_functions();
989 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000990}
991#endif
992
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000993/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 * Return the name of the executed function.
995 */
996 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100997func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000999 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000}
1001
1002/*
1003 * Return the address holding the next breakpoint line for a funccall cookie.
1004 */
1005 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001006func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007{
Bram Moolenaar33570922005-01-25 22:26:29 +00001008 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009}
1010
1011/*
1012 * Return the address holding the debug tick for a funccall cookie.
1013 */
1014 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001015func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016{
Bram Moolenaar33570922005-01-25 22:26:29 +00001017 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018}
1019
1020/*
1021 * Return the nesting level for a funccall cookie.
1022 */
1023 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001024func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025{
Bram Moolenaar33570922005-01-25 22:26:29 +00001026 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027}
1028
1029/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001030funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001032/* pointer to list of previously used funccal, still around because some
1033 * item in it is still being used. */
1034funccall_T *previous_funccal = NULL;
1035
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036/*
1037 * Return TRUE when a function was ended by a ":return" command.
1038 */
1039 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001040current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041{
1042 return current_funccal->returned;
1043}
1044
1045
1046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 * Set an internal variable to a string value. Creates the variable if it does
1048 * not already exist.
1049 */
1050 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001051set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001053 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001054 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055
1056 val = vim_strsave(value);
1057 if (val != NULL)
1058 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001059 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001060 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001062 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001063 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 }
1065 }
1066}
1067
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001068static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001069static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001070static char_u *redir_endp = NULL;
1071static char_u *redir_varname = NULL;
1072
1073/*
1074 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001075 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001076 * Returns OK if successfully completed the setup. FAIL otherwise.
1077 */
1078 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001079var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001080{
1081 int save_emsg;
1082 int err;
1083 typval_T tv;
1084
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001085 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001086 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087 {
1088 EMSG(_(e_invarg));
1089 return FAIL;
1090 }
1091
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001092 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093 redir_varname = vim_strsave(name);
1094 if (redir_varname == NULL)
1095 return FAIL;
1096
1097 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1098 if (redir_lval == NULL)
1099 {
1100 var_redir_stop();
1101 return FAIL;
1102 }
1103
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001104 /* The output is stored in growarray "redir_ga" until redirection ends. */
1105 ga_init2(&redir_ga, (int)sizeof(char), 500);
1106
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001108 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001109 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1111 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001112 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 if (redir_endp != NULL && *redir_endp != NUL)
1114 /* Trailing characters are present after the variable name */
1115 EMSG(_(e_trailing));
1116 else
1117 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001118 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 var_redir_stop();
1120 return FAIL;
1121 }
1122
1123 /* check if we can write to the variable: set it to or append an empty
1124 * string */
1125 save_emsg = did_emsg;
1126 did_emsg = FALSE;
1127 tv.v_type = VAR_STRING;
1128 tv.vval.v_string = (char_u *)"";
1129 if (append)
1130 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1131 else
1132 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001133 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001135 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136 if (err)
1137 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001138 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 var_redir_stop();
1140 return FAIL;
1141 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142
1143 return OK;
1144}
1145
1146/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001147 * Append "value[value_len]" to the variable set by var_redir_start().
1148 * The actual appending is postponed until redirection ends, because the value
1149 * appended may in fact be the string we write to, changing it may cause freed
1150 * memory to be used:
1151 * :redir => foo
1152 * :let foo
1153 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154 */
1155 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001156var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001157{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001158 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159
1160 if (redir_lval == NULL)
1161 return;
1162
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001163 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001164 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001165 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001166 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001168 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001169 {
1170 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001171 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001172 }
1173 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001174 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001175}
1176
1177/*
1178 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001179 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001180 */
1181 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001182var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001183{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001184 typval_T tv;
1185
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001186 if (redir_lval != NULL)
1187 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001188 /* If there was no error: assign the text to the variable. */
1189 if (redir_endp != NULL)
1190 {
1191 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1192 tv.v_type = VAR_STRING;
1193 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001194 /* Call get_lval() again, if it's inside a Dict or List it may
1195 * have changed. */
1196 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001197 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001198 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1199 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1200 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001201 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001202
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001203 /* free the collected output */
1204 vim_free(redir_ga.ga_data);
1205 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001206
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207 vim_free(redir_lval);
1208 redir_lval = NULL;
1209 }
1210 vim_free(redir_varname);
1211 redir_varname = NULL;
1212}
1213
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214# if defined(FEAT_MBYTE) || defined(PROTO)
1215 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001216eval_charconvert(
1217 char_u *enc_from,
1218 char_u *enc_to,
1219 char_u *fname_from,
1220 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221{
1222 int err = FALSE;
1223
1224 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1225 set_vim_var_string(VV_CC_TO, enc_to, -1);
1226 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1227 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1228 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1229 err = TRUE;
1230 set_vim_var_string(VV_CC_FROM, NULL, -1);
1231 set_vim_var_string(VV_CC_TO, NULL, -1);
1232 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1233 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1234
1235 if (err)
1236 return FAIL;
1237 return OK;
1238}
1239# endif
1240
1241# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1242 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001243eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244{
1245 int err = FALSE;
1246
1247 set_vim_var_string(VV_FNAME_IN, fname, -1);
1248 set_vim_var_string(VV_CMDARG, args, -1);
1249 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1250 err = TRUE;
1251 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1252 set_vim_var_string(VV_CMDARG, NULL, -1);
1253
1254 if (err)
1255 {
1256 mch_remove(fname);
1257 return FAIL;
1258 }
1259 return OK;
1260}
1261# endif
1262
1263# if defined(FEAT_DIFF) || defined(PROTO)
1264 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001265eval_diff(
1266 char_u *origfile,
1267 char_u *newfile,
1268 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269{
1270 int err = FALSE;
1271
1272 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1273 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1274 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1275 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1276 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1277 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1278 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1279}
1280
1281 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001282eval_patch(
1283 char_u *origfile,
1284 char_u *difffile,
1285 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286{
1287 int err;
1288
1289 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1290 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1291 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1292 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1293 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1294 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1295 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1296}
1297# endif
1298
1299/*
1300 * Top level evaluation function, returning a boolean.
1301 * Sets "error" to TRUE if there was an error.
1302 * Return TRUE or FALSE.
1303 */
1304 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001305eval_to_bool(
1306 char_u *arg,
1307 int *error,
1308 char_u **nextcmd,
1309 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310{
Bram Moolenaar33570922005-01-25 22:26:29 +00001311 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 int retval = FALSE;
1313
1314 if (skip)
1315 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001316 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 else
1319 {
1320 *error = FALSE;
1321 if (!skip)
1322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001323 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001324 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 }
1326 }
1327 if (skip)
1328 --emsg_skip;
1329
1330 return retval;
1331}
1332
1333/*
1334 * Top level evaluation function, returning a string. If "skip" is TRUE,
1335 * only parsing to "nextcmd" is done, without reporting errors. Return
1336 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1337 */
1338 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001339eval_to_string_skip(
1340 char_u *arg,
1341 char_u **nextcmd,
1342 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343{
Bram Moolenaar33570922005-01-25 22:26:29 +00001344 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 char_u *retval;
1346
1347 if (skip)
1348 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001349 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 retval = NULL;
1351 else
1352 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001353 retval = vim_strsave(get_tv_string(&tv));
1354 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356 if (skip)
1357 --emsg_skip;
1358
1359 return retval;
1360}
1361
1362/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001363 * Skip over an expression at "*pp".
1364 * Return FAIL for an error, OK otherwise.
1365 */
1366 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001367skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001368{
Bram Moolenaar33570922005-01-25 22:26:29 +00001369 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001370
1371 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001372 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001373}
1374
1375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001377 * When "convert" is TRUE convert a List into a sequence of lines and convert
1378 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 * Return pointer to allocated memory, or NULL for failure.
1380 */
1381 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001382eval_to_string(
1383 char_u *arg,
1384 char_u **nextcmd,
1385 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386{
Bram Moolenaar33570922005-01-25 22:26:29 +00001387 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001389 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001390#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001391 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001394 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 retval = NULL;
1396 else
1397 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001398 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001399 {
1400 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001401 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001402 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001403 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001404 if (tv.vval.v_list->lv_len > 0)
1405 ga_append(&ga, NL);
1406 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001407 ga_append(&ga, NUL);
1408 retval = (char_u *)ga.ga_data;
1409 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001410#ifdef FEAT_FLOAT
1411 else if (convert && tv.v_type == VAR_FLOAT)
1412 {
1413 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1414 retval = vim_strsave(numbuf);
1415 }
1416#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001417 else
1418 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001419 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 }
1421
1422 return retval;
1423}
1424
1425/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001426 * Call eval_to_string() without using current local variables and using
1427 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 */
1429 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001430eval_to_string_safe(
1431 char_u *arg,
1432 char_u **nextcmd,
1433 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434{
1435 char_u *retval;
1436 void *save_funccalp;
1437
1438 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001439 if (use_sandbox)
1440 ++sandbox;
1441 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001442 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001443 if (use_sandbox)
1444 --sandbox;
1445 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 restore_funccal(save_funccalp);
1447 return retval;
1448}
1449
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450/*
1451 * Top level evaluation function, returning a number.
1452 * Evaluates "expr" silently.
1453 * Returns -1 for an error.
1454 */
1455 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001456eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457{
Bram Moolenaar33570922005-01-25 22:26:29 +00001458 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001460 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461
1462 ++emsg_off;
1463
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001464 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 retval = -1;
1466 else
1467 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001468 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001469 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 }
1471 --emsg_off;
1472
1473 return retval;
1474}
1475
Bram Moolenaara40058a2005-07-11 22:42:07 +00001476/*
1477 * Prepare v: variable "idx" to be used.
1478 * Save the current typeval in "save_tv".
1479 * When not used yet add the variable to the v: hashtable.
1480 */
1481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001482prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001483{
1484 *save_tv = vimvars[idx].vv_tv;
1485 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1486 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1487}
1488
1489/*
1490 * Restore v: variable "idx" to typeval "save_tv".
1491 * When no longer defined, remove the variable from the v: hashtable.
1492 */
1493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001494restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001495{
1496 hashitem_T *hi;
1497
Bram Moolenaara40058a2005-07-11 22:42:07 +00001498 vimvars[idx].vv_tv = *save_tv;
1499 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1500 {
1501 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1502 if (HASHITEM_EMPTY(hi))
1503 EMSG2(_(e_intern2), "restore_vimvar()");
1504 else
1505 hash_remove(&vimvarht, hi);
1506 }
1507}
1508
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001509#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001510/*
1511 * Evaluate an expression to a list with suggestions.
1512 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001513 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001514 */
1515 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001516eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001517{
1518 typval_T save_val;
1519 typval_T rettv;
1520 list_T *list = NULL;
1521 char_u *p = skipwhite(expr);
1522
1523 /* Set "v:val" to the bad word. */
1524 prepare_vimvar(VV_VAL, &save_val);
1525 vimvars[VV_VAL].vv_type = VAR_STRING;
1526 vimvars[VV_VAL].vv_str = badword;
1527 if (p_verbose == 0)
1528 ++emsg_off;
1529
1530 if (eval1(&p, &rettv, TRUE) == OK)
1531 {
1532 if (rettv.v_type != VAR_LIST)
1533 clear_tv(&rettv);
1534 else
1535 list = rettv.vval.v_list;
1536 }
1537
1538 if (p_verbose == 0)
1539 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001540 restore_vimvar(VV_VAL, &save_val);
1541
1542 return list;
1543}
1544
1545/*
1546 * "list" is supposed to contain two items: a word and a number. Return the
1547 * word in "pp" and the number as the return value.
1548 * Return -1 if anything isn't right.
1549 * Used to get the good word and score from the eval_spell_expr() result.
1550 */
1551 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001552get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001553{
1554 listitem_T *li;
1555
1556 li = list->lv_first;
1557 if (li == NULL)
1558 return -1;
1559 *pp = get_tv_string(&li->li_tv);
1560
1561 li = li->li_next;
1562 if (li == NULL)
1563 return -1;
1564 return get_tv_number(&li->li_tv);
1565}
1566#endif
1567
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001568/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001569 * Top level evaluation function.
1570 * Returns an allocated typval_T with the result.
1571 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001572 */
1573 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001574eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001575{
1576 typval_T *tv;
1577
1578 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001579 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001580 {
1581 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001582 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001583 }
1584
1585 return tv;
1586}
1587
1588
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001590 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001591 * Uses argv[argc] for the function arguments. Only Number and String
1592 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001593 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001595 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001596call_vim_function(
1597 char_u *func,
1598 int argc,
1599 char_u **argv,
1600 int safe, /* use the sandbox */
1601 int str_arg_only, /* all arguments are strings */
1602 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603{
Bram Moolenaar33570922005-01-25 22:26:29 +00001604 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 long n;
1606 int len;
1607 int i;
1608 int doesrange;
1609 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001610 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001612 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
1616 for (i = 0; i < argc; i++)
1617 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001618 /* Pass a NULL or empty argument as an empty string */
1619 if (argv[i] == NULL || *argv[i] == NUL)
1620 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001621 argvars[i].v_type = VAR_STRING;
1622 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001623 continue;
1624 }
1625
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001626 if (str_arg_only)
1627 len = 0;
1628 else
1629 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001630 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 if (len != 0 && len == (int)STRLEN(argv[i]))
1632 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001633 argvars[i].v_type = VAR_NUMBER;
1634 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 }
1636 else
1637 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001638 argvars[i].v_type = VAR_STRING;
1639 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 }
1641 }
1642
1643 if (safe)
1644 {
1645 save_funccalp = save_funccal();
1646 ++sandbox;
1647 }
1648
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001649 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1650 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001652 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 if (safe)
1654 {
1655 --sandbox;
1656 restore_funccal(save_funccalp);
1657 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001658 vim_free(argvars);
1659
1660 if (ret == FAIL)
1661 clear_tv(rettv);
1662
1663 return ret;
1664}
1665
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001666/*
1667 * Call vimL function "func" and return the result as a number.
1668 * Returns -1 when calling the function fails.
1669 * Uses argv[argc] for the function arguments.
1670 */
1671 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001672call_func_retnr(
1673 char_u *func,
1674 int argc,
1675 char_u **argv,
1676 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001677{
1678 typval_T rettv;
1679 long retval;
1680
1681 /* All arguments are passed as strings, no conversion to number. */
1682 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1683 return -1;
1684
1685 retval = get_tv_number_chk(&rettv, NULL);
1686 clear_tv(&rettv);
1687 return retval;
1688}
1689
1690#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1691 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1692
Bram Moolenaar4f688582007-07-24 12:34:30 +00001693# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001694/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001695 * Call vimL function "func" and return the result as a string.
1696 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001697 * Uses argv[argc] for the function arguments.
1698 */
1699 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001700call_func_retstr(
1701 char_u *func,
1702 int argc,
1703 char_u **argv,
1704 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001705{
1706 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001707 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001708
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001709 /* All arguments are passed as strings, no conversion to number. */
1710 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001711 return NULL;
1712
1713 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001714 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 return retval;
1716}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001717# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001718
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001719/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001720 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001721 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001722 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001723 */
1724 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001725call_func_retlist(
1726 char_u *func,
1727 int argc,
1728 char_u **argv,
1729 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001730{
1731 typval_T rettv;
1732
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001733 /* All arguments are passed as strings, no conversion to number. */
1734 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001735 return NULL;
1736
1737 if (rettv.v_type != VAR_LIST)
1738 {
1739 clear_tv(&rettv);
1740 return NULL;
1741 }
1742
1743 return rettv.vval.v_list;
1744}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745#endif
1746
1747/*
1748 * Save the current function call pointer, and set it to NULL.
1749 * Used when executing autocommands and for ":source".
1750 */
1751 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001752save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001754 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 current_funccal = NULL;
1757 return (void *)fc;
1758}
1759
1760 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001761restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001763 funccall_T *fc = (funccall_T *)vfc;
1764
1765 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766}
1767
Bram Moolenaar05159a02005-02-26 23:04:13 +00001768#if defined(FEAT_PROFILE) || defined(PROTO)
1769/*
1770 * Prepare profiling for entering a child or something else that is not
1771 * counted for the script/function itself.
1772 * Should always be called in pair with prof_child_exit().
1773 */
1774 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001775prof_child_enter(
1776 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001777{
1778 funccall_T *fc = current_funccal;
1779
1780 if (fc != NULL && fc->func->uf_profiling)
1781 profile_start(&fc->prof_child);
1782 script_prof_save(tm);
1783}
1784
1785/*
1786 * Take care of time spent in a child.
1787 * Should always be called after prof_child_enter().
1788 */
1789 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001790prof_child_exit(
1791 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001792{
1793 funccall_T *fc = current_funccal;
1794
1795 if (fc != NULL && fc->func->uf_profiling)
1796 {
1797 profile_end(&fc->prof_child);
1798 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1799 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1800 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1801 }
1802 script_prof_restore(tm);
1803}
1804#endif
1805
1806
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807#ifdef FEAT_FOLDING
1808/*
1809 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1810 * it in "*cp". Doesn't give error messages.
1811 */
1812 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001813eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814{
Bram Moolenaar33570922005-01-25 22:26:29 +00001815 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 int retval;
1817 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001818 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1819 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820
1821 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001822 if (use_sandbox)
1823 ++sandbox;
1824 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001826 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 retval = 0;
1828 else
1829 {
1830 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 if (tv.v_type == VAR_NUMBER)
1832 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001833 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 retval = 0;
1835 else
1836 {
1837 /* If the result is a string, check if there is a non-digit before
1838 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 if (!VIM_ISDIGIT(*s) && *s != '-')
1841 *cp = *s++;
1842 retval = atol((char *)s);
1843 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 }
1846 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001847 if (use_sandbox)
1848 --sandbox;
1849 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850
1851 return retval;
1852}
1853#endif
1854
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 * ":let" list all variable values
1857 * ":let var1 var2" list variable values
1858 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001859 * ":let var += expr" assignment command.
1860 * ":let var -= expr" assignment command.
1861 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001862 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 */
1864 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001865ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866{
1867 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001868 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001869 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 int var_count = 0;
1872 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001873 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001874 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001875 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876
Bram Moolenaardb552d602006-03-23 22:59:57 +00001877 argend = skip_var_list(arg, &var_count, &semicolon);
1878 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001879 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001880 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1881 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001882 expr = skipwhite(argend);
1883 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1884 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001886 /*
1887 * ":let" without "=": list variables
1888 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001889 if (*arg == '[')
1890 EMSG(_(e_invarg));
1891 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001893 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001895 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001897 list_glob_vars(&first);
1898 list_buf_vars(&first);
1899 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001900#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001901 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001902#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001903 list_script_vars(&first);
1904 list_func_vars(&first);
1905 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 eap->nextcmd = check_nextcmd(arg);
1908 }
1909 else
1910 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001911 op[0] = '=';
1912 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001913 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001914 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001915 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1916 op[0] = *expr; /* +=, -= or .= */
1917 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001918 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001919 else
1920 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001921
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if (eap->skip)
1923 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001924 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 if (eap->skip)
1926 {
1927 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001928 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 --emsg_skip;
1930 }
1931 else if (i != FAIL)
1932 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001933 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001934 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 }
1937 }
1938}
1939
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001940/*
1941 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1942 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001943 * When "nextchars" is not NULL it points to a string with characters that
1944 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1945 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 * Returns OK or FAIL;
1947 */
1948 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001949ex_let_vars(
1950 char_u *arg_start,
1951 typval_T *tv,
1952 int copy, /* copy values from "tv", don't move */
1953 int semicolon, /* from skip_var_list() */
1954 int var_count, /* from skip_var_list() */
1955 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956{
1957 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001958 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001960 listitem_T *item;
1961 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001962
1963 if (*arg != '[')
1964 {
1965 /*
1966 * ":let var = expr" or ":for var in list"
1967 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001968 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001969 return FAIL;
1970 return OK;
1971 }
1972
1973 /*
1974 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1975 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001976 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001977 {
1978 EMSG(_(e_listreq));
1979 return FAIL;
1980 }
1981
1982 i = list_len(l);
1983 if (semicolon == 0 && var_count < i)
1984 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001985 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 return FAIL;
1987 }
1988 if (var_count - semicolon > i)
1989 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001990 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 return FAIL;
1992 }
1993
1994 item = l->lv_first;
1995 while (*arg != ']')
1996 {
1997 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001998 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 item = item->li_next;
2000 if (arg == NULL)
2001 return FAIL;
2002
2003 arg = skipwhite(arg);
2004 if (*arg == ';')
2005 {
2006 /* Put the rest of the list (may be empty) in the var after ';'.
2007 * Create a new list for this. */
2008 l = list_alloc();
2009 if (l == NULL)
2010 return FAIL;
2011 while (item != NULL)
2012 {
2013 list_append_tv(l, &item->li_tv);
2014 item = item->li_next;
2015 }
2016
2017 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002018 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019 ltv.vval.v_list = l;
2020 l->lv_refcount = 1;
2021
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002022 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2023 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024 clear_tv(&ltv);
2025 if (arg == NULL)
2026 return FAIL;
2027 break;
2028 }
2029 else if (*arg != ',' && *arg != ']')
2030 {
2031 EMSG2(_(e_intern2), "ex_let_vars()");
2032 return FAIL;
2033 }
2034 }
2035
2036 return OK;
2037}
2038
2039/*
2040 * Skip over assignable variable "var" or list of variables "[var, var]".
2041 * Used for ":let varvar = expr" and ":for varvar in expr".
2042 * For "[var, var]" increment "*var_count" for each variable.
2043 * for "[var, var; var]" set "semicolon".
2044 * Return NULL for an error.
2045 */
2046 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002047skip_var_list(
2048 char_u *arg,
2049 int *var_count,
2050 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002051{
2052 char_u *p, *s;
2053
2054 if (*arg == '[')
2055 {
2056 /* "[var, var]": find the matching ']'. */
2057 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002058 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002059 {
2060 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2061 s = skip_var_one(p);
2062 if (s == p)
2063 {
2064 EMSG2(_(e_invarg2), p);
2065 return NULL;
2066 }
2067 ++*var_count;
2068
2069 p = skipwhite(s);
2070 if (*p == ']')
2071 break;
2072 else if (*p == ';')
2073 {
2074 if (*semicolon == 1)
2075 {
2076 EMSG(_("Double ; in list of variables"));
2077 return NULL;
2078 }
2079 *semicolon = 1;
2080 }
2081 else if (*p != ',')
2082 {
2083 EMSG2(_(e_invarg2), p);
2084 return NULL;
2085 }
2086 }
2087 return p + 1;
2088 }
2089 else
2090 return skip_var_one(arg);
2091}
2092
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002093/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002094 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002095 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002096 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002097 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002098skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002099{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002100 if (*arg == '@' && arg[1] != NUL)
2101 return arg + 2;
2102 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2103 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002104}
2105
Bram Moolenaara7043832005-01-21 11:56:39 +00002106/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002107 * List variables for hashtab "ht" with prefix "prefix".
2108 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002109 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002111list_hashtable_vars(
2112 hashtab_T *ht,
2113 char_u *prefix,
2114 int empty,
2115 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002116{
Bram Moolenaar33570922005-01-25 22:26:29 +00002117 hashitem_T *hi;
2118 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002119 int todo;
2120
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002121 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002122 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2123 {
2124 if (!HASHITEM_EMPTY(hi))
2125 {
2126 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002127 di = HI2DI(hi);
2128 if (empty || di->di_tv.v_type != VAR_STRING
2129 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002130 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002131 }
2132 }
2133}
2134
2135/*
2136 * List global variables.
2137 */
2138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002139list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002140{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002141 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002142}
2143
2144/*
2145 * List buffer variables.
2146 */
2147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002148list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002149{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002150 char_u numbuf[NUMBUFLEN];
2151
Bram Moolenaar429fa852013-04-15 12:27:36 +02002152 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002153 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002154
2155 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2157 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002158}
2159
2160/*
2161 * List window variables.
2162 */
2163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002164list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002165{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002166 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002167 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002168}
2169
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002170#ifdef FEAT_WINDOWS
2171/*
2172 * List tab page variables.
2173 */
2174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002175list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002176{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002177 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002179}
2180#endif
2181
Bram Moolenaara7043832005-01-21 11:56:39 +00002182/*
2183 * List Vim variables.
2184 */
2185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002186list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189}
2190
2191/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002192 * List script-local variables, if there is a script.
2193 */
2194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002195list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002196{
2197 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002198 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2199 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002200}
2201
2202/*
2203 * List function variables, if there is a function.
2204 */
2205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002206list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002207{
2208 if (current_funccal != NULL)
2209 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002210 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002211}
2212
2213/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 * List variables in "arg".
2215 */
2216 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002217list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218{
2219 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002220 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 char_u *name_start;
2223 char_u *arg_subsc;
2224 char_u *tofree;
2225 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226
2227 while (!ends_excmd(*arg) && !got_int)
2228 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002231 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2233 {
2234 emsg_severe = TRUE;
2235 EMSG(_(e_trailing));
2236 break;
2237 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 /* get_name_len() takes care of expanding curly braces */
2242 name_start = name = arg;
2243 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2244 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 /* This is mainly to keep test 49 working: when expanding
2247 * curly braces fails overrule the exception error message. */
2248 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 emsg_severe = TRUE;
2251 EMSG2(_(e_invarg2), arg);
2252 break;
2253 }
2254 error = TRUE;
2255 }
2256 else
2257 {
2258 if (tofree != NULL)
2259 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002260 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 else
2263 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 /* handle d.key, l[idx], f(expr) */
2265 arg_subsc = arg;
2266 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002267 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002269 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002271 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002273 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002274 case 'g': list_glob_vars(first); break;
2275 case 'b': list_buf_vars(first); break;
2276 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002277#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002278 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002279#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002280 case 'v': list_vim_vars(first); break;
2281 case 's': list_script_vars(first); break;
2282 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 default:
2284 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002285 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002286 }
2287 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 {
2289 char_u numbuf[NUMBUFLEN];
2290 char_u *tf;
2291 int c;
2292 char_u *s;
2293
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002294 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 c = *arg;
2296 *arg = NUL;
2297 list_one_var_a((char_u *)"",
2298 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002299 tv.v_type,
2300 s == NULL ? (char_u *)"" : s,
2301 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302 *arg = c;
2303 vim_free(tf);
2304 }
2305 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002306 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 }
2308 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002309
2310 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312
2313 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 }
2315
2316 return arg;
2317}
2318
2319/*
2320 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2321 * Returns a pointer to the char just after the var name.
2322 * Returns NULL if there is an error.
2323 */
2324 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002325ex_let_one(
2326 char_u *arg, /* points to variable name */
2327 typval_T *tv, /* value to assign to variable */
2328 int copy, /* copy value from "tv" */
2329 char_u *endchars, /* valid chars after variable name or NULL */
2330 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331{
2332 int c1;
2333 char_u *name;
2334 char_u *p;
2335 char_u *arg_end = NULL;
2336 int len;
2337 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339
2340 /*
2341 * ":let $VAR = expr": Set environment variable.
2342 */
2343 if (*arg == '$')
2344 {
2345 /* Find the end of the name. */
2346 ++arg;
2347 name = arg;
2348 len = get_env_len(&arg);
2349 if (len == 0)
2350 EMSG2(_(e_invarg2), name - 1);
2351 else
2352 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002353 if (op != NULL && (*op == '+' || *op == '-'))
2354 EMSG2(_(e_letwrong), op);
2355 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002356 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002358 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 {
2360 c1 = name[len];
2361 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002362 p = get_tv_string_chk(tv);
2363 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002364 {
2365 int mustfree = FALSE;
2366 char_u *s = vim_getenv(name, &mustfree);
2367
2368 if (s != NULL)
2369 {
2370 p = tofree = concat_str(s, p);
2371 if (mustfree)
2372 vim_free(s);
2373 }
2374 }
2375 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002376 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002378 if (STRICMP(name, "HOME") == 0)
2379 init_homedir();
2380 else if (didset_vim && STRICMP(name, "VIM") == 0)
2381 didset_vim = FALSE;
2382 else if (didset_vimruntime
2383 && STRICMP(name, "VIMRUNTIME") == 0)
2384 didset_vimruntime = FALSE;
2385 arg_end = arg;
2386 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 }
2390 }
2391 }
2392
2393 /*
2394 * ":let &option = expr": Set option value.
2395 * ":let &l:option = expr": Set local option value.
2396 * ":let &g:option = expr": Set global option value.
2397 */
2398 else if (*arg == '&')
2399 {
2400 /* Find the end of the name. */
2401 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002402 if (p == NULL || (endchars != NULL
2403 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404 EMSG(_(e_letunexp));
2405 else
2406 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002407 long n;
2408 int opt_type;
2409 long numval;
2410 char_u *stringval = NULL;
2411 char_u *s;
2412
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002413 c1 = *p;
2414 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415
2416 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002417 s = get_tv_string_chk(tv); /* != NULL if number or string */
2418 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 {
2420 opt_type = get_option_value(arg, &numval,
2421 &stringval, opt_flags);
2422 if ((opt_type == 1 && *op == '.')
2423 || (opt_type == 0 && *op != '.'))
2424 EMSG2(_(e_letwrong), op);
2425 else
2426 {
2427 if (opt_type == 1) /* number */
2428 {
2429 if (*op == '+')
2430 n = numval + n;
2431 else
2432 n = numval - n;
2433 }
2434 else if (opt_type == 0 && stringval != NULL) /* string */
2435 {
2436 s = concat_str(stringval, s);
2437 vim_free(stringval);
2438 stringval = s;
2439 }
2440 }
2441 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002442 if (s != NULL)
2443 {
2444 set_option_value(arg, n, s, opt_flags);
2445 arg_end = p;
2446 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002447 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 }
2450 }
2451
2452 /*
2453 * ":let @r = expr": Set register contents.
2454 */
2455 else if (*arg == '@')
2456 {
2457 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002458 if (op != NULL && (*op == '+' || *op == '-'))
2459 EMSG2(_(e_letwrong), op);
2460 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002461 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 EMSG(_(e_letunexp));
2463 else
2464 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002465 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466 char_u *s;
2467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002468 p = get_tv_string_chk(tv);
2469 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002471 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 if (s != NULL)
2473 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002474 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002475 vim_free(s);
2476 }
2477 }
2478 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002479 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002481 arg_end = arg + 1;
2482 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002483 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484 }
2485 }
2486
2487 /*
2488 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002491 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002492 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002493 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002494
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002495 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2499 EMSG(_(e_letunexp));
2500 else
2501 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002502 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 arg_end = p;
2504 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507 }
2508
2509 else
2510 EMSG2(_(e_invarg2), arg);
2511
2512 return arg_end;
2513}
2514
2515/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002516 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2517 */
2518 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002519check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002520{
2521 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2522 {
2523 EMSG2(_(e_readonlyvar), arg);
2524 return TRUE;
2525 }
2526 return FALSE;
2527}
2528
2529/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 * Get an lval: variable, Dict item or List item that can be assigned a value
2531 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2532 * "name.key", "name.key[expr]" etc.
2533 * Indexing only works if "name" is an existing List or Dictionary.
2534 * "name" points to the start of the name.
2535 * If "rettv" is not NULL it points to the value to be assigned.
2536 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2537 * wrong; must end in space or cmd separator.
2538 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002539 * flags:
2540 * GLV_QUIET: do not give error messages
2541 * GLV_NO_AUTOLOAD: do not use script autoloading
2542 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002544 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002546 */
2547 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002548get_lval(
2549 char_u *name,
2550 typval_T *rettv,
2551 lval_T *lp,
2552 int unlet,
2553 int skip,
2554 int flags, /* GLV_ values */
2555 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002557 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 char_u *expr_start, *expr_end;
2559 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002560 dictitem_T *v;
2561 typval_T var1;
2562 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002564 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002565 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002566 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002567 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002568 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002571 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572
2573 if (skip)
2574 {
2575 /* When skipping just find the end of the name. */
2576 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002577 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 }
2579
2580 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002581 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 if (expr_start != NULL)
2583 {
2584 /* Don't expand the name when we already know there is an error. */
2585 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2586 && *p != '[' && *p != '.')
2587 {
2588 EMSG(_(e_trailing));
2589 return NULL;
2590 }
2591
2592 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2593 if (lp->ll_exp_name == NULL)
2594 {
2595 /* Report an invalid expression in braces, unless the
2596 * expression evaluation has been cancelled due to an
2597 * aborting error, an interrupt, or an exception. */
2598 if (!aborting() && !quiet)
2599 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002600 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 EMSG2(_(e_invarg2), name);
2602 return NULL;
2603 }
2604 }
2605 lp->ll_name = lp->ll_exp_name;
2606 }
2607 else
2608 lp->ll_name = name;
2609
2610 /* Without [idx] or .key we are done. */
2611 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2612 return p;
2613
2614 cc = *p;
2615 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002616 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 if (v == NULL && !quiet)
2618 EMSG2(_(e_undefvar), lp->ll_name);
2619 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002620 if (v == NULL)
2621 return NULL;
2622
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 /*
2624 * Loop until no more [idx] or .key is following.
2625 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002626 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002628 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2630 && !(lp->ll_tv->v_type == VAR_DICT
2631 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002632 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 if (!quiet)
2634 EMSG(_("E689: Can only index a List or Dictionary"));
2635 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002636 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002638 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 if (!quiet)
2640 EMSG(_("E708: [:] must come last"));
2641 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002642 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002643
Bram Moolenaar8c711452005-01-14 21:53:12 +00002644 len = -1;
2645 if (*p == '.')
2646 {
2647 key = p + 1;
2648 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2649 ;
2650 if (len == 0)
2651 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (!quiet)
2653 EMSG(_(e_emptykey));
2654 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002655 }
2656 p = key + len;
2657 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002658 else
2659 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002661 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 if (*p == ':')
2663 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002664 else
2665 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 empty1 = FALSE;
2667 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002669 if (get_tv_string_chk(&var1) == NULL)
2670 {
2671 /* not a number or string */
2672 clear_tv(&var1);
2673 return NULL;
2674 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 }
2676
2677 /* Optionally get the second index [ :expr]. */
2678 if (*p == ':')
2679 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002683 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002684 if (!empty1)
2685 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002687 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 if (rettv != NULL && (rettv->v_type != VAR_LIST
2689 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 if (!quiet)
2692 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 if (!empty1)
2694 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 }
2697 p = skipwhite(p + 1);
2698 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 else
2701 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2704 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 if (!empty1)
2706 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002709 if (get_tv_string_chk(&var2) == NULL)
2710 {
2711 /* not a number or string */
2712 if (!empty1)
2713 clear_tv(&var1);
2714 clear_tv(&var2);
2715 return NULL;
2716 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002719 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002722
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002724 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 if (!quiet)
2726 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 if (!empty1)
2728 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 }
2733
2734 /* Skip to past ']'. */
2735 ++p;
2736 }
2737
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 {
2740 if (len == -1)
2741 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002743 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 if (*key == NUL)
2745 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 if (!quiet)
2747 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 }
2751 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002752 lp->ll_list = NULL;
2753 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002754 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002755
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002756 /* When assigning to a scope dictionary check that a function and
2757 * variable name is valid (only variable name unless it is l: or
2758 * g: dictionary). Disallow overwriting a builtin function. */
2759 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002760 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002761 int prevval;
2762 int wrong;
2763
2764 if (len != -1)
2765 {
2766 prevval = key[len];
2767 key[len] = NUL;
2768 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002769 else
2770 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002771 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2772 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002773 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002774 || !valid_varname(key);
2775 if (len != -1)
2776 key[len] = prevval;
2777 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002778 return NULL;
2779 }
2780
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002783 /* Can't add "v:" variable. */
2784 if (lp->ll_dict == &vimvardict)
2785 {
2786 EMSG2(_(e_illvar), name);
2787 return NULL;
2788 }
2789
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002790 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002794 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 if (len == -1)
2796 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 }
2799 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002802 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002803 if (len == -1)
2804 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 p = NULL;
2807 break;
2808 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002809 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002810 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002811 return NULL;
2812
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 if (len == -1)
2814 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 }
2817 else
2818 {
2819 /*
2820 * Get the number and item for the only or first index of the List.
2821 */
2822 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 else
2825 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002826 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 clear_tv(&var1);
2828 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002829 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 lp->ll_list = lp->ll_tv->vval.v_list;
2831 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2832 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002834 if (lp->ll_n1 < 0)
2835 {
2836 lp->ll_n1 = 0;
2837 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2838 }
2839 }
2840 if (lp->ll_li == NULL)
2841 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002844 if (!quiet)
2845 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 }
2848
2849 /*
2850 * May need to find the item or absolute index for the second
2851 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 * When no index given: "lp->ll_empty2" is TRUE.
2853 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002857 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002862 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002863 {
2864 if (!quiet)
2865 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002867 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 }
2870
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2872 if (lp->ll_n1 < 0)
2873 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2874 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002875 {
2876 if (!quiet)
2877 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002879 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002880 }
2881
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002883 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002884 }
2885
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 return p;
2887}
2888
2889/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002890 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 */
2892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002893clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894{
2895 vim_free(lp->ll_exp_name);
2896 vim_free(lp->ll_newkey);
2897}
2898
2899/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002900 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002902 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 */
2904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002905set_var_lval(
2906 lval_T *lp,
2907 char_u *endp,
2908 typval_T *rettv,
2909 int copy,
2910 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911{
2912 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002913 listitem_T *ri;
2914 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915
2916 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002917 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002919 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 cc = *endp;
2921 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002922 if (op != NULL && *op != '=')
2923 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002924 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002925
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002926 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002927 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002928 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002929 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002930 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002931 if ((di == NULL
2932 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2933 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2934 FALSE)))
2935 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002936 set_var(lp->ll_name, &tv, FALSE);
2937 clear_tv(&tv);
2938 }
2939 }
2940 else
2941 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002943 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002945 else if (tv_check_lock(lp->ll_newkey == NULL
2946 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002947 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002948 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 else if (lp->ll_range)
2950 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002951 listitem_T *ll_li = lp->ll_li;
2952 int ll_n1 = lp->ll_n1;
2953
2954 /*
2955 * Check whether any of the list items is locked
2956 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002957 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002958 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002959 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002960 return;
2961 ri = ri->li_next;
2962 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2963 break;
2964 ll_li = ll_li->li_next;
2965 ++ll_n1;
2966 }
2967
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002968 /*
2969 * Assign the List values to the list items.
2970 */
2971 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002972 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002973 if (op != NULL && *op != '=')
2974 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2975 else
2976 {
2977 clear_tv(&lp->ll_li->li_tv);
2978 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2979 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 ri = ri->li_next;
2981 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2982 break;
2983 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002984 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002985 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002986 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002987 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 ri = NULL;
2989 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002990 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002991 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002992 lp->ll_li = lp->ll_li->li_next;
2993 ++lp->ll_n1;
2994 }
2995 if (ri != NULL)
2996 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002997 else if (lp->ll_empty2
2998 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002999 : lp->ll_n1 != lp->ll_n2)
3000 EMSG(_("E711: List value has not enough items"));
3001 }
3002 else
3003 {
3004 /*
3005 * Assign to a List or Dictionary item.
3006 */
3007 if (lp->ll_newkey != NULL)
3008 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003009 if (op != NULL && *op != '=')
3010 {
3011 EMSG2(_(e_letwrong), op);
3012 return;
3013 }
3014
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003015 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003016 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003017 if (di == NULL)
3018 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003019 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3020 {
3021 vim_free(di);
3022 return;
3023 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003024 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003025 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003026 else if (op != NULL && *op != '=')
3027 {
3028 tv_op(lp->ll_tv, rettv, op);
3029 return;
3030 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003031 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003032 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003033
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003034 /*
3035 * Assign the value to the variable or list item.
3036 */
3037 if (copy)
3038 copy_tv(rettv, lp->ll_tv);
3039 else
3040 {
3041 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003042 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003043 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003044 }
3045 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003046}
3047
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003048/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003049 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3050 * Returns OK or FAIL.
3051 */
3052 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003053tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003054{
3055 long n;
3056 char_u numbuf[NUMBUFLEN];
3057 char_u *s;
3058
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003059 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3060 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3061 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003062 {
3063 switch (tv1->v_type)
3064 {
3065 case VAR_DICT:
3066 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003067 case VAR_SPECIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003068 break;
3069
3070 case VAR_LIST:
3071 if (*op != '+' || tv2->v_type != VAR_LIST)
3072 break;
3073 /* List += List */
3074 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3075 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3076 return OK;
3077
3078 case VAR_NUMBER:
3079 case VAR_STRING:
3080 if (tv2->v_type == VAR_LIST)
3081 break;
3082 if (*op == '+' || *op == '-')
3083 {
3084 /* nr += nr or nr -= nr*/
3085 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003086#ifdef FEAT_FLOAT
3087 if (tv2->v_type == VAR_FLOAT)
3088 {
3089 float_T f = n;
3090
3091 if (*op == '+')
3092 f += tv2->vval.v_float;
3093 else
3094 f -= tv2->vval.v_float;
3095 clear_tv(tv1);
3096 tv1->v_type = VAR_FLOAT;
3097 tv1->vval.v_float = f;
3098 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003099 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003100#endif
3101 {
3102 if (*op == '+')
3103 n += get_tv_number(tv2);
3104 else
3105 n -= get_tv_number(tv2);
3106 clear_tv(tv1);
3107 tv1->v_type = VAR_NUMBER;
3108 tv1->vval.v_number = n;
3109 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003110 }
3111 else
3112 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003113 if (tv2->v_type == VAR_FLOAT)
3114 break;
3115
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003116 /* str .= str */
3117 s = get_tv_string(tv1);
3118 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3119 clear_tv(tv1);
3120 tv1->v_type = VAR_STRING;
3121 tv1->vval.v_string = s;
3122 }
3123 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003124
3125#ifdef FEAT_FLOAT
3126 case VAR_FLOAT:
3127 {
3128 float_T f;
3129
3130 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3131 && tv2->v_type != VAR_NUMBER
3132 && tv2->v_type != VAR_STRING))
3133 break;
3134 if (tv2->v_type == VAR_FLOAT)
3135 f = tv2->vval.v_float;
3136 else
3137 f = get_tv_number(tv2);
3138 if (*op == '+')
3139 tv1->vval.v_float += f;
3140 else
3141 tv1->vval.v_float -= f;
3142 }
3143 return OK;
3144#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003145 }
3146 }
3147
3148 EMSG2(_(e_letwrong), op);
3149 return FAIL;
3150}
3151
3152/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003153 * Add a watcher to a list.
3154 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003155 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003156list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003157{
3158 lw->lw_next = l->lv_watch;
3159 l->lv_watch = lw;
3160}
3161
3162/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003163 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003164 * No warning when it isn't found...
3165 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003166 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003167list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003168{
Bram Moolenaar33570922005-01-25 22:26:29 +00003169 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003170
3171 lwp = &l->lv_watch;
3172 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3173 {
3174 if (lw == lwrem)
3175 {
3176 *lwp = lw->lw_next;
3177 break;
3178 }
3179 lwp = &lw->lw_next;
3180 }
3181}
3182
3183/*
3184 * Just before removing an item from a list: advance watchers to the next
3185 * item.
3186 */
3187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003188list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189{
Bram Moolenaar33570922005-01-25 22:26:29 +00003190 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191
3192 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3193 if (lw->lw_item == item)
3194 lw->lw_item = item->li_next;
3195}
3196
3197/*
3198 * Evaluate the expression used in a ":for var in expr" command.
3199 * "arg" points to "var".
3200 * Set "*errp" to TRUE for an error, FALSE otherwise;
3201 * Return a pointer that holds the info. Null when there is an error.
3202 */
3203 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003204eval_for_line(
3205 char_u *arg,
3206 int *errp,
3207 char_u **nextcmdp,
3208 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003209{
Bram Moolenaar33570922005-01-25 22:26:29 +00003210 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003211 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003212 typval_T tv;
3213 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214
3215 *errp = TRUE; /* default: there is an error */
3216
Bram Moolenaar33570922005-01-25 22:26:29 +00003217 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003218 if (fi == NULL)
3219 return NULL;
3220
3221 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3222 if (expr == NULL)
3223 return fi;
3224
3225 expr = skipwhite(expr);
3226 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3227 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003228 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229 return fi;
3230 }
3231
3232 if (skip)
3233 ++emsg_skip;
3234 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3235 {
3236 *errp = FALSE;
3237 if (!skip)
3238 {
3239 l = tv.vval.v_list;
3240 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003241 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003242 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003243 clear_tv(&tv);
3244 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003245 else
3246 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003247 /* No need to increment the refcount, it's already set for the
3248 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003249 fi->fi_list = l;
3250 list_add_watch(l, &fi->fi_lw);
3251 fi->fi_lw.lw_item = l->lv_first;
3252 }
3253 }
3254 }
3255 if (skip)
3256 --emsg_skip;
3257
3258 return fi;
3259}
3260
3261/*
3262 * Use the first item in a ":for" list. Advance to the next.
3263 * Assign the values to the variable (list). "arg" points to the first one.
3264 * Return TRUE when a valid item was found, FALSE when at end of list or
3265 * something wrong.
3266 */
3267 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003268next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003269{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003270 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003271 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003272 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273
3274 item = fi->fi_lw.lw_item;
3275 if (item == NULL)
3276 result = FALSE;
3277 else
3278 {
3279 fi->fi_lw.lw_item = item->li_next;
3280 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3281 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3282 }
3283 return result;
3284}
3285
3286/*
3287 * Free the structure used to store info used by ":for".
3288 */
3289 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003290free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003291{
Bram Moolenaar33570922005-01-25 22:26:29 +00003292 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003293
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003294 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003295 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003296 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003297 list_unref(fi->fi_list);
3298 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003299 vim_free(fi);
3300}
3301
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3303
3304 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003305set_context_for_expression(
3306 expand_T *xp,
3307 char_u *arg,
3308 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309{
3310 int got_eq = FALSE;
3311 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003312 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003314 if (cmdidx == CMD_let)
3315 {
3316 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003317 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003318 {
3319 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003320 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321 {
3322 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003323 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324 if (vim_iswhite(*p))
3325 break;
3326 }
3327 return;
3328 }
3329 }
3330 else
3331 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3332 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 while ((xp->xp_pattern = vim_strpbrk(arg,
3334 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3335 {
3336 c = *xp->xp_pattern;
3337 if (c == '&')
3338 {
3339 c = xp->xp_pattern[1];
3340 if (c == '&')
3341 {
3342 ++xp->xp_pattern;
3343 xp->xp_context = cmdidx != CMD_let || got_eq
3344 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3345 }
3346 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003347 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003349 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3350 xp->xp_pattern += 2;
3351
3352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 }
3354 else if (c == '$')
3355 {
3356 /* environment variable */
3357 xp->xp_context = EXPAND_ENV_VARS;
3358 }
3359 else if (c == '=')
3360 {
3361 got_eq = TRUE;
3362 xp->xp_context = EXPAND_EXPRESSION;
3363 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003364 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 && xp->xp_context == EXPAND_FUNCTIONS
3366 && vim_strchr(xp->xp_pattern, '(') == NULL)
3367 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003368 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 break;
3370 }
3371 else if (cmdidx != CMD_let || got_eq)
3372 {
3373 if (c == '"') /* string */
3374 {
3375 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3376 if (c == '\\' && xp->xp_pattern[1] != NUL)
3377 ++xp->xp_pattern;
3378 xp->xp_context = EXPAND_NOTHING;
3379 }
3380 else if (c == '\'') /* literal string */
3381 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003382 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3384 /* skip */ ;
3385 xp->xp_context = EXPAND_NOTHING;
3386 }
3387 else if (c == '|')
3388 {
3389 if (xp->xp_pattern[1] == '|')
3390 {
3391 ++xp->xp_pattern;
3392 xp->xp_context = EXPAND_EXPRESSION;
3393 }
3394 else
3395 xp->xp_context = EXPAND_COMMANDS;
3396 }
3397 else
3398 xp->xp_context = EXPAND_EXPRESSION;
3399 }
3400 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003401 /* Doesn't look like something valid, expand as an expression
3402 * anyway. */
3403 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 arg = xp->xp_pattern;
3405 if (*arg != NUL)
3406 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3407 /* skip */ ;
3408 }
3409 xp->xp_pattern = arg;
3410}
3411
3412#endif /* FEAT_CMDL_COMPL */
3413
3414/*
3415 * ":1,25call func(arg1, arg2)" function call.
3416 */
3417 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003418ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419{
3420 char_u *arg = eap->arg;
3421 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003423 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003425 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 linenr_T lnum;
3427 int doesrange;
3428 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003429 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003431 if (eap->skip)
3432 {
3433 /* trans_function_name() doesn't work well when skipping, use eval0()
3434 * instead to skip to any following command, e.g. for:
3435 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003436 ++emsg_skip;
3437 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3438 clear_tv(&rettv);
3439 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003440 return;
3441 }
3442
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003443 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003444 if (fudi.fd_newkey != NULL)
3445 {
3446 /* Still need to give an error message for missing key. */
3447 EMSG2(_(e_dictkey), fudi.fd_newkey);
3448 vim_free(fudi.fd_newkey);
3449 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003450 if (tofree == NULL)
3451 return;
3452
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003453 /* Increase refcount on dictionary, it could get deleted when evaluating
3454 * the arguments. */
3455 if (fudi.fd_dict != NULL)
3456 ++fudi.fd_dict->dv_refcount;
3457
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003458 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003459 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003460 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461
Bram Moolenaar532c7802005-01-27 14:44:31 +00003462 /* Skip white space to allow ":call func ()". Not good, but required for
3463 * backward compatibility. */
3464 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003465 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
3467 if (*startarg != '(')
3468 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003469 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 goto end;
3471 }
3472
3473 /*
3474 * When skipping, evaluate the function once, to find the end of the
3475 * arguments.
3476 * When the function takes a range, this is discovered after the first
3477 * call, and the loop is broken.
3478 */
3479 if (eap->skip)
3480 {
3481 ++emsg_skip;
3482 lnum = eap->line2; /* do it once, also with an invalid range */
3483 }
3484 else
3485 lnum = eap->line1;
3486 for ( ; lnum <= eap->line2; ++lnum)
3487 {
3488 if (!eap->skip && eap->addr_count > 0)
3489 {
3490 curwin->w_cursor.lnum = lnum;
3491 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003492#ifdef FEAT_VIRTUALEDIT
3493 curwin->w_cursor.coladd = 0;
3494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 }
3496 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003497 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003498 eap->line1, eap->line2, &doesrange,
3499 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 {
3501 failed = TRUE;
3502 break;
3503 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003504
3505 /* Handle a function returning a Funcref, Dictionary or List. */
3506 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3507 {
3508 failed = TRUE;
3509 break;
3510 }
3511
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003512 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 if (doesrange || eap->skip)
3514 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003515
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003517 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003518 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003519 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 if (aborting())
3521 break;
3522 }
3523 if (eap->skip)
3524 --emsg_skip;
3525
3526 if (!failed)
3527 {
3528 /* Check for trailing illegal characters and a following command. */
3529 if (!ends_excmd(*arg))
3530 {
3531 emsg_severe = TRUE;
3532 EMSG(_(e_trailing));
3533 }
3534 else
3535 eap->nextcmd = check_nextcmd(arg);
3536 }
3537
3538end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003539 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003540 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541}
3542
3543/*
3544 * ":unlet[!] var1 ... " command.
3545 */
3546 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003547ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003549 ex_unletlock(eap, eap->arg, 0);
3550}
3551
3552/*
3553 * ":lockvar" and ":unlockvar" commands
3554 */
3555 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003556ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003557{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003559 int deep = 2;
3560
3561 if (eap->forceit)
3562 deep = -1;
3563 else if (vim_isdigit(*arg))
3564 {
3565 deep = getdigits(&arg);
3566 arg = skipwhite(arg);
3567 }
3568
3569 ex_unletlock(eap, arg, deep);
3570}
3571
3572/*
3573 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3574 */
3575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003576ex_unletlock(
3577 exarg_T *eap,
3578 char_u *argstart,
3579 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003580{
3581 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003584 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585
3586 do
3587 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003588 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003589 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003590 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003591 if (lv.ll_name == NULL)
3592 error = TRUE; /* error but continue parsing */
3593 if (name_end == NULL || (!vim_iswhite(*name_end)
3594 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003596 if (name_end != NULL)
3597 {
3598 emsg_severe = TRUE;
3599 EMSG(_(e_trailing));
3600 }
3601 if (!(eap->skip || error))
3602 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 break;
3604 }
3605
3606 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003607 {
3608 if (eap->cmdidx == CMD_unlet)
3609 {
3610 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3611 error = TRUE;
3612 }
3613 else
3614 {
3615 if (do_lock_var(&lv, name_end, deep,
3616 eap->cmdidx == CMD_lockvar) == FAIL)
3617 error = TRUE;
3618 }
3619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003621 if (!eap->skip)
3622 clear_lval(&lv);
3623
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 arg = skipwhite(name_end);
3625 } while (!ends_excmd(*arg));
3626
3627 eap->nextcmd = check_nextcmd(arg);
3628}
3629
Bram Moolenaar8c711452005-01-14 21:53:12 +00003630 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003631do_unlet_var(
3632 lval_T *lp,
3633 char_u *name_end,
3634 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003635{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003636 int ret = OK;
3637 int cc;
3638
3639 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003640 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003641 cc = *name_end;
3642 *name_end = NUL;
3643
3644 /* Normal name or expanded name. */
3645 if (check_changedtick(lp->ll_name))
3646 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003647 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003648 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003649 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003650 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003651 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003652 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003653 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003654 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003655 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003656 else if (lp->ll_range)
3657 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003658 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003659 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003660 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003661
3662 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3663 {
3664 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003665 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003666 return FAIL;
3667 ll_li = li;
3668 ++ll_n1;
3669 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003670
3671 /* Delete a range of List items. */
3672 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3673 {
3674 li = lp->ll_li->li_next;
3675 listitem_remove(lp->ll_list, lp->ll_li);
3676 lp->ll_li = li;
3677 ++lp->ll_n1;
3678 }
3679 }
3680 else
3681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003682 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003683 /* unlet a List item. */
3684 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003685 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003686 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003687 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003688 }
3689
3690 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003691}
3692
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693/*
3694 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003695 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 */
3697 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003698do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699{
Bram Moolenaar33570922005-01-25 22:26:29 +00003700 hashtab_T *ht;
3701 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003702 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003703 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003704 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705
Bram Moolenaar33570922005-01-25 22:26:29 +00003706 ht = find_var_ht(name, &varname);
3707 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003709 if (ht == &globvarht)
3710 d = &globvardict;
3711 else if (current_funccal != NULL
3712 && ht == &current_funccal->l_vars.dv_hashtab)
3713 d = &current_funccal->l_vars;
3714 else if (ht == &compat_hashtab)
3715 d = &vimvardict;
3716 else
3717 {
3718 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3719 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3720 }
3721 if (d == NULL)
3722 {
3723 EMSG2(_(e_intern2), "do_unlet()");
3724 return FAIL;
3725 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003726 hi = hash_find(ht, varname);
3727 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003728 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003729 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003730 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003731 || var_check_ro(di->di_flags, name, FALSE)
3732 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003733 return FAIL;
3734
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003735 delete_var(ht, hi);
3736 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003739 if (forceit)
3740 return OK;
3741 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 return FAIL;
3743}
3744
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003745/*
3746 * Lock or unlock variable indicated by "lp".
3747 * "deep" is the levels to go (-1 for unlimited);
3748 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3749 */
3750 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003751do_lock_var(
3752 lval_T *lp,
3753 char_u *name_end,
3754 int deep,
3755 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003756{
3757 int ret = OK;
3758 int cc;
3759 dictitem_T *di;
3760
3761 if (deep == 0) /* nothing to do */
3762 return OK;
3763
3764 if (lp->ll_tv == NULL)
3765 {
3766 cc = *name_end;
3767 *name_end = NUL;
3768
3769 /* Normal name or expanded name. */
3770 if (check_changedtick(lp->ll_name))
3771 ret = FAIL;
3772 else
3773 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003774 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003775 if (di == NULL)
3776 ret = FAIL;
3777 else
3778 {
3779 if (lock)
3780 di->di_flags |= DI_FLAGS_LOCK;
3781 else
3782 di->di_flags &= ~DI_FLAGS_LOCK;
3783 item_lock(&di->di_tv, deep, lock);
3784 }
3785 }
3786 *name_end = cc;
3787 }
3788 else if (lp->ll_range)
3789 {
3790 listitem_T *li = lp->ll_li;
3791
3792 /* (un)lock a range of List items. */
3793 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3794 {
3795 item_lock(&li->li_tv, deep, lock);
3796 li = li->li_next;
3797 ++lp->ll_n1;
3798 }
3799 }
3800 else if (lp->ll_list != NULL)
3801 /* (un)lock a List item. */
3802 item_lock(&lp->ll_li->li_tv, deep, lock);
3803 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003804 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003805 item_lock(&lp->ll_di->di_tv, deep, lock);
3806
3807 return ret;
3808}
3809
3810/*
3811 * Lock or unlock an item. "deep" is nr of levels to go.
3812 */
3813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003814item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003815{
3816 static int recurse = 0;
3817 list_T *l;
3818 listitem_T *li;
3819 dict_T *d;
3820 hashitem_T *hi;
3821 int todo;
3822
3823 if (recurse >= DICT_MAXNEST)
3824 {
3825 EMSG(_("E743: variable nested too deep for (un)lock"));
3826 return;
3827 }
3828 if (deep == 0)
3829 return;
3830 ++recurse;
3831
3832 /* lock/unlock the item itself */
3833 if (lock)
3834 tv->v_lock |= VAR_LOCKED;
3835 else
3836 tv->v_lock &= ~VAR_LOCKED;
3837
3838 switch (tv->v_type)
3839 {
3840 case VAR_LIST:
3841 if ((l = tv->vval.v_list) != NULL)
3842 {
3843 if (lock)
3844 l->lv_lock |= VAR_LOCKED;
3845 else
3846 l->lv_lock &= ~VAR_LOCKED;
3847 if (deep < 0 || deep > 1)
3848 /* recursive: lock/unlock the items the List contains */
3849 for (li = l->lv_first; li != NULL; li = li->li_next)
3850 item_lock(&li->li_tv, deep - 1, lock);
3851 }
3852 break;
3853 case VAR_DICT:
3854 if ((d = tv->vval.v_dict) != NULL)
3855 {
3856 if (lock)
3857 d->dv_lock |= VAR_LOCKED;
3858 else
3859 d->dv_lock &= ~VAR_LOCKED;
3860 if (deep < 0 || deep > 1)
3861 {
3862 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003863 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003864 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3865 {
3866 if (!HASHITEM_EMPTY(hi))
3867 {
3868 --todo;
3869 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3870 }
3871 }
3872 }
3873 }
3874 }
3875 --recurse;
3876}
3877
Bram Moolenaara40058a2005-07-11 22:42:07 +00003878/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003879 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3880 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003881 */
3882 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003883tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003884{
3885 return (tv->v_lock & VAR_LOCKED)
3886 || (tv->v_type == VAR_LIST
3887 && tv->vval.v_list != NULL
3888 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3889 || (tv->v_type == VAR_DICT
3890 && tv->vval.v_dict != NULL
3891 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3892}
3893
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3895/*
3896 * Delete all "menutrans_" variables.
3897 */
3898 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003899del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900{
Bram Moolenaar33570922005-01-25 22:26:29 +00003901 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003902 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903
Bram Moolenaar33570922005-01-25 22:26:29 +00003904 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003905 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003906 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003907 {
3908 if (!HASHITEM_EMPTY(hi))
3909 {
3910 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003911 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3912 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003913 }
3914 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003915 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916}
3917#endif
3918
3919#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3920
3921/*
3922 * Local string buffer for the next two functions to store a variable name
3923 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3924 * get_user_var_name().
3925 */
3926
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003927static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928
3929static char_u *varnamebuf = NULL;
3930static int varnamebuflen = 0;
3931
3932/*
3933 * Function to concatenate a prefix and a variable name.
3934 */
3935 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003936cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937{
3938 int len;
3939
3940 len = (int)STRLEN(name) + 3;
3941 if (len > varnamebuflen)
3942 {
3943 vim_free(varnamebuf);
3944 len += 10; /* some additional space */
3945 varnamebuf = alloc(len);
3946 if (varnamebuf == NULL)
3947 {
3948 varnamebuflen = 0;
3949 return NULL;
3950 }
3951 varnamebuflen = len;
3952 }
3953 *varnamebuf = prefix;
3954 varnamebuf[1] = ':';
3955 STRCPY(varnamebuf + 2, name);
3956 return varnamebuf;
3957}
3958
3959/*
3960 * Function given to ExpandGeneric() to obtain the list of user defined
3961 * (global/buffer/window/built-in) variable names.
3962 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003964get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003966 static long_u gdone;
3967 static long_u bdone;
3968 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003969#ifdef FEAT_WINDOWS
3970 static long_u tdone;
3971#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003972 static int vidx;
3973 static hashitem_T *hi;
3974 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975
3976 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003977 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003978 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003979#ifdef FEAT_WINDOWS
3980 tdone = 0;
3981#endif
3982 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003983
3984 /* Global variables */
3985 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003987 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003988 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003989 else
3990 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003991 while (HASHITEM_EMPTY(hi))
3992 ++hi;
3993 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3994 return cat_prefix_varname('g', hi->hi_key);
3995 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003997
3998 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003999 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004000 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004002 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004003 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004004 else
4005 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004006 while (HASHITEM_EMPTY(hi))
4007 ++hi;
4008 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004010 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004012 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 return (char_u *)"b:changedtick";
4014 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004015
4016 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004017 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004018 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004020 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004021 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004022 else
4023 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004024 while (HASHITEM_EMPTY(hi))
4025 ++hi;
4026 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004028
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004029#ifdef FEAT_WINDOWS
4030 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004031 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004032 if (tdone < ht->ht_used)
4033 {
4034 if (tdone++ == 0)
4035 hi = ht->ht_array;
4036 else
4037 ++hi;
4038 while (HASHITEM_EMPTY(hi))
4039 ++hi;
4040 return cat_prefix_varname('t', hi->hi_key);
4041 }
4042#endif
4043
Bram Moolenaar33570922005-01-25 22:26:29 +00004044 /* v: variables */
4045 if (vidx < VV_LEN)
4046 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047
4048 vim_free(varnamebuf);
4049 varnamebuf = NULL;
4050 varnamebuflen = 0;
4051 return NULL;
4052}
4053
4054#endif /* FEAT_CMDL_COMPL */
4055
4056/*
4057 * types for expressions.
4058 */
4059typedef enum
4060{
4061 TYPE_UNKNOWN = 0
4062 , TYPE_EQUAL /* == */
4063 , TYPE_NEQUAL /* != */
4064 , TYPE_GREATER /* > */
4065 , TYPE_GEQUAL /* >= */
4066 , TYPE_SMALLER /* < */
4067 , TYPE_SEQUAL /* <= */
4068 , TYPE_MATCH /* =~ */
4069 , TYPE_NOMATCH /* !~ */
4070} exptype_T;
4071
4072/*
4073 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004074 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4076 */
4077
4078/*
4079 * Handle zero level expression.
4080 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004081 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004082 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 * Return OK or FAIL.
4084 */
4085 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004086eval0(
4087 char_u *arg,
4088 typval_T *rettv,
4089 char_u **nextcmd,
4090 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091{
4092 int ret;
4093 char_u *p;
4094
4095 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004096 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 if (ret == FAIL || !ends_excmd(*p))
4098 {
4099 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004100 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 /*
4102 * Report the invalid expression unless the expression evaluation has
4103 * been cancelled due to an aborting error, an interrupt, or an
4104 * exception.
4105 */
4106 if (!aborting())
4107 EMSG2(_(e_invexpr2), arg);
4108 ret = FAIL;
4109 }
4110 if (nextcmd != NULL)
4111 *nextcmd = check_nextcmd(p);
4112
4113 return ret;
4114}
4115
4116/*
4117 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004118 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 *
4120 * "arg" must point to the first non-white of the expression.
4121 * "arg" is advanced to the next non-white after the recognized expression.
4122 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004123 * Note: "rettv.v_lock" is not set.
4124 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 * Return OK or FAIL.
4126 */
4127 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004128eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129{
4130 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004131 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132
4133 /*
4134 * Get the first variable.
4135 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004136 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 return FAIL;
4138
4139 if ((*arg)[0] == '?')
4140 {
4141 result = FALSE;
4142 if (evaluate)
4143 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004144 int error = FALSE;
4145
4146 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004148 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004149 if (error)
4150 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 }
4152
4153 /*
4154 * Get the second variable.
4155 */
4156 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004157 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 return FAIL;
4159
4160 /*
4161 * Check for the ":".
4162 */
4163 if ((*arg)[0] != ':')
4164 {
4165 EMSG(_("E109: Missing ':' after '?'"));
4166 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004167 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 return FAIL;
4169 }
4170
4171 /*
4172 * Get the third variable.
4173 */
4174 *arg = skipwhite(*arg + 1);
4175 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4176 {
4177 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004178 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 return FAIL;
4180 }
4181 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004182 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 }
4184
4185 return OK;
4186}
4187
4188/*
4189 * Handle first level expression:
4190 * expr2 || expr2 || expr2 logical OR
4191 *
4192 * "arg" must point to the first non-white of the expression.
4193 * "arg" is advanced to the next non-white after the recognized expression.
4194 *
4195 * Return OK or FAIL.
4196 */
4197 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004198eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199{
Bram Moolenaar33570922005-01-25 22:26:29 +00004200 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 long result;
4202 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004203 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204
4205 /*
4206 * Get the first variable.
4207 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004208 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 return FAIL;
4210
4211 /*
4212 * Repeat until there is no following "||".
4213 */
4214 first = TRUE;
4215 result = FALSE;
4216 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4217 {
4218 if (evaluate && first)
4219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004220 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004222 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004223 if (error)
4224 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 first = FALSE;
4226 }
4227
4228 /*
4229 * Get the second variable.
4230 */
4231 *arg = skipwhite(*arg + 2);
4232 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4233 return FAIL;
4234
4235 /*
4236 * Compute the result.
4237 */
4238 if (evaluate && !result)
4239 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004240 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004242 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 if (error)
4244 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 }
4246 if (evaluate)
4247 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004248 rettv->v_type = VAR_NUMBER;
4249 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 }
4251 }
4252
4253 return OK;
4254}
4255
4256/*
4257 * Handle second level expression:
4258 * expr3 && expr3 && expr3 logical AND
4259 *
4260 * "arg" must point to the first non-white of the expression.
4261 * "arg" is advanced to the next non-white after the recognized expression.
4262 *
4263 * Return OK or FAIL.
4264 */
4265 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004266eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267{
Bram Moolenaar33570922005-01-25 22:26:29 +00004268 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 long result;
4270 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004271 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272
4273 /*
4274 * Get the first variable.
4275 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004276 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 return FAIL;
4278
4279 /*
4280 * Repeat until there is no following "&&".
4281 */
4282 first = TRUE;
4283 result = TRUE;
4284 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4285 {
4286 if (evaluate && first)
4287 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004288 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004290 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004291 if (error)
4292 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 first = FALSE;
4294 }
4295
4296 /*
4297 * Get the second variable.
4298 */
4299 *arg = skipwhite(*arg + 2);
4300 if (eval4(arg, &var2, evaluate && result) == FAIL)
4301 return FAIL;
4302
4303 /*
4304 * Compute the result.
4305 */
4306 if (evaluate && result)
4307 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004308 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004310 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004311 if (error)
4312 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 }
4314 if (evaluate)
4315 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004316 rettv->v_type = VAR_NUMBER;
4317 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 }
4319 }
4320
4321 return OK;
4322}
4323
4324/*
4325 * Handle third level expression:
4326 * var1 == var2
4327 * var1 =~ var2
4328 * var1 != var2
4329 * var1 !~ var2
4330 * var1 > var2
4331 * var1 >= var2
4332 * var1 < var2
4333 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004334 * var1 is var2
4335 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 *
4337 * "arg" must point to the first non-white of the expression.
4338 * "arg" is advanced to the next non-white after the recognized expression.
4339 *
4340 * Return OK or FAIL.
4341 */
4342 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004343eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344{
Bram Moolenaar33570922005-01-25 22:26:29 +00004345 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 char_u *p;
4347 int i;
4348 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004349 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 int len = 2;
4351 long n1, n2;
4352 char_u *s1, *s2;
4353 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4354 regmatch_T regmatch;
4355 int ic;
4356 char_u *save_cpo;
4357
4358 /*
4359 * Get the first variable.
4360 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004361 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 return FAIL;
4363
4364 p = *arg;
4365 switch (p[0])
4366 {
4367 case '=': if (p[1] == '=')
4368 type = TYPE_EQUAL;
4369 else if (p[1] == '~')
4370 type = TYPE_MATCH;
4371 break;
4372 case '!': if (p[1] == '=')
4373 type = TYPE_NEQUAL;
4374 else if (p[1] == '~')
4375 type = TYPE_NOMATCH;
4376 break;
4377 case '>': if (p[1] != '=')
4378 {
4379 type = TYPE_GREATER;
4380 len = 1;
4381 }
4382 else
4383 type = TYPE_GEQUAL;
4384 break;
4385 case '<': if (p[1] != '=')
4386 {
4387 type = TYPE_SMALLER;
4388 len = 1;
4389 }
4390 else
4391 type = TYPE_SEQUAL;
4392 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004393 case 'i': if (p[1] == 's')
4394 {
4395 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4396 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004397 i = p[len];
4398 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004399 {
4400 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4401 type_is = TRUE;
4402 }
4403 }
4404 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 }
4406
4407 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004408 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 */
4410 if (type != TYPE_UNKNOWN)
4411 {
4412 /* extra question mark appended: ignore case */
4413 if (p[len] == '?')
4414 {
4415 ic = TRUE;
4416 ++len;
4417 }
4418 /* extra '#' appended: match case */
4419 else if (p[len] == '#')
4420 {
4421 ic = FALSE;
4422 ++len;
4423 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004424 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 else
4426 ic = p_ic;
4427
4428 /*
4429 * Get the second variable.
4430 */
4431 *arg = skipwhite(p + len);
4432 if (eval5(arg, &var2, evaluate) == FAIL)
4433 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004434 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 return FAIL;
4436 }
4437
4438 if (evaluate)
4439 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004440 if (type_is && rettv->v_type != var2.v_type)
4441 {
4442 /* For "is" a different type always means FALSE, for "notis"
4443 * it means TRUE. */
4444 n1 = (type == TYPE_NEQUAL);
4445 }
4446 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4447 {
4448 if (type_is)
4449 {
4450 n1 = (rettv->v_type == var2.v_type
4451 && rettv->vval.v_list == var2.vval.v_list);
4452 if (type == TYPE_NEQUAL)
4453 n1 = !n1;
4454 }
4455 else if (rettv->v_type != var2.v_type
4456 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4457 {
4458 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004459 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004460 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004461 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004462 clear_tv(rettv);
4463 clear_tv(&var2);
4464 return FAIL;
4465 }
4466 else
4467 {
4468 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004469 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4470 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004471 if (type == TYPE_NEQUAL)
4472 n1 = !n1;
4473 }
4474 }
4475
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004476 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4477 {
4478 if (type_is)
4479 {
4480 n1 = (rettv->v_type == var2.v_type
4481 && rettv->vval.v_dict == var2.vval.v_dict);
4482 if (type == TYPE_NEQUAL)
4483 n1 = !n1;
4484 }
4485 else if (rettv->v_type != var2.v_type
4486 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4487 {
4488 if (rettv->v_type != var2.v_type)
4489 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4490 else
4491 EMSG(_("E736: Invalid operation for Dictionary"));
4492 clear_tv(rettv);
4493 clear_tv(&var2);
4494 return FAIL;
4495 }
4496 else
4497 {
4498 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004499 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4500 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004501 if (type == TYPE_NEQUAL)
4502 n1 = !n1;
4503 }
4504 }
4505
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004506 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4507 {
4508 if (rettv->v_type != var2.v_type
4509 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4510 {
4511 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004512 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004513 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004514 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004515 clear_tv(rettv);
4516 clear_tv(&var2);
4517 return FAIL;
4518 }
4519 else
4520 {
4521 /* Compare two Funcrefs for being equal or unequal. */
4522 if (rettv->vval.v_string == NULL
4523 || var2.vval.v_string == NULL)
4524 n1 = FALSE;
4525 else
4526 n1 = STRCMP(rettv->vval.v_string,
4527 var2.vval.v_string) == 0;
4528 if (type == TYPE_NEQUAL)
4529 n1 = !n1;
4530 }
4531 }
4532
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004533#ifdef FEAT_FLOAT
4534 /*
4535 * If one of the two variables is a float, compare as a float.
4536 * When using "=~" or "!~", always compare as string.
4537 */
4538 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4539 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4540 {
4541 float_T f1, f2;
4542
4543 if (rettv->v_type == VAR_FLOAT)
4544 f1 = rettv->vval.v_float;
4545 else
4546 f1 = get_tv_number(rettv);
4547 if (var2.v_type == VAR_FLOAT)
4548 f2 = var2.vval.v_float;
4549 else
4550 f2 = get_tv_number(&var2);
4551 n1 = FALSE;
4552 switch (type)
4553 {
4554 case TYPE_EQUAL: n1 = (f1 == f2); break;
4555 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4556 case TYPE_GREATER: n1 = (f1 > f2); break;
4557 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4558 case TYPE_SMALLER: n1 = (f1 < f2); break;
4559 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4560 case TYPE_UNKNOWN:
4561 case TYPE_MATCH:
4562 case TYPE_NOMATCH: break; /* avoid gcc warning */
4563 }
4564 }
4565#endif
4566
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 /*
4568 * If one of the two variables is a number, compare as a number.
4569 * When using "=~" or "!~", always compare as string.
4570 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004571 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4573 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004574 n1 = get_tv_number(rettv);
4575 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 switch (type)
4577 {
4578 case TYPE_EQUAL: n1 = (n1 == n2); break;
4579 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4580 case TYPE_GREATER: n1 = (n1 > n2); break;
4581 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4582 case TYPE_SMALLER: n1 = (n1 < n2); break;
4583 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4584 case TYPE_UNKNOWN:
4585 case TYPE_MATCH:
4586 case TYPE_NOMATCH: break; /* avoid gcc warning */
4587 }
4588 }
4589 else
4590 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004591 s1 = get_tv_string_buf(rettv, buf1);
4592 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4594 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4595 else
4596 i = 0;
4597 n1 = FALSE;
4598 switch (type)
4599 {
4600 case TYPE_EQUAL: n1 = (i == 0); break;
4601 case TYPE_NEQUAL: n1 = (i != 0); break;
4602 case TYPE_GREATER: n1 = (i > 0); break;
4603 case TYPE_GEQUAL: n1 = (i >= 0); break;
4604 case TYPE_SMALLER: n1 = (i < 0); break;
4605 case TYPE_SEQUAL: n1 = (i <= 0); break;
4606
4607 case TYPE_MATCH:
4608 case TYPE_NOMATCH:
4609 /* avoid 'l' flag in 'cpoptions' */
4610 save_cpo = p_cpo;
4611 p_cpo = (char_u *)"";
4612 regmatch.regprog = vim_regcomp(s2,
4613 RE_MAGIC + RE_STRING);
4614 regmatch.rm_ic = ic;
4615 if (regmatch.regprog != NULL)
4616 {
4617 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004618 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 if (type == TYPE_NOMATCH)
4620 n1 = !n1;
4621 }
4622 p_cpo = save_cpo;
4623 break;
4624
4625 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4626 }
4627 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004628 clear_tv(rettv);
4629 clear_tv(&var2);
4630 rettv->v_type = VAR_NUMBER;
4631 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 }
4633 }
4634
4635 return OK;
4636}
4637
4638/*
4639 * Handle fourth level expression:
4640 * + number addition
4641 * - number subtraction
4642 * . string concatenation
4643 *
4644 * "arg" must point to the first non-white of the expression.
4645 * "arg" is advanced to the next non-white after the recognized expression.
4646 *
4647 * Return OK or FAIL.
4648 */
4649 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004650eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651{
Bram Moolenaar33570922005-01-25 22:26:29 +00004652 typval_T var2;
4653 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 int op;
4655 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004656#ifdef FEAT_FLOAT
4657 float_T f1 = 0, f2 = 0;
4658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 char_u *s1, *s2;
4660 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4661 char_u *p;
4662
4663 /*
4664 * Get the first variable.
4665 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004666 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 return FAIL;
4668
4669 /*
4670 * Repeat computing, until no '+', '-' or '.' is following.
4671 */
4672 for (;;)
4673 {
4674 op = **arg;
4675 if (op != '+' && op != '-' && op != '.')
4676 break;
4677
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004678 if ((op != '+' || rettv->v_type != VAR_LIST)
4679#ifdef FEAT_FLOAT
4680 && (op == '.' || rettv->v_type != VAR_FLOAT)
4681#endif
4682 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004683 {
4684 /* For "list + ...", an illegal use of the first operand as
4685 * a number cannot be determined before evaluating the 2nd
4686 * operand: if this is also a list, all is ok.
4687 * For "something . ...", "something - ..." or "non-list + ...",
4688 * we know that the first operand needs to be a string or number
4689 * without evaluating the 2nd operand. So check before to avoid
4690 * side effects after an error. */
4691 if (evaluate && get_tv_string_chk(rettv) == NULL)
4692 {
4693 clear_tv(rettv);
4694 return FAIL;
4695 }
4696 }
4697
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 /*
4699 * Get the second variable.
4700 */
4701 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004702 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004704 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 return FAIL;
4706 }
4707
4708 if (evaluate)
4709 {
4710 /*
4711 * Compute the result.
4712 */
4713 if (op == '.')
4714 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004715 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4716 s2 = get_tv_string_buf_chk(&var2, buf2);
4717 if (s2 == NULL) /* type error ? */
4718 {
4719 clear_tv(rettv);
4720 clear_tv(&var2);
4721 return FAIL;
4722 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004723 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004724 clear_tv(rettv);
4725 rettv->v_type = VAR_STRING;
4726 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004728 else if (op == '+' && rettv->v_type == VAR_LIST
4729 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004730 {
4731 /* concatenate Lists */
4732 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4733 &var3) == FAIL)
4734 {
4735 clear_tv(rettv);
4736 clear_tv(&var2);
4737 return FAIL;
4738 }
4739 clear_tv(rettv);
4740 *rettv = var3;
4741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 else
4743 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004744 int error = FALSE;
4745
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004746#ifdef FEAT_FLOAT
4747 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004748 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004749 f1 = rettv->vval.v_float;
4750 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004751 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004752 else
4753#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004754 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004755 n1 = get_tv_number_chk(rettv, &error);
4756 if (error)
4757 {
4758 /* This can only happen for "list + non-list". For
4759 * "non-list + ..." or "something - ...", we returned
4760 * before evaluating the 2nd operand. */
4761 clear_tv(rettv);
4762 return FAIL;
4763 }
4764#ifdef FEAT_FLOAT
4765 if (var2.v_type == VAR_FLOAT)
4766 f1 = n1;
4767#endif
4768 }
4769#ifdef FEAT_FLOAT
4770 if (var2.v_type == VAR_FLOAT)
4771 {
4772 f2 = var2.vval.v_float;
4773 n2 = 0;
4774 }
4775 else
4776#endif
4777 {
4778 n2 = get_tv_number_chk(&var2, &error);
4779 if (error)
4780 {
4781 clear_tv(rettv);
4782 clear_tv(&var2);
4783 return FAIL;
4784 }
4785#ifdef FEAT_FLOAT
4786 if (rettv->v_type == VAR_FLOAT)
4787 f2 = n2;
4788#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004789 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004790 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004791
4792#ifdef FEAT_FLOAT
4793 /* If there is a float on either side the result is a float. */
4794 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4795 {
4796 if (op == '+')
4797 f1 = f1 + f2;
4798 else
4799 f1 = f1 - f2;
4800 rettv->v_type = VAR_FLOAT;
4801 rettv->vval.v_float = f1;
4802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004804#endif
4805 {
4806 if (op == '+')
4807 n1 = n1 + n2;
4808 else
4809 n1 = n1 - n2;
4810 rettv->v_type = VAR_NUMBER;
4811 rettv->vval.v_number = n1;
4812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004814 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 }
4816 }
4817 return OK;
4818}
4819
4820/*
4821 * Handle fifth level expression:
4822 * * number multiplication
4823 * / number division
4824 * % number modulo
4825 *
4826 * "arg" must point to the first non-white of the expression.
4827 * "arg" is advanced to the next non-white after the recognized expression.
4828 *
4829 * Return OK or FAIL.
4830 */
4831 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004832eval6(
4833 char_u **arg,
4834 typval_T *rettv,
4835 int evaluate,
4836 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837{
Bram Moolenaar33570922005-01-25 22:26:29 +00004838 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 int op;
4840 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004841#ifdef FEAT_FLOAT
4842 int use_float = FALSE;
4843 float_T f1 = 0, f2;
4844#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004845 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846
4847 /*
4848 * Get the first variable.
4849 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004850 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 return FAIL;
4852
4853 /*
4854 * Repeat computing, until no '*', '/' or '%' is following.
4855 */
4856 for (;;)
4857 {
4858 op = **arg;
4859 if (op != '*' && op != '/' && op != '%')
4860 break;
4861
4862 if (evaluate)
4863 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004864#ifdef FEAT_FLOAT
4865 if (rettv->v_type == VAR_FLOAT)
4866 {
4867 f1 = rettv->vval.v_float;
4868 use_float = TRUE;
4869 n1 = 0;
4870 }
4871 else
4872#endif
4873 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004874 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004875 if (error)
4876 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 }
4878 else
4879 n1 = 0;
4880
4881 /*
4882 * Get the second variable.
4883 */
4884 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004885 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 return FAIL;
4887
4888 if (evaluate)
4889 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004890#ifdef FEAT_FLOAT
4891 if (var2.v_type == VAR_FLOAT)
4892 {
4893 if (!use_float)
4894 {
4895 f1 = n1;
4896 use_float = TRUE;
4897 }
4898 f2 = var2.vval.v_float;
4899 n2 = 0;
4900 }
4901 else
4902#endif
4903 {
4904 n2 = get_tv_number_chk(&var2, &error);
4905 clear_tv(&var2);
4906 if (error)
4907 return FAIL;
4908#ifdef FEAT_FLOAT
4909 if (use_float)
4910 f2 = n2;
4911#endif
4912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913
4914 /*
4915 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004916 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004918#ifdef FEAT_FLOAT
4919 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004921 if (op == '*')
4922 f1 = f1 * f2;
4923 else if (op == '/')
4924 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004925# ifdef VMS
4926 /* VMS crashes on divide by zero, work around it */
4927 if (f2 == 0.0)
4928 {
4929 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004930 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004931 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004932 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004933 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004934 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004935 }
4936 else
4937 f1 = f1 / f2;
4938# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004939 /* We rely on the floating point library to handle divide
4940 * by zero to result in "inf" and not a crash. */
4941 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004942# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004945 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004946 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004947 return FAIL;
4948 }
4949 rettv->v_type = VAR_FLOAT;
4950 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 }
4952 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004955 if (op == '*')
4956 n1 = n1 * n2;
4957 else if (op == '/')
4958 {
4959 if (n2 == 0) /* give an error message? */
4960 {
4961 if (n1 == 0)
4962 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4963 else if (n1 < 0)
4964 n1 = -0x7fffffffL;
4965 else
4966 n1 = 0x7fffffffL;
4967 }
4968 else
4969 n1 = n1 / n2;
4970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004972 {
4973 if (n2 == 0) /* give an error message? */
4974 n1 = 0;
4975 else
4976 n1 = n1 % n2;
4977 }
4978 rettv->v_type = VAR_NUMBER;
4979 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 }
4982 }
4983
4984 return OK;
4985}
4986
4987/*
4988 * Handle sixth level expression:
4989 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004990 * "string" string constant
4991 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 * &option-name option value
4993 * @r register contents
4994 * identifier variable value
4995 * function() function call
4996 * $VAR environment variable
4997 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004998 * [expr, expr] List
4999 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 *
5001 * Also handle:
5002 * ! in front logical NOT
5003 * - in front unary minus
5004 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005005 * trailing [] subscript in String or List
5006 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 *
5008 * "arg" must point to the first non-white of the expression.
5009 * "arg" is advanced to the next non-white after the recognized expression.
5010 *
5011 * Return OK or FAIL.
5012 */
5013 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005014eval7(
5015 char_u **arg,
5016 typval_T *rettv,
5017 int evaluate,
5018 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 long n;
5021 int len;
5022 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 char_u *start_leader, *end_leader;
5024 int ret = OK;
5025 char_u *alias;
5026
5027 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005028 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005029 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005031 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032
5033 /*
5034 * Skip '!' and '-' characters. They are handled later.
5035 */
5036 start_leader = *arg;
5037 while (**arg == '!' || **arg == '-' || **arg == '+')
5038 *arg = skipwhite(*arg + 1);
5039 end_leader = *arg;
5040
5041 switch (**arg)
5042 {
5043 /*
5044 * Number constant.
5045 */
5046 case '0':
5047 case '1':
5048 case '2':
5049 case '3':
5050 case '4':
5051 case '5':
5052 case '6':
5053 case '7':
5054 case '8':
5055 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005056 {
5057#ifdef FEAT_FLOAT
5058 char_u *p = skipdigits(*arg + 1);
5059 int get_float = FALSE;
5060
5061 /* We accept a float when the format matches
5062 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005063 * strict to avoid backwards compatibility problems.
5064 * Don't look for a float after the "." operator, so that
5065 * ":let vers = 1.2.3" doesn't fail. */
5066 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005068 get_float = TRUE;
5069 p = skipdigits(p + 2);
5070 if (*p == 'e' || *p == 'E')
5071 {
5072 ++p;
5073 if (*p == '-' || *p == '+')
5074 ++p;
5075 if (!vim_isdigit(*p))
5076 get_float = FALSE;
5077 else
5078 p = skipdigits(p + 1);
5079 }
5080 if (ASCII_ISALPHA(*p) || *p == '.')
5081 get_float = FALSE;
5082 }
5083 if (get_float)
5084 {
5085 float_T f;
5086
5087 *arg += string2float(*arg, &f);
5088 if (evaluate)
5089 {
5090 rettv->v_type = VAR_FLOAT;
5091 rettv->vval.v_float = f;
5092 }
5093 }
5094 else
5095#endif
5096 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005097 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005098 *arg += len;
5099 if (evaluate)
5100 {
5101 rettv->v_type = VAR_NUMBER;
5102 rettv->vval.v_number = n;
5103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 }
5105 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107
5108 /*
5109 * String constant: "string".
5110 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005111 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 break;
5113
5114 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005115 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005117 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005118 break;
5119
5120 /*
5121 * List: [expr, expr]
5122 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005123 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 break;
5125
5126 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005127 * Dictionary: {key: val, key: val}
5128 */
5129 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5130 break;
5131
5132 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005133 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005135 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 break;
5137
5138 /*
5139 * Environment variable: $VAR.
5140 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005141 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 break;
5143
5144 /*
5145 * Register contents: @r.
5146 */
5147 case '@': ++*arg;
5148 if (evaluate)
5149 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005150 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005151 rettv->vval.v_string = get_reg_contents(**arg,
5152 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
5154 if (**arg != NUL)
5155 ++*arg;
5156 break;
5157
5158 /*
5159 * nested expression: (expression).
5160 */
5161 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005162 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 if (**arg == ')')
5164 ++*arg;
5165 else if (ret == OK)
5166 {
5167 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005168 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 ret = FAIL;
5170 }
5171 break;
5172
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 break;
5175 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005176
5177 if (ret == NOTDONE)
5178 {
5179 /*
5180 * Must be a variable or function name.
5181 * Can also be a curly-braces kind of name: {expr}.
5182 */
5183 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005184 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005185 if (alias != NULL)
5186 s = alias;
5187
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005188 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005189 ret = FAIL;
5190 else
5191 {
5192 if (**arg == '(') /* recursive! */
5193 {
5194 /* If "s" is the name of a variable of type VAR_FUNC
5195 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005196 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005197
5198 /* Invoke the function. */
5199 ret = get_func_tv(s, len, rettv, arg,
5200 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005201 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005202
5203 /* If evaluate is FALSE rettv->v_type was not set in
5204 * get_func_tv, but it's needed in handle_subscript() to parse
5205 * what follows. So set it here. */
5206 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5207 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005208 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005209 rettv->v_type = VAR_FUNC;
5210 }
5211
Bram Moolenaar8c711452005-01-14 21:53:12 +00005212 /* Stop the expression evaluation when immediately
5213 * aborting on error, or when an interrupt occurred or
5214 * an exception was thrown but not caught. */
5215 if (aborting())
5216 {
5217 if (ret == OK)
5218 clear_tv(rettv);
5219 ret = FAIL;
5220 }
5221 }
5222 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005223 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005224 else
5225 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005226 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005227 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005228 }
5229
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 *arg = skipwhite(*arg);
5231
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005232 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5233 * expr(expr). */
5234 if (ret == OK)
5235 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236
5237 /*
5238 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5239 */
5240 if (ret == OK && evaluate && end_leader > start_leader)
5241 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005242 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005243 int val = 0;
5244#ifdef FEAT_FLOAT
5245 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005246
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005247 if (rettv->v_type == VAR_FLOAT)
5248 f = rettv->vval.v_float;
5249 else
5250#endif
5251 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005252 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005254 clear_tv(rettv);
5255 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005257 else
5258 {
5259 while (end_leader > start_leader)
5260 {
5261 --end_leader;
5262 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005263 {
5264#ifdef FEAT_FLOAT
5265 if (rettv->v_type == VAR_FLOAT)
5266 f = !f;
5267 else
5268#endif
5269 val = !val;
5270 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005271 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005272 {
5273#ifdef FEAT_FLOAT
5274 if (rettv->v_type == VAR_FLOAT)
5275 f = -f;
5276 else
5277#endif
5278 val = -val;
5279 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005280 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005281#ifdef FEAT_FLOAT
5282 if (rettv->v_type == VAR_FLOAT)
5283 {
5284 clear_tv(rettv);
5285 rettv->vval.v_float = f;
5286 }
5287 else
5288#endif
5289 {
5290 clear_tv(rettv);
5291 rettv->v_type = VAR_NUMBER;
5292 rettv->vval.v_number = val;
5293 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 }
5296
5297 return ret;
5298}
5299
5300/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005301 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5302 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005303 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5304 */
5305 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005306eval_index(
5307 char_u **arg,
5308 typval_T *rettv,
5309 int evaluate,
5310 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005311{
5312 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005313 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005314 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005315 long len = -1;
5316 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005317 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005318 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005319
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005320 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005321 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005322 if (verbose)
5323 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005324 return FAIL;
5325 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005326#ifdef FEAT_FLOAT
5327 else if (rettv->v_type == VAR_FLOAT)
5328 {
5329 if (verbose)
5330 EMSG(_(e_float_as_string));
5331 return FAIL;
5332 }
5333#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005334 else if (rettv->v_type == VAR_SPECIAL)
5335 {
5336 return FAIL;
5337 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005338
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005339 init_tv(&var1);
5340 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005341 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005343 /*
5344 * dict.name
5345 */
5346 key = *arg + 1;
5347 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5348 ;
5349 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005351 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352 }
5353 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005355 /*
5356 * something[idx]
5357 *
5358 * Get the (first) variable from inside the [].
5359 */
5360 *arg = skipwhite(*arg + 1);
5361 if (**arg == ':')
5362 empty1 = TRUE;
5363 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5364 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005365 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5366 {
5367 /* not a number or string */
5368 clear_tv(&var1);
5369 return FAIL;
5370 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005371
5372 /*
5373 * Get the second variable from inside the [:].
5374 */
5375 if (**arg == ':')
5376 {
5377 range = TRUE;
5378 *arg = skipwhite(*arg + 1);
5379 if (**arg == ']')
5380 empty2 = TRUE;
5381 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5382 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005383 if (!empty1)
5384 clear_tv(&var1);
5385 return FAIL;
5386 }
5387 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5388 {
5389 /* not a number or string */
5390 if (!empty1)
5391 clear_tv(&var1);
5392 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005393 return FAIL;
5394 }
5395 }
5396
5397 /* Check for the ']'. */
5398 if (**arg != ']')
5399 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005400 if (verbose)
5401 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005402 clear_tv(&var1);
5403 if (range)
5404 clear_tv(&var2);
5405 return FAIL;
5406 }
5407 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408 }
5409
5410 if (evaluate)
5411 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005412 n1 = 0;
5413 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005415 n1 = get_tv_number(&var1);
5416 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005417 }
5418 if (range)
5419 {
5420 if (empty2)
5421 n2 = -1;
5422 else
5423 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005424 n2 = get_tv_number(&var2);
5425 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005426 }
5427 }
5428
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005429 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430 {
5431 case VAR_NUMBER:
5432 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005433 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434 len = (long)STRLEN(s);
5435 if (range)
5436 {
5437 /* The resulting variable is a substring. If the indexes
5438 * are out of range the result is empty. */
5439 if (n1 < 0)
5440 {
5441 n1 = len + n1;
5442 if (n1 < 0)
5443 n1 = 0;
5444 }
5445 if (n2 < 0)
5446 n2 = len + n2;
5447 else if (n2 >= len)
5448 n2 = len;
5449 if (n1 >= len || n2 < 0 || n1 > n2)
5450 s = NULL;
5451 else
5452 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5453 }
5454 else
5455 {
5456 /* The resulting variable is a string of a single
5457 * character. If the index is too big or negative the
5458 * result is empty. */
5459 if (n1 >= len || n1 < 0)
5460 s = NULL;
5461 else
5462 s = vim_strnsave(s + n1, 1);
5463 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005464 clear_tv(rettv);
5465 rettv->v_type = VAR_STRING;
5466 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 break;
5468
5469 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005470 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 if (n1 < 0)
5472 n1 = len + n1;
5473 if (!empty1 && (n1 < 0 || n1 >= len))
5474 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005475 /* For a range we allow invalid values and return an empty
5476 * list. A list index out of range is an error. */
5477 if (!range)
5478 {
5479 if (verbose)
5480 EMSGN(_(e_listidx), n1);
5481 return FAIL;
5482 }
5483 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005484 }
5485 if (range)
5486 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005487 list_T *l;
5488 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489
5490 if (n2 < 0)
5491 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005492 else if (n2 >= len)
5493 n2 = len - 1;
5494 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005495 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005496 l = list_alloc();
5497 if (l == NULL)
5498 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005499 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005500 n1 <= n2; ++n1)
5501 {
5502 if (list_append_tv(l, &item->li_tv) == FAIL)
5503 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005504 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005505 return FAIL;
5506 }
5507 item = item->li_next;
5508 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005509 clear_tv(rettv);
5510 rettv->v_type = VAR_LIST;
5511 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005512 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005513 }
5514 else
5515 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005516 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005517 clear_tv(rettv);
5518 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005519 }
5520 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005521
5522 case VAR_DICT:
5523 if (range)
5524 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005525 if (verbose)
5526 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005527 if (len == -1)
5528 clear_tv(&var1);
5529 return FAIL;
5530 }
5531 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005532 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005533
5534 if (len == -1)
5535 {
5536 key = get_tv_string(&var1);
5537 if (*key == NUL)
5538 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005539 if (verbose)
5540 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005541 clear_tv(&var1);
5542 return FAIL;
5543 }
5544 }
5545
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005546 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005547
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005548 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005549 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005550 if (len == -1)
5551 clear_tv(&var1);
5552 if (item == NULL)
5553 return FAIL;
5554
5555 copy_tv(&item->di_tv, &var1);
5556 clear_tv(rettv);
5557 *rettv = var1;
5558 }
5559 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005560 }
5561 }
5562
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005563 return OK;
5564}
5565
5566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 * Get an option value.
5568 * "arg" points to the '&' or '+' before the option name.
5569 * "arg" is advanced to character after the option name.
5570 * Return OK or FAIL.
5571 */
5572 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005573get_option_tv(
5574 char_u **arg,
5575 typval_T *rettv, /* when NULL, only check if option exists */
5576 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
5578 char_u *option_end;
5579 long numval;
5580 char_u *stringval;
5581 int opt_type;
5582 int c;
5583 int working = (**arg == '+'); /* has("+option") */
5584 int ret = OK;
5585 int opt_flags;
5586
5587 /*
5588 * Isolate the option name and find its value.
5589 */
5590 option_end = find_option_end(arg, &opt_flags);
5591 if (option_end == NULL)
5592 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005593 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 EMSG2(_("E112: Option name missing: %s"), *arg);
5595 return FAIL;
5596 }
5597
5598 if (!evaluate)
5599 {
5600 *arg = option_end;
5601 return OK;
5602 }
5603
5604 c = *option_end;
5605 *option_end = NUL;
5606 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005607 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608
5609 if (opt_type == -3) /* invalid name */
5610 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005611 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 EMSG2(_("E113: Unknown option: %s"), *arg);
5613 ret = FAIL;
5614 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005615 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 {
5617 if (opt_type == -2) /* hidden string option */
5618 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005619 rettv->v_type = VAR_STRING;
5620 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 }
5622 else if (opt_type == -1) /* hidden number option */
5623 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005624 rettv->v_type = VAR_NUMBER;
5625 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 }
5627 else if (opt_type == 1) /* number option */
5628 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005629 rettv->v_type = VAR_NUMBER;
5630 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 }
5632 else /* string option */
5633 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005634 rettv->v_type = VAR_STRING;
5635 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 }
5637 }
5638 else if (working && (opt_type == -2 || opt_type == -1))
5639 ret = FAIL;
5640
5641 *option_end = c; /* put back for error messages */
5642 *arg = option_end;
5643
5644 return ret;
5645}
5646
5647/*
5648 * Allocate a variable for a string constant.
5649 * Return OK or FAIL.
5650 */
5651 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005652get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653{
5654 char_u *p;
5655 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 int extra = 0;
5657
5658 /*
5659 * Find the end of the string, skipping backslashed characters.
5660 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005661 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 {
5663 if (*p == '\\' && p[1] != NUL)
5664 {
5665 ++p;
5666 /* A "\<x>" form occupies at least 4 characters, and produces up
5667 * to 6 characters: reserve space for 2 extra */
5668 if (*p == '<')
5669 extra += 2;
5670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 }
5672
5673 if (*p != '"')
5674 {
5675 EMSG2(_("E114: Missing quote: %s"), *arg);
5676 return FAIL;
5677 }
5678
5679 /* If only parsing, set *arg and return here */
5680 if (!evaluate)
5681 {
5682 *arg = p + 1;
5683 return OK;
5684 }
5685
5686 /*
5687 * Copy the string into allocated memory, handling backslashed
5688 * characters.
5689 */
5690 name = alloc((unsigned)(p - *arg + extra));
5691 if (name == NULL)
5692 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005693 rettv->v_type = VAR_STRING;
5694 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695
Bram Moolenaar8c711452005-01-14 21:53:12 +00005696 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 {
5698 if (*p == '\\')
5699 {
5700 switch (*++p)
5701 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005702 case 'b': *name++ = BS; ++p; break;
5703 case 'e': *name++ = ESC; ++p; break;
5704 case 'f': *name++ = FF; ++p; break;
5705 case 'n': *name++ = NL; ++p; break;
5706 case 'r': *name++ = CAR; ++p; break;
5707 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708
5709 case 'X': /* hex: "\x1", "\x12" */
5710 case 'x':
5711 case 'u': /* Unicode: "\u0023" */
5712 case 'U':
5713 if (vim_isxdigit(p[1]))
5714 {
5715 int n, nr;
5716 int c = toupper(*p);
5717
5718 if (c == 'X')
5719 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005720 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005722 else
5723 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 nr = 0;
5725 while (--n >= 0 && vim_isxdigit(p[1]))
5726 {
5727 ++p;
5728 nr = (nr << 4) + hex2nr(*p);
5729 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731#ifdef FEAT_MBYTE
5732 /* For "\u" store the number according to
5733 * 'encoding'. */
5734 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 else
5737#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 break;
5741
5742 /* octal: "\1", "\12", "\123" */
5743 case '0':
5744 case '1':
5745 case '2':
5746 case '3':
5747 case '4':
5748 case '5':
5749 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005750 case '7': *name = *p++ - '0';
5751 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 *name = (*name << 3) + *p++ - '0';
5754 if (*p >= '0' && *p <= '7')
5755 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005757 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 break;
5759
5760 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005761 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 if (extra != 0)
5763 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005764 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 break;
5766 }
5767 /* FALLTHROUGH */
5768
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 break;
5771 }
5772 }
5773 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005777 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 *arg = p + 1;
5779
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 return OK;
5781}
5782
5783/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 * Return OK or FAIL.
5786 */
5787 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005788get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789{
5790 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005791 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005792 int reduce = 0;
5793
5794 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005796 */
5797 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5798 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005799 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005800 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005801 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005802 break;
5803 ++reduce;
5804 ++p;
5805 }
5806 }
5807
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005809 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005811 return FAIL;
5812 }
5813
Bram Moolenaar8c711452005-01-14 21:53:12 +00005814 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005815 if (!evaluate)
5816 {
5817 *arg = p + 1;
5818 return OK;
5819 }
5820
5821 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005822 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005823 */
5824 str = alloc((unsigned)((p - *arg) - reduce));
5825 if (str == NULL)
5826 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005827 rettv->v_type = VAR_STRING;
5828 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005829
Bram Moolenaar8c711452005-01-14 21:53:12 +00005830 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005831 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005833 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005834 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005835 break;
5836 ++p;
5837 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005839 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005840 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005841 *arg = p + 1;
5842
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005843 return OK;
5844}
5845
5846/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847 * Allocate a variable for a List and fill it from "*arg".
5848 * Return OK or FAIL.
5849 */
5850 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005851get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852{
Bram Moolenaar33570922005-01-25 22:26:29 +00005853 list_T *l = NULL;
5854 typval_T tv;
5855 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005856
5857 if (evaluate)
5858 {
5859 l = list_alloc();
5860 if (l == NULL)
5861 return FAIL;
5862 }
5863
5864 *arg = skipwhite(*arg + 1);
5865 while (**arg != ']' && **arg != NUL)
5866 {
5867 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5868 goto failret;
5869 if (evaluate)
5870 {
5871 item = listitem_alloc();
5872 if (item != NULL)
5873 {
5874 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005875 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005876 list_append(l, item);
5877 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005878 else
5879 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005880 }
5881
5882 if (**arg == ']')
5883 break;
5884 if (**arg != ',')
5885 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005887 goto failret;
5888 }
5889 *arg = skipwhite(*arg + 1);
5890 }
5891
5892 if (**arg != ']')
5893 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005895failret:
5896 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005897 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005898 return FAIL;
5899 }
5900
5901 *arg = skipwhite(*arg + 1);
5902 if (evaluate)
5903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005904 rettv->v_type = VAR_LIST;
5905 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906 ++l->lv_refcount;
5907 }
5908
5909 return OK;
5910}
5911
5912/*
5913 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005914 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005916 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005917list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005919 list_T *l;
5920
5921 l = (list_T *)alloc_clear(sizeof(list_T));
5922 if (l != NULL)
5923 {
5924 /* Prepend the list to the list of lists for garbage collection. */
5925 if (first_list != NULL)
5926 first_list->lv_used_prev = l;
5927 l->lv_used_prev = NULL;
5928 l->lv_used_next = first_list;
5929 first_list = l;
5930 }
5931 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005932}
5933
5934/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005935 * Allocate an empty list for a return value.
5936 * Returns OK or FAIL.
5937 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005938 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005939rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005940{
5941 list_T *l = list_alloc();
5942
5943 if (l == NULL)
5944 return FAIL;
5945
5946 rettv->vval.v_list = l;
5947 rettv->v_type = VAR_LIST;
5948 ++l->lv_refcount;
5949 return OK;
5950}
5951
5952/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005953 * Unreference a list: decrement the reference count and free it when it
5954 * becomes zero.
5955 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005956 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005957list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005959 if (l != NULL && --l->lv_refcount <= 0)
5960 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005961}
5962
5963/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01005964 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965 * Ignores the reference count.
5966 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005967 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005968list_free(
5969 list_T *l,
5970 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005971{
Bram Moolenaar33570922005-01-25 22:26:29 +00005972 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005974 /* Remove the list from the list of lists for garbage collection. */
5975 if (l->lv_used_prev == NULL)
5976 first_list = l->lv_used_next;
5977 else
5978 l->lv_used_prev->lv_used_next = l->lv_used_next;
5979 if (l->lv_used_next != NULL)
5980 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5981
Bram Moolenaard9fba312005-06-26 22:34:35 +00005982 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005984 /* Remove the item before deleting it. */
5985 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005986 if (recurse || (item->li_tv.v_type != VAR_LIST
5987 && item->li_tv.v_type != VAR_DICT))
5988 clear_tv(&item->li_tv);
5989 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990 }
5991 vim_free(l);
5992}
5993
5994/*
5995 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01005996 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005997 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005998 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01005999listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006000{
Bram Moolenaar33570922005-01-25 22:26:29 +00006001 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006002}
6003
6004/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006005 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006006 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006007 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006008listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006010 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011 vim_free(item);
6012}
6013
6014/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006015 * Remove a list item from a List and free it. Also clears the value.
6016 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006017 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006018listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006019{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006020 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006021 listitem_free(item);
6022}
6023
6024/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025 * Get the number of items in a list.
6026 */
6027 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006028list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006029{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030 if (l == NULL)
6031 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006032 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033}
6034
6035/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006036 * Return TRUE when two lists have exactly the same values.
6037 */
6038 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006039list_equal(
6040 list_T *l1,
6041 list_T *l2,
6042 int ic, /* ignore case for strings */
6043 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006044{
Bram Moolenaar33570922005-01-25 22:26:29 +00006045 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006046
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006047 if (l1 == NULL || l2 == NULL)
6048 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006049 if (l1 == l2)
6050 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006051 if (list_len(l1) != list_len(l2))
6052 return FALSE;
6053
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006054 for (item1 = l1->lv_first, item2 = l2->lv_first;
6055 item1 != NULL && item2 != NULL;
6056 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006057 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006058 return FALSE;
6059 return item1 == NULL && item2 == NULL;
6060}
6061
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006062/*
6063 * Return the dictitem that an entry in a hashtable points to.
6064 */
6065 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006066dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006067{
6068 return HI2DI(hi);
6069}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006070
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006071/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006072 * Return TRUE when two dictionaries have exactly the same key/values.
6073 */
6074 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006075dict_equal(
6076 dict_T *d1,
6077 dict_T *d2,
6078 int ic, /* ignore case for strings */
6079 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006080{
Bram Moolenaar33570922005-01-25 22:26:29 +00006081 hashitem_T *hi;
6082 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006083 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006084
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006085 if (d1 == NULL || d2 == NULL)
6086 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006087 if (d1 == d2)
6088 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006089 if (dict_len(d1) != dict_len(d2))
6090 return FALSE;
6091
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006092 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006093 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006094 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006095 if (!HASHITEM_EMPTY(hi))
6096 {
6097 item2 = dict_find(d2, hi->hi_key, -1);
6098 if (item2 == NULL)
6099 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006100 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006101 return FALSE;
6102 --todo;
6103 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006104 }
6105 return TRUE;
6106}
6107
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006108static int tv_equal_recurse_limit;
6109
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006110/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006111 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006112 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006113 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006114 */
6115 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006116tv_equal(
6117 typval_T *tv1,
6118 typval_T *tv2,
6119 int ic, /* ignore case */
6120 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006121{
6122 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006123 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006124 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006125 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006126
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006127 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006128 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006129
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006130 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006131 * recursiveness to a limit. We guess they are equal then.
6132 * A fixed limit has the problem of still taking an awful long time.
6133 * Reduce the limit every time running into it. That should work fine for
6134 * deeply linked structures that are not recursively linked and catch
6135 * recursiveness quickly. */
6136 if (!recursive)
6137 tv_equal_recurse_limit = 1000;
6138 if (recursive_cnt >= tv_equal_recurse_limit)
6139 {
6140 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006141 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006142 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006143
6144 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006145 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006146 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006147 ++recursive_cnt;
6148 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6149 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006150 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006151
6152 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006153 ++recursive_cnt;
6154 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6155 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006156 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006157
6158 case VAR_FUNC:
6159 return (tv1->vval.v_string != NULL
6160 && tv2->vval.v_string != NULL
6161 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6162
6163 case VAR_NUMBER:
6164 return tv1->vval.v_number == tv2->vval.v_number;
6165
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006166#ifdef FEAT_FLOAT
6167 case VAR_FLOAT:
6168 return tv1->vval.v_float == tv2->vval.v_float;
6169#endif
6170
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006171 case VAR_STRING:
6172 s1 = get_tv_string_buf(tv1, buf1);
6173 s2 = get_tv_string_buf(tv2, buf2);
6174 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006175
6176 case VAR_SPECIAL:
6177 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006178 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006179
6180 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006181 return TRUE;
6182}
6183
6184/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006185 * Locate item with index "n" in list "l" and return it.
6186 * A negative index is counted from the end; -1 is the last item.
6187 * Returns NULL when "n" is out of range.
6188 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006189 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006190list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006191{
Bram Moolenaar33570922005-01-25 22:26:29 +00006192 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006193 long idx;
6194
6195 if (l == NULL)
6196 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006197
6198 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006199 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006200 n = l->lv_len + n;
6201
6202 /* Check for index out of range. */
6203 if (n < 0 || n >= l->lv_len)
6204 return NULL;
6205
6206 /* When there is a cached index may start search from there. */
6207 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006208 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006209 if (n < l->lv_idx / 2)
6210 {
6211 /* closest to the start of the list */
6212 item = l->lv_first;
6213 idx = 0;
6214 }
6215 else if (n > (l->lv_idx + l->lv_len) / 2)
6216 {
6217 /* closest to the end of the list */
6218 item = l->lv_last;
6219 idx = l->lv_len - 1;
6220 }
6221 else
6222 {
6223 /* closest to the cached index */
6224 item = l->lv_idx_item;
6225 idx = l->lv_idx;
6226 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006227 }
6228 else
6229 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006230 if (n < l->lv_len / 2)
6231 {
6232 /* closest to the start of the list */
6233 item = l->lv_first;
6234 idx = 0;
6235 }
6236 else
6237 {
6238 /* closest to the end of the list */
6239 item = l->lv_last;
6240 idx = l->lv_len - 1;
6241 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006242 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006243
6244 while (n > idx)
6245 {
6246 /* search forward */
6247 item = item->li_next;
6248 ++idx;
6249 }
6250 while (n < idx)
6251 {
6252 /* search backward */
6253 item = item->li_prev;
6254 --idx;
6255 }
6256
6257 /* cache the used index */
6258 l->lv_idx = idx;
6259 l->lv_idx_item = item;
6260
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006261 return item;
6262}
6263
6264/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006265 * Get list item "l[idx]" as a number.
6266 */
6267 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006268list_find_nr(
6269 list_T *l,
6270 long idx,
6271 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006272{
6273 listitem_T *li;
6274
6275 li = list_find(l, idx);
6276 if (li == NULL)
6277 {
6278 if (errorp != NULL)
6279 *errorp = TRUE;
6280 return -1L;
6281 }
6282 return get_tv_number_chk(&li->li_tv, errorp);
6283}
6284
6285/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006286 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6287 */
6288 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006289list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006290{
6291 listitem_T *li;
6292
6293 li = list_find(l, idx - 1);
6294 if (li == NULL)
6295 {
6296 EMSGN(_(e_listidx), idx);
6297 return NULL;
6298 }
6299 return get_tv_string(&li->li_tv);
6300}
6301
6302/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006303 * Locate "item" list "l" and return its index.
6304 * Returns -1 when "item" is not in the list.
6305 */
6306 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006307list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006308{
6309 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006310 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006311
6312 if (l == NULL)
6313 return -1;
6314 idx = 0;
6315 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6316 ++idx;
6317 if (li == NULL)
6318 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006319 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006320}
6321
6322/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006323 * Append item "item" to the end of list "l".
6324 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006325 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006326list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006327{
6328 if (l->lv_last == NULL)
6329 {
6330 /* empty list */
6331 l->lv_first = item;
6332 l->lv_last = item;
6333 item->li_prev = NULL;
6334 }
6335 else
6336 {
6337 l->lv_last->li_next = item;
6338 item->li_prev = l->lv_last;
6339 l->lv_last = item;
6340 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006341 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006342 item->li_next = NULL;
6343}
6344
6345/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006346 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006347 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006348 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006349 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006350list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006351{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006352 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006353
Bram Moolenaar05159a02005-02-26 23:04:13 +00006354 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006355 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006356 copy_tv(tv, &li->li_tv);
6357 list_append(l, li);
6358 return OK;
6359}
6360
6361/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006362 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006363 * Return FAIL when out of memory.
6364 */
6365 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006366list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006367{
6368 listitem_T *li = listitem_alloc();
6369
6370 if (li == NULL)
6371 return FAIL;
6372 li->li_tv.v_type = VAR_DICT;
6373 li->li_tv.v_lock = 0;
6374 li->li_tv.vval.v_dict = dict;
6375 list_append(list, li);
6376 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006377 return OK;
6378}
6379
6380/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006381 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006382 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006383 * Returns FAIL when out of memory.
6384 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006385 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006386list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006387{
6388 listitem_T *li = listitem_alloc();
6389
6390 if (li == NULL)
6391 return FAIL;
6392 list_append(l, li);
6393 li->li_tv.v_type = VAR_STRING;
6394 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006395 if (str == NULL)
6396 li->li_tv.vval.v_string = NULL;
6397 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006398 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006399 return FAIL;
6400 return OK;
6401}
6402
6403/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006404 * Append "n" to list "l".
6405 * Returns FAIL when out of memory.
6406 */
6407 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006408list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006409{
6410 listitem_T *li;
6411
6412 li = listitem_alloc();
6413 if (li == NULL)
6414 return FAIL;
6415 li->li_tv.v_type = VAR_NUMBER;
6416 li->li_tv.v_lock = 0;
6417 li->li_tv.vval.v_number = n;
6418 list_append(l, li);
6419 return OK;
6420}
6421
6422/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006423 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424 * If "item" is NULL append at the end.
6425 * Return FAIL when out of memory.
6426 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006427 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006428list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429{
Bram Moolenaar33570922005-01-25 22:26:29 +00006430 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006431
6432 if (ni == NULL)
6433 return FAIL;
6434 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006435 list_insert(l, ni, item);
6436 return OK;
6437}
6438
6439 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006440list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006441{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006442 if (item == NULL)
6443 /* Append new item at end of list. */
6444 list_append(l, ni);
6445 else
6446 {
6447 /* Insert new item before existing item. */
6448 ni->li_prev = item->li_prev;
6449 ni->li_next = item;
6450 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006451 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006452 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006453 ++l->lv_idx;
6454 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006455 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006456 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006457 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006458 l->lv_idx_item = NULL;
6459 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006460 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006461 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006462 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006463}
6464
6465/*
6466 * Extend "l1" with "l2".
6467 * If "bef" is NULL append at the end, otherwise insert before this item.
6468 * Returns FAIL when out of memory.
6469 */
6470 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006471list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006472{
Bram Moolenaar33570922005-01-25 22:26:29 +00006473 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006474 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006475
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006476 /* We also quit the loop when we have inserted the original item count of
6477 * the list, avoid a hang when we extend a list with itself. */
6478 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006479 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6480 return FAIL;
6481 return OK;
6482}
6483
6484/*
6485 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6486 * Return FAIL when out of memory.
6487 */
6488 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006489list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006490{
Bram Moolenaar33570922005-01-25 22:26:29 +00006491 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006492
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006493 if (l1 == NULL || l2 == NULL)
6494 return FAIL;
6495
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006496 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006497 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006498 if (l == NULL)
6499 return FAIL;
6500 tv->v_type = VAR_LIST;
6501 tv->vval.v_list = l;
6502
6503 /* append all items from the second list */
6504 return list_extend(l, l2, NULL);
6505}
6506
6507/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006508 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006509 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006510 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006511 * Returns NULL when out of memory.
6512 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006513 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006514list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006515{
Bram Moolenaar33570922005-01-25 22:26:29 +00006516 list_T *copy;
6517 listitem_T *item;
6518 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006519
6520 if (orig == NULL)
6521 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006522
6523 copy = list_alloc();
6524 if (copy != NULL)
6525 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006526 if (copyID != 0)
6527 {
6528 /* Do this before adding the items, because one of the items may
6529 * refer back to this list. */
6530 orig->lv_copyID = copyID;
6531 orig->lv_copylist = copy;
6532 }
6533 for (item = orig->lv_first; item != NULL && !got_int;
6534 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006535 {
6536 ni = listitem_alloc();
6537 if (ni == NULL)
6538 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006539 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006540 {
6541 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6542 {
6543 vim_free(ni);
6544 break;
6545 }
6546 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006547 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006548 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006549 list_append(copy, ni);
6550 }
6551 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006552 if (item != NULL)
6553 {
6554 list_unref(copy);
6555 copy = NULL;
6556 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006557 }
6558
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006559 return copy;
6560}
6561
6562/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006564 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006565 * This used to be called list_remove, but that conflicts with a Sun header
6566 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006567 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006568 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006569vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006570{
Bram Moolenaar33570922005-01-25 22:26:29 +00006571 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006573 /* notify watchers */
6574 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006575 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006576 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006577 list_fix_watch(l, ip);
6578 if (ip == item2)
6579 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006580 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006581
6582 if (item2->li_next == NULL)
6583 l->lv_last = item->li_prev;
6584 else
6585 item2->li_next->li_prev = item->li_prev;
6586 if (item->li_prev == NULL)
6587 l->lv_first = item2->li_next;
6588 else
6589 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006590 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006591}
6592
6593/*
6594 * Return an allocated string with the string representation of a list.
6595 * May return NULL.
6596 */
6597 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006598list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006599{
6600 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006601
6602 if (tv->vval.v_list == NULL)
6603 return NULL;
6604 ga_init2(&ga, (int)sizeof(char), 80);
6605 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006606 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006607 {
6608 vim_free(ga.ga_data);
6609 return NULL;
6610 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006611 ga_append(&ga, ']');
6612 ga_append(&ga, NUL);
6613 return (char_u *)ga.ga_data;
6614}
6615
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006616typedef struct join_S {
6617 char_u *s;
6618 char_u *tofree;
6619} join_T;
6620
6621 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006622list_join_inner(
6623 garray_T *gap, /* to store the result in */
6624 list_T *l,
6625 char_u *sep,
6626 int echo_style,
6627 int copyID,
6628 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006629{
6630 int i;
6631 join_T *p;
6632 int len;
6633 int sumlen = 0;
6634 int first = TRUE;
6635 char_u *tofree;
6636 char_u numbuf[NUMBUFLEN];
6637 listitem_T *item;
6638 char_u *s;
6639
6640 /* Stringify each item in the list. */
6641 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6642 {
6643 if (echo_style)
6644 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6645 else
6646 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6647 if (s == NULL)
6648 return FAIL;
6649
6650 len = (int)STRLEN(s);
6651 sumlen += len;
6652
Bram Moolenaarcde88542015-08-11 19:14:00 +02006653 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006654 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6655 if (tofree != NULL || s != numbuf)
6656 {
6657 p->s = s;
6658 p->tofree = tofree;
6659 }
6660 else
6661 {
6662 p->s = vim_strnsave(s, len);
6663 p->tofree = p->s;
6664 }
6665
6666 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006667 if (did_echo_string_emsg) /* recursion error, bail out */
6668 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006669 }
6670
6671 /* Allocate result buffer with its total size, avoid re-allocation and
6672 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6673 if (join_gap->ga_len >= 2)
6674 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6675 if (ga_grow(gap, sumlen + 2) == FAIL)
6676 return FAIL;
6677
6678 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6679 {
6680 if (first)
6681 first = FALSE;
6682 else
6683 ga_concat(gap, sep);
6684 p = ((join_T *)join_gap->ga_data) + i;
6685
6686 if (p->s != NULL)
6687 ga_concat(gap, p->s);
6688 line_breakcheck();
6689 }
6690
6691 return OK;
6692}
6693
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006694/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006695 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006696 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006697 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006698 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006699 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006700list_join(
6701 garray_T *gap,
6702 list_T *l,
6703 char_u *sep,
6704 int echo_style,
6705 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006706{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006707 garray_T join_ga;
6708 int retval;
6709 join_T *p;
6710 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006711
Bram Moolenaard39a7512015-04-16 22:51:22 +02006712 if (l->lv_len < 1)
6713 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006714 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6715 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6716
6717 /* Dispose each item in join_ga. */
6718 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006719 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006720 p = (join_T *)join_ga.ga_data;
6721 for (i = 0; i < join_ga.ga_len; ++i)
6722 {
6723 vim_free(p->tofree);
6724 ++p;
6725 }
6726 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006727 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006728
6729 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006730}
6731
6732/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006733 * Return the next (unique) copy ID.
6734 * Used for serializing nested structures.
6735 */
6736 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006737get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006738{
6739 current_copyID += COPYID_INC;
6740 return current_copyID;
6741}
6742
6743/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006744 * Garbage collection for lists and dictionaries.
6745 *
6746 * We use reference counts to be able to free most items right away when they
6747 * are no longer used. But for composite items it's possible that it becomes
6748 * unused while the reference count is > 0: When there is a recursive
6749 * reference. Example:
6750 * :let l = [1, 2, 3]
6751 * :let d = {9: l}
6752 * :let l[1] = d
6753 *
6754 * Since this is quite unusual we handle this with garbage collection: every
6755 * once in a while find out which lists and dicts are not referenced from any
6756 * variable.
6757 *
6758 * Here is a good reference text about garbage collection (refers to Python
6759 * but it applies to all reference-counting mechanisms):
6760 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006761 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006762
6763/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006764 * Do garbage collection for lists and dicts.
6765 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006766 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006767 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006768garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006769{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006770 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006771 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006772 buf_T *buf;
6773 win_T *wp;
6774 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006775 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006776 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006777 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006778#ifdef FEAT_WINDOWS
6779 tabpage_T *tp;
6780#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006781
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006782 /* Only do this once. */
6783 want_garbage_collect = FALSE;
6784 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006785 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006786
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006787 /* We advance by two because we add one for items referenced through
6788 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006789 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006790
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006791 /*
6792 * 1. Go through all accessible variables and mark all lists and dicts
6793 * with copyID.
6794 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006795
6796 /* Don't free variables in the previous_funccal list unless they are only
6797 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006798 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006799 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6800 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006801 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6802 NULL);
6803 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6804 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006805 }
6806
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006807 /* script-local variables */
6808 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006809 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006810
6811 /* buffer-local variables */
6812 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006813 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6814 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006815
6816 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006817 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006818 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6819 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006820#ifdef FEAT_AUTOCMD
6821 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006822 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6823 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006824#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006825
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006826#ifdef FEAT_WINDOWS
6827 /* tabpage-local variables */
6828 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006829 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6830 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006831#endif
6832
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006833 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006834 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006835
6836 /* function-local variables */
6837 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6838 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006839 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6840 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006841 }
6842
Bram Moolenaard812df62008-11-09 12:46:09 +00006843 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006844 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006845
Bram Moolenaar1dced572012-04-05 16:54:08 +02006846#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006847 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006848#endif
6849
Bram Moolenaardb913952012-06-29 12:54:53 +02006850#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006851 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006852#endif
6853
6854#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006855 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006856#endif
6857
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006858 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006859 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006860 /*
6861 * 2. Free lists and dictionaries that are not referenced.
6862 */
6863 did_free = free_unref_items(copyID);
6864
6865 /*
6866 * 3. Check if any funccal can be freed now.
6867 */
6868 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006869 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006870 if (can_free_funccal(*pfc, copyID))
6871 {
6872 fc = *pfc;
6873 *pfc = fc->caller;
6874 free_funccal(fc, TRUE);
6875 did_free = TRUE;
6876 did_free_funccal = TRUE;
6877 }
6878 else
6879 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006880 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006881 if (did_free_funccal)
6882 /* When a funccal was freed some more items might be garbage
6883 * collected, so run again. */
6884 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006885 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006886 else if (p_verbose > 0)
6887 {
6888 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6889 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006890
6891 return did_free;
6892}
6893
6894/*
6895 * Free lists and dictionaries that are no longer referenced.
6896 */
6897 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006898free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006899{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006900 dict_T *dd, *dd_next;
6901 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006902 int did_free = FALSE;
6903
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006904 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006905 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006906 */
6907 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006908 {
6909 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006910 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006911 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006912 /* Free the Dictionary and ordinary items it contains, but don't
6913 * recurse into Lists and Dictionaries, they will be in the list
6914 * of dicts or list of lists. */
6915 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006916 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006917 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006918 dd = dd_next;
6919 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006920
6921 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006922 * Go through the list of lists and free items without the copyID.
6923 * But don't free a list that has a watcher (used in a for loop), these
6924 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006925 */
6926 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006927 {
6928 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006929 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6930 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006931 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006932 /* Free the List and ordinary items it contains, but don't recurse
6933 * into Lists and Dictionaries, they will be in the list of dicts
6934 * or list of lists. */
6935 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006936 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006937 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006938 ll = ll_next;
6939 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006940 return did_free;
6941}
6942
6943/*
6944 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006945 * "list_stack" is used to add lists to be marked. Can be NULL.
6946 *
6947 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006948 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006949 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006950set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006951{
6952 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006953 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006954 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006955 hashtab_T *cur_ht;
6956 ht_stack_T *ht_stack = NULL;
6957 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006958
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006959 cur_ht = ht;
6960 for (;;)
6961 {
6962 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006963 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006964 /* Mark each item in the hashtab. If the item contains a hashtab
6965 * it is added to ht_stack, if it contains a list it is added to
6966 * list_stack. */
6967 todo = (int)cur_ht->ht_used;
6968 for (hi = cur_ht->ht_array; todo > 0; ++hi)
6969 if (!HASHITEM_EMPTY(hi))
6970 {
6971 --todo;
6972 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
6973 &ht_stack, list_stack);
6974 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006975 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006976
6977 if (ht_stack == NULL)
6978 break;
6979
6980 /* take an item from the stack */
6981 cur_ht = ht_stack->ht;
6982 tempitem = ht_stack;
6983 ht_stack = ht_stack->prev;
6984 free(tempitem);
6985 }
6986
6987 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988}
6989
6990/*
6991 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006992 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6993 *
6994 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006995 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006996 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006997set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006998{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006999 listitem_T *li;
7000 int abort = FALSE;
7001 list_T *cur_l;
7002 list_stack_T *list_stack = NULL;
7003 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007004
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007005 cur_l = l;
7006 for (;;)
7007 {
7008 if (!abort)
7009 /* Mark each item in the list. If the item contains a hashtab
7010 * it is added to ht_stack, if it contains a list it is added to
7011 * list_stack. */
7012 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7013 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7014 ht_stack, &list_stack);
7015 if (list_stack == NULL)
7016 break;
7017
7018 /* take an item from the stack */
7019 cur_l = list_stack->list;
7020 tempitem = list_stack;
7021 list_stack = list_stack->prev;
7022 free(tempitem);
7023 }
7024
7025 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007026}
7027
7028/*
7029 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007030 * "list_stack" is used to add lists to be marked. Can be NULL.
7031 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7032 *
7033 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007034 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007035 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007036set_ref_in_item(
7037 typval_T *tv,
7038 int copyID,
7039 ht_stack_T **ht_stack,
7040 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007041{
7042 dict_T *dd;
7043 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007044 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007045
7046 switch (tv->v_type)
7047 {
7048 case VAR_DICT:
7049 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007050 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007051 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007052 /* Didn't see this dict yet. */
7053 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007054 if (ht_stack == NULL)
7055 {
7056 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7057 }
7058 else
7059 {
7060 ht_stack_T *newitem = (ht_stack_T*)malloc(
7061 sizeof(ht_stack_T));
7062 if (newitem == NULL)
7063 abort = TRUE;
7064 else
7065 {
7066 newitem->ht = &dd->dv_hashtab;
7067 newitem->prev = *ht_stack;
7068 *ht_stack = newitem;
7069 }
7070 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007071 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007072 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007073
7074 case VAR_LIST:
7075 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007076 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007077 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007078 /* Didn't see this list yet. */
7079 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007080 if (list_stack == NULL)
7081 {
7082 abort = set_ref_in_list(ll, copyID, ht_stack);
7083 }
7084 else
7085 {
7086 list_stack_T *newitem = (list_stack_T*)malloc(
7087 sizeof(list_stack_T));
7088 if (newitem == NULL)
7089 abort = TRUE;
7090 else
7091 {
7092 newitem->list = ll;
7093 newitem->prev = *list_stack;
7094 *list_stack = newitem;
7095 }
7096 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007097 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007098 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007099 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007100 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007101}
7102
7103/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007104 * Allocate an empty header for a dictionary.
7105 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007106 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007107dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007108{
Bram Moolenaar33570922005-01-25 22:26:29 +00007109 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007110
Bram Moolenaar33570922005-01-25 22:26:29 +00007111 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007112 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007113 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007114 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007115 if (first_dict != NULL)
7116 first_dict->dv_used_prev = d;
7117 d->dv_used_next = first_dict;
7118 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007119 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007120
Bram Moolenaar33570922005-01-25 22:26:29 +00007121 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007122 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007123 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007124 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007125 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007126 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007127 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007128}
7129
7130/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007131 * Allocate an empty dict for a return value.
7132 * Returns OK or FAIL.
7133 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007134 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007135rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007136{
7137 dict_T *d = dict_alloc();
7138
7139 if (d == NULL)
7140 return FAIL;
7141
7142 rettv->vval.v_dict = d;
7143 rettv->v_type = VAR_DICT;
7144 ++d->dv_refcount;
7145 return OK;
7146}
7147
7148
7149/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007150 * Unreference a Dictionary: decrement the reference count and free it when it
7151 * becomes zero.
7152 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007153 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007154dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007155{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007156 if (d != NULL && --d->dv_refcount <= 0)
7157 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007158}
7159
7160/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007161 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007162 * Ignores the reference count.
7163 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007164 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007165dict_free(
7166 dict_T *d,
7167 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007168{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007169 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007170 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007171 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007172
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007173 /* Remove the dict from the list of dicts for garbage collection. */
7174 if (d->dv_used_prev == NULL)
7175 first_dict = d->dv_used_next;
7176 else
7177 d->dv_used_prev->dv_used_next = d->dv_used_next;
7178 if (d->dv_used_next != NULL)
7179 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7180
7181 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007182 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007183 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007184 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007185 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007186 if (!HASHITEM_EMPTY(hi))
7187 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007188 /* Remove the item before deleting it, just in case there is
7189 * something recursive causing trouble. */
7190 di = HI2DI(hi);
7191 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007192 if (recurse || (di->di_tv.v_type != VAR_LIST
7193 && di->di_tv.v_type != VAR_DICT))
7194 clear_tv(&di->di_tv);
7195 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007196 --todo;
7197 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007198 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007199 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007200 vim_free(d);
7201}
7202
7203/*
7204 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007205 * The "key" is copied to the new item.
7206 * Note that the value of the item "di_tv" still needs to be initialized!
7207 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007208 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007209 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007210dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007211{
Bram Moolenaar33570922005-01-25 22:26:29 +00007212 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007213
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007214 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007215 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007216 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007217 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007218 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007219 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007220 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007221}
7222
7223/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007224 * Make a copy of a Dictionary item.
7225 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007226 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007227dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007228{
Bram Moolenaar33570922005-01-25 22:26:29 +00007229 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007230
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007231 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7232 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007233 if (di != NULL)
7234 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007235 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007236 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007237 copy_tv(&org->di_tv, &di->di_tv);
7238 }
7239 return di;
7240}
7241
7242/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007243 * Remove item "item" from Dictionary "dict" and free it.
7244 */
7245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007246dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007247{
Bram Moolenaar33570922005-01-25 22:26:29 +00007248 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007249
Bram Moolenaar33570922005-01-25 22:26:29 +00007250 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007251 if (HASHITEM_EMPTY(hi))
7252 EMSG2(_(e_intern2), "dictitem_remove()");
7253 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007254 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007255 dictitem_free(item);
7256}
7257
7258/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007259 * Free a dict item. Also clears the value.
7260 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007261 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007262dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007263{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007264 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007265 if (item->di_flags & DI_FLAGS_ALLOC)
7266 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267}
7268
7269/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007270 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7271 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007272 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007273 * Returns NULL when out of memory.
7274 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007275 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007276dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007277{
Bram Moolenaar33570922005-01-25 22:26:29 +00007278 dict_T *copy;
7279 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007280 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007281 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007282
7283 if (orig == NULL)
7284 return NULL;
7285
7286 copy = dict_alloc();
7287 if (copy != NULL)
7288 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007289 if (copyID != 0)
7290 {
7291 orig->dv_copyID = copyID;
7292 orig->dv_copydict = copy;
7293 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007294 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007295 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007296 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007297 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007298 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007299 --todo;
7300
7301 di = dictitem_alloc(hi->hi_key);
7302 if (di == NULL)
7303 break;
7304 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007305 {
7306 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7307 copyID) == FAIL)
7308 {
7309 vim_free(di);
7310 break;
7311 }
7312 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007313 else
7314 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7315 if (dict_add(copy, di) == FAIL)
7316 {
7317 dictitem_free(di);
7318 break;
7319 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007320 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007321 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007322
Bram Moolenaare9a41262005-01-15 22:18:47 +00007323 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007324 if (todo > 0)
7325 {
7326 dict_unref(copy);
7327 copy = NULL;
7328 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007329 }
7330
7331 return copy;
7332}
7333
7334/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007335 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007336 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007337 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007338 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007339dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007340{
Bram Moolenaar33570922005-01-25 22:26:29 +00007341 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007342}
7343
Bram Moolenaar8c711452005-01-14 21:53:12 +00007344/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007345 * Add a number or string entry to dictionary "d".
7346 * When "str" is NULL use number "nr", otherwise use "str".
7347 * Returns FAIL when out of memory and when key already exists.
7348 */
7349 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007350dict_add_nr_str(
7351 dict_T *d,
7352 char *key,
7353 long nr,
7354 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007355{
7356 dictitem_T *item;
7357
7358 item = dictitem_alloc((char_u *)key);
7359 if (item == NULL)
7360 return FAIL;
7361 item->di_tv.v_lock = 0;
7362 if (str == NULL)
7363 {
7364 item->di_tv.v_type = VAR_NUMBER;
7365 item->di_tv.vval.v_number = nr;
7366 }
7367 else
7368 {
7369 item->di_tv.v_type = VAR_STRING;
7370 item->di_tv.vval.v_string = vim_strsave(str);
7371 }
7372 if (dict_add(d, item) == FAIL)
7373 {
7374 dictitem_free(item);
7375 return FAIL;
7376 }
7377 return OK;
7378}
7379
7380/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007381 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007382 * Returns FAIL when out of memory and when key already exists.
7383 */
7384 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007385dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007386{
7387 dictitem_T *item;
7388
7389 item = dictitem_alloc((char_u *)key);
7390 if (item == NULL)
7391 return FAIL;
7392 item->di_tv.v_lock = 0;
7393 item->di_tv.v_type = VAR_LIST;
7394 item->di_tv.vval.v_list = list;
7395 if (dict_add(d, item) == FAIL)
7396 {
7397 dictitem_free(item);
7398 return FAIL;
7399 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007400 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007401 return OK;
7402}
7403
7404/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007405 * Get the number of items in a Dictionary.
7406 */
7407 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007408dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007409{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007410 if (d == NULL)
7411 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007412 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007413}
7414
7415/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007416 * Find item "key[len]" in Dictionary "d".
7417 * If "len" is negative use strlen(key).
7418 * Returns NULL when not found.
7419 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007420 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007421dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007422{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007423#define AKEYLEN 200
7424 char_u buf[AKEYLEN];
7425 char_u *akey;
7426 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007427 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007428
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007429 if (len < 0)
7430 akey = key;
7431 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007432 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007433 tofree = akey = vim_strnsave(key, len);
7434 if (akey == NULL)
7435 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007436 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007437 else
7438 {
7439 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007440 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007441 akey = buf;
7442 }
7443
Bram Moolenaar33570922005-01-25 22:26:29 +00007444 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007445 vim_free(tofree);
7446 if (HASHITEM_EMPTY(hi))
7447 return NULL;
7448 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007449}
7450
7451/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007452 * Get a string item from a dictionary.
7453 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007454 * Returns NULL if the entry doesn't exist or out of memory.
7455 */
7456 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007457get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007458{
7459 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007460 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007461
7462 di = dict_find(d, key, -1);
7463 if (di == NULL)
7464 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007465 s = get_tv_string(&di->di_tv);
7466 if (save && s != NULL)
7467 s = vim_strsave(s);
7468 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007469}
7470
7471/*
7472 * Get a number item from a dictionary.
7473 * Returns 0 if the entry doesn't exist or out of memory.
7474 */
7475 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007476get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007477{
7478 dictitem_T *di;
7479
7480 di = dict_find(d, key, -1);
7481 if (di == NULL)
7482 return 0;
7483 return get_tv_number(&di->di_tv);
7484}
7485
7486/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007487 * Return an allocated string with the string representation of a Dictionary.
7488 * May return NULL.
7489 */
7490 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007491dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007492{
7493 garray_T ga;
7494 int first = TRUE;
7495 char_u *tofree;
7496 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007497 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007498 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007499 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007500 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007501
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007502 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007503 return NULL;
7504 ga_init2(&ga, (int)sizeof(char), 80);
7505 ga_append(&ga, '{');
7506
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007507 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007508 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007509 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007510 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007511 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007512 --todo;
7513
7514 if (first)
7515 first = FALSE;
7516 else
7517 ga_concat(&ga, (char_u *)", ");
7518
7519 tofree = string_quote(hi->hi_key, FALSE);
7520 if (tofree != NULL)
7521 {
7522 ga_concat(&ga, tofree);
7523 vim_free(tofree);
7524 }
7525 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007526 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007527 if (s != NULL)
7528 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007529 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007530 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007531 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007532 line_breakcheck();
7533
Bram Moolenaar8c711452005-01-14 21:53:12 +00007534 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007535 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007536 if (todo > 0)
7537 {
7538 vim_free(ga.ga_data);
7539 return NULL;
7540 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007541
7542 ga_append(&ga, '}');
7543 ga_append(&ga, NUL);
7544 return (char_u *)ga.ga_data;
7545}
7546
7547/*
7548 * Allocate a variable for a Dictionary and fill it from "*arg".
7549 * Return OK or FAIL. Returns NOTDONE for {expr}.
7550 */
7551 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007552get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007553{
Bram Moolenaar33570922005-01-25 22:26:29 +00007554 dict_T *d = NULL;
7555 typval_T tvkey;
7556 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007557 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007558 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007559 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007560 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007561
7562 /*
7563 * First check if it's not a curly-braces thing: {expr}.
7564 * Must do this without evaluating, otherwise a function may be called
7565 * twice. Unfortunately this means we need to call eval1() twice for the
7566 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007567 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007568 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007569 if (*start != '}')
7570 {
7571 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7572 return FAIL;
7573 if (*start == '}')
7574 return NOTDONE;
7575 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576
7577 if (evaluate)
7578 {
7579 d = dict_alloc();
7580 if (d == NULL)
7581 return FAIL;
7582 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007583 tvkey.v_type = VAR_UNKNOWN;
7584 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007585
7586 *arg = skipwhite(*arg + 1);
7587 while (**arg != '}' && **arg != NUL)
7588 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007589 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007590 goto failret;
7591 if (**arg != ':')
7592 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007593 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007594 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007595 goto failret;
7596 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007597 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007598 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007599 key = get_tv_string_buf_chk(&tvkey, buf);
7600 if (key == NULL || *key == NUL)
7601 {
7602 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7603 if (key != NULL)
7604 EMSG(_(e_emptykey));
7605 clear_tv(&tvkey);
7606 goto failret;
7607 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007608 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007609
7610 *arg = skipwhite(*arg + 1);
7611 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7612 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007613 if (evaluate)
7614 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007615 goto failret;
7616 }
7617 if (evaluate)
7618 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007619 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007620 if (item != NULL)
7621 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007622 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007623 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007624 clear_tv(&tv);
7625 goto failret;
7626 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007627 item = dictitem_alloc(key);
7628 clear_tv(&tvkey);
7629 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007630 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007631 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007632 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007633 if (dict_add(d, item) == FAIL)
7634 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007635 }
7636 }
7637
7638 if (**arg == '}')
7639 break;
7640 if (**arg != ',')
7641 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007642 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007643 goto failret;
7644 }
7645 *arg = skipwhite(*arg + 1);
7646 }
7647
7648 if (**arg != '}')
7649 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007650 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007651failret:
7652 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007653 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007654 return FAIL;
7655 }
7656
7657 *arg = skipwhite(*arg + 1);
7658 if (evaluate)
7659 {
7660 rettv->v_type = VAR_DICT;
7661 rettv->vval.v_dict = d;
7662 ++d->dv_refcount;
7663 }
7664
7665 return OK;
7666}
7667
Bram Moolenaar17a13432016-01-24 14:22:10 +01007668 static char *
7669get_var_special_name(int nr)
7670{
7671 switch (nr)
7672 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007673 case VVAL_FALSE: return "v:false";
7674 case VVAL_TRUE: return "v:true";
7675 case VVAL_NONE: return "v:none";
7676 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007677 }
7678 EMSG2(_(e_intern2), "get_var_special_name()");
7679 return "42";
7680}
7681
Bram Moolenaar8c711452005-01-14 21:53:12 +00007682/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007683 * Return a string with the string representation of a variable.
7684 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007685 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007686 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007687 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007688 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007689 */
7690 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007691echo_string(
7692 typval_T *tv,
7693 char_u **tofree,
7694 char_u *numbuf,
7695 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007696{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007697 static int recurse = 0;
7698 char_u *r = NULL;
7699
Bram Moolenaar33570922005-01-25 22:26:29 +00007700 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007701 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007702 if (!did_echo_string_emsg)
7703 {
7704 /* Only give this message once for a recursive call to avoid
7705 * flooding the user with errors. And stop iterating over lists
7706 * and dicts. */
7707 did_echo_string_emsg = TRUE;
7708 EMSG(_("E724: variable nested too deep for displaying"));
7709 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007710 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007711 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007712 }
7713 ++recurse;
7714
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007715 switch (tv->v_type)
7716 {
7717 case VAR_FUNC:
7718 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007719 r = tv->vval.v_string;
7720 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007721
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007722 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007723 if (tv->vval.v_list == NULL)
7724 {
7725 *tofree = NULL;
7726 r = NULL;
7727 }
7728 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7729 {
7730 *tofree = NULL;
7731 r = (char_u *)"[...]";
7732 }
7733 else
7734 {
7735 tv->vval.v_list->lv_copyID = copyID;
7736 *tofree = list2string(tv, copyID);
7737 r = *tofree;
7738 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007739 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007740
Bram Moolenaar8c711452005-01-14 21:53:12 +00007741 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007742 if (tv->vval.v_dict == NULL)
7743 {
7744 *tofree = NULL;
7745 r = NULL;
7746 }
7747 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7748 {
7749 *tofree = NULL;
7750 r = (char_u *)"{...}";
7751 }
7752 else
7753 {
7754 tv->vval.v_dict->dv_copyID = copyID;
7755 *tofree = dict2string(tv, copyID);
7756 r = *tofree;
7757 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007758 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007759
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007760 case VAR_STRING:
7761 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007762 *tofree = NULL;
7763 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007764 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007765
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007766#ifdef FEAT_FLOAT
7767 case VAR_FLOAT:
7768 *tofree = NULL;
7769 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7770 r = numbuf;
7771 break;
7772#endif
7773
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007774 case VAR_SPECIAL:
7775 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007776 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007777 break;
7778
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007779 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007780 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007781 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007782 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007783
Bram Moolenaar8502c702014-06-17 12:51:16 +02007784 if (--recurse == 0)
7785 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007786 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007787}
7788
7789/*
7790 * Return a string with the string representation of a variable.
7791 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7792 * "numbuf" is used for a number.
7793 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007794 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007795 */
7796 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007797tv2string(
7798 typval_T *tv,
7799 char_u **tofree,
7800 char_u *numbuf,
7801 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007802{
7803 switch (tv->v_type)
7804 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007805 case VAR_FUNC:
7806 *tofree = string_quote(tv->vval.v_string, TRUE);
7807 return *tofree;
7808 case VAR_STRING:
7809 *tofree = string_quote(tv->vval.v_string, FALSE);
7810 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007811#ifdef FEAT_FLOAT
7812 case VAR_FLOAT:
7813 *tofree = NULL;
7814 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7815 return numbuf;
7816#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007817 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007818 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007819 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007820 case VAR_SPECIAL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007821 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007822 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007823 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007824 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007825 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007826}
7827
7828/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007829 * Return string "str" in ' quotes, doubling ' characters.
7830 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007831 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007832 */
7833 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007834string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007835{
Bram Moolenaar33570922005-01-25 22:26:29 +00007836 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007837 char_u *p, *r, *s;
7838
Bram Moolenaar33570922005-01-25 22:26:29 +00007839 len = (function ? 13 : 3);
7840 if (str != NULL)
7841 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007842 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007843 for (p = str; *p != NUL; mb_ptr_adv(p))
7844 if (*p == '\'')
7845 ++len;
7846 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007847 s = r = alloc(len);
7848 if (r != NULL)
7849 {
7850 if (function)
7851 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007852 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007853 r += 10;
7854 }
7855 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007856 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007857 if (str != NULL)
7858 for (p = str; *p != NUL; )
7859 {
7860 if (*p == '\'')
7861 *r++ = '\'';
7862 MB_COPY_CHAR(p, r);
7863 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007864 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007865 if (function)
7866 *r++ = ')';
7867 *r++ = NUL;
7868 }
7869 return s;
7870}
7871
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007872#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007873/*
7874 * Convert the string "text" to a floating point number.
7875 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7876 * this always uses a decimal point.
7877 * Returns the length of the text that was consumed.
7878 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007879 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007880string2float(
7881 char_u *text,
7882 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007883{
7884 char *s = (char *)text;
7885 float_T f;
7886
7887 f = strtod(s, &s);
7888 *value = f;
7889 return (int)((char_u *)s - text);
7890}
7891#endif
7892
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007893/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 * Get the value of an environment variable.
7895 * "arg" is pointing to the '$'. It is advanced to after the name.
7896 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007897 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 */
7899 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007900get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901{
7902 char_u *string = NULL;
7903 int len;
7904 int cc;
7905 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007906 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907
7908 ++*arg;
7909 name = *arg;
7910 len = get_env_len(arg);
7911 if (evaluate)
7912 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007913 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01007914 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007915
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007916 cc = name[len];
7917 name[len] = NUL;
7918 /* first try vim_getenv(), fast for normal environment vars */
7919 string = vim_getenv(name, &mustfree);
7920 if (string != NULL && *string != NUL)
7921 {
7922 if (!mustfree)
7923 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007925 else
7926 {
7927 if (mustfree)
7928 vim_free(string);
7929
7930 /* next try expanding things like $VIM and ${HOME} */
7931 string = expand_env_save(name - 1);
7932 if (string != NULL && *string == '$')
7933 {
7934 vim_free(string);
7935 string = NULL;
7936 }
7937 }
7938 name[len] = cc;
7939
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007940 rettv->v_type = VAR_STRING;
7941 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 }
7943
7944 return OK;
7945}
7946
7947/*
7948 * Array with names and number of arguments of all internal functions
7949 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7950 */
7951static struct fst
7952{
7953 char *f_name; /* function name */
7954 char f_min_argc; /* minimal number of arguments */
7955 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01007956 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007957 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958} functions[] =
7959{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007960#ifdef FEAT_FLOAT
7961 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007962 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007963#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007964 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01007965 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007966 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 {"append", 2, 2, f_append},
7968 {"argc", 0, 0, f_argc},
7969 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007970 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007971 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01007972#ifdef FEAT_FLOAT
7973 {"asin", 1, 1, f_asin}, /* WJMc */
7974#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01007975 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01007976 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01007977 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01007978 {"assert_false", 1, 2, f_assert_false},
7979 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007980#ifdef FEAT_FLOAT
7981 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007982 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007985 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 {"bufexists", 1, 1, f_bufexists},
7987 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7988 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7989 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7990 {"buflisted", 1, 1, f_buflisted},
7991 {"bufloaded", 1, 1, f_bufloaded},
7992 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007993 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 {"bufwinnr", 1, 1, f_bufwinnr},
7995 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007996 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007997 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007998 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007999#ifdef FEAT_FLOAT
8000 {"ceil", 1, 1, f_ceil},
8001#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008002#ifdef FEAT_CHANNEL
8003 {"ch_close", 1, 1, f_ch_close},
8004 {"ch_open", 2, 3, f_ch_open},
8005 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8006 {"ch_sendraw", 2, 3, f_ch_sendraw},
8007#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008008 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008009 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008011 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008013#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008014 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008015 {"complete_add", 1, 1, f_complete_add},
8016 {"complete_check", 0, 0, f_complete_check},
8017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008019 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008020#ifdef FEAT_FLOAT
8021 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008022 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008023#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008024 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008026 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008027 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008028 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008030 {"diff_filler", 1, 1, f_diff_filler},
8031 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008032 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008034 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 {"eventhandler", 0, 0, f_eventhandler},
8036 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008037 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008039#ifdef FEAT_FLOAT
8040 {"exp", 1, 1, f_exp},
8041#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008042 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008043 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008044 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8046 {"filereadable", 1, 1, f_filereadable},
8047 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008048 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008049 {"finddir", 1, 3, f_finddir},
8050 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008051#ifdef FEAT_FLOAT
8052 {"float2nr", 1, 1, f_float2nr},
8053 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008054 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008055#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008056 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 {"fnamemodify", 2, 2, f_fnamemodify},
8058 {"foldclosed", 1, 1, f_foldclosed},
8059 {"foldclosedend", 1, 1, f_foldclosedend},
8060 {"foldlevel", 1, 1, f_foldlevel},
8061 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008062 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008064 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008065 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008066 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008067 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008068 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 {"getchar", 0, 1, f_getchar},
8070 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008071 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 {"getcmdline", 0, 0, f_getcmdline},
8073 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008074 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008075 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008076 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008077 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008078 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008079 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {"getfsize", 1, 1, f_getfsize},
8081 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008082 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008083 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008084 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008085 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008086 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008087 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008088 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008089 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008091 {"gettabvar", 2, 3, f_gettabvar},
8092 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {"getwinposx", 0, 0, f_getwinposx},
8094 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008095 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008096 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008097 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008098 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008100 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008101 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008102 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8104 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8105 {"histadd", 2, 2, f_histadd},
8106 {"histdel", 1, 2, f_histdel},
8107 {"histget", 1, 2, f_histget},
8108 {"histnr", 1, 1, f_histnr},
8109 {"hlID", 1, 1, f_hlID},
8110 {"hlexists", 1, 1, f_hlexists},
8111 {"hostname", 0, 0, f_hostname},
8112 {"iconv", 3, 3, f_iconv},
8113 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008114 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008115 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008117 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 {"inputrestore", 0, 0, f_inputrestore},
8119 {"inputsave", 0, 0, f_inputsave},
8120 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008121 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008122 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008124 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008125 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008126 {"join", 1, 2, f_join},
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008127 {"jsondecode", 1, 1, f_jsondecode},
8128 {"jsonencode", 1, 1, f_jsonencode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008129 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008131 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 {"libcall", 3, 3, f_libcall},
8133 {"libcallnr", 3, 3, f_libcallnr},
8134 {"line", 1, 1, f_line},
8135 {"line2byte", 1, 1, f_line2byte},
8136 {"lispindent", 1, 1, f_lispindent},
8137 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008138#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008139 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008140 {"log10", 1, 1, f_log10},
8141#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008142#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008143 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008144#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008145 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008146 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008147 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008148 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008149 {"matchadd", 2, 5, f_matchadd},
8150 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008151 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008152 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008153 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008154 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008155 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008156 {"max", 1, 1, f_max},
8157 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008158#ifdef vim_mkdir
8159 {"mkdir", 1, 3, f_mkdir},
8160#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008161 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008162#ifdef FEAT_MZSCHEME
8163 {"mzeval", 1, 1, f_mzeval},
8164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008166 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008167 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008168 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008169#ifdef FEAT_PERL
8170 {"perleval", 1, 1, f_perleval},
8171#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008172#ifdef FEAT_FLOAT
8173 {"pow", 2, 2, f_pow},
8174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008176 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008177 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008178#ifdef FEAT_PYTHON3
8179 {"py3eval", 1, 1, f_py3eval},
8180#endif
8181#ifdef FEAT_PYTHON
8182 {"pyeval", 1, 1, f_pyeval},
8183#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008184 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008185 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008186 {"reltime", 0, 2, f_reltime},
8187 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 {"remote_expr", 2, 3, f_remote_expr},
8189 {"remote_foreground", 1, 1, f_remote_foreground},
8190 {"remote_peek", 1, 2, f_remote_peek},
8191 {"remote_read", 1, 1, f_remote_read},
8192 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008193 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008195 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008197 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008198#ifdef FEAT_FLOAT
8199 {"round", 1, 1, f_round},
8200#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008201 {"screenattr", 2, 2, f_screenattr},
8202 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008203 {"screencol", 0, 0, f_screencol},
8204 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008205 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008206 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008207 {"searchpair", 3, 7, f_searchpair},
8208 {"searchpairpos", 3, 7, f_searchpairpos},
8209 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 {"server2client", 2, 2, f_server2client},
8211 {"serverlist", 0, 0, f_serverlist},
8212 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008213 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 {"setcmdpos", 1, 1, f_setcmdpos},
8215 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008216 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008217 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008218 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008219 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008221 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008222 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008224#ifdef FEAT_CRYPT
8225 {"sha256", 1, 1, f_sha256},
8226#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008227 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008228 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008230#ifdef FEAT_FLOAT
8231 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008232 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008233#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008234 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008235 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008236 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008237 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008238 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008239#ifdef FEAT_FLOAT
8240 {"sqrt", 1, 1, f_sqrt},
8241 {"str2float", 1, 1, f_str2float},
8242#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008243 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008244 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008245 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246#ifdef HAVE_STRFTIME
8247 {"strftime", 1, 2, f_strftime},
8248#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008249 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008250 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 {"strlen", 1, 1, f_strlen},
8252 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008253 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008255 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008256 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 {"substitute", 4, 4, f_substitute},
8258 {"synID", 3, 3, f_synID},
8259 {"synIDattr", 2, 3, f_synIDattr},
8260 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008261 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008262 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008263 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008264 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008265 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008266 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008267 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008268 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008269 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008270#ifdef FEAT_FLOAT
8271 {"tan", 1, 1, f_tan},
8272 {"tanh", 1, 1, f_tanh},
8273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008275 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 {"tolower", 1, 1, f_tolower},
8277 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008278 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008279#ifdef FEAT_FLOAT
8280 {"trunc", 1, 1, f_trunc},
8281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008283 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008284 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008285 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008286 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 {"virtcol", 1, 1, f_virtcol},
8288 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008289 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 {"winbufnr", 1, 1, f_winbufnr},
8291 {"wincol", 0, 0, f_wincol},
8292 {"winheight", 1, 1, f_winheight},
8293 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008294 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008296 {"winrestview", 1, 1, f_winrestview},
8297 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008299 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008300 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008301 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302};
8303
8304#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8305
8306/*
8307 * Function given to ExpandGeneric() to obtain the list of internal
8308 * or user defined function names.
8309 */
8310 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008311get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312{
8313 static int intidx = -1;
8314 char_u *name;
8315
8316 if (idx == 0)
8317 intidx = -1;
8318 if (intidx < 0)
8319 {
8320 name = get_user_func_name(xp, idx);
8321 if (name != NULL)
8322 return name;
8323 }
8324 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8325 {
8326 STRCPY(IObuff, functions[intidx].f_name);
8327 STRCAT(IObuff, "(");
8328 if (functions[intidx].f_max_argc == 0)
8329 STRCAT(IObuff, ")");
8330 return IObuff;
8331 }
8332
8333 return NULL;
8334}
8335
8336/*
8337 * Function given to ExpandGeneric() to obtain the list of internal or
8338 * user defined variable or function names.
8339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008341get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342{
8343 static int intidx = -1;
8344 char_u *name;
8345
8346 if (idx == 0)
8347 intidx = -1;
8348 if (intidx < 0)
8349 {
8350 name = get_function_name(xp, idx);
8351 if (name != NULL)
8352 return name;
8353 }
8354 return get_user_var_name(xp, ++intidx);
8355}
8356
8357#endif /* FEAT_CMDL_COMPL */
8358
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008359#if defined(EBCDIC) || defined(PROTO)
8360/*
8361 * Compare struct fst by function name.
8362 */
8363 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008364compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008365{
8366 struct fst *p1 = (struct fst *)s1;
8367 struct fst *p2 = (struct fst *)s2;
8368
8369 return STRCMP(p1->f_name, p2->f_name);
8370}
8371
8372/*
8373 * Sort the function table by function name.
8374 * The sorting of the table above is ASCII dependant.
8375 * On machines using EBCDIC we have to sort it.
8376 */
8377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008378sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008379{
8380 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8381
8382 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8383}
8384#endif
8385
8386
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387/*
8388 * Find internal function in table above.
8389 * Return index, or -1 if not found
8390 */
8391 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008392find_internal_func(
8393 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394{
8395 int first = 0;
8396 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8397 int cmp;
8398 int x;
8399
8400 /*
8401 * Find the function name in the table. Binary search.
8402 */
8403 while (first <= last)
8404 {
8405 x = first + ((unsigned)(last - first) >> 1);
8406 cmp = STRCMP(name, functions[x].f_name);
8407 if (cmp < 0)
8408 last = x - 1;
8409 else if (cmp > 0)
8410 first = x + 1;
8411 else
8412 return x;
8413 }
8414 return -1;
8415}
8416
8417/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008418 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8419 * name it contains, otherwise return "name".
8420 */
8421 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008422deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008423{
Bram Moolenaar33570922005-01-25 22:26:29 +00008424 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008425 int cc;
8426
8427 cc = name[*lenp];
8428 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008429 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008430 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008431 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008432 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008433 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008434 {
8435 *lenp = 0;
8436 return (char_u *)""; /* just in case */
8437 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008438 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008439 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008440 }
8441
8442 return name;
8443}
8444
8445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 * Allocate a variable for the result of a function.
8447 * Return OK or FAIL.
8448 */
8449 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008450get_func_tv(
8451 char_u *name, /* name of the function */
8452 int len, /* length of "name" */
8453 typval_T *rettv,
8454 char_u **arg, /* argument, pointing to the '(' */
8455 linenr_T firstline, /* first line of range */
8456 linenr_T lastline, /* last line of range */
8457 int *doesrange, /* return: function handled range */
8458 int evaluate,
8459 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460{
8461 char_u *argp;
8462 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008463 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 int argcount = 0; /* number of arguments found */
8465
8466 /*
8467 * Get the arguments.
8468 */
8469 argp = *arg;
8470 while (argcount < MAX_FUNC_ARGS)
8471 {
8472 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8473 if (*argp == ')' || *argp == ',' || *argp == NUL)
8474 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8476 {
8477 ret = FAIL;
8478 break;
8479 }
8480 ++argcount;
8481 if (*argp != ',')
8482 break;
8483 }
8484 if (*argp == ')')
8485 ++argp;
8486 else
8487 ret = FAIL;
8488
8489 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008490 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008491 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008493 {
8494 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008495 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008496 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008497 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499
8500 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008501 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502
8503 *arg = skipwhite(argp);
8504 return ret;
8505}
8506
8507
8508/*
8509 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008510 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008511 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008513 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008514call_func(
8515 char_u *funcname, /* name of the function */
8516 int len, /* length of "name" */
8517 typval_T *rettv, /* return value goes here */
8518 int argcount, /* number of "argvars" */
8519 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008520 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008521 linenr_T firstline, /* first line of range */
8522 linenr_T lastline, /* last line of range */
8523 int *doesrange, /* return: function handled range */
8524 int evaluate,
8525 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526{
8527 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528#define ERROR_UNKNOWN 0
8529#define ERROR_TOOMANY 1
8530#define ERROR_TOOFEW 2
8531#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008532#define ERROR_DICT 4
8533#define ERROR_NONE 5
8534#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 int error = ERROR_NONE;
8536 int i;
8537 int llen;
8538 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539#define FLEN_FIXED 40
8540 char_u fname_buf[FLEN_FIXED + 1];
8541 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008542 char_u *name;
8543
8544 /* Make a copy of the name, if it comes from a funcref variable it could
8545 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008546 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008547 if (name == NULL)
8548 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549
8550 /*
8551 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8552 * Change <SNR>123_name() to K_SNR 123_name().
8553 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8554 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 llen = eval_fname_script(name);
8556 if (llen > 0)
8557 {
8558 fname_buf[0] = K_SPECIAL;
8559 fname_buf[1] = KS_EXTRA;
8560 fname_buf[2] = (int)KE_SNR;
8561 i = 3;
8562 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8563 {
8564 if (current_SID <= 0)
8565 error = ERROR_SCRIPT;
8566 else
8567 {
8568 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8569 i = (int)STRLEN(fname_buf);
8570 }
8571 }
8572 if (i + STRLEN(name + llen) < FLEN_FIXED)
8573 {
8574 STRCPY(fname_buf + i, name + llen);
8575 fname = fname_buf;
8576 }
8577 else
8578 {
8579 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8580 if (fname == NULL)
8581 error = ERROR_OTHER;
8582 else
8583 {
8584 mch_memmove(fname, fname_buf, (size_t)i);
8585 STRCPY(fname + i, name + llen);
8586 }
8587 }
8588 }
8589 else
8590 fname = name;
8591
8592 *doesrange = FALSE;
8593
8594
8595 /* execute the function if no errors detected and executing */
8596 if (evaluate && error == ERROR_NONE)
8597 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008598 char_u *rfname = fname;
8599
8600 /* Ignore "g:" before a function name. */
8601 if (fname[0] == 'g' && fname[1] == ':')
8602 rfname = fname + 2;
8603
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008604 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8605 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 error = ERROR_UNKNOWN;
8607
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008608 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 {
8610 /*
8611 * User defined function.
8612 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008613 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008614
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008616 /* Trigger FuncUndefined event, may load the function. */
8617 if (fp == NULL
8618 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008619 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008620 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008622 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008623 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 }
8625#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008626 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008627 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008628 {
8629 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008630 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008631 }
8632
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 if (fp != NULL)
8634 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008635 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008637 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008639 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008641 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008642 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 else
8644 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008645 int did_save_redo = FALSE;
8646
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 /*
8648 * Call the user function.
8649 * Save and restore search patterns, script variables and
8650 * redo buffer.
8651 */
8652 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008653#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008654 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008655#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008656 {
8657 saveRedobuff();
8658 did_save_redo = TRUE;
8659 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008660 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008661 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008662 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008663 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8664 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8665 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008666 /* Function was unreferenced while being used, free it
8667 * now. */
8668 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008669 if (did_save_redo)
8670 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 restore_search_patterns();
8672 error = ERROR_NONE;
8673 }
8674 }
8675 }
8676 else
8677 {
8678 /*
8679 * Find the function name in the table, call its implementation.
8680 */
8681 i = find_internal_func(fname);
8682 if (i >= 0)
8683 {
8684 if (argcount < functions[i].f_min_argc)
8685 error = ERROR_TOOFEW;
8686 else if (argcount > functions[i].f_max_argc)
8687 error = ERROR_TOOMANY;
8688 else
8689 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008690 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008691 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 error = ERROR_NONE;
8693 }
8694 }
8695 }
8696 /*
8697 * The function call (or "FuncUndefined" autocommand sequence) might
8698 * have been aborted by an error, an interrupt, or an explicitly thrown
8699 * exception that has not been caught so far. This situation can be
8700 * tested for by calling aborting(). For an error in an internal
8701 * function or for the "E132" error in call_user_func(), however, the
8702 * throw point at which the "force_abort" flag (temporarily reset by
8703 * emsg()) is normally updated has not been reached yet. We need to
8704 * update that flag first to make aborting() reliable.
8705 */
8706 update_force_abort();
8707 }
8708 if (error == ERROR_NONE)
8709 ret = OK;
8710
8711 /*
8712 * Report an error unless the argument evaluation or function call has been
8713 * cancelled due to an aborting error, an interrupt, or an exception.
8714 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008715 if (!aborting())
8716 {
8717 switch (error)
8718 {
8719 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008720 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008721 break;
8722 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008723 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008724 break;
8725 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008726 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008727 name);
8728 break;
8729 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008730 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008731 name);
8732 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008733 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008734 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008735 name);
8736 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008737 }
8738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 if (fname != name && fname != fname_buf)
8741 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008742 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743
8744 return ret;
8745}
8746
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008747/*
8748 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008749 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008750 */
8751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008752emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008753{
8754 char_u *p;
8755
8756 if (*name == K_SPECIAL)
8757 p = concat_str((char_u *)"<SNR>", name + 3);
8758 else
8759 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008760 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008761 if (p != name)
8762 vim_free(p);
8763}
8764
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008765/*
8766 * Return TRUE for a non-zero Number and a non-empty String.
8767 */
8768 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008769non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008770{
8771 return ((argvars[0].v_type == VAR_NUMBER
8772 && argvars[0].vval.v_number != 0)
8773 || (argvars[0].v_type == VAR_STRING
8774 && argvars[0].vval.v_string != NULL
8775 && *argvars[0].vval.v_string != NUL));
8776}
8777
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778/*********************************************
8779 * Implementation of the built-in functions
8780 */
8781
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008782#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008783static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008784
8785/*
8786 * Get the float value of "argvars[0]" into "f".
8787 * Returns FAIL when the argument is not a Number or Float.
8788 */
8789 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008790get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008791{
8792 if (argvars[0].v_type == VAR_FLOAT)
8793 {
8794 *f = argvars[0].vval.v_float;
8795 return OK;
8796 }
8797 if (argvars[0].v_type == VAR_NUMBER)
8798 {
8799 *f = (float_T)argvars[0].vval.v_number;
8800 return OK;
8801 }
8802 EMSG(_("E808: Number or Float required"));
8803 return FAIL;
8804}
8805
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008806/*
8807 * "abs(expr)" function
8808 */
8809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008810f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008811{
8812 if (argvars[0].v_type == VAR_FLOAT)
8813 {
8814 rettv->v_type = VAR_FLOAT;
8815 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8816 }
8817 else
8818 {
8819 varnumber_T n;
8820 int error = FALSE;
8821
8822 n = get_tv_number_chk(&argvars[0], &error);
8823 if (error)
8824 rettv->vval.v_number = -1;
8825 else if (n > 0)
8826 rettv->vval.v_number = n;
8827 else
8828 rettv->vval.v_number = -n;
8829 }
8830}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008831
8832/*
8833 * "acos()" function
8834 */
8835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008836f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008837{
8838 float_T f;
8839
8840 rettv->v_type = VAR_FLOAT;
8841 if (get_float_arg(argvars, &f) == OK)
8842 rettv->vval.v_float = acos(f);
8843 else
8844 rettv->vval.v_float = 0.0;
8845}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008846#endif
8847
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008849 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 */
8851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008852f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853{
Bram Moolenaar33570922005-01-25 22:26:29 +00008854 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008856 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008857 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008859 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008860 && !tv_check_lock(l->lv_lock,
8861 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008862 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008863 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008864 }
8865 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008866 EMSG(_(e_listreq));
8867}
8868
8869/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008870 * "alloc_fail(id, countdown, repeat)" function
8871 */
8872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008873f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008874{
8875 if (argvars[0].v_type != VAR_NUMBER
8876 || argvars[0].vval.v_number <= 0
8877 || argvars[1].v_type != VAR_NUMBER
8878 || argvars[1].vval.v_number < 0
8879 || argvars[2].v_type != VAR_NUMBER)
8880 EMSG(_(e_invarg));
8881 else
8882 {
8883 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008884 if (alloc_fail_id >= aid_last)
8885 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008886 alloc_fail_countdown = argvars[1].vval.v_number;
8887 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008888 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008889 }
8890}
8891
8892/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008893 * "and(expr, expr)" function
8894 */
8895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008896f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008897{
8898 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8899 & get_tv_number_chk(&argvars[1], NULL);
8900}
8901
8902/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008903 * "append(lnum, string/list)" function
8904 */
8905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008906f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008907{
8908 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008909 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008910 list_T *l = NULL;
8911 listitem_T *li = NULL;
8912 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008913 long added = 0;
8914
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008915 /* When coming here from Insert mode, sync undo, so that this can be
8916 * undone separately from what was previously inserted. */
8917 if (u_sync_once == 2)
8918 {
8919 u_sync_once = 1; /* notify that u_sync() was called */
8920 u_sync(TRUE);
8921 }
8922
Bram Moolenaar0d660222005-01-07 21:51:51 +00008923 lnum = get_tv_lnum(argvars);
8924 if (lnum >= 0
8925 && lnum <= curbuf->b_ml.ml_line_count
8926 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008927 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008928 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008929 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008930 l = argvars[1].vval.v_list;
8931 if (l == NULL)
8932 return;
8933 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008934 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008935 for (;;)
8936 {
8937 if (l == NULL)
8938 tv = &argvars[1]; /* append a string */
8939 else if (li == NULL)
8940 break; /* end of list */
8941 else
8942 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008943 line = get_tv_string_chk(tv);
8944 if (line == NULL) /* type error */
8945 {
8946 rettv->vval.v_number = 1; /* Failed */
8947 break;
8948 }
8949 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008950 ++added;
8951 if (l == NULL)
8952 break;
8953 li = li->li_next;
8954 }
8955
8956 appended_lines_mark(lnum, added);
8957 if (curwin->w_cursor.lnum > lnum)
8958 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008960 else
8961 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962}
8963
8964/*
8965 * "argc()" function
8966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008968f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008970 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971}
8972
8973/*
8974 * "argidx()" function
8975 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008977f_argidx(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 = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980}
8981
8982/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008983 * "arglistid()" function
8984 */
8985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008986f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008987{
8988 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008989
8990 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01008991 wp = find_tabwin(&argvars[0], &argvars[1]);
8992 if (wp != NULL)
8993 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008994}
8995
8996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 * "argv(nr)" function
8998 */
8999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009000f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001{
9002 int idx;
9003
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009004 if (argvars[0].v_type != VAR_UNKNOWN)
9005 {
9006 idx = get_tv_number_chk(&argvars[0], NULL);
9007 if (idx >= 0 && idx < ARGCOUNT)
9008 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9009 else
9010 rettv->vval.v_string = NULL;
9011 rettv->v_type = VAR_STRING;
9012 }
9013 else if (rettv_list_alloc(rettv) == OK)
9014 for (idx = 0; idx < ARGCOUNT; ++idx)
9015 list_append_string(rettv->vval.v_list,
9016 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017}
9018
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009019static void prepare_assert_error(garray_T*gap);
9020static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9021static void assert_error(garray_T *gap);
9022static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009023
9024/*
9025 * Prepare "gap" for an assert error and add the sourcing position.
9026 */
9027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009028prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009029{
9030 char buf[NUMBUFLEN];
9031
9032 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009033 if (sourcing_name != NULL)
9034 {
9035 ga_concat(gap, sourcing_name);
9036 if (sourcing_lnum > 0)
9037 ga_concat(gap, (char_u *)" ");
9038 }
9039 if (sourcing_lnum > 0)
9040 {
9041 sprintf(buf, "line %ld", (long)sourcing_lnum);
9042 ga_concat(gap, (char_u *)buf);
9043 }
9044 if (sourcing_name != NULL || sourcing_lnum > 0)
9045 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009046}
9047
9048/*
9049 * Fill "gap" with information about an assert error.
9050 */
9051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009052fill_assert_error(
9053 garray_T *gap,
9054 typval_T *opt_msg_tv,
9055 char_u *exp_str,
9056 typval_T *exp_tv,
9057 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009058{
9059 char_u numbuf[NUMBUFLEN];
9060 char_u *tofree;
9061
9062 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9063 {
9064 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9065 vim_free(tofree);
9066 }
9067 else
9068 {
9069 ga_concat(gap, (char_u *)"Expected ");
9070 if (exp_str == NULL)
9071 {
9072 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9073 vim_free(tofree);
9074 }
9075 else
9076 ga_concat(gap, exp_str);
9077 ga_concat(gap, (char_u *)" but got ");
9078 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9079 vim_free(tofree);
9080 }
9081}
Bram Moolenaar43345542015-11-29 17:35:35 +01009082
9083/*
9084 * Add an assert error to v:errors.
9085 */
9086 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009087assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009088{
9089 struct vimvar *vp = &vimvars[VV_ERRORS];
9090
9091 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9092 /* Make sure v:errors is a list. */
9093 set_vim_var_list(VV_ERRORS, list_alloc());
9094 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9095}
9096
Bram Moolenaar43345542015-11-29 17:35:35 +01009097/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009098 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009099 */
9100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009101f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009102{
9103 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009104
9105 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9106 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009107 prepare_assert_error(&ga);
9108 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9109 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009110 ga_clear(&ga);
9111 }
9112}
9113
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009114/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009115 * "assert_exception(string[, msg])" function
9116 */
9117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009118f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009119{
9120 garray_T ga;
9121 char *error;
9122
9123 error = (char *)get_tv_string_chk(&argvars[0]);
9124 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9125 {
9126 prepare_assert_error(&ga);
9127 ga_concat(&ga, (char_u *)"v:exception is not set");
9128 assert_error(&ga);
9129 ga_clear(&ga);
9130 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009131 else if (error != NULL
9132 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009133 {
9134 prepare_assert_error(&ga);
9135 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9136 &vimvars[VV_EXCEPTION].vv_tv);
9137 assert_error(&ga);
9138 ga_clear(&ga);
9139 }
9140}
9141
9142/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009143 * "assert_fails(cmd [, error])" function
9144 */
9145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009146f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009147{
9148 char_u *cmd = get_tv_string_chk(&argvars[0]);
9149 garray_T ga;
9150
9151 called_emsg = FALSE;
9152 suppress_errthrow = TRUE;
9153 emsg_silent = TRUE;
9154 do_cmdline_cmd(cmd);
9155 if (!called_emsg)
9156 {
9157 prepare_assert_error(&ga);
9158 ga_concat(&ga, (char_u *)"command did not fail: ");
9159 ga_concat(&ga, cmd);
9160 assert_error(&ga);
9161 ga_clear(&ga);
9162 }
9163 else if (argvars[1].v_type != VAR_UNKNOWN)
9164 {
9165 char_u buf[NUMBUFLEN];
9166 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9167
9168 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9169 {
9170 prepare_assert_error(&ga);
9171 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9172 &vimvars[VV_ERRMSG].vv_tv);
9173 assert_error(&ga);
9174 ga_clear(&ga);
9175 }
9176 }
9177
9178 called_emsg = FALSE;
9179 suppress_errthrow = FALSE;
9180 emsg_silent = FALSE;
9181 emsg_on_display = FALSE;
9182 set_vim_var_string(VV_ERRMSG, NULL, 0);
9183}
9184
9185/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009186 * Common for assert_true() and assert_false().
9187 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009189assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009190{
9191 int error = FALSE;
9192 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009193
9194 if (argvars[0].v_type != VAR_NUMBER
9195 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9196 || error)
9197 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009198 prepare_assert_error(&ga);
9199 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009200 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009201 NULL, &argvars[0]);
9202 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009203 ga_clear(&ga);
9204 }
9205}
9206
9207/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009208 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009209 */
9210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009211f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009212{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009213 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009214}
9215
9216/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009217 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009218 */
9219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009220f_assert_true(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, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009223}
9224
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009225#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009226/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009227 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009228 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009230f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009231{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009232 float_T f;
9233
9234 rettv->v_type = VAR_FLOAT;
9235 if (get_float_arg(argvars, &f) == OK)
9236 rettv->vval.v_float = asin(f);
9237 else
9238 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009239}
9240
9241/*
9242 * "atan()" function
9243 */
9244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009245f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009246{
9247 float_T f;
9248
9249 rettv->v_type = VAR_FLOAT;
9250 if (get_float_arg(argvars, &f) == OK)
9251 rettv->vval.v_float = atan(f);
9252 else
9253 rettv->vval.v_float = 0.0;
9254}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009255
9256/*
9257 * "atan2()" function
9258 */
9259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009260f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009261{
9262 float_T fx, fy;
9263
9264 rettv->v_type = VAR_FLOAT;
9265 if (get_float_arg(argvars, &fx) == OK
9266 && get_float_arg(&argvars[1], &fy) == OK)
9267 rettv->vval.v_float = atan2(fx, fy);
9268 else
9269 rettv->vval.v_float = 0.0;
9270}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009271#endif
9272
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273/*
9274 * "browse(save, title, initdir, default)" function
9275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009277f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278{
9279#ifdef FEAT_BROWSE
9280 int save;
9281 char_u *title;
9282 char_u *initdir;
9283 char_u *defname;
9284 char_u buf[NUMBUFLEN];
9285 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009286 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009288 save = get_tv_number_chk(&argvars[0], &error);
9289 title = get_tv_string_chk(&argvars[1]);
9290 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9291 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009293 if (error || title == NULL || initdir == NULL || defname == NULL)
9294 rettv->vval.v_string = NULL;
9295 else
9296 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009297 do_browse(save ? BROWSE_SAVE : 0,
9298 title, defname, NULL, initdir, NULL, curbuf);
9299#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009300 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009301#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009302 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009303}
9304
9305/*
9306 * "browsedir(title, initdir)" function
9307 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009309f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009310{
9311#ifdef FEAT_BROWSE
9312 char_u *title;
9313 char_u *initdir;
9314 char_u buf[NUMBUFLEN];
9315
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009316 title = get_tv_string_chk(&argvars[0]);
9317 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009318
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009319 if (title == NULL || initdir == NULL)
9320 rettv->vval.v_string = NULL;
9321 else
9322 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009323 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009325 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009327 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328}
9329
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009330static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009331
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332/*
9333 * Find a buffer by number or exact name.
9334 */
9335 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009336find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337{
9338 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009340 if (avar->v_type == VAR_NUMBER)
9341 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009342 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009344 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009345 if (buf == NULL)
9346 {
9347 /* No full path name match, try a match with a URL or a "nofile"
9348 * buffer, these don't use the full path. */
9349 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9350 if (buf->b_fname != NULL
9351 && (path_with_url(buf->b_fname)
9352#ifdef FEAT_QUICKFIX
9353 || bt_nofile(buf)
9354#endif
9355 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009356 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009357 break;
9358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 }
9360 return buf;
9361}
9362
9363/*
9364 * "bufexists(expr)" function
9365 */
9366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009367f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009369 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370}
9371
9372/*
9373 * "buflisted(expr)" function
9374 */
9375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009376f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377{
9378 buf_T *buf;
9379
9380 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009381 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382}
9383
9384/*
9385 * "bufloaded(expr)" function
9386 */
9387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009388f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389{
9390 buf_T *buf;
9391
9392 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009393 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394}
9395
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009396static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009397
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398/*
9399 * Get buffer by number or pattern.
9400 */
9401 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009402get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009404 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 int save_magic;
9406 char_u *save_cpo;
9407 buf_T *buf;
9408
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009409 if (tv->v_type == VAR_NUMBER)
9410 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009411 if (tv->v_type != VAR_STRING)
9412 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 if (name == NULL || *name == NUL)
9414 return curbuf;
9415 if (name[0] == '$' && name[1] == NUL)
9416 return lastbuf;
9417
9418 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9419 save_magic = p_magic;
9420 p_magic = TRUE;
9421 save_cpo = p_cpo;
9422 p_cpo = (char_u *)"";
9423
9424 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009425 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426
9427 p_magic = save_magic;
9428 p_cpo = save_cpo;
9429
9430 /* If not found, try expanding the name, like done for bufexists(). */
9431 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009432 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433
9434 return buf;
9435}
9436
9437/*
9438 * "bufname(expr)" function
9439 */
9440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009441f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442{
9443 buf_T *buf;
9444
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009445 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009447 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009448 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009450 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009452 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 --emsg_off;
9454}
9455
9456/*
9457 * "bufnr(expr)" function
9458 */
9459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009460f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461{
9462 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009463 int error = FALSE;
9464 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009466 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009468 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009469 --emsg_off;
9470
9471 /* If the buffer isn't found and the second argument is not zero create a
9472 * new buffer. */
9473 if (buf == NULL
9474 && argvars[1].v_type != VAR_UNKNOWN
9475 && get_tv_number_chk(&argvars[1], &error) != 0
9476 && !error
9477 && (name = get_tv_string_chk(&argvars[0])) != NULL
9478 && !error)
9479 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9480
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009482 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009484 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485}
9486
9487/*
9488 * "bufwinnr(nr)" function
9489 */
9490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009491f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492{
9493#ifdef FEAT_WINDOWS
9494 win_T *wp;
9495 int winnr = 0;
9496#endif
9497 buf_T *buf;
9498
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009499 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009501 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502#ifdef FEAT_WINDOWS
9503 for (wp = firstwin; wp; wp = wp->w_next)
9504 {
9505 ++winnr;
9506 if (wp->w_buffer == buf)
9507 break;
9508 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009509 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009511 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512#endif
9513 --emsg_off;
9514}
9515
9516/*
9517 * "byte2line(byte)" function
9518 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009520f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521{
9522#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009523 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524#else
9525 long boff = 0;
9526
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009527 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009529 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009531 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 (linenr_T)0, &boff);
9533#endif
9534}
9535
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009537byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009538{
9539#ifdef FEAT_MBYTE
9540 char_u *t;
9541#endif
9542 char_u *str;
9543 long idx;
9544
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009545 str = get_tv_string_chk(&argvars[0]);
9546 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009547 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009548 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009549 return;
9550
9551#ifdef FEAT_MBYTE
9552 t = str;
9553 for ( ; idx > 0; idx--)
9554 {
9555 if (*t == NUL) /* EOL reached */
9556 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009557 if (enc_utf8 && comp)
9558 t += utf_ptr2len(t);
9559 else
9560 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009561 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009562 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009563#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009564 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009565 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009566#endif
9567}
9568
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009569/*
9570 * "byteidx()" function
9571 */
9572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009573f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009574{
9575 byteidx(argvars, rettv, FALSE);
9576}
9577
9578/*
9579 * "byteidxcomp()" function
9580 */
9581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009582f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009583{
9584 byteidx(argvars, rettv, TRUE);
9585}
9586
Bram Moolenaardb913952012-06-29 12:54:53 +02009587 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009588func_call(
9589 char_u *name,
9590 typval_T *args,
9591 dict_T *selfdict,
9592 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009593{
9594 listitem_T *item;
9595 typval_T argv[MAX_FUNC_ARGS + 1];
9596 int argc = 0;
9597 int dummy;
9598 int r = 0;
9599
9600 for (item = args->vval.v_list->lv_first; item != NULL;
9601 item = item->li_next)
9602 {
9603 if (argc == MAX_FUNC_ARGS)
9604 {
9605 EMSG(_("E699: Too many arguments"));
9606 break;
9607 }
9608 /* Make a copy of each argument. This is needed to be able to set
9609 * v_lock to VAR_FIXED in the copy without changing the original list.
9610 */
9611 copy_tv(&item->li_tv, &argv[argc++]);
9612 }
9613
9614 if (item == NULL)
9615 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9616 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9617 &dummy, TRUE, selfdict);
9618
9619 /* Free the arguments. */
9620 while (argc > 0)
9621 clear_tv(&argv[--argc]);
9622
9623 return r;
9624}
9625
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009626/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009627 * "call(func, arglist)" function
9628 */
9629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009630f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009631{
9632 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009633 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009634
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009635 if (argvars[1].v_type != VAR_LIST)
9636 {
9637 EMSG(_(e_listreq));
9638 return;
9639 }
9640 if (argvars[1].vval.v_list == NULL)
9641 return;
9642
9643 if (argvars[0].v_type == VAR_FUNC)
9644 func = argvars[0].vval.v_string;
9645 else
9646 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009647 if (*func == NUL)
9648 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009649
Bram Moolenaare9a41262005-01-15 22:18:47 +00009650 if (argvars[2].v_type != VAR_UNKNOWN)
9651 {
9652 if (argvars[2].v_type != VAR_DICT)
9653 {
9654 EMSG(_(e_dictreq));
9655 return;
9656 }
9657 selfdict = argvars[2].vval.v_dict;
9658 }
9659
Bram Moolenaardb913952012-06-29 12:54:53 +02009660 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009661}
9662
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009663#ifdef FEAT_FLOAT
9664/*
9665 * "ceil({float})" function
9666 */
9667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009668f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009669{
9670 float_T f;
9671
9672 rettv->v_type = VAR_FLOAT;
9673 if (get_float_arg(argvars, &f) == OK)
9674 rettv->vval.v_float = ceil(f);
9675 else
9676 rettv->vval.v_float = 0.0;
9677}
9678#endif
9679
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009680#ifdef FEAT_CHANNEL
9681/*
9682 * Get the channel index from the handle argument.
9683 * Returns -1 if the handle is invalid or the channel is closed.
9684 */
9685 static int
9686get_channel_arg(typval_T *tv)
9687{
9688 int ch_idx;
9689
9690 if (tv->v_type != VAR_NUMBER)
9691 {
9692 EMSG2(_(e_invarg2), get_tv_string(tv));
9693 return -1;
9694 }
9695 ch_idx = tv->vval.v_number;
9696
9697 if (!channel_is_open(ch_idx))
9698 {
9699 EMSGN(_("E906: not an open channel"), ch_idx);
9700 return -1;
9701 }
9702 return ch_idx;
9703}
9704
9705/*
9706 * "ch_close()" function
9707 */
9708 static void
9709f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9710{
9711 int ch_idx = get_channel_arg(&argvars[0]);
9712
9713 if (ch_idx >= 0)
9714 channel_close(ch_idx);
9715}
9716
9717/*
9718 * Get a callback from "arg". It can be a Funcref or a function name.
9719 * When "arg" is zero return an empty string.
9720 * Return NULL for an invalid argument.
9721 */
9722 static char_u *
9723get_callback(typval_T *arg)
9724{
9725 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9726 return arg->vval.v_string;
9727 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9728 return (char_u *)"";
9729 EMSG(_("E999: Invalid callback argument"));
9730 return NULL;
9731}
9732
9733/*
9734 * "ch_open()" function
9735 */
9736 static void
9737f_ch_open(typval_T *argvars, typval_T *rettv)
9738{
9739 char_u *address;
9740 char_u *mode;
9741 char_u *callback = NULL;
9742 char_u buf1[NUMBUFLEN];
9743 char_u *p;
9744 int port;
9745 int json_mode = FALSE;
9746
9747 /* default: fail */
9748 rettv->vval.v_number = -1;
9749
9750 address = get_tv_string(&argvars[0]);
9751 mode = get_tv_string_buf(&argvars[1], buf1);
9752 if (argvars[2].v_type != VAR_UNKNOWN)
9753 {
9754 callback = get_callback(&argvars[2]);
9755 if (callback == NULL)
9756 return;
9757 }
9758
9759 /* parse address */
9760 p = vim_strchr(address, ':');
9761 if (p == NULL)
9762 {
9763 EMSG2(_(e_invarg2), address);
9764 return;
9765 }
9766 *p++ = NUL;
9767 port = atoi((char *)p);
9768 if (*address == NUL || port <= 0)
9769 {
9770 p[-1] = ':';
9771 EMSG2(_(e_invarg2), address);
9772 return;
9773 }
9774
9775 /* parse mode */
9776 if (STRCMP(mode, "json") == 0)
9777 json_mode = TRUE;
9778 else if (STRCMP(mode, "raw") != 0)
9779 {
9780 EMSG2(_(e_invarg2), mode);
9781 return;
9782 }
9783
9784 rettv->vval.v_number = channel_open((char *)address, port, NULL);
9785 if (rettv->vval.v_number >= 0)
9786 {
9787 channel_set_json_mode(rettv->vval.v_number, json_mode);
9788 if (callback != NULL && *callback != NUL)
9789 channel_set_callback(rettv->vval.v_number, callback);
9790 }
9791}
9792
9793/*
9794 * common for "sendexpr()" and "sendraw()"
9795 * Returns the channel index if the caller should read the response.
9796 * Otherwise returns -1.
9797 */
9798 static int
9799send_common(typval_T *argvars, char_u *text, char *fun)
9800{
9801 int ch_idx;
9802 char_u *callback = NULL;
9803
9804 ch_idx = get_channel_arg(&argvars[0]);
9805 if (ch_idx < 0)
9806 return -1;
9807
9808 if (argvars[2].v_type != VAR_UNKNOWN)
9809 {
9810 callback = get_callback(&argvars[2]);
9811 if (callback == NULL)
9812 return -1;
9813 }
9814 /* Set the callback or clear it. An empty callback means no callback and
9815 * not reading the response. */
9816 channel_set_req_callback(ch_idx,
9817 callback != NULL && *callback == NUL ? NULL : callback);
9818
9819 if (channel_send(ch_idx, text, fun) == OK && callback == NULL)
9820 return ch_idx;
9821 return -1;
9822}
9823
9824/*
9825 * "ch_sendexpr()" function
9826 */
9827 static void
9828f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
9829{
9830 char_u *text;
9831 typval_T *listtv;
9832 int ch_idx;
9833 int id;
9834
9835 /* return an empty string by default */
9836 rettv->v_type = VAR_STRING;
9837 rettv->vval.v_string = NULL;
9838
9839 id = channel_get_id();
9840 text = json_encode_nr_expr(id, &argvars[1]);
9841 if (text == NULL)
9842 return;
9843
9844 ch_idx = send_common(argvars, text, "sendexpr");
9845 if (ch_idx >= 0)
9846 {
9847 if (channel_read_json_block(ch_idx, id, &listtv) == OK)
9848 {
9849 if (listtv->v_type == VAR_LIST)
9850 {
9851 list_T *list = listtv->vval.v_list;
9852
9853 if (list->lv_len == 2)
9854 {
9855 /* Move the item from the list and then change the type to
9856 * avoid the value being freed. */
9857 *rettv = list->lv_last->li_tv;
9858 list->lv_last->li_tv.v_type = VAR_NUMBER;
9859 }
9860 }
9861 clear_tv(listtv);
9862 }
9863 }
9864}
9865
9866/*
9867 * "ch_sendraw()" function
9868 */
9869 static void
9870f_ch_sendraw(typval_T *argvars, typval_T *rettv)
9871{
9872 char_u buf[NUMBUFLEN];
9873 char_u *text;
9874 int ch_idx;
9875
9876 /* return an empty string by default */
9877 rettv->v_type = VAR_STRING;
9878 rettv->vval.v_string = NULL;
9879
9880 text = get_tv_string_buf(&argvars[1], buf);
9881 ch_idx = send_common(argvars, text, "sendraw");
9882 if (ch_idx >= 0)
9883 rettv->vval.v_string = channel_read_block(ch_idx);
9884}
9885#endif
9886
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009887/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009888 * "changenr()" function
9889 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009891f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009892{
9893 rettv->vval.v_number = curbuf->b_u_seq_cur;
9894}
9895
9896/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 * "char2nr(string)" function
9898 */
9899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009900f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901{
9902#ifdef FEAT_MBYTE
9903 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009904 {
9905 int utf8 = 0;
9906
9907 if (argvars[1].v_type != VAR_UNKNOWN)
9908 utf8 = get_tv_number_chk(&argvars[1], NULL);
9909
9910 if (utf8)
9911 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9912 else
9913 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 else
9916#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009917 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918}
9919
9920/*
9921 * "cindent(lnum)" function
9922 */
9923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009924f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925{
9926#ifdef FEAT_CINDENT
9927 pos_T pos;
9928 linenr_T lnum;
9929
9930 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009931 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9933 {
9934 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009935 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 curwin->w_cursor = pos;
9937 }
9938 else
9939#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009940 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941}
9942
9943/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009944 * "clearmatches()" function
9945 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009947f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009948{
9949#ifdef FEAT_SEARCH_EXTRA
9950 clear_matches(curwin);
9951#endif
9952}
9953
9954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 * "col(string)" function
9956 */
9957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009958f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959{
9960 colnr_T col = 0;
9961 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009962 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009964 fp = var2fpos(&argvars[0], FALSE, &fnum);
9965 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 {
9967 if (fp->col == MAXCOL)
9968 {
9969 /* '> can be MAXCOL, get the length of the line then */
9970 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009971 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 else
9973 col = MAXCOL;
9974 }
9975 else
9976 {
9977 col = fp->col + 1;
9978#ifdef FEAT_VIRTUALEDIT
9979 /* col(".") when the cursor is on the NUL at the end of the line
9980 * because of "coladd" can be seen as an extra column. */
9981 if (virtual_active() && fp == &curwin->w_cursor)
9982 {
9983 char_u *p = ml_get_cursor();
9984
9985 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9986 curwin->w_virtcol - curwin->w_cursor.coladd))
9987 {
9988# ifdef FEAT_MBYTE
9989 int l;
9990
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009991 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 col += l;
9993# else
9994 if (*p != NUL && p[1] == NUL)
9995 ++col;
9996# endif
9997 }
9998 }
9999#endif
10000 }
10001 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010002 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003}
10004
Bram Moolenaar572cb562005-08-05 21:35:02 +000010005#if defined(FEAT_INS_EXPAND)
10006/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010007 * "complete()" function
10008 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010010f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010011{
10012 int startcol;
10013
10014 if ((State & INSERT) == 0)
10015 {
10016 EMSG(_("E785: complete() can only be used in Insert mode"));
10017 return;
10018 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010019
10020 /* Check for undo allowed here, because if something was already inserted
10021 * the line was already saved for undo and this check isn't done. */
10022 if (!undo_allowed())
10023 return;
10024
Bram Moolenaarade00832006-03-10 21:46:58 +000010025 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10026 {
10027 EMSG(_(e_invarg));
10028 return;
10029 }
10030
10031 startcol = get_tv_number_chk(&argvars[0], NULL);
10032 if (startcol <= 0)
10033 return;
10034
10035 set_completion(startcol - 1, argvars[1].vval.v_list);
10036}
10037
10038/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010039 * "complete_add()" function
10040 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010041 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010042f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010043{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010044 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010045}
10046
10047/*
10048 * "complete_check()" function
10049 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010050 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010051f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010052{
10053 int saved = RedrawingDisabled;
10054
10055 RedrawingDisabled = 0;
10056 ins_compl_check_keys(0);
10057 rettv->vval.v_number = compl_interrupted;
10058 RedrawingDisabled = saved;
10059}
10060#endif
10061
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062/*
10063 * "confirm(message, buttons[, default [, type]])" function
10064 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010066f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067{
10068#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10069 char_u *message;
10070 char_u *buttons = NULL;
10071 char_u buf[NUMBUFLEN];
10072 char_u buf2[NUMBUFLEN];
10073 int def = 1;
10074 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010075 char_u *typestr;
10076 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010078 message = get_tv_string_chk(&argvars[0]);
10079 if (message == NULL)
10080 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010081 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010083 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10084 if (buttons == NULL)
10085 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010086 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010088 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010089 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010091 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10092 if (typestr == NULL)
10093 error = TRUE;
10094 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010096 switch (TOUPPER_ASC(*typestr))
10097 {
10098 case 'E': type = VIM_ERROR; break;
10099 case 'Q': type = VIM_QUESTION; break;
10100 case 'I': type = VIM_INFO; break;
10101 case 'W': type = VIM_WARNING; break;
10102 case 'G': type = VIM_GENERIC; break;
10103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 }
10105 }
10106 }
10107 }
10108
10109 if (buttons == NULL || *buttons == NUL)
10110 buttons = (char_u *)_("&Ok");
10111
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010112 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010113 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010114 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115#endif
10116}
10117
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010118/*
10119 * "copy()" function
10120 */
10121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010122f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010123{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010124 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010125}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010127#ifdef FEAT_FLOAT
10128/*
10129 * "cos()" function
10130 */
10131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010132f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010133{
10134 float_T f;
10135
10136 rettv->v_type = VAR_FLOAT;
10137 if (get_float_arg(argvars, &f) == OK)
10138 rettv->vval.v_float = cos(f);
10139 else
10140 rettv->vval.v_float = 0.0;
10141}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010142
10143/*
10144 * "cosh()" function
10145 */
10146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010147f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010148{
10149 float_T f;
10150
10151 rettv->v_type = VAR_FLOAT;
10152 if (get_float_arg(argvars, &f) == OK)
10153 rettv->vval.v_float = cosh(f);
10154 else
10155 rettv->vval.v_float = 0.0;
10156}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010157#endif
10158
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010160 * "count()" function
10161 */
10162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010163f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010164{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010165 long n = 0;
10166 int ic = FALSE;
10167
Bram Moolenaare9a41262005-01-15 22:18:47 +000010168 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010169 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010170 listitem_T *li;
10171 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010172 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010173
Bram Moolenaare9a41262005-01-15 22:18:47 +000010174 if ((l = argvars[0].vval.v_list) != NULL)
10175 {
10176 li = l->lv_first;
10177 if (argvars[2].v_type != VAR_UNKNOWN)
10178 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010179 int error = FALSE;
10180
10181 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010182 if (argvars[3].v_type != VAR_UNKNOWN)
10183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010184 idx = get_tv_number_chk(&argvars[3], &error);
10185 if (!error)
10186 {
10187 li = list_find(l, idx);
10188 if (li == NULL)
10189 EMSGN(_(e_listidx), idx);
10190 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010191 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010192 if (error)
10193 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010194 }
10195
10196 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010197 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010198 ++n;
10199 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010200 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010201 else if (argvars[0].v_type == VAR_DICT)
10202 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010203 int todo;
10204 dict_T *d;
10205 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010206
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010207 if ((d = argvars[0].vval.v_dict) != NULL)
10208 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010209 int error = FALSE;
10210
Bram Moolenaare9a41262005-01-15 22:18:47 +000010211 if (argvars[2].v_type != VAR_UNKNOWN)
10212 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010213 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010214 if (argvars[3].v_type != VAR_UNKNOWN)
10215 EMSG(_(e_invarg));
10216 }
10217
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010218 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010219 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010220 {
10221 if (!HASHITEM_EMPTY(hi))
10222 {
10223 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010224 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010225 ++n;
10226 }
10227 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010228 }
10229 }
10230 else
10231 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010232 rettv->vval.v_number = n;
10233}
10234
10235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10237 *
10238 * Checks the existence of a cscope connection.
10239 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010241f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242{
10243#ifdef FEAT_CSCOPE
10244 int num = 0;
10245 char_u *dbpath = NULL;
10246 char_u *prepend = NULL;
10247 char_u buf[NUMBUFLEN];
10248
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010249 if (argvars[0].v_type != VAR_UNKNOWN
10250 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010252 num = (int)get_tv_number(&argvars[0]);
10253 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010254 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010255 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 }
10257
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010258 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259#endif
10260}
10261
10262/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010263 * "cursor(lnum, col)" function, or
10264 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010266 * Moves the cursor to the specified line and column.
10267 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010270f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271{
10272 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010273#ifdef FEAT_VIRTUALEDIT
10274 long coladd = 0;
10275#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010276 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010278 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010279 if (argvars[1].v_type == VAR_UNKNOWN)
10280 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010281 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010282 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010283
Bram Moolenaar493c1782014-05-28 14:34:46 +020010284 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010285 {
10286 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010287 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010288 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010289 line = pos.lnum;
10290 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010291#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010292 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010293#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010294 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010295 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010296 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010297 set_curswant = FALSE;
10298 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010299 }
10300 else
10301 {
10302 line = get_tv_lnum(argvars);
10303 col = get_tv_number_chk(&argvars[1], NULL);
10304#ifdef FEAT_VIRTUALEDIT
10305 if (argvars[2].v_type != VAR_UNKNOWN)
10306 coladd = get_tv_number_chk(&argvars[2], NULL);
10307#endif
10308 }
10309 if (line < 0 || col < 0
10310#ifdef FEAT_VIRTUALEDIT
10311 || coladd < 0
10312#endif
10313 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010314 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 if (line > 0)
10316 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 if (col > 0)
10318 curwin->w_cursor.col = col - 1;
10319#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010320 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321#endif
10322
10323 /* Make sure the cursor is in a valid position. */
10324 check_cursor();
10325#ifdef FEAT_MBYTE
10326 /* Correct cursor for multi-byte character. */
10327 if (has_mbyte)
10328 mb_adjust_cursor();
10329#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010330
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010331 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010332 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333}
10334
10335/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010336 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 */
10338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010339f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010341 int noref = 0;
10342
10343 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010344 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010345 if (noref < 0 || noref > 1)
10346 EMSG(_(e_invarg));
10347 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010348 {
10349 current_copyID += COPYID_INC;
10350 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352}
10353
10354/*
10355 * "delete()" function
10356 */
10357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010358f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010360 char_u nbuf[NUMBUFLEN];
10361 char_u *name;
10362 char_u *flags;
10363
10364 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010366 return;
10367
10368 name = get_tv_string(&argvars[0]);
10369 if (name == NULL || *name == NUL)
10370 {
10371 EMSG(_(e_invarg));
10372 return;
10373 }
10374
10375 if (argvars[1].v_type != VAR_UNKNOWN)
10376 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010378 flags = (char_u *)"";
10379
10380 if (*flags == NUL)
10381 /* delete a file */
10382 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10383 else if (STRCMP(flags, "d") == 0)
10384 /* delete an empty directory */
10385 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10386 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010387 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010388 rettv->vval.v_number = delete_recursive(name);
10389 else
10390 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391}
10392
10393/*
10394 * "did_filetype()" function
10395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010397f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398{
10399#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010400 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401#endif
10402}
10403
10404/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010405 * "diff_filler()" function
10406 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010408f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010409{
10410#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010411 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010412#endif
10413}
10414
10415/*
10416 * "diff_hlID()" function
10417 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010419f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010420{
10421#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010422 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010423 static linenr_T prev_lnum = 0;
10424 static int changedtick = 0;
10425 static int fnum = 0;
10426 static int change_start = 0;
10427 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010428 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010429 int filler_lines;
10430 int col;
10431
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010432 if (lnum < 0) /* ignore type error in {lnum} arg */
10433 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010434 if (lnum != prev_lnum
10435 || changedtick != curbuf->b_changedtick
10436 || fnum != curbuf->b_fnum)
10437 {
10438 /* New line, buffer, change: need to get the values. */
10439 filler_lines = diff_check(curwin, lnum);
10440 if (filler_lines < 0)
10441 {
10442 if (filler_lines == -1)
10443 {
10444 change_start = MAXCOL;
10445 change_end = -1;
10446 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10447 hlID = HLF_ADD; /* added line */
10448 else
10449 hlID = HLF_CHD; /* changed line */
10450 }
10451 else
10452 hlID = HLF_ADD; /* added line */
10453 }
10454 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010455 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010456 prev_lnum = lnum;
10457 changedtick = curbuf->b_changedtick;
10458 fnum = curbuf->b_fnum;
10459 }
10460
10461 if (hlID == HLF_CHD || hlID == HLF_TXD)
10462 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010463 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010464 if (col >= change_start && col <= change_end)
10465 hlID = HLF_TXD; /* changed text */
10466 else
10467 hlID = HLF_CHD; /* changed line */
10468 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010469 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010470#endif
10471}
10472
10473/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010474 * "empty({expr})" function
10475 */
10476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010477f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010478{
10479 int n;
10480
10481 switch (argvars[0].v_type)
10482 {
10483 case VAR_STRING:
10484 case VAR_FUNC:
10485 n = argvars[0].vval.v_string == NULL
10486 || *argvars[0].vval.v_string == NUL;
10487 break;
10488 case VAR_NUMBER:
10489 n = argvars[0].vval.v_number == 0;
10490 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010491#ifdef FEAT_FLOAT
10492 case VAR_FLOAT:
10493 n = argvars[0].vval.v_float == 0.0;
10494 break;
10495#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010496 case VAR_LIST:
10497 n = argvars[0].vval.v_list == NULL
10498 || argvars[0].vval.v_list->lv_first == NULL;
10499 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010500 case VAR_DICT:
10501 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010502 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010503 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010504 case VAR_SPECIAL:
10505 n = argvars[0].vval.v_number != VVAL_TRUE;
10506 break;
10507
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010508 default:
10509 EMSG2(_(e_intern2), "f_empty()");
10510 n = 0;
10511 }
10512
10513 rettv->vval.v_number = n;
10514}
10515
10516/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517 * "escape({string}, {chars})" function
10518 */
10519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010520f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521{
10522 char_u buf[NUMBUFLEN];
10523
Bram Moolenaar758711c2005-02-02 23:11:38 +000010524 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10525 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010526 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527}
10528
10529/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010530 * "eval()" function
10531 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010533f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010534{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010535 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010536
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010537 s = get_tv_string_chk(&argvars[0]);
10538 if (s != NULL)
10539 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010540
Bram Moolenaar615b9972015-01-14 17:15:05 +010010541 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010542 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10543 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010544 if (p != NULL && !aborting())
10545 EMSG2(_(e_invexpr2), p);
10546 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010547 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010548 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010549 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010550 else if (*s != NUL)
10551 EMSG(_(e_trailing));
10552}
10553
10554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555 * "eventhandler()" function
10556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010558f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010560 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561}
10562
10563/*
10564 * "executable()" function
10565 */
10566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010567f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010569 char_u *name = get_tv_string(&argvars[0]);
10570
10571 /* Check in $PATH and also check directly if there is a directory name. */
10572 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10573 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010574}
10575
10576/*
10577 * "exepath()" function
10578 */
10579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010580f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010581{
10582 char_u *p = NULL;
10583
Bram Moolenaarb5971142015-03-21 17:32:19 +010010584 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010585 rettv->v_type = VAR_STRING;
10586 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587}
10588
10589/*
10590 * "exists()" function
10591 */
10592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010593f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594{
10595 char_u *p;
10596 char_u *name;
10597 int n = FALSE;
10598 int len = 0;
10599
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010600 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601 if (*p == '$') /* environment variable */
10602 {
10603 /* first try "normal" environment variables (fast) */
10604 if (mch_getenv(p + 1) != NULL)
10605 n = TRUE;
10606 else
10607 {
10608 /* try expanding things like $VIM and ${HOME} */
10609 p = expand_env_save(p);
10610 if (p != NULL && *p != '$')
10611 n = TRUE;
10612 vim_free(p);
10613 }
10614 }
10615 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010616 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010617 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010618 if (*skipwhite(p) != NUL)
10619 n = FALSE; /* trailing garbage */
10620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621 else if (*p == '*') /* internal or user defined function */
10622 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010623 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624 }
10625 else if (*p == ':')
10626 {
10627 n = cmd_exists(p + 1);
10628 }
10629 else if (*p == '#')
10630 {
10631#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010632 if (p[1] == '#')
10633 n = autocmd_supported(p + 2);
10634 else
10635 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636#endif
10637 }
10638 else /* internal variable */
10639 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010640 char_u *tofree;
10641 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010643 /* get_name_len() takes care of expanding curly braces */
10644 name = p;
10645 len = get_name_len(&p, &tofree, TRUE, FALSE);
10646 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010648 if (tofree != NULL)
10649 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010650 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010651 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010653 /* handle d.key, l[idx], f(expr) */
10654 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10655 if (n)
10656 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657 }
10658 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010659 if (*p != NUL)
10660 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010662 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 }
10664
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010665 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666}
10667
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010668#ifdef FEAT_FLOAT
10669/*
10670 * "exp()" function
10671 */
10672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010673f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010674{
10675 float_T f;
10676
10677 rettv->v_type = VAR_FLOAT;
10678 if (get_float_arg(argvars, &f) == OK)
10679 rettv->vval.v_float = exp(f);
10680 else
10681 rettv->vval.v_float = 0.0;
10682}
10683#endif
10684
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685/*
10686 * "expand()" function
10687 */
10688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010689f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690{
10691 char_u *s;
10692 int len;
10693 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010694 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010696 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010697 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010699 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010700 if (argvars[1].v_type != VAR_UNKNOWN
10701 && argvars[2].v_type != VAR_UNKNOWN
10702 && get_tv_number_chk(&argvars[2], &error)
10703 && !error)
10704 {
10705 rettv->v_type = VAR_LIST;
10706 rettv->vval.v_list = NULL;
10707 }
10708
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010709 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 if (*s == '%' || *s == '#' || *s == '<')
10711 {
10712 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010713 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010715 if (rettv->v_type == VAR_LIST)
10716 {
10717 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10718 list_append_string(rettv->vval.v_list, result, -1);
10719 else
10720 vim_free(result);
10721 }
10722 else
10723 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 }
10725 else
10726 {
10727 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010728 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010729 if (argvars[1].v_type != VAR_UNKNOWN
10730 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010731 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010732 if (!error)
10733 {
10734 ExpandInit(&xpc);
10735 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010736 if (p_wic)
10737 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010738 if (rettv->v_type == VAR_STRING)
10739 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10740 options, WILD_ALL);
10741 else if (rettv_list_alloc(rettv) != FAIL)
10742 {
10743 int i;
10744
10745 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10746 for (i = 0; i < xpc.xp_numfiles; i++)
10747 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10748 ExpandCleanup(&xpc);
10749 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010750 }
10751 else
10752 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 }
10754}
10755
10756/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010757 * Go over all entries in "d2" and add them to "d1".
10758 * When "action" is "error" then a duplicate key is an error.
10759 * When "action" is "force" then a duplicate key is overwritten.
10760 * Otherwise duplicate keys are ignored ("action" is "keep").
10761 */
10762 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010763dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020010764{
10765 dictitem_T *di1;
10766 hashitem_T *hi2;
10767 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010768 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010769
10770 todo = (int)d2->dv_hashtab.ht_used;
10771 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10772 {
10773 if (!HASHITEM_EMPTY(hi2))
10774 {
10775 --todo;
10776 di1 = dict_find(d1, hi2->hi_key, -1);
10777 if (d1->dv_scope != 0)
10778 {
10779 /* Disallow replacing a builtin function in l: and g:.
10780 * Check the key to be valid when adding to any
10781 * scope. */
10782 if (d1->dv_scope == VAR_DEF_SCOPE
10783 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10784 && var_check_func_name(hi2->hi_key,
10785 di1 == NULL))
10786 break;
10787 if (!valid_varname(hi2->hi_key))
10788 break;
10789 }
10790 if (di1 == NULL)
10791 {
10792 di1 = dictitem_copy(HI2DI(hi2));
10793 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10794 dictitem_free(di1);
10795 }
10796 else if (*action == 'e')
10797 {
10798 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10799 break;
10800 }
10801 else if (*action == 'f' && HI2DI(hi2) != di1)
10802 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010803 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10804 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010805 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010806 clear_tv(&di1->di_tv);
10807 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10808 }
10809 }
10810 }
10811}
10812
10813/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010814 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010815 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010816 */
10817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010818f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010819{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010820 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010821
Bram Moolenaare9a41262005-01-15 22:18:47 +000010822 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010823 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010824 list_T *l1, *l2;
10825 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010826 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010827 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010828
Bram Moolenaare9a41262005-01-15 22:18:47 +000010829 l1 = argvars[0].vval.v_list;
10830 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010831 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010832 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010833 {
10834 if (argvars[2].v_type != VAR_UNKNOWN)
10835 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010836 before = get_tv_number_chk(&argvars[2], &error);
10837 if (error)
10838 return; /* type error; errmsg already given */
10839
Bram Moolenaar758711c2005-02-02 23:11:38 +000010840 if (before == l1->lv_len)
10841 item = NULL;
10842 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010843 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010844 item = list_find(l1, before);
10845 if (item == NULL)
10846 {
10847 EMSGN(_(e_listidx), before);
10848 return;
10849 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010850 }
10851 }
10852 else
10853 item = NULL;
10854 list_extend(l1, l2, item);
10855
Bram Moolenaare9a41262005-01-15 22:18:47 +000010856 copy_tv(&argvars[0], rettv);
10857 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010858 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010859 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10860 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010861 dict_T *d1, *d2;
10862 char_u *action;
10863 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010864
10865 d1 = argvars[0].vval.v_dict;
10866 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010867 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010868 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010869 {
10870 /* Check the third argument. */
10871 if (argvars[2].v_type != VAR_UNKNOWN)
10872 {
10873 static char *(av[]) = {"keep", "force", "error"};
10874
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010875 action = get_tv_string_chk(&argvars[2]);
10876 if (action == NULL)
10877 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010878 for (i = 0; i < 3; ++i)
10879 if (STRCMP(action, av[i]) == 0)
10880 break;
10881 if (i == 3)
10882 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010883 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010884 return;
10885 }
10886 }
10887 else
10888 action = (char_u *)"force";
10889
Bram Moolenaara9922d62013-05-30 13:01:18 +020010890 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010891
Bram Moolenaare9a41262005-01-15 22:18:47 +000010892 copy_tv(&argvars[0], rettv);
10893 }
10894 }
10895 else
10896 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010897}
10898
10899/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010900 * "feedkeys()" function
10901 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010903f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010904{
10905 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010906 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010907 char_u *keys, *flags;
10908 char_u nbuf[NUMBUFLEN];
10909 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010910 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010911 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010912
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010913 /* This is not allowed in the sandbox. If the commands would still be
10914 * executed in the sandbox it would be OK, but it probably happens later,
10915 * when "sandbox" is no longer set. */
10916 if (check_secure())
10917 return;
10918
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010919 keys = get_tv_string(&argvars[0]);
10920 if (*keys != NUL)
10921 {
10922 if (argvars[1].v_type != VAR_UNKNOWN)
10923 {
10924 flags = get_tv_string_buf(&argvars[1], nbuf);
10925 for ( ; *flags != NUL; ++flags)
10926 {
10927 switch (*flags)
10928 {
10929 case 'n': remap = FALSE; break;
10930 case 'm': remap = TRUE; break;
10931 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010932 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010933 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010934 }
10935 }
10936 }
10937
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010938 /* Need to escape K_SPECIAL and CSI before putting the string in the
10939 * typeahead buffer. */
10940 keys_esc = vim_strsave_escape_csi(keys);
10941 if (keys_esc != NULL)
10942 {
10943 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010944 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010945 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010946 if (vgetc_busy)
10947 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010948 if (execute)
10949 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010950 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010951 }
10952}
10953
10954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 * "filereadable()" function
10956 */
10957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010958f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010960 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 char_u *p;
10962 int n;
10963
Bram Moolenaarc236c162008-07-13 17:41:49 +000010964#ifndef O_NONBLOCK
10965# define O_NONBLOCK 0
10966#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010967 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010968 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10969 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970 {
10971 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010972 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973 }
10974 else
10975 n = FALSE;
10976
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010977 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978}
10979
10980/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010981 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 * rights to write into.
10983 */
10984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010985f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010987 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988}
10989
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010990 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010010991findfilendir(
10992 typval_T *argvars UNUSED,
10993 typval_T *rettv,
10994 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010995{
10996#ifdef FEAT_SEARCHPATH
10997 char_u *fname;
10998 char_u *fresult = NULL;
10999 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11000 char_u *p;
11001 char_u pathbuf[NUMBUFLEN];
11002 int count = 1;
11003 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011004 int error = FALSE;
11005#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011006
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011007 rettv->vval.v_string = NULL;
11008 rettv->v_type = VAR_STRING;
11009
11010#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011011 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011012
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011013 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011014 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011015 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11016 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011017 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011018 else
11019 {
11020 if (*p != NUL)
11021 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011022
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011023 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011024 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011025 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011026 }
11027
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011028 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11029 error = TRUE;
11030
11031 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011032 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011033 do
11034 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011035 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011036 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011037 fresult = find_file_in_path_option(first ? fname : NULL,
11038 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011039 0, first, path,
11040 find_what,
11041 curbuf->b_ffname,
11042 find_what == FINDFILE_DIR
11043 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011044 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011045
11046 if (fresult != NULL && rettv->v_type == VAR_LIST)
11047 list_append_string(rettv->vval.v_list, fresult, -1);
11048
11049 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011050 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011051
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011052 if (rettv->v_type == VAR_STRING)
11053 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011054#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011055}
11056
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011057static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11058static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011059
11060/*
11061 * Implementation of map() and filter().
11062 */
11063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011064filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011065{
11066 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011067 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011068 listitem_T *li, *nli;
11069 list_T *l = NULL;
11070 dictitem_T *di;
11071 hashtab_T *ht;
11072 hashitem_T *hi;
11073 dict_T *d = NULL;
11074 typval_T save_val;
11075 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011076 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011077 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011078 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011079 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011080 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011081 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011082 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011083
Bram Moolenaare9a41262005-01-15 22:18:47 +000011084 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011085 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011086 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011087 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011088 return;
11089 }
11090 else if (argvars[0].v_type == VAR_DICT)
11091 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011092 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011093 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011094 return;
11095 }
11096 else
11097 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011098 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011099 return;
11100 }
11101
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011102 expr = get_tv_string_buf_chk(&argvars[1], buf);
11103 /* On type errors, the preceding call has already displayed an error
11104 * message. Avoid a misleading error message for an empty string that
11105 * was not passed as argument. */
11106 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011107 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011108 prepare_vimvar(VV_VAL, &save_val);
11109 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011110
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011111 /* We reset "did_emsg" to be able to detect whether an error
11112 * occurred during evaluation of the expression. */
11113 save_did_emsg = did_emsg;
11114 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011115
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011116 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011117 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011118 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011119 vimvars[VV_KEY].vv_type = VAR_STRING;
11120
11121 ht = &d->dv_hashtab;
11122 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011123 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011124 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011125 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011126 if (!HASHITEM_EMPTY(hi))
11127 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011128 int r;
11129
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011130 --todo;
11131 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011132 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011133 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11134 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011135 break;
11136 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011137 r = filter_map_one(&di->di_tv, expr, map, &rem);
11138 clear_tv(&vimvars[VV_KEY].vv_tv);
11139 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011140 break;
11141 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011142 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011143 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11144 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011145 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011146 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011147 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011148 }
11149 }
11150 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011151 }
11152 else
11153 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011154 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11155
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011156 for (li = l->lv_first; li != NULL; li = nli)
11157 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011158 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011159 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011160 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011161 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011162 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011163 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011164 break;
11165 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011166 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011167 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011168 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011169 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011170
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011171 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011172 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011173
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011174 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011175 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011176
11177 copy_tv(&argvars[0], rettv);
11178}
11179
11180 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011181filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011182{
Bram Moolenaar33570922005-01-25 22:26:29 +000011183 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011184 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011185 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011186
Bram Moolenaar33570922005-01-25 22:26:29 +000011187 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011188 s = expr;
11189 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011190 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011191 if (*s != NUL) /* check for trailing chars after expr */
11192 {
11193 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011194 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011195 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011196 }
11197 if (map)
11198 {
11199 /* map(): replace the list item value */
11200 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011201 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011202 *tv = rettv;
11203 }
11204 else
11205 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011206 int error = FALSE;
11207
Bram Moolenaare9a41262005-01-15 22:18:47 +000011208 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011209 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011210 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011211 /* On type error, nothing has been removed; return FAIL to stop the
11212 * loop. The error message was given by get_tv_number_chk(). */
11213 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011214 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011215 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011216 retval = OK;
11217theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011218 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011219 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011220}
11221
11222/*
11223 * "filter()" function
11224 */
11225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011226f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011227{
11228 filter_map(argvars, rettv, FALSE);
11229}
11230
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011231/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011232 * "finddir({fname}[, {path}[, {count}]])" function
11233 */
11234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011235f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011236{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011237 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011238}
11239
11240/*
11241 * "findfile({fname}[, {path}[, {count}]])" function
11242 */
11243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011244f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011245{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011246 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011247}
11248
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011249#ifdef FEAT_FLOAT
11250/*
11251 * "float2nr({float})" function
11252 */
11253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011254f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011255{
11256 float_T f;
11257
11258 if (get_float_arg(argvars, &f) == OK)
11259 {
11260 if (f < -0x7fffffff)
11261 rettv->vval.v_number = -0x7fffffff;
11262 else if (f > 0x7fffffff)
11263 rettv->vval.v_number = 0x7fffffff;
11264 else
11265 rettv->vval.v_number = (varnumber_T)f;
11266 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011267}
11268
11269/*
11270 * "floor({float})" function
11271 */
11272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011273f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011274{
11275 float_T f;
11276
11277 rettv->v_type = VAR_FLOAT;
11278 if (get_float_arg(argvars, &f) == OK)
11279 rettv->vval.v_float = floor(f);
11280 else
11281 rettv->vval.v_float = 0.0;
11282}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011283
11284/*
11285 * "fmod()" function
11286 */
11287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011288f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011289{
11290 float_T fx, fy;
11291
11292 rettv->v_type = VAR_FLOAT;
11293 if (get_float_arg(argvars, &fx) == OK
11294 && get_float_arg(&argvars[1], &fy) == OK)
11295 rettv->vval.v_float = fmod(fx, fy);
11296 else
11297 rettv->vval.v_float = 0.0;
11298}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011299#endif
11300
Bram Moolenaar0d660222005-01-07 21:51:51 +000011301/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011302 * "fnameescape({string})" function
11303 */
11304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011305f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011306{
11307 rettv->vval.v_string = vim_strsave_fnameescape(
11308 get_tv_string(&argvars[0]), FALSE);
11309 rettv->v_type = VAR_STRING;
11310}
11311
11312/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313 * "fnamemodify({fname}, {mods})" function
11314 */
11315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011316f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317{
11318 char_u *fname;
11319 char_u *mods;
11320 int usedlen = 0;
11321 int len;
11322 char_u *fbuf = NULL;
11323 char_u buf[NUMBUFLEN];
11324
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011325 fname = get_tv_string_chk(&argvars[0]);
11326 mods = get_tv_string_buf_chk(&argvars[1], buf);
11327 if (fname == NULL || mods == NULL)
11328 fname = NULL;
11329 else
11330 {
11331 len = (int)STRLEN(fname);
11332 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011335 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011337 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011339 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340 vim_free(fbuf);
11341}
11342
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011343static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344
11345/*
11346 * "foldclosed()" function
11347 */
11348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011349foldclosed_both(
11350 typval_T *argvars UNUSED,
11351 typval_T *rettv,
11352 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353{
11354#ifdef FEAT_FOLDING
11355 linenr_T lnum;
11356 linenr_T first, last;
11357
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011358 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11360 {
11361 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11362 {
11363 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011364 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011366 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367 return;
11368 }
11369 }
11370#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011371 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372}
11373
11374/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011375 * "foldclosed()" function
11376 */
11377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011378f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011379{
11380 foldclosed_both(argvars, rettv, FALSE);
11381}
11382
11383/*
11384 * "foldclosedend()" function
11385 */
11386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011387f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011388{
11389 foldclosed_both(argvars, rettv, TRUE);
11390}
11391
11392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393 * "foldlevel()" function
11394 */
11395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011396f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397{
11398#ifdef FEAT_FOLDING
11399 linenr_T lnum;
11400
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011401 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011403 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405}
11406
11407/*
11408 * "foldtext()" function
11409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011411f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412{
11413#ifdef FEAT_FOLDING
11414 linenr_T lnum;
11415 char_u *s;
11416 char_u *r;
11417 int len;
11418 char *txt;
11419#endif
11420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011421 rettv->v_type = VAR_STRING;
11422 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011424 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11425 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11426 <= curbuf->b_ml.ml_line_count
11427 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428 {
11429 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011430 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11431 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432 {
11433 if (!linewhite(lnum))
11434 break;
11435 ++lnum;
11436 }
11437
11438 /* Find interesting text in this line. */
11439 s = skipwhite(ml_get(lnum));
11440 /* skip C comment-start */
11441 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011442 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011444 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011445 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011446 {
11447 s = skipwhite(ml_get(lnum + 1));
11448 if (*s == '*')
11449 s = skipwhite(s + 1);
11450 }
11451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452 txt = _("+-%s%3ld lines: ");
11453 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011454 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455 + 20 /* for %3ld */
11456 + STRLEN(s))); /* concatenated */
11457 if (r != NULL)
11458 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011459 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11460 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11461 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011462 len = (int)STRLEN(r);
11463 STRCAT(r, s);
11464 /* remove 'foldmarker' and 'commentstring' */
11465 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467 }
11468 }
11469#endif
11470}
11471
11472/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011473 * "foldtextresult(lnum)" function
11474 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011476f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011477{
11478#ifdef FEAT_FOLDING
11479 linenr_T lnum;
11480 char_u *text;
11481 char_u buf[51];
11482 foldinfo_T foldinfo;
11483 int fold_count;
11484#endif
11485
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011486 rettv->v_type = VAR_STRING;
11487 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011488#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011489 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011490 /* treat illegal types and illegal string values for {lnum} the same */
11491 if (lnum < 0)
11492 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011493 fold_count = foldedCount(curwin, lnum, &foldinfo);
11494 if (fold_count > 0)
11495 {
11496 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11497 &foldinfo, buf);
11498 if (text == buf)
11499 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011500 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011501 }
11502#endif
11503}
11504
11505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506 * "foreground()" function
11507 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011509f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011511#ifdef FEAT_GUI
11512 if (gui.in_use)
11513 gui_mch_set_foreground();
11514#else
11515# ifdef WIN32
11516 win32_set_foreground();
11517# endif
11518#endif
11519}
11520
11521/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011522 * "function()" function
11523 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011525f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011526{
11527 char_u *s;
11528
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011529 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011530 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011531 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011532 /* Don't check an autoload name for existence here. */
11533 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011534 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011535 else
11536 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011537 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011538 {
11539 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011540 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011541
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011542 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11543 * also be called from another script. Using trans_function_name()
11544 * would also work, but some plugins depend on the name being
11545 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011546 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011547 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011548 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011549 if (rettv->vval.v_string != NULL)
11550 {
11551 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011552 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011553 }
11554 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011555 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011556 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011557 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011558 }
11559}
11560
11561/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011562 * "garbagecollect()" function
11563 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011565f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011566{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011567 /* This is postponed until we are back at the toplevel, because we may be
11568 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11569 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011570
11571 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11572 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011573}
11574
11575/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011576 * "get()" function
11577 */
11578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011579f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011580{
Bram Moolenaar33570922005-01-25 22:26:29 +000011581 listitem_T *li;
11582 list_T *l;
11583 dictitem_T *di;
11584 dict_T *d;
11585 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011586
Bram Moolenaare9a41262005-01-15 22:18:47 +000011587 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011588 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011589 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011590 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011591 int error = FALSE;
11592
11593 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11594 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011595 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011596 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011597 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011598 else if (argvars[0].v_type == VAR_DICT)
11599 {
11600 if ((d = argvars[0].vval.v_dict) != NULL)
11601 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011602 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011603 if (di != NULL)
11604 tv = &di->di_tv;
11605 }
11606 }
11607 else
11608 EMSG2(_(e_listdictarg), "get()");
11609
11610 if (tv == NULL)
11611 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011612 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011613 copy_tv(&argvars[2], rettv);
11614 }
11615 else
11616 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011617}
11618
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011619static 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 +000011620
11621/*
11622 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011623 * Return a range (from start to end) of lines in rettv from the specified
11624 * buffer.
11625 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011626 */
11627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011628get_buffer_lines(
11629 buf_T *buf,
11630 linenr_T start,
11631 linenr_T end,
11632 int retlist,
11633 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011634{
11635 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011636
Bram Moolenaar959a1432013-12-14 12:17:38 +010011637 rettv->v_type = VAR_STRING;
11638 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011639 if (retlist && rettv_list_alloc(rettv) == FAIL)
11640 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011641
11642 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11643 return;
11644
11645 if (!retlist)
11646 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011647 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11648 p = ml_get_buf(buf, start, FALSE);
11649 else
11650 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011651 rettv->vval.v_string = vim_strsave(p);
11652 }
11653 else
11654 {
11655 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011656 return;
11657
11658 if (start < 1)
11659 start = 1;
11660 if (end > buf->b_ml.ml_line_count)
11661 end = buf->b_ml.ml_line_count;
11662 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011663 if (list_append_string(rettv->vval.v_list,
11664 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011665 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011666 }
11667}
11668
11669/*
11670 * "getbufline()" function
11671 */
11672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011673f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011674{
11675 linenr_T lnum;
11676 linenr_T end;
11677 buf_T *buf;
11678
11679 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11680 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011681 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011682 --emsg_off;
11683
Bram Moolenaar661b1822005-07-28 22:36:45 +000011684 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011685 if (argvars[2].v_type == VAR_UNKNOWN)
11686 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011687 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011688 end = get_tv_lnum_buf(&argvars[2], buf);
11689
Bram Moolenaar342337a2005-07-21 21:11:17 +000011690 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011691}
11692
Bram Moolenaar0d660222005-01-07 21:51:51 +000011693/*
11694 * "getbufvar()" function
11695 */
11696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011697f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011698{
11699 buf_T *buf;
11700 buf_T *save_curbuf;
11701 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011702 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011703 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011704
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011705 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11706 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011707 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011708 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011709
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011710 rettv->v_type = VAR_STRING;
11711 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011712
11713 if (buf != NULL && varname != NULL)
11714 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011715 /* set curbuf to be our buf, temporarily */
11716 save_curbuf = curbuf;
11717 curbuf = buf;
11718
Bram Moolenaar0d660222005-01-07 21:51:51 +000011719 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011720 {
11721 if (get_option_tv(&varname, rettv, TRUE) == OK)
11722 done = TRUE;
11723 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011724 else if (STRCMP(varname, "changedtick") == 0)
11725 {
11726 rettv->v_type = VAR_NUMBER;
11727 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011728 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011729 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011730 else
11731 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011732 /* Look up the variable. */
11733 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11734 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11735 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011736 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011737 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011738 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011739 done = TRUE;
11740 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011741 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011742
11743 /* restore previous notion of curbuf */
11744 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011745 }
11746
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011747 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11748 /* use the default value */
11749 copy_tv(&argvars[2], rettv);
11750
Bram Moolenaar0d660222005-01-07 21:51:51 +000011751 --emsg_off;
11752}
11753
11754/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755 * "getchar()" function
11756 */
11757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011758f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759{
11760 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011761 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011763 /* Position the cursor. Needed after a message that ends in a space. */
11764 windgoto(msg_row, msg_col);
11765
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766 ++no_mapping;
11767 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011768 for (;;)
11769 {
11770 if (argvars[0].v_type == VAR_UNKNOWN)
11771 /* getchar(): blocking wait. */
11772 n = safe_vgetc();
11773 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11774 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011775 n = vpeekc_any();
11776 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011777 /* illegal argument or getchar(0) and no char avail: return zero */
11778 n = 0;
11779 else
11780 /* getchar(0) and char avail: return char */
11781 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011782
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011783 if (n == K_IGNORE)
11784 continue;
11785 break;
11786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011787 --no_mapping;
11788 --allow_keys;
11789
Bram Moolenaar219b8702006-11-01 14:32:36 +000011790 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11791 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11792 vimvars[VV_MOUSE_COL].vv_nr = 0;
11793
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011794 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795 if (IS_SPECIAL(n) || mod_mask != 0)
11796 {
11797 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11798 int i = 0;
11799
11800 /* Turn a special key into three bytes, plus modifier. */
11801 if (mod_mask != 0)
11802 {
11803 temp[i++] = K_SPECIAL;
11804 temp[i++] = KS_MODIFIER;
11805 temp[i++] = mod_mask;
11806 }
11807 if (IS_SPECIAL(n))
11808 {
11809 temp[i++] = K_SPECIAL;
11810 temp[i++] = K_SECOND(n);
11811 temp[i++] = K_THIRD(n);
11812 }
11813#ifdef FEAT_MBYTE
11814 else if (has_mbyte)
11815 i += (*mb_char2bytes)(n, temp + i);
11816#endif
11817 else
11818 temp[i++] = n;
11819 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011820 rettv->v_type = VAR_STRING;
11821 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011822
11823#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011824 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011825 {
11826 int row = mouse_row;
11827 int col = mouse_col;
11828 win_T *win;
11829 linenr_T lnum;
11830# ifdef FEAT_WINDOWS
11831 win_T *wp;
11832# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011833 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011834
11835 if (row >= 0 && col >= 0)
11836 {
11837 /* Find the window at the mouse coordinates and compute the
11838 * text position. */
11839 win = mouse_find_win(&row, &col);
11840 (void)mouse_comp_pos(win, &row, &col, &lnum);
11841# ifdef FEAT_WINDOWS
11842 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011843 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011844# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011845 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011846 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11847 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11848 }
11849 }
11850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851 }
11852}
11853
11854/*
11855 * "getcharmod()" function
11856 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011858f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011860 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861}
11862
11863/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011864 * "getcharsearch()" function
11865 */
11866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011867f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011868{
11869 if (rettv_dict_alloc(rettv) != FAIL)
11870 {
11871 dict_T *dict = rettv->vval.v_dict;
11872
11873 dict_add_nr_str(dict, "char", 0L, last_csearch());
11874 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11875 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11876 }
11877}
11878
11879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011880 * "getcmdline()" function
11881 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011883f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011884{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011885 rettv->v_type = VAR_STRING;
11886 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011887}
11888
11889/*
11890 * "getcmdpos()" function
11891 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011893f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011894{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011895 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896}
11897
11898/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011899 * "getcmdtype()" function
11900 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011902f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011903{
11904 rettv->v_type = VAR_STRING;
11905 rettv->vval.v_string = alloc(2);
11906 if (rettv->vval.v_string != NULL)
11907 {
11908 rettv->vval.v_string[0] = get_cmdline_type();
11909 rettv->vval.v_string[1] = NUL;
11910 }
11911}
11912
11913/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011914 * "getcmdwintype()" function
11915 */
11916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011917f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011918{
11919 rettv->v_type = VAR_STRING;
11920 rettv->vval.v_string = NULL;
11921#ifdef FEAT_CMDWIN
11922 rettv->vval.v_string = alloc(2);
11923 if (rettv->vval.v_string != NULL)
11924 {
11925 rettv->vval.v_string[0] = cmdwin_type;
11926 rettv->vval.v_string[1] = NUL;
11927 }
11928#endif
11929}
11930
11931/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011932 * "getcwd()" function
11933 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011935f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936{
Bram Moolenaarc9703302016-01-17 21:49:33 +010011937 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011938 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011939
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011940 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011941 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010011942
11943 wp = find_tabwin(&argvars[0], &argvars[1]);
11944 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011945 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010011946 if (wp->w_localdir != NULL)
11947 rettv->vval.v_string = vim_strsave(wp->w_localdir);
11948 else if(globaldir != NULL)
11949 rettv->vval.v_string = vim_strsave(globaldir);
11950 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020011951 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010011952 cwd = alloc(MAXPATHL);
11953 if (cwd != NULL)
11954 {
11955 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11956 rettv->vval.v_string = vim_strsave(cwd);
11957 vim_free(cwd);
11958 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020011959 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010011960#ifdef BACKSLASH_IN_FILENAME
11961 if (rettv->vval.v_string != NULL)
11962 slash_adjust(rettv->vval.v_string);
11963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011964 }
11965}
11966
11967/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011968 * "getfontname()" function
11969 */
11970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011971f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011972{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011973 rettv->v_type = VAR_STRING;
11974 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011975#ifdef FEAT_GUI
11976 if (gui.in_use)
11977 {
11978 GuiFont font;
11979 char_u *name = NULL;
11980
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011981 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011982 {
11983 /* Get the "Normal" font. Either the name saved by
11984 * hl_set_font_name() or from the font ID. */
11985 font = gui.norm_font;
11986 name = hl_get_font_name();
11987 }
11988 else
11989 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011990 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011991 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11992 return;
11993 font = gui_mch_get_font(name, FALSE);
11994 if (font == NOFONT)
11995 return; /* Invalid font name, return empty string. */
11996 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011997 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011998 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011999 gui_mch_free_font(font);
12000 }
12001#endif
12002}
12003
12004/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012005 * "getfperm({fname})" function
12006 */
12007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012008f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012009{
12010 char_u *fname;
12011 struct stat st;
12012 char_u *perm = NULL;
12013 char_u flags[] = "rwx";
12014 int i;
12015
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012016 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012017
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012018 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012019 if (mch_stat((char *)fname, &st) >= 0)
12020 {
12021 perm = vim_strsave((char_u *)"---------");
12022 if (perm != NULL)
12023 {
12024 for (i = 0; i < 9; i++)
12025 {
12026 if (st.st_mode & (1 << (8 - i)))
12027 perm[i] = flags[i % 3];
12028 }
12029 }
12030 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012031 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012032}
12033
12034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012035 * "getfsize({fname})" function
12036 */
12037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012038f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012039{
12040 char_u *fname;
12041 struct stat st;
12042
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012043 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012045 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046
12047 if (mch_stat((char *)fname, &st) >= 0)
12048 {
12049 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012050 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012052 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012053 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012054
12055 /* non-perfect check for overflow */
12056 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12057 rettv->vval.v_number = -2;
12058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059 }
12060 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012061 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062}
12063
12064/*
12065 * "getftime({fname})" function
12066 */
12067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012068f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012069{
12070 char_u *fname;
12071 struct stat st;
12072
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012073 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012074
12075 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012076 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012078 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079}
12080
12081/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012082 * "getftype({fname})" function
12083 */
12084 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012085f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012086{
12087 char_u *fname;
12088 struct stat st;
12089 char_u *type = NULL;
12090 char *t;
12091
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012092 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012093
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012094 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012095 if (mch_lstat((char *)fname, &st) >= 0)
12096 {
12097#ifdef S_ISREG
12098 if (S_ISREG(st.st_mode))
12099 t = "file";
12100 else if (S_ISDIR(st.st_mode))
12101 t = "dir";
12102# ifdef S_ISLNK
12103 else if (S_ISLNK(st.st_mode))
12104 t = "link";
12105# endif
12106# ifdef S_ISBLK
12107 else if (S_ISBLK(st.st_mode))
12108 t = "bdev";
12109# endif
12110# ifdef S_ISCHR
12111 else if (S_ISCHR(st.st_mode))
12112 t = "cdev";
12113# endif
12114# ifdef S_ISFIFO
12115 else if (S_ISFIFO(st.st_mode))
12116 t = "fifo";
12117# endif
12118# ifdef S_ISSOCK
12119 else if (S_ISSOCK(st.st_mode))
12120 t = "fifo";
12121# endif
12122 else
12123 t = "other";
12124#else
12125# ifdef S_IFMT
12126 switch (st.st_mode & S_IFMT)
12127 {
12128 case S_IFREG: t = "file"; break;
12129 case S_IFDIR: t = "dir"; break;
12130# ifdef S_IFLNK
12131 case S_IFLNK: t = "link"; break;
12132# endif
12133# ifdef S_IFBLK
12134 case S_IFBLK: t = "bdev"; break;
12135# endif
12136# ifdef S_IFCHR
12137 case S_IFCHR: t = "cdev"; break;
12138# endif
12139# ifdef S_IFIFO
12140 case S_IFIFO: t = "fifo"; break;
12141# endif
12142# ifdef S_IFSOCK
12143 case S_IFSOCK: t = "socket"; break;
12144# endif
12145 default: t = "other";
12146 }
12147# else
12148 if (mch_isdir(fname))
12149 t = "dir";
12150 else
12151 t = "file";
12152# endif
12153#endif
12154 type = vim_strsave((char_u *)t);
12155 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012156 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012157}
12158
12159/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012160 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012161 */
12162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012163f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012164{
12165 linenr_T lnum;
12166 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012167 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012168
12169 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012170 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012171 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012172 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012173 retlist = FALSE;
12174 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012175 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012176 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012177 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012178 retlist = TRUE;
12179 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012180
Bram Moolenaar342337a2005-07-21 21:11:17 +000012181 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012182}
12183
12184/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012185 * "getmatches()" function
12186 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012188f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012189{
12190#ifdef FEAT_SEARCH_EXTRA
12191 dict_T *dict;
12192 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012193 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012194
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012195 if (rettv_list_alloc(rettv) == OK)
12196 {
12197 while (cur != NULL)
12198 {
12199 dict = dict_alloc();
12200 if (dict == NULL)
12201 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012202 if (cur->match.regprog == NULL)
12203 {
12204 /* match added with matchaddpos() */
12205 for (i = 0; i < MAXPOSMATCH; ++i)
12206 {
12207 llpos_T *llpos;
12208 char buf[6];
12209 list_T *l;
12210
12211 llpos = &cur->pos.pos[i];
12212 if (llpos->lnum == 0)
12213 break;
12214 l = list_alloc();
12215 if (l == NULL)
12216 break;
12217 list_append_number(l, (varnumber_T)llpos->lnum);
12218 if (llpos->col > 0)
12219 {
12220 list_append_number(l, (varnumber_T)llpos->col);
12221 list_append_number(l, (varnumber_T)llpos->len);
12222 }
12223 sprintf(buf, "pos%d", i + 1);
12224 dict_add_list(dict, buf, l);
12225 }
12226 }
12227 else
12228 {
12229 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12230 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012231 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012232 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12233 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012234# ifdef FEAT_CONCEAL
12235 if (cur->conceal_char)
12236 {
12237 char_u buf[MB_MAXBYTES + 1];
12238
12239 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12240 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12241 }
12242# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012243 list_append_dict(rettv->vval.v_list, dict);
12244 cur = cur->next;
12245 }
12246 }
12247#endif
12248}
12249
12250/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012251 * "getpid()" function
12252 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012254f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012255{
12256 rettv->vval.v_number = mch_get_pid();
12257}
12258
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012259static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012260
12261/*
12262 * "getcurpos()" function
12263 */
12264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012265f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012266{
12267 getpos_both(argvars, rettv, TRUE);
12268}
12269
Bram Moolenaar18081e32008-02-20 19:11:07 +000012270/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012271 * "getpos(string)" function
12272 */
12273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012274f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012275{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012276 getpos_both(argvars, rettv, FALSE);
12277}
12278
12279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012280getpos_both(
12281 typval_T *argvars,
12282 typval_T *rettv,
12283 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012284{
Bram Moolenaara5525202006-03-02 22:52:09 +000012285 pos_T *fp;
12286 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012287 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012288
12289 if (rettv_list_alloc(rettv) == OK)
12290 {
12291 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012292 if (getcurpos)
12293 fp = &curwin->w_cursor;
12294 else
12295 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012296 if (fnum != -1)
12297 list_append_number(l, (varnumber_T)fnum);
12298 else
12299 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012300 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12301 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012302 list_append_number(l, (fp != NULL)
12303 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012304 : (varnumber_T)0);
12305 list_append_number(l,
12306#ifdef FEAT_VIRTUALEDIT
12307 (fp != NULL) ? (varnumber_T)fp->coladd :
12308#endif
12309 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012310 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012311 list_append_number(l, curwin->w_curswant == MAXCOL ?
12312 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012313 }
12314 else
12315 rettv->vval.v_number = FALSE;
12316}
12317
12318/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012319 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012320 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012322f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012323{
12324#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012325 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012326#endif
12327
Bram Moolenaar2641f772005-03-25 21:58:17 +000012328#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012329 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012330 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012331 wp = NULL;
12332 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12333 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012334 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012335 if (wp == NULL)
12336 return;
12337 }
12338
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012339 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012340 }
12341#endif
12342}
12343
12344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345 * "getreg()" function
12346 */
12347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012348f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349{
12350 char_u *strregname;
12351 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012352 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012353 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012354 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012356 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012357 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012358 strregname = get_tv_string_chk(&argvars[0]);
12359 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012360 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012361 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012362 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012363 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12364 return_list = get_tv_number_chk(&argvars[2], &error);
12365 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012367 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012368 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012369
12370 if (error)
12371 return;
12372
Bram Moolenaar071d4272004-06-13 20:20:40 +000012373 regname = (strregname == NULL ? '"' : *strregname);
12374 if (regname == 0)
12375 regname = '"';
12376
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012377 if (return_list)
12378 {
12379 rettv->v_type = VAR_LIST;
12380 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12381 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012382 if (rettv->vval.v_list != NULL)
12383 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012384 }
12385 else
12386 {
12387 rettv->v_type = VAR_STRING;
12388 rettv->vval.v_string = get_reg_contents(regname,
12389 arg2 ? GREG_EXPR_SRC : 0);
12390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012391}
12392
12393/*
12394 * "getregtype()" function
12395 */
12396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012397f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398{
12399 char_u *strregname;
12400 int regname;
12401 char_u buf[NUMBUFLEN + 2];
12402 long reglen = 0;
12403
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012404 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012405 {
12406 strregname = get_tv_string_chk(&argvars[0]);
12407 if (strregname == NULL) /* type error; errmsg already given */
12408 {
12409 rettv->v_type = VAR_STRING;
12410 rettv->vval.v_string = NULL;
12411 return;
12412 }
12413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012414 else
12415 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012416 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012417
12418 regname = (strregname == NULL ? '"' : *strregname);
12419 if (regname == 0)
12420 regname = '"';
12421
12422 buf[0] = NUL;
12423 buf[1] = NUL;
12424 switch (get_reg_type(regname, &reglen))
12425 {
12426 case MLINE: buf[0] = 'V'; break;
12427 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428 case MBLOCK:
12429 buf[0] = Ctrl_V;
12430 sprintf((char *)buf + 1, "%ld", reglen + 1);
12431 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012433 rettv->v_type = VAR_STRING;
12434 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012435}
12436
12437/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012438 * "gettabvar()" function
12439 */
12440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012441f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012442{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012443 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012444 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012445 dictitem_T *v;
12446 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012447 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012448
12449 rettv->v_type = VAR_STRING;
12450 rettv->vval.v_string = NULL;
12451
12452 varname = get_tv_string_chk(&argvars[1]);
12453 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12454 if (tp != NULL && varname != NULL)
12455 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012456 /* Set tp to be our tabpage, temporarily. Also set the window to the
12457 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012458 if (switch_win(&oldcurwin, &oldtabpage,
12459 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012460 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012461 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012462 /* look up the variable */
12463 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12464 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12465 if (v != NULL)
12466 {
12467 copy_tv(&v->di_tv, rettv);
12468 done = TRUE;
12469 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012470 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012471
12472 /* restore previous notion of curwin */
12473 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012474 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012475
12476 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012477 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012478}
12479
12480/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012481 * "gettabwinvar()" function
12482 */
12483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012484f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012485{
12486 getwinvar(argvars, rettv, 1);
12487}
12488
12489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012490 * "getwinposx()" function
12491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012493f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012494{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012495 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012496#ifdef FEAT_GUI
12497 if (gui.in_use)
12498 {
12499 int x, y;
12500
12501 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012502 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012503 }
12504#endif
12505}
12506
12507/*
12508 * "getwinposy()" function
12509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012511f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012513 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514#ifdef FEAT_GUI
12515 if (gui.in_use)
12516 {
12517 int x, y;
12518
12519 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012520 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012521 }
12522#endif
12523}
12524
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012525/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012526 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012527 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012528 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012529find_win_by_nr(
12530 typval_T *vp,
12531 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012532{
12533#ifdef FEAT_WINDOWS
12534 win_T *wp;
12535#endif
12536 int nr;
12537
12538 nr = get_tv_number_chk(vp, NULL);
12539
12540#ifdef FEAT_WINDOWS
12541 if (nr < 0)
12542 return NULL;
12543 if (nr == 0)
12544 return curwin;
12545
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012546 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12547 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012548 if (--nr <= 0)
12549 break;
12550 return wp;
12551#else
12552 if (nr == 0 || nr == 1)
12553 return curwin;
12554 return NULL;
12555#endif
12556}
12557
Bram Moolenaar071d4272004-06-13 20:20:40 +000012558/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012559 * Find window specified by "wvp" in tabpage "tvp".
12560 */
12561 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012562find_tabwin(
12563 typval_T *wvp, /* VAR_UNKNOWN for current window */
12564 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012565{
12566 win_T *wp = NULL;
12567 tabpage_T *tp = NULL;
12568 long n;
12569
12570 if (wvp->v_type != VAR_UNKNOWN)
12571 {
12572 if (tvp->v_type != VAR_UNKNOWN)
12573 {
12574 n = get_tv_number(tvp);
12575 if (n >= 0)
12576 tp = find_tabpage(n);
12577 }
12578 else
12579 tp = curtab;
12580
12581 if (tp != NULL)
12582 wp = find_win_by_nr(wvp, tp);
12583 }
12584 else
12585 wp = curwin;
12586
12587 return wp;
12588}
12589
12590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012591 * "getwinvar()" function
12592 */
12593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012594f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012596 getwinvar(argvars, rettv, 0);
12597}
12598
12599/*
12600 * getwinvar() and gettabwinvar()
12601 */
12602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012603getwinvar(
12604 typval_T *argvars,
12605 typval_T *rettv,
12606 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012607{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012608 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012609 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012610 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012611 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012612 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012613#ifdef FEAT_WINDOWS
12614 win_T *oldcurwin;
12615 tabpage_T *oldtabpage;
12616 int need_switch_win;
12617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012618
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012619#ifdef FEAT_WINDOWS
12620 if (off == 1)
12621 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12622 else
12623 tp = curtab;
12624#endif
12625 win = find_win_by_nr(&argvars[off], tp);
12626 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012627 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012628
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012629 rettv->v_type = VAR_STRING;
12630 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012631
12632 if (win != NULL && varname != NULL)
12633 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012634#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012635 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012636 * otherwise the window is not valid. Only do this when needed,
12637 * autocommands get blocked. */
12638 need_switch_win = !(tp == curtab && win == curwin);
12639 if (!need_switch_win
12640 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12641#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012642 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012643 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012644 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012645 if (get_option_tv(&varname, rettv, 1) == OK)
12646 done = TRUE;
12647 }
12648 else
12649 {
12650 /* Look up the variable. */
12651 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12652 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12653 varname, FALSE);
12654 if (v != NULL)
12655 {
12656 copy_tv(&v->di_tv, rettv);
12657 done = TRUE;
12658 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012660 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012661
Bram Moolenaarba117c22015-09-29 16:53:22 +020012662#ifdef FEAT_WINDOWS
12663 if (need_switch_win)
12664 /* restore previous notion of curwin */
12665 restore_win(oldcurwin, oldtabpage, TRUE);
12666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012667 }
12668
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012669 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12670 /* use the default return value */
12671 copy_tv(&argvars[off + 2], rettv);
12672
Bram Moolenaar071d4272004-06-13 20:20:40 +000012673 --emsg_off;
12674}
12675
12676/*
12677 * "glob()" function
12678 */
12679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012680f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012682 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012684 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012686 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012687 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012688 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012689 if (argvars[1].v_type != VAR_UNKNOWN)
12690 {
12691 if (get_tv_number_chk(&argvars[1], &error))
12692 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012693 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012694 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012695 if (get_tv_number_chk(&argvars[2], &error))
12696 {
12697 rettv->v_type = VAR_LIST;
12698 rettv->vval.v_list = NULL;
12699 }
12700 if (argvars[3].v_type != VAR_UNKNOWN
12701 && get_tv_number_chk(&argvars[3], &error))
12702 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012703 }
12704 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012705 if (!error)
12706 {
12707 ExpandInit(&xpc);
12708 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012709 if (p_wic)
12710 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012711 if (rettv->v_type == VAR_STRING)
12712 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012713 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012714 else if (rettv_list_alloc(rettv) != FAIL)
12715 {
12716 int i;
12717
12718 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12719 NULL, options, WILD_ALL_KEEP);
12720 for (i = 0; i < xpc.xp_numfiles; i++)
12721 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12722
12723 ExpandCleanup(&xpc);
12724 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012725 }
12726 else
12727 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728}
12729
12730/*
12731 * "globpath()" function
12732 */
12733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012734f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012735{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012736 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012737 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012738 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012739 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012740 garray_T ga;
12741 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012743 /* When the optional second argument is non-zero, don't remove matches
12744 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012745 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012746 if (argvars[2].v_type != VAR_UNKNOWN)
12747 {
12748 if (get_tv_number_chk(&argvars[2], &error))
12749 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012750 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012751 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012752 if (get_tv_number_chk(&argvars[3], &error))
12753 {
12754 rettv->v_type = VAR_LIST;
12755 rettv->vval.v_list = NULL;
12756 }
12757 if (argvars[4].v_type != VAR_UNKNOWN
12758 && get_tv_number_chk(&argvars[4], &error))
12759 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012760 }
12761 }
12762 if (file != NULL && !error)
12763 {
12764 ga_init2(&ga, (int)sizeof(char_u *), 10);
12765 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12766 if (rettv->v_type == VAR_STRING)
12767 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12768 else if (rettv_list_alloc(rettv) != FAIL)
12769 for (i = 0; i < ga.ga_len; ++i)
12770 list_append_string(rettv->vval.v_list,
12771 ((char_u **)(ga.ga_data))[i], -1);
12772 ga_clear_strings(&ga);
12773 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012774 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012775 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776}
12777
12778/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012779 * "glob2regpat()" function
12780 */
12781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012782f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012783{
12784 char_u *pat = get_tv_string_chk(&argvars[0]);
12785
12786 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010012787 rettv->vval.v_string = (pat == NULL)
12788 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012789}
12790
12791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792 * "has()" function
12793 */
12794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012795f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012796{
12797 int i;
12798 char_u *name;
12799 int n = FALSE;
12800 static char *(has_list[]) =
12801 {
12802#ifdef AMIGA
12803 "amiga",
12804# ifdef FEAT_ARP
12805 "arp",
12806# endif
12807#endif
12808#ifdef __BEOS__
12809 "beos",
12810#endif
12811#ifdef MSDOS
12812# ifdef DJGPP
12813 "dos32",
12814# else
12815 "dos16",
12816# endif
12817#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012818#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819 "mac",
12820#endif
12821#if defined(MACOS_X_UNIX)
12822 "macunix",
12823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824#ifdef __QNX__
12825 "qnx",
12826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012827#ifdef UNIX
12828 "unix",
12829#endif
12830#ifdef VMS
12831 "vms",
12832#endif
12833#ifdef WIN16
12834 "win16",
12835#endif
12836#ifdef WIN32
12837 "win32",
12838#endif
12839#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12840 "win32unix",
12841#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012842#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843 "win64",
12844#endif
12845#ifdef EBCDIC
12846 "ebcdic",
12847#endif
12848#ifndef CASE_INSENSITIVE_FILENAME
12849 "fname_case",
12850#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012851#ifdef HAVE_ACL
12852 "acl",
12853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854#ifdef FEAT_ARABIC
12855 "arabic",
12856#endif
12857#ifdef FEAT_AUTOCMD
12858 "autocmd",
12859#endif
12860#ifdef FEAT_BEVAL
12861 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012862# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12863 "balloon_multiline",
12864# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865#endif
12866#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12867 "builtin_terms",
12868# ifdef ALL_BUILTIN_TCAPS
12869 "all_builtin_terms",
12870# endif
12871#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012872#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12873 || defined(FEAT_GUI_W32) \
12874 || defined(FEAT_GUI_MOTIF))
12875 "browsefilter",
12876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877#ifdef FEAT_BYTEOFF
12878 "byte_offset",
12879#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010012880#ifdef FEAT_CHANNEL
12881 "channel",
12882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883#ifdef FEAT_CINDENT
12884 "cindent",
12885#endif
12886#ifdef FEAT_CLIENTSERVER
12887 "clientserver",
12888#endif
12889#ifdef FEAT_CLIPBOARD
12890 "clipboard",
12891#endif
12892#ifdef FEAT_CMDL_COMPL
12893 "cmdline_compl",
12894#endif
12895#ifdef FEAT_CMDHIST
12896 "cmdline_hist",
12897#endif
12898#ifdef FEAT_COMMENTS
12899 "comments",
12900#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012901#ifdef FEAT_CONCEAL
12902 "conceal",
12903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904#ifdef FEAT_CRYPT
12905 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010012906 "crypt-blowfish",
12907 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908#endif
12909#ifdef FEAT_CSCOPE
12910 "cscope",
12911#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012912#ifdef FEAT_CURSORBIND
12913 "cursorbind",
12914#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012915#ifdef CURSOR_SHAPE
12916 "cursorshape",
12917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012918#ifdef DEBUG
12919 "debug",
12920#endif
12921#ifdef FEAT_CON_DIALOG
12922 "dialog_con",
12923#endif
12924#ifdef FEAT_GUI_DIALOG
12925 "dialog_gui",
12926#endif
12927#ifdef FEAT_DIFF
12928 "diff",
12929#endif
12930#ifdef FEAT_DIGRAPHS
12931 "digraphs",
12932#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012933#ifdef FEAT_DIRECTX
12934 "directx",
12935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936#ifdef FEAT_DND
12937 "dnd",
12938#endif
12939#ifdef FEAT_EMACS_TAGS
12940 "emacs_tags",
12941#endif
12942 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010012943 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012944#ifdef FEAT_SEARCH_EXTRA
12945 "extra_search",
12946#endif
12947#ifdef FEAT_FKMAP
12948 "farsi",
12949#endif
12950#ifdef FEAT_SEARCHPATH
12951 "file_in_path",
12952#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012953#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012954 "filterpipe",
12955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012956#ifdef FEAT_FIND_ID
12957 "find_in_path",
12958#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012959#ifdef FEAT_FLOAT
12960 "float",
12961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012962#ifdef FEAT_FOLDING
12963 "folding",
12964#endif
12965#ifdef FEAT_FOOTER
12966 "footer",
12967#endif
12968#if !defined(USE_SYSTEM) && defined(UNIX)
12969 "fork",
12970#endif
12971#ifdef FEAT_GETTEXT
12972 "gettext",
12973#endif
12974#ifdef FEAT_GUI
12975 "gui",
12976#endif
12977#ifdef FEAT_GUI_ATHENA
12978# ifdef FEAT_GUI_NEXTAW
12979 "gui_neXtaw",
12980# else
12981 "gui_athena",
12982# endif
12983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012984#ifdef FEAT_GUI_GTK
12985 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012986 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012987#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012988#ifdef FEAT_GUI_GNOME
12989 "gui_gnome",
12990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012991#ifdef FEAT_GUI_MAC
12992 "gui_mac",
12993#endif
12994#ifdef FEAT_GUI_MOTIF
12995 "gui_motif",
12996#endif
12997#ifdef FEAT_GUI_PHOTON
12998 "gui_photon",
12999#endif
13000#ifdef FEAT_GUI_W16
13001 "gui_win16",
13002#endif
13003#ifdef FEAT_GUI_W32
13004 "gui_win32",
13005#endif
13006#ifdef FEAT_HANGULIN
13007 "hangul_input",
13008#endif
13009#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13010 "iconv",
13011#endif
13012#ifdef FEAT_INS_EXPAND
13013 "insert_expand",
13014#endif
13015#ifdef FEAT_JUMPLIST
13016 "jumplist",
13017#endif
13018#ifdef FEAT_KEYMAP
13019 "keymap",
13020#endif
13021#ifdef FEAT_LANGMAP
13022 "langmap",
13023#endif
13024#ifdef FEAT_LIBCALL
13025 "libcall",
13026#endif
13027#ifdef FEAT_LINEBREAK
13028 "linebreak",
13029#endif
13030#ifdef FEAT_LISP
13031 "lispindent",
13032#endif
13033#ifdef FEAT_LISTCMDS
13034 "listcmds",
13035#endif
13036#ifdef FEAT_LOCALMAP
13037 "localmap",
13038#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013039#ifdef FEAT_LUA
13040# ifndef DYNAMIC_LUA
13041 "lua",
13042# endif
13043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044#ifdef FEAT_MENU
13045 "menu",
13046#endif
13047#ifdef FEAT_SESSION
13048 "mksession",
13049#endif
13050#ifdef FEAT_MODIFY_FNAME
13051 "modify_fname",
13052#endif
13053#ifdef FEAT_MOUSE
13054 "mouse",
13055#endif
13056#ifdef FEAT_MOUSESHAPE
13057 "mouseshape",
13058#endif
13059#if defined(UNIX) || defined(VMS)
13060# ifdef FEAT_MOUSE_DEC
13061 "mouse_dec",
13062# endif
13063# ifdef FEAT_MOUSE_GPM
13064 "mouse_gpm",
13065# endif
13066# ifdef FEAT_MOUSE_JSB
13067 "mouse_jsbterm",
13068# endif
13069# ifdef FEAT_MOUSE_NET
13070 "mouse_netterm",
13071# endif
13072# ifdef FEAT_MOUSE_PTERM
13073 "mouse_pterm",
13074# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013075# ifdef FEAT_MOUSE_SGR
13076 "mouse_sgr",
13077# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013078# ifdef FEAT_SYSMOUSE
13079 "mouse_sysmouse",
13080# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013081# ifdef FEAT_MOUSE_URXVT
13082 "mouse_urxvt",
13083# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013084# ifdef FEAT_MOUSE_XTERM
13085 "mouse_xterm",
13086# endif
13087#endif
13088#ifdef FEAT_MBYTE
13089 "multi_byte",
13090#endif
13091#ifdef FEAT_MBYTE_IME
13092 "multi_byte_ime",
13093#endif
13094#ifdef FEAT_MULTI_LANG
13095 "multi_lang",
13096#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013097#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013098#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013099 "mzscheme",
13100#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013102#ifdef FEAT_OLE
13103 "ole",
13104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013105#ifdef FEAT_PATH_EXTRA
13106 "path_extra",
13107#endif
13108#ifdef FEAT_PERL
13109#ifndef DYNAMIC_PERL
13110 "perl",
13111#endif
13112#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013113#ifdef FEAT_PERSISTENT_UNDO
13114 "persistent_undo",
13115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013116#ifdef FEAT_PYTHON
13117#ifndef DYNAMIC_PYTHON
13118 "python",
13119#endif
13120#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013121#ifdef FEAT_PYTHON3
13122#ifndef DYNAMIC_PYTHON3
13123 "python3",
13124#endif
13125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013126#ifdef FEAT_POSTSCRIPT
13127 "postscript",
13128#endif
13129#ifdef FEAT_PRINTER
13130 "printer",
13131#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013132#ifdef FEAT_PROFILE
13133 "profile",
13134#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013135#ifdef FEAT_RELTIME
13136 "reltime",
13137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138#ifdef FEAT_QUICKFIX
13139 "quickfix",
13140#endif
13141#ifdef FEAT_RIGHTLEFT
13142 "rightleft",
13143#endif
13144#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13145 "ruby",
13146#endif
13147#ifdef FEAT_SCROLLBIND
13148 "scrollbind",
13149#endif
13150#ifdef FEAT_CMDL_INFO
13151 "showcmd",
13152 "cmdline_info",
13153#endif
13154#ifdef FEAT_SIGNS
13155 "signs",
13156#endif
13157#ifdef FEAT_SMARTINDENT
13158 "smartindent",
13159#endif
13160#ifdef FEAT_SNIFF
13161 "sniff",
13162#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013163#ifdef STARTUPTIME
13164 "startuptime",
13165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166#ifdef FEAT_STL_OPT
13167 "statusline",
13168#endif
13169#ifdef FEAT_SUN_WORKSHOP
13170 "sun_workshop",
13171#endif
13172#ifdef FEAT_NETBEANS_INTG
13173 "netbeans_intg",
13174#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013175#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013176 "spell",
13177#endif
13178#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013179 "syntax",
13180#endif
13181#if defined(USE_SYSTEM) || !defined(UNIX)
13182 "system",
13183#endif
13184#ifdef FEAT_TAG_BINS
13185 "tag_binary",
13186#endif
13187#ifdef FEAT_TAG_OLDSTATIC
13188 "tag_old_static",
13189#endif
13190#ifdef FEAT_TAG_ANYWHITE
13191 "tag_any_white",
13192#endif
13193#ifdef FEAT_TCL
13194# ifndef DYNAMIC_TCL
13195 "tcl",
13196# endif
13197#endif
13198#ifdef TERMINFO
13199 "terminfo",
13200#endif
13201#ifdef FEAT_TERMRESPONSE
13202 "termresponse",
13203#endif
13204#ifdef FEAT_TEXTOBJ
13205 "textobjects",
13206#endif
13207#ifdef HAVE_TGETENT
13208 "tgetent",
13209#endif
13210#ifdef FEAT_TITLE
13211 "title",
13212#endif
13213#ifdef FEAT_TOOLBAR
13214 "toolbar",
13215#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013216#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13217 "unnamedplus",
13218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219#ifdef FEAT_USR_CMDS
13220 "user-commands", /* was accidentally included in 5.4 */
13221 "user_commands",
13222#endif
13223#ifdef FEAT_VIMINFO
13224 "viminfo",
13225#endif
13226#ifdef FEAT_VERTSPLIT
13227 "vertsplit",
13228#endif
13229#ifdef FEAT_VIRTUALEDIT
13230 "virtualedit",
13231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013232 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233#ifdef FEAT_VISUALEXTRA
13234 "visualextra",
13235#endif
13236#ifdef FEAT_VREPLACE
13237 "vreplace",
13238#endif
13239#ifdef FEAT_WILDIGN
13240 "wildignore",
13241#endif
13242#ifdef FEAT_WILDMENU
13243 "wildmenu",
13244#endif
13245#ifdef FEAT_WINDOWS
13246 "windows",
13247#endif
13248#ifdef FEAT_WAK
13249 "winaltkeys",
13250#endif
13251#ifdef FEAT_WRITEBACKUP
13252 "writebackup",
13253#endif
13254#ifdef FEAT_XIM
13255 "xim",
13256#endif
13257#ifdef FEAT_XFONTSET
13258 "xfontset",
13259#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013260#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013261 "xpm",
13262 "xpm_w32", /* for backward compatibility */
13263#else
13264# if defined(HAVE_XPM)
13265 "xpm",
13266# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013268#ifdef USE_XSMP
13269 "xsmp",
13270#endif
13271#ifdef USE_XSMP_INTERACT
13272 "xsmp_interact",
13273#endif
13274#ifdef FEAT_XCLIPBOARD
13275 "xterm_clipboard",
13276#endif
13277#ifdef FEAT_XTERM_SAVE
13278 "xterm_save",
13279#endif
13280#if defined(UNIX) && defined(FEAT_X11)
13281 "X11",
13282#endif
13283 NULL
13284 };
13285
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013286 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287 for (i = 0; has_list[i] != NULL; ++i)
13288 if (STRICMP(name, has_list[i]) == 0)
13289 {
13290 n = TRUE;
13291 break;
13292 }
13293
13294 if (n == FALSE)
13295 {
13296 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013297 {
13298 if (name[5] == '-'
13299 && STRLEN(name) > 11
13300 && vim_isdigit(name[6])
13301 && vim_isdigit(name[8])
13302 && vim_isdigit(name[10]))
13303 {
13304 int major = atoi((char *)name + 6);
13305 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013306
13307 /* Expect "patch-9.9.01234". */
13308 n = (major < VIM_VERSION_MAJOR
13309 || (major == VIM_VERSION_MAJOR
13310 && (minor < VIM_VERSION_MINOR
13311 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013312 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013313 }
13314 else
13315 n = has_patch(atoi((char *)name + 5));
13316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317 else if (STRICMP(name, "vim_starting") == 0)
13318 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013319#ifdef FEAT_MBYTE
13320 else if (STRICMP(name, "multi_byte_encoding") == 0)
13321 n = has_mbyte;
13322#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013323#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13324 else if (STRICMP(name, "balloon_multiline") == 0)
13325 n = multiline_balloon_available();
13326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013327#ifdef DYNAMIC_TCL
13328 else if (STRICMP(name, "tcl") == 0)
13329 n = tcl_enabled(FALSE);
13330#endif
13331#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13332 else if (STRICMP(name, "iconv") == 0)
13333 n = iconv_enabled(FALSE);
13334#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013335#ifdef DYNAMIC_LUA
13336 else if (STRICMP(name, "lua") == 0)
13337 n = lua_enabled(FALSE);
13338#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013339#ifdef DYNAMIC_MZSCHEME
13340 else if (STRICMP(name, "mzscheme") == 0)
13341 n = mzscheme_enabled(FALSE);
13342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343#ifdef DYNAMIC_RUBY
13344 else if (STRICMP(name, "ruby") == 0)
13345 n = ruby_enabled(FALSE);
13346#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013347#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013348#ifdef DYNAMIC_PYTHON
13349 else if (STRICMP(name, "python") == 0)
13350 n = python_enabled(FALSE);
13351#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013352#endif
13353#ifdef FEAT_PYTHON3
13354#ifdef DYNAMIC_PYTHON3
13355 else if (STRICMP(name, "python3") == 0)
13356 n = python3_enabled(FALSE);
13357#endif
13358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013359#ifdef DYNAMIC_PERL
13360 else if (STRICMP(name, "perl") == 0)
13361 n = perl_enabled(FALSE);
13362#endif
13363#ifdef FEAT_GUI
13364 else if (STRICMP(name, "gui_running") == 0)
13365 n = (gui.in_use || gui.starting);
13366# ifdef FEAT_GUI_W32
13367 else if (STRICMP(name, "gui_win32s") == 0)
13368 n = gui_is_win32s();
13369# endif
13370# ifdef FEAT_BROWSE
13371 else if (STRICMP(name, "browse") == 0)
13372 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13373# endif
13374#endif
13375#ifdef FEAT_SYN_HL
13376 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013377 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013378#endif
13379#if defined(WIN3264)
13380 else if (STRICMP(name, "win95") == 0)
13381 n = mch_windows95();
13382#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013383#ifdef FEAT_NETBEANS_INTG
13384 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013385 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387 }
13388
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013389 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390}
13391
13392/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013393 * "has_key()" function
13394 */
13395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013396f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013397{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013398 if (argvars[0].v_type != VAR_DICT)
13399 {
13400 EMSG(_(e_dictreq));
13401 return;
13402 }
13403 if (argvars[0].vval.v_dict == NULL)
13404 return;
13405
13406 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013407 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013408}
13409
13410/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013411 * "haslocaldir()" function
13412 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013414f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013415{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013416 win_T *wp = NULL;
13417
13418 wp = find_tabwin(&argvars[0], &argvars[1]);
13419 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013420}
13421
13422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423 * "hasmapto()" function
13424 */
13425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013426f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013427{
13428 char_u *name;
13429 char_u *mode;
13430 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013431 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013433 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013434 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435 mode = (char_u *)"nvo";
13436 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013437 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013438 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013439 if (argvars[2].v_type != VAR_UNKNOWN)
13440 abbr = get_tv_number(&argvars[2]);
13441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442
Bram Moolenaar2c932302006-03-18 21:42:09 +000013443 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013444 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013445 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013446 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013447}
13448
13449/*
13450 * "histadd()" function
13451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013453f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013454{
13455#ifdef FEAT_CMDHIST
13456 int histype;
13457 char_u *str;
13458 char_u buf[NUMBUFLEN];
13459#endif
13460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013461 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462 if (check_restricted() || check_secure())
13463 return;
13464#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013465 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13466 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467 if (histype >= 0)
13468 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013469 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470 if (*str != NUL)
13471 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013472 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013473 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013474 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475 return;
13476 }
13477 }
13478#endif
13479}
13480
13481/*
13482 * "histdel()" function
13483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013485f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486{
13487#ifdef FEAT_CMDHIST
13488 int n;
13489 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013490 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013492 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13493 if (str == NULL)
13494 n = 0;
13495 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013497 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013498 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013500 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013501 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502 else
13503 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013504 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013505 get_tv_string_buf(&argvars[1], buf));
13506 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013507#endif
13508}
13509
13510/*
13511 * "histget()" function
13512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013514f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515{
13516#ifdef FEAT_CMDHIST
13517 int type;
13518 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013519 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013521 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13522 if (str == NULL)
13523 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013525 {
13526 type = get_histtype(str);
13527 if (argvars[1].v_type == VAR_UNKNOWN)
13528 idx = get_history_idx(type);
13529 else
13530 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13531 /* -1 on type error */
13532 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013535 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013537 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538}
13539
13540/*
13541 * "histnr()" function
13542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013544f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545{
13546 int i;
13547
13548#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013549 char_u *history = get_tv_string_chk(&argvars[0]);
13550
13551 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013552 if (i >= HIST_CMD && i < HIST_COUNT)
13553 i = get_history_idx(i);
13554 else
13555#endif
13556 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013557 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013558}
13559
13560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013561 * "highlightID(name)" function
13562 */
13563 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013564f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013566 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013567}
13568
13569/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013570 * "highlight_exists()" function
13571 */
13572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013573f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013574{
13575 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13576}
13577
13578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579 * "hostname()" function
13580 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013582f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583{
13584 char_u hostname[256];
13585
13586 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013587 rettv->v_type = VAR_STRING;
13588 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589}
13590
13591/*
13592 * iconv() function
13593 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013595f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596{
13597#ifdef FEAT_MBYTE
13598 char_u buf1[NUMBUFLEN];
13599 char_u buf2[NUMBUFLEN];
13600 char_u *from, *to, *str;
13601 vimconv_T vimconv;
13602#endif
13603
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013604 rettv->v_type = VAR_STRING;
13605 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606
13607#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013608 str = get_tv_string(&argvars[0]);
13609 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13610 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013611 vimconv.vc_type = CONV_NONE;
13612 convert_setup(&vimconv, from, to);
13613
13614 /* If the encodings are equal, no conversion needed. */
13615 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013616 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013618 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619
13620 convert_setup(&vimconv, NULL, NULL);
13621 vim_free(from);
13622 vim_free(to);
13623#endif
13624}
13625
13626/*
13627 * "indent()" function
13628 */
13629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013630f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631{
13632 linenr_T lnum;
13633
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013634 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013636 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013638 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639}
13640
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013641/*
13642 * "index()" function
13643 */
13644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013645f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013646{
Bram Moolenaar33570922005-01-25 22:26:29 +000013647 list_T *l;
13648 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013649 long idx = 0;
13650 int ic = FALSE;
13651
13652 rettv->vval.v_number = -1;
13653 if (argvars[0].v_type != VAR_LIST)
13654 {
13655 EMSG(_(e_listreq));
13656 return;
13657 }
13658 l = argvars[0].vval.v_list;
13659 if (l != NULL)
13660 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013661 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013662 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013663 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013664 int error = FALSE;
13665
Bram Moolenaar758711c2005-02-02 23:11:38 +000013666 /* Start at specified item. Use the cached index that list_find()
13667 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013668 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013669 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013670 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013671 ic = get_tv_number_chk(&argvars[3], &error);
13672 if (error)
13673 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013674 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013675
Bram Moolenaar758711c2005-02-02 23:11:38 +000013676 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013677 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013678 {
13679 rettv->vval.v_number = idx;
13680 break;
13681 }
13682 }
13683}
13684
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685static int inputsecret_flag = 0;
13686
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013687static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013688
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013690 * This function is used by f_input() and f_inputdialog() functions. The third
13691 * argument to f_input() specifies the type of completion to use at the
13692 * prompt. The third argument to f_inputdialog() specifies the value to return
13693 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694 */
13695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013696get_user_input(
13697 typval_T *argvars,
13698 typval_T *rettv,
13699 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013701 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702 char_u *p = NULL;
13703 int c;
13704 char_u buf[NUMBUFLEN];
13705 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013706 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013707 int xp_type = EXPAND_NOTHING;
13708 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013710 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013711 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013712
13713#ifdef NO_CONSOLE_INPUT
13714 /* While starting up, there is no place to enter text. */
13715 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717#endif
13718
13719 cmd_silent = FALSE; /* Want to see the prompt. */
13720 if (prompt != NULL)
13721 {
13722 /* Only the part of the message after the last NL is considered as
13723 * prompt for the command line */
13724 p = vim_strrchr(prompt, '\n');
13725 if (p == NULL)
13726 p = prompt;
13727 else
13728 {
13729 ++p;
13730 c = *p;
13731 *p = NUL;
13732 msg_start();
13733 msg_clr_eos();
13734 msg_puts_attr(prompt, echo_attr);
13735 msg_didout = FALSE;
13736 msg_starthere();
13737 *p = c;
13738 }
13739 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013741 if (argvars[1].v_type != VAR_UNKNOWN)
13742 {
13743 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13744 if (defstr != NULL)
13745 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013746
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013747 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013748 {
13749 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013750 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013751 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013752
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013753 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013754 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013755
Bram Moolenaar4463f292005-09-25 22:20:24 +000013756 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13757 if (xp_name == NULL)
13758 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013759
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013760 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013761
Bram Moolenaar4463f292005-09-25 22:20:24 +000013762 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13763 &xp_arg) == FAIL)
13764 return;
13765 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013766 }
13767
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013768 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013769 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013770 int save_ex_normal_busy = ex_normal_busy;
13771 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013772 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013773 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13774 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013775 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013776 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013777 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013778 && argvars[1].v_type != VAR_UNKNOWN
13779 && argvars[2].v_type != VAR_UNKNOWN)
13780 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13781 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013782
13783 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013784
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013785 /* since the user typed this, no need to wait for return */
13786 need_wait_return = FALSE;
13787 msg_didout = FALSE;
13788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789 cmd_silent = cmd_silent_save;
13790}
13791
13792/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013793 * "input()" function
13794 * Also handles inputsecret() when inputsecret is set.
13795 */
13796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013797f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013798{
13799 get_user_input(argvars, rettv, FALSE);
13800}
13801
13802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013803 * "inputdialog()" function
13804 */
13805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013806f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807{
13808#if defined(FEAT_GUI_TEXTDIALOG)
13809 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13810 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13811 {
13812 char_u *message;
13813 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013814 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013816 message = get_tv_string_chk(&argvars[0]);
13817 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013818 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013819 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820 else
13821 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013822 if (message != NULL && defstr != NULL
13823 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013824 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013825 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013826 else
13827 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013828 if (message != NULL && defstr != NULL
13829 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013830 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013831 rettv->vval.v_string = vim_strsave(
13832 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013834 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013836 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837 }
13838 else
13839#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013840 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841}
13842
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013843/*
13844 * "inputlist()" function
13845 */
13846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013847f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013848{
13849 listitem_T *li;
13850 int selected;
13851 int mouse_used;
13852
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013853#ifdef NO_CONSOLE_INPUT
13854 /* While starting up, there is no place to enter text. */
13855 if (no_console_input())
13856 return;
13857#endif
13858 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13859 {
13860 EMSG2(_(e_listarg), "inputlist()");
13861 return;
13862 }
13863
13864 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013865 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013866 lines_left = Rows; /* avoid more prompt */
13867 msg_scroll = TRUE;
13868 msg_clr_eos();
13869
13870 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13871 {
13872 msg_puts(get_tv_string(&li->li_tv));
13873 msg_putchar('\n');
13874 }
13875
13876 /* Ask for choice. */
13877 selected = prompt_for_number(&mouse_used);
13878 if (mouse_used)
13879 selected -= lines_left;
13880
13881 rettv->vval.v_number = selected;
13882}
13883
13884
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13886
13887/*
13888 * "inputrestore()" function
13889 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013891f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013892{
13893 if (ga_userinput.ga_len > 0)
13894 {
13895 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13897 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013898 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899 }
13900 else if (p_verbose > 1)
13901 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013902 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013903 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904 }
13905}
13906
13907/*
13908 * "inputsave()" function
13909 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013911f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013912{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013913 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013914 if (ga_grow(&ga_userinput, 1) == OK)
13915 {
13916 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13917 + ga_userinput.ga_len);
13918 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013919 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013920 }
13921 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013922 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013923}
13924
13925/*
13926 * "inputsecret()" function
13927 */
13928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013929f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930{
13931 ++cmdline_star;
13932 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013933 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934 --cmdline_star;
13935 --inputsecret_flag;
13936}
13937
13938/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013939 * "insert()" function
13940 */
13941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013942f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013943{
13944 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013945 listitem_T *item;
13946 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013947 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013948
13949 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013950 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013951 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013952 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013953 {
13954 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013955 before = get_tv_number_chk(&argvars[2], &error);
13956 if (error)
13957 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013958
Bram Moolenaar758711c2005-02-02 23:11:38 +000013959 if (before == l->lv_len)
13960 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013961 else
13962 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013963 item = list_find(l, before);
13964 if (item == NULL)
13965 {
13966 EMSGN(_(e_listidx), before);
13967 l = NULL;
13968 }
13969 }
13970 if (l != NULL)
13971 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013972 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013973 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013974 }
13975 }
13976}
13977
13978/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013979 * "invert(expr)" function
13980 */
13981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013982f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013983{
13984 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13985}
13986
13987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988 * "isdirectory()" function
13989 */
13990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013991f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013992{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013993 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994}
13995
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013996/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013997 * "islocked()" function
13998 */
13999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014000f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014001{
14002 lval_T lv;
14003 char_u *end;
14004 dictitem_T *di;
14005
14006 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014007 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14008 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014009 if (end != NULL && lv.ll_name != NULL)
14010 {
14011 if (*end != NUL)
14012 EMSG(_(e_trailing));
14013 else
14014 {
14015 if (lv.ll_tv == NULL)
14016 {
14017 if (check_changedtick(lv.ll_name))
14018 rettv->vval.v_number = 1; /* always locked */
14019 else
14020 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014021 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014022 if (di != NULL)
14023 {
14024 /* Consider a variable locked when:
14025 * 1. the variable itself is locked
14026 * 2. the value of the variable is locked.
14027 * 3. the List or Dict value is locked.
14028 */
14029 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14030 || tv_islocked(&di->di_tv));
14031 }
14032 }
14033 }
14034 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014035 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014036 else if (lv.ll_newkey != NULL)
14037 EMSG2(_(e_dictkey), lv.ll_newkey);
14038 else if (lv.ll_list != NULL)
14039 /* List item. */
14040 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14041 else
14042 /* Dictionary item. */
14043 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14044 }
14045 }
14046
14047 clear_lval(&lv);
14048}
14049
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014050static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014051
14052/*
14053 * Turn a dict into a list:
14054 * "what" == 0: list of keys
14055 * "what" == 1: list of values
14056 * "what" == 2: list of items
14057 */
14058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014059dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014060{
Bram Moolenaar33570922005-01-25 22:26:29 +000014061 list_T *l2;
14062 dictitem_T *di;
14063 hashitem_T *hi;
14064 listitem_T *li;
14065 listitem_T *li2;
14066 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014067 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014068
Bram Moolenaar8c711452005-01-14 21:53:12 +000014069 if (argvars[0].v_type != VAR_DICT)
14070 {
14071 EMSG(_(e_dictreq));
14072 return;
14073 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014074 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014075 return;
14076
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014077 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014078 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014079
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014080 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014081 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014082 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014083 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014084 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014085 --todo;
14086 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014087
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014088 li = listitem_alloc();
14089 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014090 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014091 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014092
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014093 if (what == 0)
14094 {
14095 /* keys() */
14096 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014097 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014098 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14099 }
14100 else if (what == 1)
14101 {
14102 /* values() */
14103 copy_tv(&di->di_tv, &li->li_tv);
14104 }
14105 else
14106 {
14107 /* items() */
14108 l2 = list_alloc();
14109 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014110 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014111 li->li_tv.vval.v_list = l2;
14112 if (l2 == NULL)
14113 break;
14114 ++l2->lv_refcount;
14115
14116 li2 = listitem_alloc();
14117 if (li2 == NULL)
14118 break;
14119 list_append(l2, li2);
14120 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014121 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014122 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14123
14124 li2 = listitem_alloc();
14125 if (li2 == NULL)
14126 break;
14127 list_append(l2, li2);
14128 copy_tv(&di->di_tv, &li2->li_tv);
14129 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014130 }
14131 }
14132}
14133
14134/*
14135 * "items(dict)" function
14136 */
14137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014138f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014139{
14140 dict_list(argvars, rettv, 2);
14141}
14142
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014144 * "join()" function
14145 */
14146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014147f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014148{
14149 garray_T ga;
14150 char_u *sep;
14151
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014152 if (argvars[0].v_type != VAR_LIST)
14153 {
14154 EMSG(_(e_listreq));
14155 return;
14156 }
14157 if (argvars[0].vval.v_list == NULL)
14158 return;
14159 if (argvars[1].v_type == VAR_UNKNOWN)
14160 sep = (char_u *)" ";
14161 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014162 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014163
14164 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014165
14166 if (sep != NULL)
14167 {
14168 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014169 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014170 ga_append(&ga, NUL);
14171 rettv->vval.v_string = (char_u *)ga.ga_data;
14172 }
14173 else
14174 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014175}
14176
14177/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014178 * "jsondecode()" function
14179 */
14180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014181f_jsondecode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014182{
14183 js_read_T reader;
14184
14185 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014186 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014187 reader.js_used = 0;
Bram Moolenaar56ead342016-02-02 18:20:08 +010014188 if (json_decode_all(&reader, rettv) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014189 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014190}
14191
14192/*
14193 * "jsonencode()" function
14194 */
14195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014196f_jsonencode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014197{
14198 rettv->v_type = VAR_STRING;
14199 rettv->vval.v_string = json_encode(&argvars[0]);
14200}
14201
14202/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014203 * "keys()" function
14204 */
14205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014206f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014207{
14208 dict_list(argvars, rettv, 0);
14209}
14210
14211/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014212 * "last_buffer_nr()" function.
14213 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014215f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014216{
14217 int n = 0;
14218 buf_T *buf;
14219
14220 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14221 if (n < buf->b_fnum)
14222 n = buf->b_fnum;
14223
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014224 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014225}
14226
14227/*
14228 * "len()" function
14229 */
14230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014231f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014232{
14233 switch (argvars[0].v_type)
14234 {
14235 case VAR_STRING:
14236 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014237 rettv->vval.v_number = (varnumber_T)STRLEN(
14238 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014239 break;
14240 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014241 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014242 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014243 case VAR_DICT:
14244 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14245 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014246 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014247 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014248 break;
14249 }
14250}
14251
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014252static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014253
14254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014255libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014256{
14257#ifdef FEAT_LIBCALL
14258 char_u *string_in;
14259 char_u **string_result;
14260 int nr_result;
14261#endif
14262
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014263 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014264 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014265 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014266
14267 if (check_restricted() || check_secure())
14268 return;
14269
14270#ifdef FEAT_LIBCALL
14271 /* The first two args must be strings, otherwise its meaningless */
14272 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14273 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014274 string_in = NULL;
14275 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014276 string_in = argvars[2].vval.v_string;
14277 if (type == VAR_NUMBER)
14278 string_result = NULL;
14279 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014280 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014281 if (mch_libcall(argvars[0].vval.v_string,
14282 argvars[1].vval.v_string,
14283 string_in,
14284 argvars[2].vval.v_number,
14285 string_result,
14286 &nr_result) == OK
14287 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014288 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014289 }
14290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291}
14292
14293/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014294 * "libcall()" function
14295 */
14296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014297f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014298{
14299 libcall_common(argvars, rettv, VAR_STRING);
14300}
14301
14302/*
14303 * "libcallnr()" function
14304 */
14305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014306f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014307{
14308 libcall_common(argvars, rettv, VAR_NUMBER);
14309}
14310
14311/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312 * "line(string)" function
14313 */
14314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014315f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014316{
14317 linenr_T lnum = 0;
14318 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014319 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014320
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014321 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014322 if (fp != NULL)
14323 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014324 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014325}
14326
14327/*
14328 * "line2byte(lnum)" function
14329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014331f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014332{
14333#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014334 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014335#else
14336 linenr_T lnum;
14337
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014338 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014340 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014341 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014342 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14343 if (rettv->vval.v_number >= 0)
14344 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345#endif
14346}
14347
14348/*
14349 * "lispindent(lnum)" function
14350 */
14351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014352f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353{
14354#ifdef FEAT_LISP
14355 pos_T pos;
14356 linenr_T lnum;
14357
14358 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014359 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14361 {
14362 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014363 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364 curwin->w_cursor = pos;
14365 }
14366 else
14367#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014368 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369}
14370
14371/*
14372 * "localtime()" function
14373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014375f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014377 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378}
14379
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014380static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014381
14382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014383get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384{
14385 char_u *keys;
14386 char_u *which;
14387 char_u buf[NUMBUFLEN];
14388 char_u *keys_buf = NULL;
14389 char_u *rhs;
14390 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014391 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014392 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014393 mapblock_T *mp;
14394 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395
14396 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014397 rettv->v_type = VAR_STRING;
14398 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014400 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401 if (*keys == NUL)
14402 return;
14403
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014404 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014405 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014406 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014407 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014408 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014409 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014410 if (argvars[3].v_type != VAR_UNKNOWN)
14411 get_dict = get_tv_number(&argvars[3]);
14412 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414 else
14415 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014416 if (which == NULL)
14417 return;
14418
Bram Moolenaar071d4272004-06-13 20:20:40 +000014419 mode = get_map_mode(&which, 0);
14420
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014421 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014422 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014423 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014424
14425 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014427 /* Return a string. */
14428 if (rhs != NULL)
14429 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430
Bram Moolenaarbd743252010-10-20 21:23:33 +020014431 }
14432 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14433 {
14434 /* Return a dictionary. */
14435 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14436 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14437 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014438
Bram Moolenaarbd743252010-10-20 21:23:33 +020014439 dict_add_nr_str(dict, "lhs", 0L, lhs);
14440 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14441 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14442 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14443 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14444 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14445 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014446 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014447 dict_add_nr_str(dict, "mode", 0L, mapmode);
14448
14449 vim_free(lhs);
14450 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451 }
14452}
14453
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014454#ifdef FEAT_FLOAT
14455/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014456 * "log()" function
14457 */
14458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014459f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014460{
14461 float_T f;
14462
14463 rettv->v_type = VAR_FLOAT;
14464 if (get_float_arg(argvars, &f) == OK)
14465 rettv->vval.v_float = log(f);
14466 else
14467 rettv->vval.v_float = 0.0;
14468}
14469
14470/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014471 * "log10()" function
14472 */
14473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014474f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014475{
14476 float_T f;
14477
14478 rettv->v_type = VAR_FLOAT;
14479 if (get_float_arg(argvars, &f) == OK)
14480 rettv->vval.v_float = log10(f);
14481 else
14482 rettv->vval.v_float = 0.0;
14483}
14484#endif
14485
Bram Moolenaar1dced572012-04-05 16:54:08 +020014486#ifdef FEAT_LUA
14487/*
14488 * "luaeval()" function
14489 */
14490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014491f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020014492{
14493 char_u *str;
14494 char_u buf[NUMBUFLEN];
14495
14496 str = get_tv_string_buf(&argvars[0], buf);
14497 do_luaeval(str, argvars + 1, rettv);
14498}
14499#endif
14500
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014502 * "map()" function
14503 */
14504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014505f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014506{
14507 filter_map(argvars, rettv, TRUE);
14508}
14509
14510/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014511 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014512 */
14513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014514f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014515{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014516 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517}
14518
14519/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014520 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014521 */
14522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014523f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014525 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526}
14527
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014528static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529
14530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014531find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014533 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014534 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014535 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536 char_u *pat;
14537 regmatch_T regmatch;
14538 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014539 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540 char_u *save_cpo;
14541 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014542 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014543 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014544 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014545 list_T *l = NULL;
14546 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014547 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014548 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014549
14550 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14551 save_cpo = p_cpo;
14552 p_cpo = (char_u *)"";
14553
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014554 rettv->vval.v_number = -1;
14555 if (type == 3)
14556 {
14557 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014558 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014559 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014560 }
14561 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014563 rettv->v_type = VAR_STRING;
14564 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014566
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014567 if (argvars[0].v_type == VAR_LIST)
14568 {
14569 if ((l = argvars[0].vval.v_list) == NULL)
14570 goto theend;
14571 li = l->lv_first;
14572 }
14573 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014574 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014575 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014576 len = (long)STRLEN(str);
14577 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014578
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014579 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14580 if (pat == NULL)
14581 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014582
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014583 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014585 int error = FALSE;
14586
14587 start = get_tv_number_chk(&argvars[2], &error);
14588 if (error)
14589 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014590 if (l != NULL)
14591 {
14592 li = list_find(l, start);
14593 if (li == NULL)
14594 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014595 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014596 }
14597 else
14598 {
14599 if (start < 0)
14600 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014601 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014602 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014603 /* When "count" argument is there ignore matches before "start",
14604 * otherwise skip part of the string. Differs when pattern is "^"
14605 * or "\<". */
14606 if (argvars[3].v_type != VAR_UNKNOWN)
14607 startcol = start;
14608 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014609 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014610 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014611 len -= start;
14612 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014613 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014614
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014615 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014616 nth = get_tv_number_chk(&argvars[3], &error);
14617 if (error)
14618 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014619 }
14620
14621 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14622 if (regmatch.regprog != NULL)
14623 {
14624 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014625
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014626 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014627 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014628 if (l != NULL)
14629 {
14630 if (li == NULL)
14631 {
14632 match = FALSE;
14633 break;
14634 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014635 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014636 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014637 if (str == NULL)
14638 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014639 }
14640
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014641 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014642
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014643 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014644 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014645 if (l == NULL && !match)
14646 break;
14647
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014648 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014649 if (l != NULL)
14650 {
14651 li = li->li_next;
14652 ++idx;
14653 }
14654 else
14655 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014656#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014657 startcol = (colnr_T)(regmatch.startp[0]
14658 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014659#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014660 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014661#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014662 if (startcol > (colnr_T)len
14663 || str + startcol <= regmatch.startp[0])
14664 {
14665 match = FALSE;
14666 break;
14667 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014668 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014669 }
14670
14671 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014672 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014673 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014674 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014675 int i;
14676
14677 /* return list with matched string and submatches */
14678 for (i = 0; i < NSUBEXP; ++i)
14679 {
14680 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014681 {
14682 if (list_append_string(rettv->vval.v_list,
14683 (char_u *)"", 0) == FAIL)
14684 break;
14685 }
14686 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014687 regmatch.startp[i],
14688 (int)(regmatch.endp[i] - regmatch.startp[i]))
14689 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014690 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014691 }
14692 }
14693 else if (type == 2)
14694 {
14695 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014696 if (l != NULL)
14697 copy_tv(&li->li_tv, rettv);
14698 else
14699 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014700 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014701 }
14702 else if (l != NULL)
14703 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014704 else
14705 {
14706 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014707 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014708 (varnumber_T)(regmatch.startp[0] - str);
14709 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014710 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014711 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014712 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713 }
14714 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014715 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014716 }
14717
14718theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014719 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014720 p_cpo = save_cpo;
14721}
14722
14723/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014724 * "match()" function
14725 */
14726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014727f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014728{
14729 find_some_match(argvars, rettv, 1);
14730}
14731
14732/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014733 * "matchadd()" function
14734 */
14735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014736f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014737{
14738#ifdef FEAT_SEARCH_EXTRA
14739 char_u buf[NUMBUFLEN];
14740 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14741 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14742 int prio = 10; /* default priority */
14743 int id = -1;
14744 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014745 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014746
14747 rettv->vval.v_number = -1;
14748
14749 if (grp == NULL || pat == NULL)
14750 return;
14751 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014752 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014753 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014754 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014755 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014756 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014757 if (argvars[4].v_type != VAR_UNKNOWN)
14758 {
14759 if (argvars[4].v_type != VAR_DICT)
14760 {
14761 EMSG(_(e_dictreq));
14762 return;
14763 }
14764 if (dict_find(argvars[4].vval.v_dict,
14765 (char_u *)"conceal", -1) != NULL)
14766 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14767 (char_u *)"conceal", FALSE);
14768 }
14769 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014770 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014771 if (error == TRUE)
14772 return;
14773 if (id >= 1 && id <= 3)
14774 {
14775 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14776 return;
14777 }
14778
Bram Moolenaar6561d522015-07-21 15:48:27 +020014779 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14780 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014781#endif
14782}
14783
14784/*
14785 * "matchaddpos()" function
14786 */
14787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014788f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020014789{
14790#ifdef FEAT_SEARCH_EXTRA
14791 char_u buf[NUMBUFLEN];
14792 char_u *group;
14793 int prio = 10;
14794 int id = -1;
14795 int error = FALSE;
14796 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014797 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014798
14799 rettv->vval.v_number = -1;
14800
14801 group = get_tv_string_buf_chk(&argvars[0], buf);
14802 if (group == NULL)
14803 return;
14804
14805 if (argvars[1].v_type != VAR_LIST)
14806 {
14807 EMSG2(_(e_listarg), "matchaddpos()");
14808 return;
14809 }
14810 l = argvars[1].vval.v_list;
14811 if (l == NULL)
14812 return;
14813
14814 if (argvars[2].v_type != VAR_UNKNOWN)
14815 {
14816 prio = get_tv_number_chk(&argvars[2], &error);
14817 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014818 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014819 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014820 if (argvars[4].v_type != VAR_UNKNOWN)
14821 {
14822 if (argvars[4].v_type != VAR_DICT)
14823 {
14824 EMSG(_(e_dictreq));
14825 return;
14826 }
14827 if (dict_find(argvars[4].vval.v_dict,
14828 (char_u *)"conceal", -1) != NULL)
14829 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14830 (char_u *)"conceal", FALSE);
14831 }
14832 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014833 }
14834 if (error == TRUE)
14835 return;
14836
14837 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14838 if (id == 1 || id == 2)
14839 {
14840 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14841 return;
14842 }
14843
Bram Moolenaar6561d522015-07-21 15:48:27 +020014844 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14845 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014846#endif
14847}
14848
14849/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014850 * "matcharg()" function
14851 */
14852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014853f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014854{
14855 if (rettv_list_alloc(rettv) == OK)
14856 {
14857#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014858 int id = get_tv_number(&argvars[0]);
14859 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014860
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014861 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014862 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014863 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14864 {
14865 list_append_string(rettv->vval.v_list,
14866 syn_id2name(m->hlg_id), -1);
14867 list_append_string(rettv->vval.v_list, m->pattern, -1);
14868 }
14869 else
14870 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014871 list_append_string(rettv->vval.v_list, NULL, -1);
14872 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014873 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014874 }
14875#endif
14876 }
14877}
14878
14879/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014880 * "matchdelete()" function
14881 */
14882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014883f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014884{
14885#ifdef FEAT_SEARCH_EXTRA
14886 rettv->vval.v_number = match_delete(curwin,
14887 (int)get_tv_number(&argvars[0]), TRUE);
14888#endif
14889}
14890
14891/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014892 * "matchend()" function
14893 */
14894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014895f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014896{
14897 find_some_match(argvars, rettv, 0);
14898}
14899
14900/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014901 * "matchlist()" function
14902 */
14903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014904f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014905{
14906 find_some_match(argvars, rettv, 3);
14907}
14908
14909/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014910 * "matchstr()" function
14911 */
14912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014913f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014914{
14915 find_some_match(argvars, rettv, 2);
14916}
14917
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014918static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014919
14920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014921max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014922{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014923 long n = 0;
14924 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014925 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014926
14927 if (argvars[0].v_type == VAR_LIST)
14928 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014929 list_T *l;
14930 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014931
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014932 l = argvars[0].vval.v_list;
14933 if (l != NULL)
14934 {
14935 li = l->lv_first;
14936 if (li != NULL)
14937 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014938 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014939 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014940 {
14941 li = li->li_next;
14942 if (li == NULL)
14943 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014944 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014945 if (domax ? i > n : i < n)
14946 n = i;
14947 }
14948 }
14949 }
14950 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014951 else if (argvars[0].v_type == VAR_DICT)
14952 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014953 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014954 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014955 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014956 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014957
14958 d = argvars[0].vval.v_dict;
14959 if (d != NULL)
14960 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014961 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014962 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014963 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014964 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014965 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014966 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014967 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014968 if (first)
14969 {
14970 n = i;
14971 first = FALSE;
14972 }
14973 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014974 n = i;
14975 }
14976 }
14977 }
14978 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014979 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014980 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014981 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014982}
14983
14984/*
14985 * "max()" function
14986 */
14987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014988f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014989{
14990 max_min(argvars, rettv, TRUE);
14991}
14992
14993/*
14994 * "min()" function
14995 */
14996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014997f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014998{
14999 max_min(argvars, rettv, FALSE);
15000}
15001
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015002static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015003
15004/*
15005 * Create the directory in which "dir" is located, and higher levels when
15006 * needed.
15007 */
15008 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015009mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015010{
15011 char_u *p;
15012 char_u *updir;
15013 int r = FAIL;
15014
15015 /* Get end of directory name in "dir".
15016 * We're done when it's "/" or "c:/". */
15017 p = gettail_sep(dir);
15018 if (p <= get_past_head(dir))
15019 return OK;
15020
15021 /* If the directory exists we're done. Otherwise: create it.*/
15022 updir = vim_strnsave(dir, (int)(p - dir));
15023 if (updir == NULL)
15024 return FAIL;
15025 if (mch_isdir(updir))
15026 r = OK;
15027 else if (mkdir_recurse(updir, prot) == OK)
15028 r = vim_mkdir_emsg(updir, prot);
15029 vim_free(updir);
15030 return r;
15031}
15032
15033#ifdef vim_mkdir
15034/*
15035 * "mkdir()" function
15036 */
15037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015038f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015039{
15040 char_u *dir;
15041 char_u buf[NUMBUFLEN];
15042 int prot = 0755;
15043
15044 rettv->vval.v_number = FAIL;
15045 if (check_restricted() || check_secure())
15046 return;
15047
15048 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015049 if (*dir == NUL)
15050 rettv->vval.v_number = FAIL;
15051 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015052 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015053 if (*gettail(dir) == NUL)
15054 /* remove trailing slashes */
15055 *gettail_sep(dir) = NUL;
15056
15057 if (argvars[1].v_type != VAR_UNKNOWN)
15058 {
15059 if (argvars[2].v_type != VAR_UNKNOWN)
15060 prot = get_tv_number_chk(&argvars[2], NULL);
15061 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15062 mkdir_recurse(dir, prot);
15063 }
15064 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015065 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015066}
15067#endif
15068
Bram Moolenaar0d660222005-01-07 21:51:51 +000015069/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015070 * "mode()" function
15071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015073f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015074{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015075 char_u buf[3];
15076
15077 buf[1] = NUL;
15078 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015079
Bram Moolenaar071d4272004-06-13 20:20:40 +000015080 if (VIsual_active)
15081 {
15082 if (VIsual_select)
15083 buf[0] = VIsual_mode + 's' - 'v';
15084 else
15085 buf[0] = VIsual_mode;
15086 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015087 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015088 || State == CONFIRM)
15089 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015090 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015091 if (State == ASKMORE)
15092 buf[1] = 'm';
15093 else if (State == CONFIRM)
15094 buf[1] = '?';
15095 }
15096 else if (State == EXTERNCMD)
15097 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015098 else if (State & INSERT)
15099 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015100#ifdef FEAT_VREPLACE
15101 if (State & VREPLACE_FLAG)
15102 {
15103 buf[0] = 'R';
15104 buf[1] = 'v';
15105 }
15106 else
15107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015108 if (State & REPLACE_FLAG)
15109 buf[0] = 'R';
15110 else
15111 buf[0] = 'i';
15112 }
15113 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015114 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015115 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015116 if (exmode_active)
15117 buf[1] = 'v';
15118 }
15119 else if (exmode_active)
15120 {
15121 buf[0] = 'c';
15122 buf[1] = 'e';
15123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015124 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015125 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015126 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015127 if (finish_op)
15128 buf[1] = 'o';
15129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015130
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015131 /* Clear out the minor mode when the argument is not a non-zero number or
15132 * non-empty string. */
15133 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015134 buf[1] = NUL;
15135
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015136 rettv->vval.v_string = vim_strsave(buf);
15137 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138}
15139
Bram Moolenaar429fa852013-04-15 12:27:36 +020015140#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015141/*
15142 * "mzeval()" function
15143 */
15144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015145f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015146{
15147 char_u *str;
15148 char_u buf[NUMBUFLEN];
15149
15150 str = get_tv_string_buf(&argvars[0], buf);
15151 do_mzeval(str, rettv);
15152}
Bram Moolenaar75676462013-01-30 14:55:42 +010015153
15154 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015155mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015156{
15157 typval_T argvars[3];
15158
15159 argvars[0].v_type = VAR_STRING;
15160 argvars[0].vval.v_string = name;
15161 copy_tv(args, &argvars[1]);
15162 argvars[2].v_type = VAR_UNKNOWN;
15163 f_call(argvars, rettv);
15164 clear_tv(&argvars[1]);
15165}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015166#endif
15167
Bram Moolenaar071d4272004-06-13 20:20:40 +000015168/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015169 * "nextnonblank()" function
15170 */
15171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015172f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015173{
15174 linenr_T lnum;
15175
15176 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15177 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015178 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015179 {
15180 lnum = 0;
15181 break;
15182 }
15183 if (*skipwhite(ml_get(lnum)) != NUL)
15184 break;
15185 }
15186 rettv->vval.v_number = lnum;
15187}
15188
15189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015190 * "nr2char()" function
15191 */
15192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015193f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015194{
15195 char_u buf[NUMBUFLEN];
15196
15197#ifdef FEAT_MBYTE
15198 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015199 {
15200 int utf8 = 0;
15201
15202 if (argvars[1].v_type != VAR_UNKNOWN)
15203 utf8 = get_tv_number_chk(&argvars[1], NULL);
15204 if (utf8)
15205 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15206 else
15207 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015209 else
15210#endif
15211 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015212 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015213 buf[1] = NUL;
15214 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015215 rettv->v_type = VAR_STRING;
15216 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015217}
15218
15219/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015220 * "or(expr, expr)" function
15221 */
15222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015223f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015224{
15225 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15226 | get_tv_number_chk(&argvars[1], NULL);
15227}
15228
15229/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015230 * "pathshorten()" function
15231 */
15232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015233f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015234{
15235 char_u *p;
15236
15237 rettv->v_type = VAR_STRING;
15238 p = get_tv_string_chk(&argvars[0]);
15239 if (p == NULL)
15240 rettv->vval.v_string = NULL;
15241 else
15242 {
15243 p = vim_strsave(p);
15244 rettv->vval.v_string = p;
15245 if (p != NULL)
15246 shorten_dir(p);
15247 }
15248}
15249
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015250#ifdef FEAT_PERL
15251/*
15252 * "perleval()" function
15253 */
15254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015255f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015256{
15257 char_u *str;
15258 char_u buf[NUMBUFLEN];
15259
15260 str = get_tv_string_buf(&argvars[0], buf);
15261 do_perleval(str, rettv);
15262}
15263#endif
15264
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015265#ifdef FEAT_FLOAT
15266/*
15267 * "pow()" function
15268 */
15269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015270f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015271{
15272 float_T fx, fy;
15273
15274 rettv->v_type = VAR_FLOAT;
15275 if (get_float_arg(argvars, &fx) == OK
15276 && get_float_arg(&argvars[1], &fy) == OK)
15277 rettv->vval.v_float = pow(fx, fy);
15278 else
15279 rettv->vval.v_float = 0.0;
15280}
15281#endif
15282
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015283/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015284 * "prevnonblank()" function
15285 */
15286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015287f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015288{
15289 linenr_T lnum;
15290
15291 lnum = get_tv_lnum(argvars);
15292 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15293 lnum = 0;
15294 else
15295 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15296 --lnum;
15297 rettv->vval.v_number = lnum;
15298}
15299
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015300/* This dummy va_list is here because:
15301 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15302 * - locally in the function results in a "used before set" warning
15303 * - using va_start() to initialize it gives "function with fixed args" error */
15304static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015305
Bram Moolenaar8c711452005-01-14 21:53:12 +000015306/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015307 * "printf()" function
15308 */
15309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015310f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015311{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015312 char_u buf[NUMBUFLEN];
15313 int len;
15314 char_u *s;
15315 int saved_did_emsg = did_emsg;
15316 char *fmt;
15317
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015318 rettv->v_type = VAR_STRING;
15319 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015320
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015321 /* Get the required length, allocate the buffer and do it for real. */
15322 did_emsg = FALSE;
15323 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15324 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15325 if (!did_emsg)
15326 {
15327 s = alloc(len + 1);
15328 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015329 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015330 rettv->vval.v_string = s;
15331 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015332 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015333 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015334 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015335}
15336
15337/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015338 * "pumvisible()" function
15339 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015341f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015342{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015343#ifdef FEAT_INS_EXPAND
15344 if (pum_visible())
15345 rettv->vval.v_number = 1;
15346#endif
15347}
15348
Bram Moolenaardb913952012-06-29 12:54:53 +020015349#ifdef FEAT_PYTHON3
15350/*
15351 * "py3eval()" function
15352 */
15353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015354f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015355{
15356 char_u *str;
15357 char_u buf[NUMBUFLEN];
15358
15359 str = get_tv_string_buf(&argvars[0], buf);
15360 do_py3eval(str, rettv);
15361}
15362#endif
15363
15364#ifdef FEAT_PYTHON
15365/*
15366 * "pyeval()" function
15367 */
15368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015369f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015370{
15371 char_u *str;
15372 char_u buf[NUMBUFLEN];
15373
15374 str = get_tv_string_buf(&argvars[0], buf);
15375 do_pyeval(str, rettv);
15376}
15377#endif
15378
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015379/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015380 * "range()" function
15381 */
15382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015383f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015384{
15385 long start;
15386 long end;
15387 long stride = 1;
15388 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015389 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015390
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015391 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015392 if (argvars[1].v_type == VAR_UNKNOWN)
15393 {
15394 end = start - 1;
15395 start = 0;
15396 }
15397 else
15398 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015399 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015400 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015401 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015402 }
15403
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015404 if (error)
15405 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015406 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015407 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015408 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015409 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015410 else
15411 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015412 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015413 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015414 if (list_append_number(rettv->vval.v_list,
15415 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015416 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015417 }
15418}
15419
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015420/*
15421 * "readfile()" function
15422 */
15423 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015424f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015425{
15426 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015427 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015428 char_u *fname;
15429 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015430 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15431 int io_size = sizeof(buf);
15432 int readlen; /* size of last fread() */
15433 char_u *prev = NULL; /* previously read bytes, if any */
15434 long prevlen = 0; /* length of data in prev */
15435 long prevsize = 0; /* size of prev buffer */
15436 long maxline = MAXLNUM;
15437 long cnt = 0;
15438 char_u *p; /* position in buf */
15439 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015440
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015441 if (argvars[1].v_type != VAR_UNKNOWN)
15442 {
15443 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15444 binary = TRUE;
15445 if (argvars[2].v_type != VAR_UNKNOWN)
15446 maxline = get_tv_number(&argvars[2]);
15447 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015448
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015449 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015450 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015451
15452 /* Always open the file in binary mode, library functions have a mind of
15453 * their own about CR-LF conversion. */
15454 fname = get_tv_string(&argvars[0]);
15455 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15456 {
15457 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15458 return;
15459 }
15460
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015461 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015462 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015463 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015464
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015465 /* This for loop processes what was read, but is also entered at end
15466 * of file so that either:
15467 * - an incomplete line gets written
15468 * - a "binary" file gets an empty line at the end if it ends in a
15469 * newline. */
15470 for (p = buf, start = buf;
15471 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15472 ++p)
15473 {
15474 if (*p == '\n' || readlen <= 0)
15475 {
15476 listitem_T *li;
15477 char_u *s = NULL;
15478 long_u len = p - start;
15479
15480 /* Finished a line. Remove CRs before NL. */
15481 if (readlen > 0 && !binary)
15482 {
15483 while (len > 0 && start[len - 1] == '\r')
15484 --len;
15485 /* removal may cross back to the "prev" string */
15486 if (len == 0)
15487 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15488 --prevlen;
15489 }
15490 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015491 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015492 else
15493 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015494 /* Change "prev" buffer to be the right size. This way
15495 * the bytes are only copied once, and very long lines are
15496 * allocated only once. */
15497 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015498 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015499 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015500 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015501 prev = NULL; /* the list will own the string */
15502 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015503 }
15504 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015505 if (s == NULL)
15506 {
15507 do_outofmem_msg((long_u) prevlen + len + 1);
15508 failed = TRUE;
15509 break;
15510 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015511
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015512 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015513 {
15514 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015515 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015516 break;
15517 }
15518 li->li_tv.v_type = VAR_STRING;
15519 li->li_tv.v_lock = 0;
15520 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015521 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015522
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015523 start = p + 1; /* step over newline */
15524 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015525 break;
15526 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015527 else if (*p == NUL)
15528 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015529#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015530 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15531 * when finding the BF and check the previous two bytes. */
15532 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015533 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015534 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15535 * + 1, these may be in the "prev" string. */
15536 char_u back1 = p >= buf + 1 ? p[-1]
15537 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15538 char_u back2 = p >= buf + 2 ? p[-2]
15539 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15540 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015541
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015542 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015543 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015544 char_u *dest = p - 2;
15545
15546 /* Usually a BOM is at the beginning of a file, and so at
15547 * the beginning of a line; then we can just step over it.
15548 */
15549 if (start == dest)
15550 start = p + 1;
15551 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015552 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015553 /* have to shuffle buf to close gap */
15554 int adjust_prevlen = 0;
15555
15556 if (dest < buf)
15557 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015558 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015559 dest = buf;
15560 }
15561 if (readlen > p - buf + 1)
15562 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15563 readlen -= 3 - adjust_prevlen;
15564 prevlen -= adjust_prevlen;
15565 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015566 }
15567 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015568 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015569#endif
15570 } /* for */
15571
15572 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15573 break;
15574 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015575 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015576 /* There's part of a line in buf, store it in "prev". */
15577 if (p - start + prevlen >= prevsize)
15578 {
15579 /* need bigger "prev" buffer */
15580 char_u *newprev;
15581
15582 /* A common use case is ordinary text files and "prev" gets a
15583 * fragment of a line, so the first allocation is made
15584 * small, to avoid repeatedly 'allocing' large and
15585 * 'reallocing' small. */
15586 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015587 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015588 else
15589 {
15590 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015591 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015592 prevsize = grow50pc > growmin ? grow50pc : growmin;
15593 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015594 newprev = prev == NULL ? alloc(prevsize)
15595 : vim_realloc(prev, prevsize);
15596 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015597 {
15598 do_outofmem_msg((long_u)prevsize);
15599 failed = TRUE;
15600 break;
15601 }
15602 prev = newprev;
15603 }
15604 /* Add the line part to end of "prev". */
15605 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015606 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015607 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015608 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015609
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015610 /*
15611 * For a negative line count use only the lines at the end of the file,
15612 * free the rest.
15613 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015614 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015615 while (cnt > -maxline)
15616 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015617 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015618 --cnt;
15619 }
15620
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015621 if (failed)
15622 {
15623 list_free(rettv->vval.v_list, TRUE);
15624 /* readfile doc says an empty list is returned on error */
15625 rettv->vval.v_list = list_alloc();
15626 }
15627
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015628 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015629 fclose(fd);
15630}
15631
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015632#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015633static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015634
15635/*
15636 * Convert a List to proftime_T.
15637 * Return FAIL when there is something wrong.
15638 */
15639 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015640list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015641{
15642 long n1, n2;
15643 int error = FALSE;
15644
15645 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15646 || arg->vval.v_list->lv_len != 2)
15647 return FAIL;
15648 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15649 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15650# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015651 tm->HighPart = n1;
15652 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015653# else
15654 tm->tv_sec = n1;
15655 tm->tv_usec = n2;
15656# endif
15657 return error ? FAIL : OK;
15658}
15659#endif /* FEAT_RELTIME */
15660
15661/*
15662 * "reltime()" function
15663 */
15664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015665f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015666{
15667#ifdef FEAT_RELTIME
15668 proftime_T res;
15669 proftime_T start;
15670
15671 if (argvars[0].v_type == VAR_UNKNOWN)
15672 {
15673 /* No arguments: get current time. */
15674 profile_start(&res);
15675 }
15676 else if (argvars[1].v_type == VAR_UNKNOWN)
15677 {
15678 if (list2proftime(&argvars[0], &res) == FAIL)
15679 return;
15680 profile_end(&res);
15681 }
15682 else
15683 {
15684 /* Two arguments: compute the difference. */
15685 if (list2proftime(&argvars[0], &start) == FAIL
15686 || list2proftime(&argvars[1], &res) == FAIL)
15687 return;
15688 profile_sub(&res, &start);
15689 }
15690
15691 if (rettv_list_alloc(rettv) == OK)
15692 {
15693 long n1, n2;
15694
15695# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015696 n1 = res.HighPart;
15697 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015698# else
15699 n1 = res.tv_sec;
15700 n2 = res.tv_usec;
15701# endif
15702 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15703 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15704 }
15705#endif
15706}
15707
15708/*
15709 * "reltimestr()" function
15710 */
15711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015712f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015713{
15714#ifdef FEAT_RELTIME
15715 proftime_T tm;
15716#endif
15717
15718 rettv->v_type = VAR_STRING;
15719 rettv->vval.v_string = NULL;
15720#ifdef FEAT_RELTIME
15721 if (list2proftime(&argvars[0], &tm) == OK)
15722 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15723#endif
15724}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015725
Bram Moolenaar0d660222005-01-07 21:51:51 +000015726#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015727static void make_connection(void);
15728static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015729
15730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015731make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015732{
15733 if (X_DISPLAY == NULL
15734# ifdef FEAT_GUI
15735 && !gui.in_use
15736# endif
15737 )
15738 {
15739 x_force_connect = TRUE;
15740 setup_term_clip();
15741 x_force_connect = FALSE;
15742 }
15743}
15744
15745 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015746check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015747{
15748 make_connection();
15749 if (X_DISPLAY == NULL)
15750 {
15751 EMSG(_("E240: No connection to Vim server"));
15752 return FAIL;
15753 }
15754 return OK;
15755}
15756#endif
15757
15758#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015759static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015760
15761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015762remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015763{
15764 char_u *server_name;
15765 char_u *keys;
15766 char_u *r = NULL;
15767 char_u buf[NUMBUFLEN];
15768# ifdef WIN32
15769 HWND w;
15770# else
15771 Window w;
15772# endif
15773
15774 if (check_restricted() || check_secure())
15775 return;
15776
15777# ifdef FEAT_X11
15778 if (check_connection() == FAIL)
15779 return;
15780# endif
15781
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015782 server_name = get_tv_string_chk(&argvars[0]);
15783 if (server_name == NULL)
15784 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015785 keys = get_tv_string_buf(&argvars[1], buf);
15786# ifdef WIN32
15787 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15788# else
15789 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15790 < 0)
15791# endif
15792 {
15793 if (r != NULL)
15794 EMSG(r); /* sending worked but evaluation failed */
15795 else
15796 EMSG2(_("E241: Unable to send to %s"), server_name);
15797 return;
15798 }
15799
15800 rettv->vval.v_string = r;
15801
15802 if (argvars[2].v_type != VAR_UNKNOWN)
15803 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015804 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015805 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015806 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015807
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015808 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015809 v.di_tv.v_type = VAR_STRING;
15810 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015811 idvar = get_tv_string_chk(&argvars[2]);
15812 if (idvar != NULL)
15813 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015814 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015815 }
15816}
15817#endif
15818
15819/*
15820 * "remote_expr()" function
15821 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015823f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015824{
15825 rettv->v_type = VAR_STRING;
15826 rettv->vval.v_string = NULL;
15827#ifdef FEAT_CLIENTSERVER
15828 remote_common(argvars, rettv, TRUE);
15829#endif
15830}
15831
15832/*
15833 * "remote_foreground()" function
15834 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015836f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015837{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015838#ifdef FEAT_CLIENTSERVER
15839# ifdef WIN32
15840 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015841 {
15842 char_u *server_name = get_tv_string_chk(&argvars[0]);
15843
15844 if (server_name != NULL)
15845 serverForeground(server_name);
15846 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015847# else
15848 /* Send a foreground() expression to the server. */
15849 argvars[1].v_type = VAR_STRING;
15850 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15851 argvars[2].v_type = VAR_UNKNOWN;
15852 remote_common(argvars, rettv, TRUE);
15853 vim_free(argvars[1].vval.v_string);
15854# endif
15855#endif
15856}
15857
Bram Moolenaar0d660222005-01-07 21:51:51 +000015858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015859f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015860{
15861#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015862 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015863 char_u *s = NULL;
15864# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015865 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015866# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015867 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015868
15869 if (check_restricted() || check_secure())
15870 {
15871 rettv->vval.v_number = -1;
15872 return;
15873 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015874 serverid = get_tv_string_chk(&argvars[0]);
15875 if (serverid == NULL)
15876 {
15877 rettv->vval.v_number = -1;
15878 return; /* type error; errmsg already given */
15879 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015880# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015881 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015882 if (n == 0)
15883 rettv->vval.v_number = -1;
15884 else
15885 {
15886 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15887 rettv->vval.v_number = (s != NULL);
15888 }
15889# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015890 if (check_connection() == FAIL)
15891 return;
15892
15893 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015894 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015895# endif
15896
15897 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15898 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015899 char_u *retvar;
15900
Bram Moolenaar33570922005-01-25 22:26:29 +000015901 v.di_tv.v_type = VAR_STRING;
15902 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015903 retvar = get_tv_string_chk(&argvars[1]);
15904 if (retvar != NULL)
15905 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015906 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015907 }
15908#else
15909 rettv->vval.v_number = -1;
15910#endif
15911}
15912
Bram Moolenaar0d660222005-01-07 21:51:51 +000015913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015914f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015915{
15916 char_u *r = NULL;
15917
15918#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015919 char_u *serverid = get_tv_string_chk(&argvars[0]);
15920
15921 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015922 {
15923# ifdef WIN32
15924 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015925 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015926
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015927 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015928 if (n != 0)
15929 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15930 if (r == NULL)
15931# else
15932 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015933 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015934# endif
15935 EMSG(_("E277: Unable to read a server reply"));
15936 }
15937#endif
15938 rettv->v_type = VAR_STRING;
15939 rettv->vval.v_string = r;
15940}
15941
15942/*
15943 * "remote_send()" function
15944 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015946f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015947{
15948 rettv->v_type = VAR_STRING;
15949 rettv->vval.v_string = NULL;
15950#ifdef FEAT_CLIENTSERVER
15951 remote_common(argvars, rettv, FALSE);
15952#endif
15953}
15954
15955/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015956 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015957 */
15958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015959f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015960{
Bram Moolenaar33570922005-01-25 22:26:29 +000015961 list_T *l;
15962 listitem_T *item, *item2;
15963 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015964 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015965 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015966 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015967 dict_T *d;
15968 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020015969 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015970
Bram Moolenaar8c711452005-01-14 21:53:12 +000015971 if (argvars[0].v_type == VAR_DICT)
15972 {
15973 if (argvars[2].v_type != VAR_UNKNOWN)
15974 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015975 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015976 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015977 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015978 key = get_tv_string_chk(&argvars[1]);
15979 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015980 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015981 di = dict_find(d, key, -1);
15982 if (di == NULL)
15983 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020015984 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
15985 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015986 {
15987 *rettv = di->di_tv;
15988 init_tv(&di->di_tv);
15989 dictitem_remove(d, di);
15990 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015991 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015992 }
15993 }
15994 else if (argvars[0].v_type != VAR_LIST)
15995 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015996 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015997 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015998 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015999 int error = FALSE;
16000
16001 idx = get_tv_number_chk(&argvars[1], &error);
16002 if (error)
16003 ; /* type error: do nothing, errmsg already given */
16004 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016005 EMSGN(_(e_listidx), idx);
16006 else
16007 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016008 if (argvars[2].v_type == VAR_UNKNOWN)
16009 {
16010 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016011 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016012 *rettv = item->li_tv;
16013 vim_free(item);
16014 }
16015 else
16016 {
16017 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016018 end = get_tv_number_chk(&argvars[2], &error);
16019 if (error)
16020 ; /* type error: do nothing */
16021 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016022 EMSGN(_(e_listidx), end);
16023 else
16024 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016025 int cnt = 0;
16026
16027 for (li = item; li != NULL; li = li->li_next)
16028 {
16029 ++cnt;
16030 if (li == item2)
16031 break;
16032 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016033 if (li == NULL) /* didn't find "item2" after "item" */
16034 EMSG(_(e_invrange));
16035 else
16036 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016037 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016038 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016039 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016040 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016041 l->lv_first = item;
16042 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016043 item->li_prev = NULL;
16044 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016045 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016046 }
16047 }
16048 }
16049 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016050 }
16051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016052}
16053
16054/*
16055 * "rename({from}, {to})" function
16056 */
16057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016058f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016059{
16060 char_u buf[NUMBUFLEN];
16061
16062 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016063 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016064 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016065 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16066 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016067}
16068
16069/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016070 * "repeat()" function
16071 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016073f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016074{
16075 char_u *p;
16076 int n;
16077 int slen;
16078 int len;
16079 char_u *r;
16080 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016081
16082 n = get_tv_number(&argvars[1]);
16083 if (argvars[0].v_type == VAR_LIST)
16084 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016085 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016086 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016087 if (list_extend(rettv->vval.v_list,
16088 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016089 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016090 }
16091 else
16092 {
16093 p = get_tv_string(&argvars[0]);
16094 rettv->v_type = VAR_STRING;
16095 rettv->vval.v_string = NULL;
16096
16097 slen = (int)STRLEN(p);
16098 len = slen * n;
16099 if (len <= 0)
16100 return;
16101
16102 r = alloc(len + 1);
16103 if (r != NULL)
16104 {
16105 for (i = 0; i < n; i++)
16106 mch_memmove(r + i * slen, p, (size_t)slen);
16107 r[len] = NUL;
16108 }
16109
16110 rettv->vval.v_string = r;
16111 }
16112}
16113
16114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016115 * "resolve()" function
16116 */
16117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016118f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119{
16120 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016121#ifdef HAVE_READLINK
16122 char_u *buf = NULL;
16123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016124
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016125 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016126#ifdef FEAT_SHORTCUT
16127 {
16128 char_u *v = NULL;
16129
16130 v = mch_resolve_shortcut(p);
16131 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016132 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016133 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016134 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016135 }
16136#else
16137# ifdef HAVE_READLINK
16138 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016139 char_u *cpy;
16140 int len;
16141 char_u *remain = NULL;
16142 char_u *q;
16143 int is_relative_to_current = FALSE;
16144 int has_trailing_pathsep = FALSE;
16145 int limit = 100;
16146
16147 p = vim_strsave(p);
16148
16149 if (p[0] == '.' && (vim_ispathsep(p[1])
16150 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16151 is_relative_to_current = TRUE;
16152
16153 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016154 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016155 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016156 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016157 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016159
16160 q = getnextcomp(p);
16161 if (*q != NUL)
16162 {
16163 /* Separate the first path component in "p", and keep the
16164 * remainder (beginning with the path separator). */
16165 remain = vim_strsave(q - 1);
16166 q[-1] = NUL;
16167 }
16168
Bram Moolenaard9462e32011-04-11 21:35:11 +020016169 buf = alloc(MAXPATHL + 1);
16170 if (buf == NULL)
16171 goto fail;
16172
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173 for (;;)
16174 {
16175 for (;;)
16176 {
16177 len = readlink((char *)p, (char *)buf, MAXPATHL);
16178 if (len <= 0)
16179 break;
16180 buf[len] = NUL;
16181
16182 if (limit-- == 0)
16183 {
16184 vim_free(p);
16185 vim_free(remain);
16186 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016187 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016188 goto fail;
16189 }
16190
16191 /* Ensure that the result will have a trailing path separator
16192 * if the argument has one. */
16193 if (remain == NULL && has_trailing_pathsep)
16194 add_pathsep(buf);
16195
16196 /* Separate the first path component in the link value and
16197 * concatenate the remainders. */
16198 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16199 if (*q != NUL)
16200 {
16201 if (remain == NULL)
16202 remain = vim_strsave(q - 1);
16203 else
16204 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016205 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206 if (cpy != NULL)
16207 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016208 vim_free(remain);
16209 remain = cpy;
16210 }
16211 }
16212 q[-1] = NUL;
16213 }
16214
16215 q = gettail(p);
16216 if (q > p && *q == NUL)
16217 {
16218 /* Ignore trailing path separator. */
16219 q[-1] = NUL;
16220 q = gettail(p);
16221 }
16222 if (q > p && !mch_isFullName(buf))
16223 {
16224 /* symlink is relative to directory of argument */
16225 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16226 if (cpy != NULL)
16227 {
16228 STRCPY(cpy, p);
16229 STRCPY(gettail(cpy), buf);
16230 vim_free(p);
16231 p = cpy;
16232 }
16233 }
16234 else
16235 {
16236 vim_free(p);
16237 p = vim_strsave(buf);
16238 }
16239 }
16240
16241 if (remain == NULL)
16242 break;
16243
16244 /* Append the first path component of "remain" to "p". */
16245 q = getnextcomp(remain + 1);
16246 len = q - remain - (*q != NUL);
16247 cpy = vim_strnsave(p, STRLEN(p) + len);
16248 if (cpy != NULL)
16249 {
16250 STRNCAT(cpy, remain, len);
16251 vim_free(p);
16252 p = cpy;
16253 }
16254 /* Shorten "remain". */
16255 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016256 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016257 else
16258 {
16259 vim_free(remain);
16260 remain = NULL;
16261 }
16262 }
16263
16264 /* If the result is a relative path name, make it explicitly relative to
16265 * the current directory if and only if the argument had this form. */
16266 if (!vim_ispathsep(*p))
16267 {
16268 if (is_relative_to_current
16269 && *p != NUL
16270 && !(p[0] == '.'
16271 && (p[1] == NUL
16272 || vim_ispathsep(p[1])
16273 || (p[1] == '.'
16274 && (p[2] == NUL
16275 || vim_ispathsep(p[2]))))))
16276 {
16277 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016278 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016279 if (cpy != NULL)
16280 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016281 vim_free(p);
16282 p = cpy;
16283 }
16284 }
16285 else if (!is_relative_to_current)
16286 {
16287 /* Strip leading "./". */
16288 q = p;
16289 while (q[0] == '.' && vim_ispathsep(q[1]))
16290 q += 2;
16291 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016292 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016293 }
16294 }
16295
16296 /* Ensure that the result will have no trailing path separator
16297 * if the argument had none. But keep "/" or "//". */
16298 if (!has_trailing_pathsep)
16299 {
16300 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016301 if (after_pathsep(p, q))
16302 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016303 }
16304
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016305 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016306 }
16307# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016308 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016309# endif
16310#endif
16311
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016312 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016313
16314#ifdef HAVE_READLINK
16315fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016316 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016317#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016318 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016319}
16320
16321/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016322 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016323 */
16324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016325f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016326{
Bram Moolenaar33570922005-01-25 22:26:29 +000016327 list_T *l;
16328 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016329
Bram Moolenaar0d660222005-01-07 21:51:51 +000016330 if (argvars[0].v_type != VAR_LIST)
16331 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016332 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016333 && !tv_check_lock(l->lv_lock,
16334 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016335 {
16336 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016337 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016338 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016339 while (li != NULL)
16340 {
16341 ni = li->li_prev;
16342 list_append(l, li);
16343 li = ni;
16344 }
16345 rettv->vval.v_list = l;
16346 rettv->v_type = VAR_LIST;
16347 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016348 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016350}
16351
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016352#define SP_NOMOVE 0x01 /* don't move cursor */
16353#define SP_REPEAT 0x02 /* repeat to find outer pair */
16354#define SP_RETCOUNT 0x04 /* return matchcount */
16355#define SP_SETPCMARK 0x08 /* set previous context mark */
16356#define SP_START 0x10 /* accept match at start position */
16357#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16358#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016359#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016360
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016361static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016362
16363/*
16364 * Get flags for a search function.
16365 * Possibly sets "p_ws".
16366 * Returns BACKWARD, FORWARD or zero (for an error).
16367 */
16368 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016369get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016370{
16371 int dir = FORWARD;
16372 char_u *flags;
16373 char_u nbuf[NUMBUFLEN];
16374 int mask;
16375
16376 if (varp->v_type != VAR_UNKNOWN)
16377 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016378 flags = get_tv_string_buf_chk(varp, nbuf);
16379 if (flags == NULL)
16380 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016381 while (*flags != NUL)
16382 {
16383 switch (*flags)
16384 {
16385 case 'b': dir = BACKWARD; break;
16386 case 'w': p_ws = TRUE; break;
16387 case 'W': p_ws = FALSE; break;
16388 default: mask = 0;
16389 if (flagsp != NULL)
16390 switch (*flags)
16391 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016392 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016393 case 'e': mask = SP_END; break;
16394 case 'm': mask = SP_RETCOUNT; break;
16395 case 'n': mask = SP_NOMOVE; break;
16396 case 'p': mask = SP_SUBPAT; break;
16397 case 'r': mask = SP_REPEAT; break;
16398 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016399 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016400 }
16401 if (mask == 0)
16402 {
16403 EMSG2(_(e_invarg2), flags);
16404 dir = 0;
16405 }
16406 else
16407 *flagsp |= mask;
16408 }
16409 if (dir == 0)
16410 break;
16411 ++flags;
16412 }
16413 }
16414 return dir;
16415}
16416
Bram Moolenaar071d4272004-06-13 20:20:40 +000016417/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016418 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016419 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016420 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016421search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016422{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016423 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016424 char_u *pat;
16425 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016426 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016427 int save_p_ws = p_ws;
16428 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016429 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016430 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016431 proftime_T tm;
16432#ifdef FEAT_RELTIME
16433 long time_limit = 0;
16434#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016435 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016436 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016437
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016438 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016439 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016440 if (dir == 0)
16441 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016442 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016443 if (flags & SP_START)
16444 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016445 if (flags & SP_END)
16446 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016447 if (flags & SP_COLUMN)
16448 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016449
Bram Moolenaar76929292008-01-06 19:07:36 +000016450 /* Optional arguments: line number to stop searching and timeout. */
16451 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016452 {
16453 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16454 if (lnum_stop < 0)
16455 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016456#ifdef FEAT_RELTIME
16457 if (argvars[3].v_type != VAR_UNKNOWN)
16458 {
16459 time_limit = get_tv_number_chk(&argvars[3], NULL);
16460 if (time_limit < 0)
16461 goto theend;
16462 }
16463#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016464 }
16465
Bram Moolenaar76929292008-01-06 19:07:36 +000016466#ifdef FEAT_RELTIME
16467 /* Set the time limit, if there is one. */
16468 profile_setlimit(time_limit, &tm);
16469#endif
16470
Bram Moolenaar231334e2005-07-25 20:46:57 +000016471 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016472 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016473 * Check to make sure only those flags are set.
16474 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16475 * flags cannot be set. Check for that condition also.
16476 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016477 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016478 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016479 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016480 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016481 goto theend;
16482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016483
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016484 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016485 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016486 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016487 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016488 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016489 if (flags & SP_SUBPAT)
16490 retval = subpatnum;
16491 else
16492 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016493 if (flags & SP_SETPCMARK)
16494 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016495 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016496 if (match_pos != NULL)
16497 {
16498 /* Store the match cursor position */
16499 match_pos->lnum = pos.lnum;
16500 match_pos->col = pos.col + 1;
16501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016502 /* "/$" will put the cursor after the end of the line, may need to
16503 * correct that here */
16504 check_cursor();
16505 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016506
16507 /* If 'n' flag is used: restore cursor position. */
16508 if (flags & SP_NOMOVE)
16509 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016510 else
16511 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016512theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016513 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016514
16515 return retval;
16516}
16517
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016518#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016519
16520/*
16521 * round() is not in C90, use ceil() or floor() instead.
16522 */
16523 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010016524vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016525{
16526 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16527}
16528
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016529/*
16530 * "round({float})" function
16531 */
16532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016533f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016534{
16535 float_T f;
16536
16537 rettv->v_type = VAR_FLOAT;
16538 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016539 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016540 else
16541 rettv->vval.v_float = 0.0;
16542}
16543#endif
16544
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016545/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016546 * "screenattr()" function
16547 */
16548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016549f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016550{
16551 int row;
16552 int col;
16553 int c;
16554
16555 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16556 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16557 if (row < 0 || row >= screen_Rows
16558 || col < 0 || col >= screen_Columns)
16559 c = -1;
16560 else
16561 c = ScreenAttrs[LineOffset[row] + col];
16562 rettv->vval.v_number = c;
16563}
16564
16565/*
16566 * "screenchar()" function
16567 */
16568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016569f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016570{
16571 int row;
16572 int col;
16573 int off;
16574 int c;
16575
16576 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16577 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16578 if (row < 0 || row >= screen_Rows
16579 || col < 0 || col >= screen_Columns)
16580 c = -1;
16581 else
16582 {
16583 off = LineOffset[row] + col;
16584#ifdef FEAT_MBYTE
16585 if (enc_utf8 && ScreenLinesUC[off] != 0)
16586 c = ScreenLinesUC[off];
16587 else
16588#endif
16589 c = ScreenLines[off];
16590 }
16591 rettv->vval.v_number = c;
16592}
16593
16594/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016595 * "screencol()" function
16596 *
16597 * First column is 1 to be consistent with virtcol().
16598 */
16599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016600f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016601{
16602 rettv->vval.v_number = screen_screencol() + 1;
16603}
16604
16605/*
16606 * "screenrow()" function
16607 */
16608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016609f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016610{
16611 rettv->vval.v_number = screen_screenrow() + 1;
16612}
16613
16614/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016615 * "search()" function
16616 */
16617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016618f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016619{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016620 int flags = 0;
16621
16622 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016623}
16624
Bram Moolenaar071d4272004-06-13 20:20:40 +000016625/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016626 * "searchdecl()" function
16627 */
16628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016629f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016630{
16631 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016632 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016633 int error = FALSE;
16634 char_u *name;
16635
16636 rettv->vval.v_number = 1; /* default: FAIL */
16637
16638 name = get_tv_string_chk(&argvars[0]);
16639 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016640 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016641 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016642 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16643 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16644 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016645 if (!error && name != NULL)
16646 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016647 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016648}
16649
16650/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016651 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016653 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016654searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016655{
16656 char_u *spat, *mpat, *epat;
16657 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016658 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016659 int dir;
16660 int flags = 0;
16661 char_u nbuf1[NUMBUFLEN];
16662 char_u nbuf2[NUMBUFLEN];
16663 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016664 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016665 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016666 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016667
Bram Moolenaar071d4272004-06-13 20:20:40 +000016668 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016669 spat = get_tv_string_chk(&argvars[0]);
16670 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16671 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16672 if (spat == NULL || mpat == NULL || epat == NULL)
16673 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016674
Bram Moolenaar071d4272004-06-13 20:20:40 +000016675 /* Handle the optional fourth argument: flags */
16676 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016677 if (dir == 0)
16678 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016679
16680 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016681 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16682 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016683 if ((flags & (SP_END | SP_SUBPAT)) != 0
16684 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016685 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016686 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016687 goto theend;
16688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016689
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016690 /* Using 'r' implies 'W', otherwise it doesn't work. */
16691 if (flags & SP_REPEAT)
16692 p_ws = FALSE;
16693
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016694 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016695 if (argvars[3].v_type == VAR_UNKNOWN
16696 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016697 skip = (char_u *)"";
16698 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016699 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016700 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016701 if (argvars[5].v_type != VAR_UNKNOWN)
16702 {
16703 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16704 if (lnum_stop < 0)
16705 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016706#ifdef FEAT_RELTIME
16707 if (argvars[6].v_type != VAR_UNKNOWN)
16708 {
16709 time_limit = get_tv_number_chk(&argvars[6], NULL);
16710 if (time_limit < 0)
16711 goto theend;
16712 }
16713#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016714 }
16715 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016716 if (skip == NULL)
16717 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016719 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016720 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016721
16722theend:
16723 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016724
16725 return retval;
16726}
16727
16728/*
16729 * "searchpair()" function
16730 */
16731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016732f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016733{
16734 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16735}
16736
16737/*
16738 * "searchpairpos()" function
16739 */
16740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016741f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016742{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016743 pos_T match_pos;
16744 int lnum = 0;
16745 int col = 0;
16746
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016747 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016748 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016749
16750 if (searchpair_cmn(argvars, &match_pos) > 0)
16751 {
16752 lnum = match_pos.lnum;
16753 col = match_pos.col;
16754 }
16755
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016756 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16757 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016758}
16759
16760/*
16761 * Search for a start/middle/end thing.
16762 * Used by searchpair(), see its documentation for the details.
16763 * Returns 0 or -1 for no match,
16764 */
16765 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010016766do_searchpair(
16767 char_u *spat, /* start pattern */
16768 char_u *mpat, /* middle pattern */
16769 char_u *epat, /* end pattern */
16770 int dir, /* BACKWARD or FORWARD */
16771 char_u *skip, /* skip expression */
16772 int flags, /* SP_SETPCMARK and other SP_ values */
16773 pos_T *match_pos,
16774 linenr_T lnum_stop, /* stop at this line if not zero */
16775 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016776{
16777 char_u *save_cpo;
16778 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16779 long retval = 0;
16780 pos_T pos;
16781 pos_T firstpos;
16782 pos_T foundpos;
16783 pos_T save_cursor;
16784 pos_T save_pos;
16785 int n;
16786 int r;
16787 int nest = 1;
16788 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016789 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016790 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016791
16792 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16793 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016794 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016795
Bram Moolenaar76929292008-01-06 19:07:36 +000016796#ifdef FEAT_RELTIME
16797 /* Set the time limit, if there is one. */
16798 profile_setlimit(time_limit, &tm);
16799#endif
16800
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016801 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16802 * start/middle/end (pat3, for the top pair). */
16803 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16804 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16805 if (pat2 == NULL || pat3 == NULL)
16806 goto theend;
16807 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16808 if (*mpat == NUL)
16809 STRCPY(pat3, pat2);
16810 else
16811 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16812 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016813 if (flags & SP_START)
16814 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016815
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816 save_cursor = curwin->w_cursor;
16817 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016818 clearpos(&firstpos);
16819 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016820 pat = pat3;
16821 for (;;)
16822 {
16823 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016824 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16826 /* didn't find it or found the first match again: FAIL */
16827 break;
16828
16829 if (firstpos.lnum == 0)
16830 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016831 if (equalpos(pos, foundpos))
16832 {
16833 /* Found the same position again. Can happen with a pattern that
16834 * has "\zs" at the end and searching backwards. Advance one
16835 * character and try again. */
16836 if (dir == BACKWARD)
16837 decl(&pos);
16838 else
16839 incl(&pos);
16840 }
16841 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016842
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016843 /* clear the start flag to avoid getting stuck here */
16844 options &= ~SEARCH_START;
16845
Bram Moolenaar071d4272004-06-13 20:20:40 +000016846 /* If the skip pattern matches, ignore this match. */
16847 if (*skip != NUL)
16848 {
16849 save_pos = curwin->w_cursor;
16850 curwin->w_cursor = pos;
16851 r = eval_to_bool(skip, &err, NULL, FALSE);
16852 curwin->w_cursor = save_pos;
16853 if (err)
16854 {
16855 /* Evaluating {skip} caused an error, break here. */
16856 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016857 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016858 break;
16859 }
16860 if (r)
16861 continue;
16862 }
16863
16864 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16865 {
16866 /* Found end when searching backwards or start when searching
16867 * forward: nested pair. */
16868 ++nest;
16869 pat = pat2; /* nested, don't search for middle */
16870 }
16871 else
16872 {
16873 /* Found end when searching forward or start when searching
16874 * backward: end of (nested) pair; or found middle in outer pair. */
16875 if (--nest == 1)
16876 pat = pat3; /* outer level, search for middle */
16877 }
16878
16879 if (nest == 0)
16880 {
16881 /* Found the match: return matchcount or line number. */
16882 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016883 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016885 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016886 if (flags & SP_SETPCMARK)
16887 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016888 curwin->w_cursor = pos;
16889 if (!(flags & SP_REPEAT))
16890 break;
16891 nest = 1; /* search for next unmatched */
16892 }
16893 }
16894
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016895 if (match_pos != NULL)
16896 {
16897 /* Store the match cursor position */
16898 match_pos->lnum = curwin->w_cursor.lnum;
16899 match_pos->col = curwin->w_cursor.col + 1;
16900 }
16901
Bram Moolenaar071d4272004-06-13 20:20:40 +000016902 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016903 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016904 curwin->w_cursor = save_cursor;
16905
16906theend:
16907 vim_free(pat2);
16908 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016909 if (p_cpo == empty_option)
16910 p_cpo = save_cpo;
16911 else
16912 /* Darn, evaluating the {skip} expression changed the value. */
16913 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016914
16915 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016916}
16917
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016918/*
16919 * "searchpos()" function
16920 */
16921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016922f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016923{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016924 pos_T match_pos;
16925 int lnum = 0;
16926 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016927 int n;
16928 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016929
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016930 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016931 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016932
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016933 n = search_cmn(argvars, &match_pos, &flags);
16934 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016935 {
16936 lnum = match_pos.lnum;
16937 col = match_pos.col;
16938 }
16939
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016940 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16941 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016942 if (flags & SP_SUBPAT)
16943 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016944}
16945
Bram Moolenaar0d660222005-01-07 21:51:51 +000016946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016947f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016948{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016949#ifdef FEAT_CLIENTSERVER
16950 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016951 char_u *server = get_tv_string_chk(&argvars[0]);
16952 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016953
Bram Moolenaar0d660222005-01-07 21:51:51 +000016954 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016955 if (server == NULL || reply == NULL)
16956 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016957 if (check_restricted() || check_secure())
16958 return;
16959# ifdef FEAT_X11
16960 if (check_connection() == FAIL)
16961 return;
16962# endif
16963
16964 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016965 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016966 EMSG(_("E258: Unable to send to client"));
16967 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016968 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016969 rettv->vval.v_number = 0;
16970#else
16971 rettv->vval.v_number = -1;
16972#endif
16973}
16974
Bram Moolenaar0d660222005-01-07 21:51:51 +000016975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016976f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016977{
16978 char_u *r = NULL;
16979
16980#ifdef FEAT_CLIENTSERVER
16981# ifdef WIN32
16982 r = serverGetVimNames();
16983# else
16984 make_connection();
16985 if (X_DISPLAY != NULL)
16986 r = serverGetVimNames(X_DISPLAY);
16987# endif
16988#endif
16989 rettv->v_type = VAR_STRING;
16990 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991}
16992
16993/*
16994 * "setbufvar()" function
16995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016997f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016998{
16999 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017002 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003 char_u nbuf[NUMBUFLEN];
17004
17005 if (check_restricted() || check_secure())
17006 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017007 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17008 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017009 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017010 varp = &argvars[2];
17011
17012 if (buf != NULL && varname != NULL && varp != NULL)
17013 {
17014 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016
17017 if (*varname == '&')
17018 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017019 long numval;
17020 char_u *strval;
17021 int error = FALSE;
17022
Bram Moolenaar071d4272004-06-13 20:20:40 +000017023 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017024 numval = get_tv_number_chk(varp, &error);
17025 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017026 if (!error && strval != NULL)
17027 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028 }
17029 else
17030 {
17031 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17032 if (bufvarname != NULL)
17033 {
17034 STRCPY(bufvarname, "b:");
17035 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017036 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017037 vim_free(bufvarname);
17038 }
17039 }
17040
17041 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044}
17045
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017047f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017048{
17049 dict_T *d;
17050 dictitem_T *di;
17051 char_u *csearch;
17052
17053 if (argvars[0].v_type != VAR_DICT)
17054 {
17055 EMSG(_(e_dictreq));
17056 return;
17057 }
17058
17059 if ((d = argvars[0].vval.v_dict) != NULL)
17060 {
17061 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17062 if (csearch != NULL)
17063 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017064#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017065 if (enc_utf8)
17066 {
17067 int pcc[MAX_MCO];
17068 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017069
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017070 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17071 }
17072 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017073#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017074 set_last_csearch(PTR2CHAR(csearch),
17075 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017076 }
17077
17078 di = dict_find(d, (char_u *)"forward", -1);
17079 if (di != NULL)
17080 set_csearch_direction(get_tv_number(&di->di_tv)
17081 ? FORWARD : BACKWARD);
17082
17083 di = dict_find(d, (char_u *)"until", -1);
17084 if (di != NULL)
17085 set_csearch_until(!!get_tv_number(&di->di_tv));
17086 }
17087}
17088
Bram Moolenaar071d4272004-06-13 20:20:40 +000017089/*
17090 * "setcmdpos()" function
17091 */
17092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017093f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017094{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017095 int pos = (int)get_tv_number(&argvars[0]) - 1;
17096
17097 if (pos >= 0)
17098 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017099}
17100
17101/*
17102 * "setline()" function
17103 */
17104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017105f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106{
17107 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017108 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017109 list_T *l = NULL;
17110 listitem_T *li = NULL;
17111 long added = 0;
17112 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017113
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017114 lnum = get_tv_lnum(&argvars[0]);
17115 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017116 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017117 l = argvars[1].vval.v_list;
17118 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017120 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017121 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017122
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017123 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017124 for (;;)
17125 {
17126 if (l != NULL)
17127 {
17128 /* list argument, get next string */
17129 if (li == NULL)
17130 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017131 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017132 li = li->li_next;
17133 }
17134
17135 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017136 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017137 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017138
17139 /* When coming here from Insert mode, sync undo, so that this can be
17140 * undone separately from what was previously inserted. */
17141 if (u_sync_once == 2)
17142 {
17143 u_sync_once = 1; /* notify that u_sync() was called */
17144 u_sync(TRUE);
17145 }
17146
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017147 if (lnum <= curbuf->b_ml.ml_line_count)
17148 {
17149 /* existing line, replace it */
17150 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17151 {
17152 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017153 if (lnum == curwin->w_cursor.lnum)
17154 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017155 rettv->vval.v_number = 0; /* OK */
17156 }
17157 }
17158 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17159 {
17160 /* lnum is one past the last line, append the line */
17161 ++added;
17162 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17163 rettv->vval.v_number = 0; /* OK */
17164 }
17165
17166 if (l == NULL) /* only one string argument */
17167 break;
17168 ++lnum;
17169 }
17170
17171 if (added > 0)
17172 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017173}
17174
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017175static 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 +000017176
Bram Moolenaar071d4272004-06-13 20:20:40 +000017177/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017178 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017179 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017181set_qf_ll_list(
17182 win_T *wp UNUSED,
17183 typval_T *list_arg UNUSED,
17184 typval_T *action_arg UNUSED,
17185 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017186{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017187#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017188 char_u *act;
17189 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017190#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017191
Bram Moolenaar2641f772005-03-25 21:58:17 +000017192 rettv->vval.v_number = -1;
17193
17194#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017195 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017196 EMSG(_(e_listreq));
17197 else
17198 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017199 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017200
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017201 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017202 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017203 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017204 if (act == NULL)
17205 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017206 if (*act == 'a' || *act == 'r')
17207 action = *act;
17208 }
17209
Bram Moolenaar81484f42012-12-05 15:16:47 +010017210 if (l != NULL && set_errorlist(wp, l, action,
17211 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017212 rettv->vval.v_number = 0;
17213 }
17214#endif
17215}
17216
17217/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017218 * "setloclist()" function
17219 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017221f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017222{
17223 win_T *win;
17224
17225 rettv->vval.v_number = -1;
17226
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017227 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017228 if (win != NULL)
17229 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17230}
17231
17232/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017233 * "setmatches()" function
17234 */
17235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017236f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017237{
17238#ifdef FEAT_SEARCH_EXTRA
17239 list_T *l;
17240 listitem_T *li;
17241 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017242 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017243
17244 rettv->vval.v_number = -1;
17245 if (argvars[0].v_type != VAR_LIST)
17246 {
17247 EMSG(_(e_listreq));
17248 return;
17249 }
17250 if ((l = argvars[0].vval.v_list) != NULL)
17251 {
17252
17253 /* To some extent make sure that we are dealing with a list from
17254 * "getmatches()". */
17255 li = l->lv_first;
17256 while (li != NULL)
17257 {
17258 if (li->li_tv.v_type != VAR_DICT
17259 || (d = li->li_tv.vval.v_dict) == NULL)
17260 {
17261 EMSG(_(e_invarg));
17262 return;
17263 }
17264 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017265 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17266 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017267 && dict_find(d, (char_u *)"priority", -1) != NULL
17268 && dict_find(d, (char_u *)"id", -1) != NULL))
17269 {
17270 EMSG(_(e_invarg));
17271 return;
17272 }
17273 li = li->li_next;
17274 }
17275
17276 clear_matches(curwin);
17277 li = l->lv_first;
17278 while (li != NULL)
17279 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017280 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017281 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017282 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017283 char_u *group;
17284 int priority;
17285 int id;
17286 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017287
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017288 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017289 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17290 {
17291 if (s == NULL)
17292 {
17293 s = list_alloc();
17294 if (s == NULL)
17295 return;
17296 }
17297
17298 /* match from matchaddpos() */
17299 for (i = 1; i < 9; i++)
17300 {
17301 sprintf((char *)buf, (char *)"pos%d", i);
17302 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17303 {
17304 if (di->di_tv.v_type != VAR_LIST)
17305 return;
17306
17307 list_append_tv(s, &di->di_tv);
17308 s->lv_refcount++;
17309 }
17310 else
17311 break;
17312 }
17313 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017314
17315 group = get_dict_string(d, (char_u *)"group", FALSE);
17316 priority = (int)get_dict_number(d, (char_u *)"priority");
17317 id = (int)get_dict_number(d, (char_u *)"id");
17318 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17319 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17320 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017321 if (i == 0)
17322 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017323 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017324 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017325 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017326 }
17327 else
17328 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017329 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017330 list_unref(s);
17331 s = NULL;
17332 }
17333
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017334 li = li->li_next;
17335 }
17336 rettv->vval.v_number = 0;
17337 }
17338#endif
17339}
17340
17341/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017342 * "setpos()" function
17343 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017345f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017346{
17347 pos_T pos;
17348 int fnum;
17349 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017350 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017351
Bram Moolenaar08250432008-02-13 11:42:46 +000017352 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017353 name = get_tv_string_chk(argvars);
17354 if (name != NULL)
17355 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017356 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017357 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017358 if (--pos.col < 0)
17359 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017360 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017361 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017362 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017363 if (fnum == curbuf->b_fnum)
17364 {
17365 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017366 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017367 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017368 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017369 curwin->w_set_curswant = FALSE;
17370 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017371 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017372 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017373 }
17374 else
17375 EMSG(_(e_invarg));
17376 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017377 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17378 {
17379 /* set mark */
17380 if (setmark_pos(name[1], &pos, fnum) == OK)
17381 rettv->vval.v_number = 0;
17382 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017383 else
17384 EMSG(_(e_invarg));
17385 }
17386 }
17387}
17388
17389/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017390 * "setqflist()" function
17391 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017393f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017394{
17395 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17396}
17397
17398/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399 * "setreg()" function
17400 */
17401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017402f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403{
17404 int regname;
17405 char_u *strregname;
17406 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017407 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408 int append;
17409 char_u yank_type;
17410 long block_len;
17411
17412 block_len = -1;
17413 yank_type = MAUTO;
17414 append = FALSE;
17415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017416 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017417 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017418
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017419 if (strregname == NULL)
17420 return; /* type error; errmsg already given */
17421 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422 if (regname == 0 || regname == '@')
17423 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017425 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017427 stropt = get_tv_string_chk(&argvars[2]);
17428 if (stropt == NULL)
17429 return; /* type error */
17430 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017431 switch (*stropt)
17432 {
17433 case 'a': case 'A': /* append */
17434 append = TRUE;
17435 break;
17436 case 'v': case 'c': /* character-wise selection */
17437 yank_type = MCHAR;
17438 break;
17439 case 'V': case 'l': /* line-wise selection */
17440 yank_type = MLINE;
17441 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017442 case 'b': case Ctrl_V: /* block-wise selection */
17443 yank_type = MBLOCK;
17444 if (VIM_ISDIGIT(stropt[1]))
17445 {
17446 ++stropt;
17447 block_len = getdigits(&stropt) - 1;
17448 --stropt;
17449 }
17450 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017451 }
17452 }
17453
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017454 if (argvars[1].v_type == VAR_LIST)
17455 {
17456 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017457 char_u **allocval;
17458 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017459 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017460 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017461 int len = argvars[1].vval.v_list->lv_len;
17462 listitem_T *li;
17463
Bram Moolenaar7d647822014-04-05 21:28:56 +020017464 /* First half: use for pointers to result lines; second half: use for
17465 * pointers to allocated copies. */
17466 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017467 if (lstval == NULL)
17468 return;
17469 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017470 allocval = lstval + len + 2;
17471 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017472
17473 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17474 li = li->li_next)
17475 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017476 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017477 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017478 goto free_lstval;
17479 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017480 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017481 /* Need to make a copy, next get_tv_string_buf_chk() will
17482 * overwrite the string. */
17483 strval = vim_strsave(buf);
17484 if (strval == NULL)
17485 goto free_lstval;
17486 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017487 }
17488 *curval++ = strval;
17489 }
17490 *curval++ = NULL;
17491
17492 write_reg_contents_lst(regname, lstval, -1,
17493 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017494free_lstval:
17495 while (curallocval > allocval)
17496 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017497 vim_free(lstval);
17498 }
17499 else
17500 {
17501 strval = get_tv_string_chk(&argvars[1]);
17502 if (strval == NULL)
17503 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017504 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017505 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017506 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017507 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017508}
17509
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017510/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017511 * "settabvar()" function
17512 */
17513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017514f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017515{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017516#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017517 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017518 tabpage_T *tp;
17519#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017520 char_u *varname, *tabvarname;
17521 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017522
17523 rettv->vval.v_number = 0;
17524
17525 if (check_restricted() || check_secure())
17526 return;
17527
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017528#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017529 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017530#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017531 varname = get_tv_string_chk(&argvars[1]);
17532 varp = &argvars[2];
17533
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017534 if (varname != NULL && varp != NULL
17535#ifdef FEAT_WINDOWS
17536 && tp != NULL
17537#endif
17538 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017539 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017540#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017541 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017542 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017543#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017544
17545 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17546 if (tabvarname != NULL)
17547 {
17548 STRCPY(tabvarname, "t:");
17549 STRCPY(tabvarname + 2, varname);
17550 set_var(tabvarname, varp, TRUE);
17551 vim_free(tabvarname);
17552 }
17553
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017554#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017555 /* Restore current tabpage */
17556 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017557 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017558#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017559 }
17560}
17561
17562/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017563 * "settabwinvar()" function
17564 */
17565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017566f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017567{
17568 setwinvar(argvars, rettv, 1);
17569}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017570
17571/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017572 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017575f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017576{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017577 setwinvar(argvars, rettv, 0);
17578}
17579
17580/*
17581 * "setwinvar()" and "settabwinvar()" functions
17582 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017583
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017585setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017586{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587 win_T *win;
17588#ifdef FEAT_WINDOWS
17589 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017590 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017591 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017592#endif
17593 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017594 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017595 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017596 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017597
17598 if (check_restricted() || check_secure())
17599 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017600
17601#ifdef FEAT_WINDOWS
17602 if (off == 1)
17603 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17604 else
17605 tp = curtab;
17606#endif
17607 win = find_win_by_nr(&argvars[off], tp);
17608 varname = get_tv_string_chk(&argvars[off + 1]);
17609 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017610
17611 if (win != NULL && varname != NULL && varp != NULL)
17612 {
17613#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017614 need_switch_win = !(tp == curtab && win == curwin);
17615 if (!need_switch_win
17616 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017619 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017620 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017621 long numval;
17622 char_u *strval;
17623 int error = FALSE;
17624
17625 ++varname;
17626 numval = get_tv_number_chk(varp, &error);
17627 strval = get_tv_string_buf_chk(varp, nbuf);
17628 if (!error && strval != NULL)
17629 set_option_value(varname, numval, strval, OPT_LOCAL);
17630 }
17631 else
17632 {
17633 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17634 if (winvarname != NULL)
17635 {
17636 STRCPY(winvarname, "w:");
17637 STRCPY(winvarname + 2, varname);
17638 set_var(winvarname, varp, TRUE);
17639 vim_free(winvarname);
17640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017641 }
17642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017643#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017644 if (need_switch_win)
17645 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017646#endif
17647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017648}
17649
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017650#ifdef FEAT_CRYPT
17651/*
17652 * "sha256({string})" function
17653 */
17654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017655f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017656{
17657 char_u *p;
17658
17659 p = get_tv_string(&argvars[0]);
17660 rettv->vval.v_string = vim_strsave(
17661 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17662 rettv->v_type = VAR_STRING;
17663}
17664#endif /* FEAT_CRYPT */
17665
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017667 * "shellescape({string})" function
17668 */
17669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017670f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017671{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017672 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017673 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017674 rettv->v_type = VAR_STRING;
17675}
17676
17677/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017678 * shiftwidth() function
17679 */
17680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017681f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017682{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017683 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017684}
17685
17686/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017687 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017688 */
17689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017690f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017692 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017693
Bram Moolenaar0d660222005-01-07 21:51:51 +000017694 p = get_tv_string(&argvars[0]);
17695 rettv->vval.v_string = vim_strsave(p);
17696 simplify_filename(rettv->vval.v_string); /* simplify in place */
17697 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017698}
17699
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017700#ifdef FEAT_FLOAT
17701/*
17702 * "sin()" function
17703 */
17704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017705f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017706{
17707 float_T f;
17708
17709 rettv->v_type = VAR_FLOAT;
17710 if (get_float_arg(argvars, &f) == OK)
17711 rettv->vval.v_float = sin(f);
17712 else
17713 rettv->vval.v_float = 0.0;
17714}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017715
17716/*
17717 * "sinh()" function
17718 */
17719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017720f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017721{
17722 float_T f;
17723
17724 rettv->v_type = VAR_FLOAT;
17725 if (get_float_arg(argvars, &f) == OK)
17726 rettv->vval.v_float = sinh(f);
17727 else
17728 rettv->vval.v_float = 0.0;
17729}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017730#endif
17731
Bram Moolenaar0d660222005-01-07 21:51:51 +000017732static int
17733#ifdef __BORLANDC__
17734 _RTLENTRYF
17735#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017736 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017737static int
17738#ifdef __BORLANDC__
17739 _RTLENTRYF
17740#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017741 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017742
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017743/* struct used in the array that's given to qsort() */
17744typedef struct
17745{
17746 listitem_T *item;
17747 int idx;
17748} sortItem_T;
17749
Bram Moolenaar0d660222005-01-07 21:51:51 +000017750static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017751static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017752static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017753#ifdef FEAT_FLOAT
17754static int item_compare_float;
17755#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017756static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017757static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017758static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017759static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017760static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017761#define ITEM_COMPARE_FAIL 999
17762
Bram Moolenaar071d4272004-06-13 20:20:40 +000017763/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017764 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017766 static int
17767#ifdef __BORLANDC__
17768_RTLENTRYF
17769#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010017770item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017771{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017772 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017773 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017774 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017775 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017776 int res;
17777 char_u numbuf1[NUMBUFLEN];
17778 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017779
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017780 si1 = (sortItem_T *)s1;
17781 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017782 tv1 = &si1->item->li_tv;
17783 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017784
17785 if (item_compare_numbers)
17786 {
17787 long v1 = get_tv_number(tv1);
17788 long v2 = get_tv_number(tv2);
17789
17790 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17791 }
17792
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017793#ifdef FEAT_FLOAT
17794 if (item_compare_float)
17795 {
17796 float_T v1 = get_tv_float(tv1);
17797 float_T v2 = get_tv_float(tv2);
17798
17799 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17800 }
17801#endif
17802
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017803 /* tv2string() puts quotes around a string and allocates memory. Don't do
17804 * that for string variables. Use a single quote when comparing with a
17805 * non-string to do what the docs promise. */
17806 if (tv1->v_type == VAR_STRING)
17807 {
17808 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17809 p1 = (char_u *)"'";
17810 else
17811 p1 = tv1->vval.v_string;
17812 }
17813 else
17814 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17815 if (tv2->v_type == VAR_STRING)
17816 {
17817 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17818 p2 = (char_u *)"'";
17819 else
17820 p2 = tv2->vval.v_string;
17821 }
17822 else
17823 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017824 if (p1 == NULL)
17825 p1 = (char_u *)"";
17826 if (p2 == NULL)
17827 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017828 if (!item_compare_numeric)
17829 {
17830 if (item_compare_ic)
17831 res = STRICMP(p1, p2);
17832 else
17833 res = STRCMP(p1, p2);
17834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017835 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017836 {
17837 double n1, n2;
17838 n1 = strtod((char *)p1, (char **)&p1);
17839 n2 = strtod((char *)p2, (char **)&p2);
17840 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17841 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017842
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017843 /* When the result would be zero, compare the item indexes. Makes the
17844 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017845 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017846 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017847
Bram Moolenaar0d660222005-01-07 21:51:51 +000017848 vim_free(tofree1);
17849 vim_free(tofree2);
17850 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017851}
17852
17853 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017854#ifdef __BORLANDC__
17855_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017856#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010017857item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017858{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017859 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017860 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017861 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017862 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017863 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017864
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017865 /* shortcut after failure in previous call; compare all items equal */
17866 if (item_compare_func_err)
17867 return 0;
17868
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017869 si1 = (sortItem_T *)s1;
17870 si2 = (sortItem_T *)s2;
17871
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017872 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017873 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017874 copy_tv(&si1->item->li_tv, &argv[0]);
17875 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017876
17877 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017878 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017879 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17880 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017881 clear_tv(&argv[0]);
17882 clear_tv(&argv[1]);
17883
17884 if (res == FAIL)
17885 res = ITEM_COMPARE_FAIL;
17886 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017887 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17888 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017889 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017890 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017891
17892 /* When the result would be zero, compare the pointers themselves. Makes
17893 * the sort stable. */
17894 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017895 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017896
Bram Moolenaar0d660222005-01-07 21:51:51 +000017897 return res;
17898}
17899
17900/*
17901 * "sort({list})" function
17902 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017904do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905{
Bram Moolenaar33570922005-01-25 22:26:29 +000017906 list_T *l;
17907 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017908 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017909 long len;
17910 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017911
Bram Moolenaar0d660222005-01-07 21:51:51 +000017912 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017913 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017914 else
17915 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017916 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017917 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020017918 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
17919 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017920 return;
17921 rettv->vval.v_list = l;
17922 rettv->v_type = VAR_LIST;
17923 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017924
Bram Moolenaar0d660222005-01-07 21:51:51 +000017925 len = list_len(l);
17926 if (len <= 1)
17927 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017928
Bram Moolenaar0d660222005-01-07 21:51:51 +000017929 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017930 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017931 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017932#ifdef FEAT_FLOAT
17933 item_compare_float = FALSE;
17934#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017935 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017936 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017937 if (argvars[1].v_type != VAR_UNKNOWN)
17938 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017939 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017940 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017941 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017942 else
17943 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017944 int error = FALSE;
17945
17946 i = get_tv_number_chk(&argvars[1], &error);
17947 if (error)
17948 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017949 if (i == 1)
17950 item_compare_ic = TRUE;
17951 else
17952 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017953 if (item_compare_func != NULL)
17954 {
17955 if (STRCMP(item_compare_func, "n") == 0)
17956 {
17957 item_compare_func = NULL;
17958 item_compare_numeric = TRUE;
17959 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017960 else if (STRCMP(item_compare_func, "N") == 0)
17961 {
17962 item_compare_func = NULL;
17963 item_compare_numbers = TRUE;
17964 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017965#ifdef FEAT_FLOAT
17966 else if (STRCMP(item_compare_func, "f") == 0)
17967 {
17968 item_compare_func = NULL;
17969 item_compare_float = TRUE;
17970 }
17971#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020017972 else if (STRCMP(item_compare_func, "i") == 0)
17973 {
17974 item_compare_func = NULL;
17975 item_compare_ic = TRUE;
17976 }
17977 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017978 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017979
17980 if (argvars[2].v_type != VAR_UNKNOWN)
17981 {
17982 /* optional third argument: {dict} */
17983 if (argvars[2].v_type != VAR_DICT)
17984 {
17985 EMSG(_(e_dictreq));
17986 return;
17987 }
17988 item_compare_selfdict = argvars[2].vval.v_dict;
17989 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017991
Bram Moolenaar0d660222005-01-07 21:51:51 +000017992 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017993 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017994 if (ptrs == NULL)
17995 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996
Bram Moolenaar327aa022014-03-25 18:24:23 +010017997 i = 0;
17998 if (sort)
17999 {
18000 /* sort(): ptrs will be the list to sort */
18001 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018002 {
18003 ptrs[i].item = li;
18004 ptrs[i].idx = i;
18005 ++i;
18006 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018007
18008 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018009 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018010 /* test the compare function */
18011 if (item_compare_func != NULL
18012 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018013 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018014 EMSG(_("E702: Sort compare function failed"));
18015 else
18016 {
18017 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018018 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018019 item_compare_func == NULL ? item_compare : item_compare2);
18020
18021 if (!item_compare_func_err)
18022 {
18023 /* Clear the List and append the items in sorted order. */
18024 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18025 l->lv_len = 0;
18026 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018027 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018028 }
18029 }
18030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018031 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018032 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018033 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018034
18035 /* f_uniq(): ptrs will be a stack of items to remove */
18036 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018037 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018038 item_compare_func_ptr = item_compare_func
18039 ? item_compare2 : item_compare;
18040
18041 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18042 li = li->li_next)
18043 {
18044 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18045 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018046 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018047 if (item_compare_func_err)
18048 {
18049 EMSG(_("E882: Uniq compare function failed"));
18050 break;
18051 }
18052 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018053
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018054 if (!item_compare_func_err)
18055 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018056 while (--i >= 0)
18057 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018058 li = ptrs[i].item->li_next;
18059 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018060 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018061 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018062 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018063 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018064 list_fix_watch(l, li);
18065 listitem_free(li);
18066 l->lv_len--;
18067 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018068 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018069 }
18070
18071 vim_free(ptrs);
18072 }
18073}
18074
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018075/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018076 * "sort({list})" function
18077 */
18078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018079f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018080{
18081 do_sort_uniq(argvars, rettv, TRUE);
18082}
18083
18084/*
18085 * "uniq({list})" function
18086 */
18087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018088f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018089{
18090 do_sort_uniq(argvars, rettv, FALSE);
18091}
18092
18093/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018094 * "soundfold({word})" function
18095 */
18096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018097f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018098{
18099 char_u *s;
18100
18101 rettv->v_type = VAR_STRING;
18102 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018103#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018104 rettv->vval.v_string = eval_soundfold(s);
18105#else
18106 rettv->vval.v_string = vim_strsave(s);
18107#endif
18108}
18109
18110/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018111 * "spellbadword()" function
18112 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018114f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018115{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018116 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018117 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018118 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018119
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018120 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018121 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018122
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018123#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018124 if (argvars[0].v_type == VAR_UNKNOWN)
18125 {
18126 /* Find the start and length of the badly spelled word. */
18127 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18128 if (len != 0)
18129 word = ml_get_cursor();
18130 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018131 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018132 {
18133 char_u *str = get_tv_string_chk(&argvars[0]);
18134 int capcol = -1;
18135
18136 if (str != NULL)
18137 {
18138 /* Check the argument for spelling. */
18139 while (*str != NUL)
18140 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018141 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018142 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018143 {
18144 word = str;
18145 break;
18146 }
18147 str += len;
18148 }
18149 }
18150 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018151#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018152
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018153 list_append_string(rettv->vval.v_list, word, len);
18154 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018155 attr == HLF_SPB ? "bad" :
18156 attr == HLF_SPR ? "rare" :
18157 attr == HLF_SPL ? "local" :
18158 attr == HLF_SPC ? "caps" :
18159 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018160}
18161
18162/*
18163 * "spellsuggest()" function
18164 */
18165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018166f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018167{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018168#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018169 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018170 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018171 int maxcount;
18172 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018173 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018174 listitem_T *li;
18175 int need_capital = FALSE;
18176#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018177
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018178 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018179 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018180
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018181#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018182 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018183 {
18184 str = get_tv_string(&argvars[0]);
18185 if (argvars[1].v_type != VAR_UNKNOWN)
18186 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018187 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018188 if (maxcount <= 0)
18189 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018190 if (argvars[2].v_type != VAR_UNKNOWN)
18191 {
18192 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18193 if (typeerr)
18194 return;
18195 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018196 }
18197 else
18198 maxcount = 25;
18199
Bram Moolenaar4770d092006-01-12 23:22:24 +000018200 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018201
18202 for (i = 0; i < ga.ga_len; ++i)
18203 {
18204 str = ((char_u **)ga.ga_data)[i];
18205
18206 li = listitem_alloc();
18207 if (li == NULL)
18208 vim_free(str);
18209 else
18210 {
18211 li->li_tv.v_type = VAR_STRING;
18212 li->li_tv.v_lock = 0;
18213 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018214 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018215 }
18216 }
18217 ga_clear(&ga);
18218 }
18219#endif
18220}
18221
Bram Moolenaar0d660222005-01-07 21:51:51 +000018222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018223f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018224{
18225 char_u *str;
18226 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018227 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018228 regmatch_T regmatch;
18229 char_u patbuf[NUMBUFLEN];
18230 char_u *save_cpo;
18231 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018232 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018233 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018234 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018235
18236 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18237 save_cpo = p_cpo;
18238 p_cpo = (char_u *)"";
18239
18240 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018241 if (argvars[1].v_type != VAR_UNKNOWN)
18242 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018243 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18244 if (pat == NULL)
18245 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018246 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018247 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018248 }
18249 if (pat == NULL || *pat == NUL)
18250 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018251
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018252 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018253 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018254 if (typeerr)
18255 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018256
Bram Moolenaar0d660222005-01-07 21:51:51 +000018257 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18258 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018259 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018260 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018261 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018262 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018263 if (*str == NUL)
18264 match = FALSE; /* empty item at the end */
18265 else
18266 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018267 if (match)
18268 end = regmatch.startp[0];
18269 else
18270 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018271 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18272 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018273 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018274 if (list_append_string(rettv->vval.v_list, str,
18275 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018276 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018277 }
18278 if (!match)
18279 break;
18280 /* Advance to just after the match. */
18281 if (regmatch.endp[0] > str)
18282 col = 0;
18283 else
18284 {
18285 /* Don't get stuck at the same match. */
18286#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018287 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018288#else
18289 col = 1;
18290#endif
18291 }
18292 str = regmatch.endp[0];
18293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018294
Bram Moolenaar473de612013-06-08 18:19:48 +020018295 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018297
Bram Moolenaar0d660222005-01-07 21:51:51 +000018298 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018299}
18300
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018301#ifdef FEAT_FLOAT
18302/*
18303 * "sqrt()" function
18304 */
18305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018306f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018307{
18308 float_T f;
18309
18310 rettv->v_type = VAR_FLOAT;
18311 if (get_float_arg(argvars, &f) == OK)
18312 rettv->vval.v_float = sqrt(f);
18313 else
18314 rettv->vval.v_float = 0.0;
18315}
18316
18317/*
18318 * "str2float()" function
18319 */
18320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018321f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018322{
18323 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18324
18325 if (*p == '+')
18326 p = skipwhite(p + 1);
18327 (void)string2float(p, &rettv->vval.v_float);
18328 rettv->v_type = VAR_FLOAT;
18329}
18330#endif
18331
Bram Moolenaar2c932302006-03-18 21:42:09 +000018332/*
18333 * "str2nr()" function
18334 */
18335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018336f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018337{
18338 int base = 10;
18339 char_u *p;
18340 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018341 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018342
18343 if (argvars[1].v_type != VAR_UNKNOWN)
18344 {
18345 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018346 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018347 {
18348 EMSG(_(e_invarg));
18349 return;
18350 }
18351 }
18352
18353 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018354 if (*p == '+')
18355 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018356 switch (base)
18357 {
18358 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18359 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18360 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18361 default: what = 0;
18362 }
18363 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018364 rettv->vval.v_number = n;
18365}
18366
Bram Moolenaar071d4272004-06-13 20:20:40 +000018367#ifdef HAVE_STRFTIME
18368/*
18369 * "strftime({format}[, {time}])" function
18370 */
18371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018372f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018373{
18374 char_u result_buf[256];
18375 struct tm *curtime;
18376 time_t seconds;
18377 char_u *p;
18378
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018379 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018380
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018381 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018382 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018383 seconds = time(NULL);
18384 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018385 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018386 curtime = localtime(&seconds);
18387 /* MSVC returns NULL for an invalid value of seconds. */
18388 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018389 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018390 else
18391 {
18392# ifdef FEAT_MBYTE
18393 vimconv_T conv;
18394 char_u *enc;
18395
18396 conv.vc_type = CONV_NONE;
18397 enc = enc_locale();
18398 convert_setup(&conv, p_enc, enc);
18399 if (conv.vc_type != CONV_NONE)
18400 p = string_convert(&conv, p, NULL);
18401# endif
18402 if (p != NULL)
18403 (void)strftime((char *)result_buf, sizeof(result_buf),
18404 (char *)p, curtime);
18405 else
18406 result_buf[0] = NUL;
18407
18408# ifdef FEAT_MBYTE
18409 if (conv.vc_type != CONV_NONE)
18410 vim_free(p);
18411 convert_setup(&conv, enc, p_enc);
18412 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018413 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018414 else
18415# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018416 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018417
18418# ifdef FEAT_MBYTE
18419 /* Release conversion descriptors */
18420 convert_setup(&conv, NULL, NULL);
18421 vim_free(enc);
18422# endif
18423 }
18424}
18425#endif
18426
18427/*
18428 * "stridx()" function
18429 */
18430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018431f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018432{
18433 char_u buf[NUMBUFLEN];
18434 char_u *needle;
18435 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018436 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018437 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018438 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018439
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018440 needle = get_tv_string_chk(&argvars[1]);
18441 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018442 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018443 if (needle == NULL || haystack == NULL)
18444 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018445
Bram Moolenaar33570922005-01-25 22:26:29 +000018446 if (argvars[2].v_type != VAR_UNKNOWN)
18447 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018448 int error = FALSE;
18449
18450 start_idx = get_tv_number_chk(&argvars[2], &error);
18451 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018452 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018453 if (start_idx >= 0)
18454 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018455 }
18456
18457 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18458 if (pos != NULL)
18459 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018460}
18461
18462/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018463 * "string()" function
18464 */
18465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018466f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018467{
18468 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018469 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018471 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018472 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018473 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018474 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018475 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018476}
18477
18478/*
18479 * "strlen()" function
18480 */
18481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018482f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018483{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018484 rettv->vval.v_number = (varnumber_T)(STRLEN(
18485 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018486}
18487
18488/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018489 * "strchars()" function
18490 */
18491 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018492f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018493{
18494 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018495 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018496#ifdef FEAT_MBYTE
18497 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018498 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018499#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018500
18501 if (argvars[1].v_type != VAR_UNKNOWN)
18502 skipcc = get_tv_number_chk(&argvars[1], NULL);
18503 if (skipcc < 0 || skipcc > 1)
18504 EMSG(_(e_invarg));
18505 else
18506 {
18507#ifdef FEAT_MBYTE
18508 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18509 while (*s != NUL)
18510 {
18511 func_mb_ptr2char_adv(&s);
18512 ++len;
18513 }
18514 rettv->vval.v_number = len;
18515#else
18516 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18517#endif
18518 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018519}
18520
18521/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018522 * "strdisplaywidth()" function
18523 */
18524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018525f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020018526{
18527 char_u *s = get_tv_string(&argvars[0]);
18528 int col = 0;
18529
18530 if (argvars[1].v_type != VAR_UNKNOWN)
18531 col = get_tv_number(&argvars[1]);
18532
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018533 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018534}
18535
18536/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018537 * "strwidth()" function
18538 */
18539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018540f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018541{
18542 char_u *s = get_tv_string(&argvars[0]);
18543
18544 rettv->vval.v_number = (varnumber_T)(
18545#ifdef FEAT_MBYTE
18546 mb_string2cells(s, -1)
18547#else
18548 STRLEN(s)
18549#endif
18550 );
18551}
18552
18553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018554 * "strpart()" function
18555 */
18556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018557f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018558{
18559 char_u *p;
18560 int n;
18561 int len;
18562 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018563 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018565 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566 slen = (int)STRLEN(p);
18567
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018568 n = get_tv_number_chk(&argvars[1], &error);
18569 if (error)
18570 len = 0;
18571 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018572 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573 else
18574 len = slen - n; /* default len: all bytes that are available. */
18575
18576 /*
18577 * Only return the overlap between the specified part and the actual
18578 * string.
18579 */
18580 if (n < 0)
18581 {
18582 len += n;
18583 n = 0;
18584 }
18585 else if (n > slen)
18586 n = slen;
18587 if (len < 0)
18588 len = 0;
18589 else if (n + len > slen)
18590 len = slen - n;
18591
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018592 rettv->v_type = VAR_STRING;
18593 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018594}
18595
18596/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018597 * "strridx()" function
18598 */
18599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018600f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018601{
18602 char_u buf[NUMBUFLEN];
18603 char_u *needle;
18604 char_u *haystack;
18605 char_u *rest;
18606 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018607 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018608
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018609 needle = get_tv_string_chk(&argvars[1]);
18610 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018611
18612 rettv->vval.v_number = -1;
18613 if (needle == NULL || haystack == NULL)
18614 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018615
18616 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018617 if (argvars[2].v_type != VAR_UNKNOWN)
18618 {
18619 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018620 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018621 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018622 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018623 }
18624 else
18625 end_idx = haystack_len;
18626
Bram Moolenaar0d660222005-01-07 21:51:51 +000018627 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018628 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018629 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018630 lastmatch = haystack + end_idx;
18631 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018632 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018633 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018634 for (rest = haystack; *rest != '\0'; ++rest)
18635 {
18636 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018637 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018638 break;
18639 lastmatch = rest;
18640 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018641 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018642
18643 if (lastmatch == NULL)
18644 rettv->vval.v_number = -1;
18645 else
18646 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18647}
18648
18649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650 * "strtrans()" function
18651 */
18652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018653f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018654{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018655 rettv->v_type = VAR_STRING;
18656 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657}
18658
18659/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018660 * "submatch()" function
18661 */
18662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018663f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018664{
Bram Moolenaar41571762014-04-02 19:00:58 +020018665 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018666 int no;
18667 int retList = 0;
18668
18669 no = (int)get_tv_number_chk(&argvars[0], &error);
18670 if (error)
18671 return;
18672 error = FALSE;
18673 if (argvars[1].v_type != VAR_UNKNOWN)
18674 retList = get_tv_number_chk(&argvars[1], &error);
18675 if (error)
18676 return;
18677
18678 if (retList == 0)
18679 {
18680 rettv->v_type = VAR_STRING;
18681 rettv->vval.v_string = reg_submatch(no);
18682 }
18683 else
18684 {
18685 rettv->v_type = VAR_LIST;
18686 rettv->vval.v_list = reg_submatch_list(no);
18687 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018688}
18689
18690/*
18691 * "substitute()" function
18692 */
18693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018694f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018695{
18696 char_u patbuf[NUMBUFLEN];
18697 char_u subbuf[NUMBUFLEN];
18698 char_u flagsbuf[NUMBUFLEN];
18699
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018700 char_u *str = get_tv_string_chk(&argvars[0]);
18701 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18702 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18703 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18704
Bram Moolenaar0d660222005-01-07 21:51:51 +000018705 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018706 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18707 rettv->vval.v_string = NULL;
18708 else
18709 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018710}
18711
18712/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018713 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018716f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018717{
18718 int id = 0;
18719#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018720 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018721 long col;
18722 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018723 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018724
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018725 lnum = get_tv_lnum(argvars); /* -1 on type error */
18726 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18727 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018729 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018730 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018731 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018732#endif
18733
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018734 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018735}
18736
18737/*
18738 * "synIDattr(id, what [, mode])" function
18739 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018741f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742{
18743 char_u *p = NULL;
18744#ifdef FEAT_SYN_HL
18745 int id;
18746 char_u *what;
18747 char_u *mode;
18748 char_u modebuf[NUMBUFLEN];
18749 int modec;
18750
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018751 id = get_tv_number(&argvars[0]);
18752 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018753 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018755 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018757 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758 modec = 0; /* replace invalid with current */
18759 }
18760 else
18761 {
18762#ifdef FEAT_GUI
18763 if (gui.in_use)
18764 modec = 'g';
18765 else
18766#endif
18767 if (t_colors > 1)
18768 modec = 'c';
18769 else
18770 modec = 't';
18771 }
18772
18773
18774 switch (TOLOWER_ASC(what[0]))
18775 {
18776 case 'b':
18777 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18778 p = highlight_color(id, what, modec);
18779 else /* bold */
18780 p = highlight_has_attr(id, HL_BOLD, modec);
18781 break;
18782
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018783 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018784 p = highlight_color(id, what, modec);
18785 break;
18786
18787 case 'i':
18788 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18789 p = highlight_has_attr(id, HL_INVERSE, modec);
18790 else /* italic */
18791 p = highlight_has_attr(id, HL_ITALIC, modec);
18792 break;
18793
18794 case 'n': /* name */
18795 p = get_highlight_name(NULL, id - 1);
18796 break;
18797
18798 case 'r': /* reverse */
18799 p = highlight_has_attr(id, HL_INVERSE, modec);
18800 break;
18801
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018802 case 's':
18803 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18804 p = highlight_color(id, what, modec);
18805 else /* standout */
18806 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807 break;
18808
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018809 case 'u':
18810 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18811 /* underline */
18812 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18813 else
18814 /* undercurl */
18815 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018816 break;
18817 }
18818
18819 if (p != NULL)
18820 p = vim_strsave(p);
18821#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018822 rettv->v_type = VAR_STRING;
18823 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018824}
18825
18826/*
18827 * "synIDtrans(id)" function
18828 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018830f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018831{
18832 int id;
18833
18834#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018835 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836
18837 if (id > 0)
18838 id = syn_get_final_id(id);
18839 else
18840#endif
18841 id = 0;
18842
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018843 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018844}
18845
18846/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018847 * "synconcealed(lnum, col)" function
18848 */
18849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018850f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018851{
18852#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18853 long lnum;
18854 long col;
18855 int syntax_flags = 0;
18856 int cchar;
18857 int matchid = 0;
18858 char_u str[NUMBUFLEN];
18859#endif
18860
18861 rettv->v_type = VAR_LIST;
18862 rettv->vval.v_list = NULL;
18863
18864#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18865 lnum = get_tv_lnum(argvars); /* -1 on type error */
18866 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18867
18868 vim_memset(str, NUL, sizeof(str));
18869
18870 if (rettv_list_alloc(rettv) != FAIL)
18871 {
18872 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18873 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18874 && curwin->w_p_cole > 0)
18875 {
18876 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18877 syntax_flags = get_syntax_info(&matchid);
18878
18879 /* get the conceal character */
18880 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18881 {
18882 cchar = syn_get_sub_char();
18883 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18884 cchar = lcs_conceal;
18885 if (cchar != NUL)
18886 {
18887# ifdef FEAT_MBYTE
18888 if (has_mbyte)
18889 (*mb_char2bytes)(cchar, str);
18890 else
18891# endif
18892 str[0] = cchar;
18893 }
18894 }
18895 }
18896
18897 list_append_number(rettv->vval.v_list,
18898 (syntax_flags & HL_CONCEAL) != 0);
18899 /* -1 to auto-determine strlen */
18900 list_append_string(rettv->vval.v_list, str, -1);
18901 list_append_number(rettv->vval.v_list, matchid);
18902 }
18903#endif
18904}
18905
18906/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018907 * "synstack(lnum, col)" function
18908 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018910f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018911{
18912#ifdef FEAT_SYN_HL
18913 long lnum;
18914 long col;
18915 int i;
18916 int id;
18917#endif
18918
18919 rettv->v_type = VAR_LIST;
18920 rettv->vval.v_list = NULL;
18921
18922#ifdef FEAT_SYN_HL
18923 lnum = get_tv_lnum(argvars); /* -1 on type error */
18924 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18925
18926 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018927 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018928 && rettv_list_alloc(rettv) != FAIL)
18929 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018930 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018931 for (i = 0; ; ++i)
18932 {
18933 id = syn_get_stack_item(i);
18934 if (id < 0)
18935 break;
18936 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18937 break;
18938 }
18939 }
18940#endif
18941}
18942
Bram Moolenaar071d4272004-06-13 20:20:40 +000018943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018944get_cmd_output_as_rettv(
18945 typval_T *argvars,
18946 typval_T *rettv,
18947 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018949 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018950 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018951 char_u *infile = NULL;
18952 char_u buf[NUMBUFLEN];
18953 int err = FALSE;
18954 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018955 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020018956 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018957
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018958 rettv->v_type = VAR_STRING;
18959 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018960 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018961 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018962
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018963 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018964 {
18965 /*
18966 * Write the string to a temp file, to be used for input of the shell
18967 * command.
18968 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020018969 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018970 {
18971 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018972 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018973 }
18974
18975 fd = mch_fopen((char *)infile, WRITEBIN);
18976 if (fd == NULL)
18977 {
18978 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018979 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018980 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018981 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018982 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018983 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18984 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018985 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018986 else
18987 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018988 size_t len;
18989
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018990 p = get_tv_string_buf_chk(&argvars[1], buf);
18991 if (p == NULL)
18992 {
18993 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018994 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018995 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018996 len = STRLEN(p);
18997 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018998 err = TRUE;
18999 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019000 if (fclose(fd) != 0)
19001 err = TRUE;
19002 if (err)
19003 {
19004 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019005 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019006 }
19007 }
19008
Bram Moolenaar52a72462014-08-29 15:53:52 +020019009 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19010 * echoes typeahead, that messes up the display. */
19011 if (!msg_silent)
19012 flags += SHELL_COOKED;
19013
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019014 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019016 int len;
19017 listitem_T *li;
19018 char_u *s = NULL;
19019 char_u *start;
19020 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019021 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019022
Bram Moolenaar52a72462014-08-29 15:53:52 +020019023 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019024 if (res == NULL)
19025 goto errret;
19026
19027 list = list_alloc();
19028 if (list == NULL)
19029 goto errret;
19030
19031 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019032 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019033 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019034 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019035 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019036 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019037
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019038 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019039 if (s == NULL)
19040 goto errret;
19041
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019042 for (p = s; start < end; ++p, ++start)
19043 *p = *start == NUL ? NL : *start;
19044 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019045
19046 li = listitem_alloc();
19047 if (li == NULL)
19048 {
19049 vim_free(s);
19050 goto errret;
19051 }
19052 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019053 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019054 li->li_tv.vval.v_string = s;
19055 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019056 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019057
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019058 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019059 rettv->v_type = VAR_LIST;
19060 rettv->vval.v_list = list;
19061 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019062 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019063 else
19064 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019065 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019066#ifdef USE_CR
19067 /* translate <CR> into <NL> */
19068 if (res != NULL)
19069 {
19070 char_u *s;
19071
19072 for (s = res; *s; ++s)
19073 {
19074 if (*s == CAR)
19075 *s = NL;
19076 }
19077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019078#else
19079# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019080 /* translate <CR><NL> into <NL> */
19081 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019083 char_u *s, *d;
19084
19085 d = res;
19086 for (s = res; *s; ++s)
19087 {
19088 if (s[0] == CAR && s[1] == NL)
19089 ++s;
19090 *d++ = *s;
19091 }
19092 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019094# endif
19095#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019096 rettv->vval.v_string = res;
19097 res = NULL;
19098 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019099
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019100errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019101 if (infile != NULL)
19102 {
19103 mch_remove(infile);
19104 vim_free(infile);
19105 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019106 if (res != NULL)
19107 vim_free(res);
19108 if (list != NULL)
19109 list_free(list, TRUE);
19110}
19111
19112/*
19113 * "system()" function
19114 */
19115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019116f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019117{
19118 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19119}
19120
19121/*
19122 * "systemlist()" function
19123 */
19124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019125f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019126{
19127 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019128}
19129
19130/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019131 * "tabpagebuflist()" function
19132 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019134f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019135{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019136#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019137 tabpage_T *tp;
19138 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019139
19140 if (argvars[0].v_type == VAR_UNKNOWN)
19141 wp = firstwin;
19142 else
19143 {
19144 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19145 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019146 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019147 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019148 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019149 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019150 for (; wp != NULL; wp = wp->w_next)
19151 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019152 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019153 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019154 }
19155#endif
19156}
19157
19158
19159/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019160 * "tabpagenr()" function
19161 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019163f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019164{
19165 int nr = 1;
19166#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019167 char_u *arg;
19168
19169 if (argvars[0].v_type != VAR_UNKNOWN)
19170 {
19171 arg = get_tv_string_chk(&argvars[0]);
19172 nr = 0;
19173 if (arg != NULL)
19174 {
19175 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019176 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019177 else
19178 EMSG2(_(e_invexpr2), arg);
19179 }
19180 }
19181 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019182 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019183#endif
19184 rettv->vval.v_number = nr;
19185}
19186
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019187
19188#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019189static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019190
19191/*
19192 * Common code for tabpagewinnr() and winnr().
19193 */
19194 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019195get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019196{
19197 win_T *twin;
19198 int nr = 1;
19199 win_T *wp;
19200 char_u *arg;
19201
19202 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19203 if (argvar->v_type != VAR_UNKNOWN)
19204 {
19205 arg = get_tv_string_chk(argvar);
19206 if (arg == NULL)
19207 nr = 0; /* type error; errmsg already given */
19208 else if (STRCMP(arg, "$") == 0)
19209 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19210 else if (STRCMP(arg, "#") == 0)
19211 {
19212 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19213 if (twin == NULL)
19214 nr = 0;
19215 }
19216 else
19217 {
19218 EMSG2(_(e_invexpr2), arg);
19219 nr = 0;
19220 }
19221 }
19222
19223 if (nr > 0)
19224 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19225 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019226 {
19227 if (wp == NULL)
19228 {
19229 /* didn't find it in this tabpage */
19230 nr = 0;
19231 break;
19232 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019233 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019234 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019235 return nr;
19236}
19237#endif
19238
19239/*
19240 * "tabpagewinnr()" function
19241 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019243f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019244{
19245 int nr = 1;
19246#ifdef FEAT_WINDOWS
19247 tabpage_T *tp;
19248
19249 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19250 if (tp == NULL)
19251 nr = 0;
19252 else
19253 nr = get_winnr(tp, &argvars[1]);
19254#endif
19255 rettv->vval.v_number = nr;
19256}
19257
19258
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019259/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019260 * "tagfiles()" function
19261 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019263f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019264{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019265 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019266 tagname_T tn;
19267 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019268
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019269 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019270 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019271 fname = alloc(MAXPATHL);
19272 if (fname == NULL)
19273 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019274
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019275 for (first = TRUE; ; first = FALSE)
19276 if (get_tagfname(&tn, first, fname) == FAIL
19277 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019278 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019279 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019280 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019281}
19282
19283/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019284 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019285 */
19286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019287f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019288{
19289 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019290
19291 tag_pattern = get_tv_string(&argvars[0]);
19292
19293 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019294 if (*tag_pattern == NUL)
19295 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019296
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019297 if (rettv_list_alloc(rettv) == OK)
19298 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019299}
19300
19301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019302 * "tempname()" function
19303 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019305f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019306{
19307 static int x = 'A';
19308
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019309 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019310 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019311
19312 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19313 * names. Skip 'I' and 'O', they are used for shell redirection. */
19314 do
19315 {
19316 if (x == 'Z')
19317 x = '0';
19318 else if (x == '9')
19319 x = 'A';
19320 else
19321 {
19322#ifdef EBCDIC
19323 if (x == 'I')
19324 x = 'J';
19325 else if (x == 'R')
19326 x = 'S';
19327 else
19328#endif
19329 ++x;
19330 }
19331 } while (x == 'I' || x == 'O');
19332}
19333
19334/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019335 * "test(list)" function: Just checking the walls...
19336 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019338f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019339{
19340 /* Used for unit testing. Change the code below to your liking. */
19341#if 0
19342 listitem_T *li;
19343 list_T *l;
19344 char_u *bad, *good;
19345
19346 if (argvars[0].v_type != VAR_LIST)
19347 return;
19348 l = argvars[0].vval.v_list;
19349 if (l == NULL)
19350 return;
19351 li = l->lv_first;
19352 if (li == NULL)
19353 return;
19354 bad = get_tv_string(&li->li_tv);
19355 li = li->li_next;
19356 if (li == NULL)
19357 return;
19358 good = get_tv_string(&li->li_tv);
19359 rettv->vval.v_number = test_edit_score(bad, good);
19360#endif
19361}
19362
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019363#ifdef FEAT_FLOAT
19364/*
19365 * "tan()" function
19366 */
19367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019368f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019369{
19370 float_T f;
19371
19372 rettv->v_type = VAR_FLOAT;
19373 if (get_float_arg(argvars, &f) == OK)
19374 rettv->vval.v_float = tan(f);
19375 else
19376 rettv->vval.v_float = 0.0;
19377}
19378
19379/*
19380 * "tanh()" function
19381 */
19382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019383f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019384{
19385 float_T f;
19386
19387 rettv->v_type = VAR_FLOAT;
19388 if (get_float_arg(argvars, &f) == OK)
19389 rettv->vval.v_float = tanh(f);
19390 else
19391 rettv->vval.v_float = 0.0;
19392}
19393#endif
19394
Bram Moolenaard52d9742005-08-21 22:20:28 +000019395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019396 * "tolower(string)" function
19397 */
19398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019399f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400{
19401 char_u *p;
19402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019403 p = vim_strsave(get_tv_string(&argvars[0]));
19404 rettv->v_type = VAR_STRING;
19405 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019406
19407 if (p != NULL)
19408 while (*p != NUL)
19409 {
19410#ifdef FEAT_MBYTE
19411 int l;
19412
19413 if (enc_utf8)
19414 {
19415 int c, lc;
19416
19417 c = utf_ptr2char(p);
19418 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019419 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420 /* TODO: reallocate string when byte count changes. */
19421 if (utf_char2len(lc) == l)
19422 utf_char2bytes(lc, p);
19423 p += l;
19424 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019425 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019426 p += l; /* skip multi-byte character */
19427 else
19428#endif
19429 {
19430 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19431 ++p;
19432 }
19433 }
19434}
19435
19436/*
19437 * "toupper(string)" function
19438 */
19439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019440f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019441{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019442 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019443 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019444}
19445
19446/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019447 * "tr(string, fromstr, tostr)" function
19448 */
19449 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019450f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019451{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019452 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019453 char_u *fromstr;
19454 char_u *tostr;
19455 char_u *p;
19456#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019457 int inlen;
19458 int fromlen;
19459 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019460 int idx;
19461 char_u *cpstr;
19462 int cplen;
19463 int first = TRUE;
19464#endif
19465 char_u buf[NUMBUFLEN];
19466 char_u buf2[NUMBUFLEN];
19467 garray_T ga;
19468
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019469 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019470 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19471 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019472
19473 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019474 rettv->v_type = VAR_STRING;
19475 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019476 if (fromstr == NULL || tostr == NULL)
19477 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019478 ga_init2(&ga, (int)sizeof(char), 80);
19479
19480#ifdef FEAT_MBYTE
19481 if (!has_mbyte)
19482#endif
19483 /* not multi-byte: fromstr and tostr must be the same length */
19484 if (STRLEN(fromstr) != STRLEN(tostr))
19485 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019486#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019487error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019488#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019489 EMSG2(_(e_invarg2), fromstr);
19490 ga_clear(&ga);
19491 return;
19492 }
19493
19494 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019495 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019496 {
19497#ifdef FEAT_MBYTE
19498 if (has_mbyte)
19499 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019500 inlen = (*mb_ptr2len)(in_str);
19501 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019502 cplen = inlen;
19503 idx = 0;
19504 for (p = fromstr; *p != NUL; p += fromlen)
19505 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019506 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019507 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019508 {
19509 for (p = tostr; *p != NUL; p += tolen)
19510 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019511 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019512 if (idx-- == 0)
19513 {
19514 cplen = tolen;
19515 cpstr = p;
19516 break;
19517 }
19518 }
19519 if (*p == NUL) /* tostr is shorter than fromstr */
19520 goto error;
19521 break;
19522 }
19523 ++idx;
19524 }
19525
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019526 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019527 {
19528 /* Check that fromstr and tostr have the same number of
19529 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019530 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019531 first = FALSE;
19532 for (p = tostr; *p != NUL; p += tolen)
19533 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019534 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019535 --idx;
19536 }
19537 if (idx != 0)
19538 goto error;
19539 }
19540
Bram Moolenaarcde88542015-08-11 19:14:00 +020019541 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019542 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019543 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019544
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019545 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019546 }
19547 else
19548#endif
19549 {
19550 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019551 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019552 if (p != NULL)
19553 ga_append(&ga, tostr[p - fromstr]);
19554 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019555 ga_append(&ga, *in_str);
19556 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019557 }
19558 }
19559
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019560 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019561 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019562 ga_append(&ga, NUL);
19563
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019564 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019565}
19566
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019567#ifdef FEAT_FLOAT
19568/*
19569 * "trunc({float})" function
19570 */
19571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019572f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019573{
19574 float_T f;
19575
19576 rettv->v_type = VAR_FLOAT;
19577 if (get_float_arg(argvars, &f) == OK)
19578 /* trunc() is not in C90, use floor() or ceil() instead. */
19579 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19580 else
19581 rettv->vval.v_float = 0.0;
19582}
19583#endif
19584
Bram Moolenaar8299df92004-07-10 09:47:34 +000019585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019586 * "type(expr)" function
19587 */
19588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019589f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019590{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019591 int n;
19592
19593 switch (argvars[0].v_type)
19594 {
19595 case VAR_NUMBER: n = 0; break;
19596 case VAR_STRING: n = 1; break;
19597 case VAR_FUNC: n = 2; break;
19598 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019599 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019600#ifdef FEAT_FLOAT
19601 case VAR_FLOAT: n = 5; break;
19602#endif
Bram Moolenaarf95534c2016-01-23 21:59:52 +010019603 case VAR_SPECIAL:
19604 if (argvars[0].vval.v_number == VVAL_FALSE
19605 || argvars[0].vval.v_number == VVAL_TRUE)
19606 n = 6;
19607 else
19608 n = 7;
19609 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019610 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19611 }
19612 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019613}
19614
19615/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019616 * "undofile(name)" function
19617 */
19618 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019619f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019620{
19621 rettv->v_type = VAR_STRING;
19622#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019623 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019624 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019625
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019626 if (*fname == NUL)
19627 {
19628 /* If there is no file name there will be no undo file. */
19629 rettv->vval.v_string = NULL;
19630 }
19631 else
19632 {
19633 char_u *ffname = FullName_save(fname, FALSE);
19634
19635 if (ffname != NULL)
19636 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19637 vim_free(ffname);
19638 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019639 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019640#else
19641 rettv->vval.v_string = NULL;
19642#endif
19643}
19644
19645/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019646 * "undotree()" function
19647 */
19648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019649f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020019650{
19651 if (rettv_dict_alloc(rettv) == OK)
19652 {
19653 dict_T *dict = rettv->vval.v_dict;
19654 list_T *list;
19655
Bram Moolenaar730cde92010-06-27 05:18:54 +020019656 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019657 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019658 dict_add_nr_str(dict, "save_last",
19659 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019660 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19661 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019662 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019663
19664 list = list_alloc();
19665 if (list != NULL)
19666 {
19667 u_eval_tree(curbuf->b_u_oldhead, list);
19668 dict_add_list(dict, "entries", list);
19669 }
19670 }
19671}
19672
19673/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019674 * "values(dict)" function
19675 */
19676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019677f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000019678{
19679 dict_list(argvars, rettv, 1);
19680}
19681
19682/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019683 * "virtcol(string)" function
19684 */
19685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019686f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019687{
19688 colnr_T vcol = 0;
19689 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019690 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019691
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019692 fp = var2fpos(&argvars[0], FALSE, &fnum);
19693 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19694 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019695 {
19696 getvvcol(curwin, fp, NULL, NULL, &vcol);
19697 ++vcol;
19698 }
19699
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019700 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019701}
19702
19703/*
19704 * "visualmode()" function
19705 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019707f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019709 char_u str[2];
19710
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019711 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019712 str[0] = curbuf->b_visual_mode_eval;
19713 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019714 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019715
19716 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019717 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019719}
19720
19721/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019722 * "wildmenumode()" function
19723 */
19724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019725f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019726{
19727#ifdef FEAT_WILDMENU
19728 if (wild_menu_showing)
19729 rettv->vval.v_number = 1;
19730#endif
19731}
19732
19733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019734 * "winbufnr(nr)" function
19735 */
19736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019737f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019738{
19739 win_T *wp;
19740
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019741 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019742 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019743 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019744 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019745 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019746}
19747
19748/*
19749 * "wincol()" function
19750 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019752f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019753{
19754 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019755 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019756}
19757
19758/*
19759 * "winheight(nr)" function
19760 */
19761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019762f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019763{
19764 win_T *wp;
19765
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019766 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019768 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019770 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771}
19772
19773/*
19774 * "winline()" function
19775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019777f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019778{
19779 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019780 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019781}
19782
19783/*
19784 * "winnr()" function
19785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019787f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788{
19789 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019790
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019792 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019794 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019795}
19796
19797/*
19798 * "winrestcmd()" function
19799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019801f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802{
19803#ifdef FEAT_WINDOWS
19804 win_T *wp;
19805 int winnr = 1;
19806 garray_T ga;
19807 char_u buf[50];
19808
19809 ga_init2(&ga, (int)sizeof(char), 70);
19810 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19811 {
19812 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19813 ga_concat(&ga, buf);
19814# ifdef FEAT_VERTSPLIT
19815 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19816 ga_concat(&ga, buf);
19817# endif
19818 ++winnr;
19819 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019820 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019822 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019823#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019824 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019825#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019826 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019827}
19828
19829/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019830 * "winrestview()" function
19831 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019833f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019834{
19835 dict_T *dict;
19836
19837 if (argvars[0].v_type != VAR_DICT
19838 || (dict = argvars[0].vval.v_dict) == NULL)
19839 EMSG(_(e_invarg));
19840 else
19841 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019842 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19843 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19844 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19845 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019846#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019847 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19848 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019849#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019850 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19851 {
19852 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19853 curwin->w_set_curswant = FALSE;
19854 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019855
Bram Moolenaar82c25852014-05-28 16:47:16 +020019856 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19857 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019858#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019859 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19860 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019861#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019862 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19863 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19864 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19865 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019866
19867 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019868 win_new_height(curwin, curwin->w_height);
19869# ifdef FEAT_VERTSPLIT
19870 win_new_width(curwin, W_WIDTH(curwin));
19871# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019872 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019873
Bram Moolenaarb851a962014-10-31 15:45:52 +010019874 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019875 curwin->w_topline = 1;
19876 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19877 curwin->w_topline = curbuf->b_ml.ml_line_count;
19878#ifdef FEAT_DIFF
19879 check_topfill(curwin, TRUE);
19880#endif
19881 }
19882}
19883
19884/*
19885 * "winsaveview()" function
19886 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019888f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019889{
19890 dict_T *dict;
19891
Bram Moolenaara800b422010-06-27 01:15:55 +020019892 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019893 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019894 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019895
19896 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19897 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19898#ifdef FEAT_VIRTUALEDIT
19899 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19900#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019901 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019902 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19903
19904 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19905#ifdef FEAT_DIFF
19906 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19907#endif
19908 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19909 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19910}
19911
19912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019913 * "winwidth(nr)" function
19914 */
19915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019916f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019917{
19918 win_T *wp;
19919
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019920 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019922 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019923 else
19924#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019925 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019926#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019927 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019928#endif
19929}
19930
Bram Moolenaar071d4272004-06-13 20:20:40 +000019931/*
Bram Moolenaared767a22016-01-03 22:49:16 +010019932 * "wordcount()" function
19933 */
19934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019935f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010019936{
19937 if (rettv_dict_alloc(rettv) == FAIL)
19938 return;
19939 cursor_pos_info(rettv->vval.v_dict);
19940}
19941
19942/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019943 * Write list of strings to file
19944 */
19945 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019946write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019947{
19948 listitem_T *li;
19949 int c;
19950 int ret = OK;
19951 char_u *s;
19952
19953 for (li = list->lv_first; li != NULL; li = li->li_next)
19954 {
19955 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19956 {
19957 if (*s == '\n')
19958 c = putc(NUL, fd);
19959 else
19960 c = putc(*s, fd);
19961 if (c == EOF)
19962 {
19963 ret = FAIL;
19964 break;
19965 }
19966 }
19967 if (!binary || li->li_next != NULL)
19968 if (putc('\n', fd) == EOF)
19969 {
19970 ret = FAIL;
19971 break;
19972 }
19973 if (ret == FAIL)
19974 {
19975 EMSG(_(e_write));
19976 break;
19977 }
19978 }
19979 return ret;
19980}
19981
19982/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019983 * "writefile()" function
19984 */
19985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019986f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019987{
19988 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019989 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019990 char_u *fname;
19991 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019992 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019993
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019994 if (check_restricted() || check_secure())
19995 return;
19996
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019997 if (argvars[0].v_type != VAR_LIST)
19998 {
19999 EMSG2(_(e_listarg), "writefile()");
20000 return;
20001 }
20002 if (argvars[0].vval.v_list == NULL)
20003 return;
20004
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020005 if (argvars[2].v_type != VAR_UNKNOWN)
20006 {
20007 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20008 binary = TRUE;
20009 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20010 append = TRUE;
20011 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020012
20013 /* Always open the file in binary mode, library functions have a mind of
20014 * their own about CR-LF conversion. */
20015 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020016 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20017 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020018 {
20019 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20020 ret = -1;
20021 }
20022 else
20023 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020024 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20025 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020026 fclose(fd);
20027 }
20028
20029 rettv->vval.v_number = ret;
20030}
20031
20032/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020033 * "xor(expr, expr)" function
20034 */
20035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020036f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020037{
20038 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20039 ^ get_tv_number_chk(&argvars[1], NULL);
20040}
20041
20042
20043/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020045 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020046 */
20047 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020048var2fpos(
20049 typval_T *varp,
20050 int dollar_lnum, /* TRUE when $ is last line */
20051 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020052{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020053 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020054 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020055 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056
Bram Moolenaara5525202006-03-02 22:52:09 +000020057 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020058 if (varp->v_type == VAR_LIST)
20059 {
20060 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020061 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020062 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020063 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020064
20065 l = varp->vval.v_list;
20066 if (l == NULL)
20067 return NULL;
20068
20069 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020070 pos.lnum = list_find_nr(l, 0L, &error);
20071 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020072 return NULL; /* invalid line number */
20073
20074 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020075 pos.col = list_find_nr(l, 1L, &error);
20076 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020077 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020078 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020079
20080 /* We accept "$" for the column number: last column. */
20081 li = list_find(l, 1L);
20082 if (li != NULL && li->li_tv.v_type == VAR_STRING
20083 && li->li_tv.vval.v_string != NULL
20084 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20085 pos.col = len + 1;
20086
Bram Moolenaara5525202006-03-02 22:52:09 +000020087 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020088 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020089 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020090 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020091
Bram Moolenaara5525202006-03-02 22:52:09 +000020092#ifdef FEAT_VIRTUALEDIT
20093 /* Get the virtual offset. Defaults to zero. */
20094 pos.coladd = list_find_nr(l, 2L, &error);
20095 if (error)
20096 pos.coladd = 0;
20097#endif
20098
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020099 return &pos;
20100 }
20101
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020102 name = get_tv_string_chk(varp);
20103 if (name == NULL)
20104 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020105 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020106 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020107 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20108 {
20109 if (VIsual_active)
20110 return &VIsual;
20111 return &curwin->w_cursor;
20112 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020113 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020114 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020115 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020116 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20117 return NULL;
20118 return pp;
20119 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020120
20121#ifdef FEAT_VIRTUALEDIT
20122 pos.coladd = 0;
20123#endif
20124
Bram Moolenaar477933c2007-07-17 14:32:23 +000020125 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020126 {
20127 pos.col = 0;
20128 if (name[1] == '0') /* "w0": first visible line */
20129 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020130 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020131 pos.lnum = curwin->w_topline;
20132 return &pos;
20133 }
20134 else if (name[1] == '$') /* "w$": last visible line */
20135 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020136 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020137 pos.lnum = curwin->w_botline - 1;
20138 return &pos;
20139 }
20140 }
20141 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020143 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144 {
20145 pos.lnum = curbuf->b_ml.ml_line_count;
20146 pos.col = 0;
20147 }
20148 else
20149 {
20150 pos.lnum = curwin->w_cursor.lnum;
20151 pos.col = (colnr_T)STRLEN(ml_get_curline());
20152 }
20153 return &pos;
20154 }
20155 return NULL;
20156}
20157
20158/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020159 * Convert list in "arg" into a position and optional file number.
20160 * When "fnump" is NULL there is no file number, only 3 items.
20161 * Note that the column is passed on as-is, the caller may want to decrement
20162 * it to use 1 for the first column.
20163 * Return FAIL when conversion is not possible, doesn't check the position for
20164 * validity.
20165 */
20166 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020167list2fpos(
20168 typval_T *arg,
20169 pos_T *posp,
20170 int *fnump,
20171 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020172{
20173 list_T *l = arg->vval.v_list;
20174 long i = 0;
20175 long n;
20176
Bram Moolenaar493c1782014-05-28 14:34:46 +020020177 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20178 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020179 if (arg->v_type != VAR_LIST
20180 || l == NULL
20181 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020182 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020183 return FAIL;
20184
20185 if (fnump != NULL)
20186 {
20187 n = list_find_nr(l, i++, NULL); /* fnum */
20188 if (n < 0)
20189 return FAIL;
20190 if (n == 0)
20191 n = curbuf->b_fnum; /* current buffer */
20192 *fnump = n;
20193 }
20194
20195 n = list_find_nr(l, i++, NULL); /* lnum */
20196 if (n < 0)
20197 return FAIL;
20198 posp->lnum = n;
20199
20200 n = list_find_nr(l, i++, NULL); /* col */
20201 if (n < 0)
20202 return FAIL;
20203 posp->col = n;
20204
20205#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020206 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020207 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020208 posp->coladd = 0;
20209 else
20210 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020211#endif
20212
Bram Moolenaar493c1782014-05-28 14:34:46 +020020213 if (curswantp != NULL)
20214 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20215
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020216 return OK;
20217}
20218
20219/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220 * Get the length of an environment variable name.
20221 * Advance "arg" to the first character after the name.
20222 * Return 0 for error.
20223 */
20224 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020225get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226{
20227 char_u *p;
20228 int len;
20229
20230 for (p = *arg; vim_isIDc(*p); ++p)
20231 ;
20232 if (p == *arg) /* no name found */
20233 return 0;
20234
20235 len = (int)(p - *arg);
20236 *arg = p;
20237 return len;
20238}
20239
20240/*
20241 * Get the length of the name of a function or internal variable.
20242 * "arg" is advanced to the first non-white character after the name.
20243 * Return 0 if something is wrong.
20244 */
20245 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020246get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020247{
20248 char_u *p;
20249 int len;
20250
20251 /* Find the end of the name. */
20252 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020253 {
20254 if (*p == ':')
20255 {
20256 /* "s:" is start of "s:var", but "n:" is not and can be used in
20257 * slice "[n:]". Also "xx:" is not a namespace. */
20258 len = (int)(p - *arg);
20259 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20260 || len > 1)
20261 break;
20262 }
20263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020264 if (p == *arg) /* no name found */
20265 return 0;
20266
20267 len = (int)(p - *arg);
20268 *arg = skipwhite(p);
20269
20270 return len;
20271}
20272
20273/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020274 * Get the length of the name of a variable or function.
20275 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020276 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020277 * Return -1 if curly braces expansion failed.
20278 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020279 * If the name contains 'magic' {}'s, expand them and return the
20280 * expanded name in an allocated string via 'alias' - caller must free.
20281 */
20282 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020283get_name_len(
20284 char_u **arg,
20285 char_u **alias,
20286 int evaluate,
20287 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288{
20289 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020290 char_u *p;
20291 char_u *expr_start;
20292 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020293
20294 *alias = NULL; /* default to no alias */
20295
20296 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20297 && (*arg)[2] == (int)KE_SNR)
20298 {
20299 /* hard coded <SNR>, already translated */
20300 *arg += 3;
20301 return get_id_len(arg) + 3;
20302 }
20303 len = eval_fname_script(*arg);
20304 if (len > 0)
20305 {
20306 /* literal "<SID>", "s:" or "<SNR>" */
20307 *arg += len;
20308 }
20309
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020311 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020312 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020313 p = find_name_end(*arg, &expr_start, &expr_end,
20314 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020315 if (expr_start != NULL)
20316 {
20317 char_u *temp_string;
20318
20319 if (!evaluate)
20320 {
20321 len += (int)(p - *arg);
20322 *arg = skipwhite(p);
20323 return len;
20324 }
20325
20326 /*
20327 * Include any <SID> etc in the expanded string:
20328 * Thus the -len here.
20329 */
20330 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20331 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020332 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020333 *alias = temp_string;
20334 *arg = skipwhite(p);
20335 return (int)STRLEN(temp_string);
20336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020337
20338 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020339 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020340 EMSG2(_(e_invexpr2), *arg);
20341
20342 return len;
20343}
20344
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020345/*
20346 * Find the end of a variable or function name, taking care of magic braces.
20347 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20348 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020349 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020350 * Return a pointer to just after the name. Equal to "arg" if there is no
20351 * valid name.
20352 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020353 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020354find_name_end(
20355 char_u *arg,
20356 char_u **expr_start,
20357 char_u **expr_end,
20358 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020359{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020360 int mb_nest = 0;
20361 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020363 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020364
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020365 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020366 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020367 *expr_start = NULL;
20368 *expr_end = NULL;
20369 }
20370
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020371 /* Quick check for valid starting character. */
20372 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20373 return arg;
20374
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020375 for (p = arg; *p != NUL
20376 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020377 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020378 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020379 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020380 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020381 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020382 if (*p == '\'')
20383 {
20384 /* skip over 'string' to avoid counting [ and ] inside it. */
20385 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20386 ;
20387 if (*p == NUL)
20388 break;
20389 }
20390 else if (*p == '"')
20391 {
20392 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20393 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20394 if (*p == '\\' && p[1] != NUL)
20395 ++p;
20396 if (*p == NUL)
20397 break;
20398 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020399 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20400 {
20401 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020402 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020403 len = (int)(p - arg);
20404 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020405 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020406 break;
20407 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020408
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020409 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020410 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020411 if (*p == '[')
20412 ++br_nest;
20413 else if (*p == ']')
20414 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020415 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020417 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020418 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020419 if (*p == '{')
20420 {
20421 mb_nest++;
20422 if (expr_start != NULL && *expr_start == NULL)
20423 *expr_start = p;
20424 }
20425 else if (*p == '}')
20426 {
20427 mb_nest--;
20428 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20429 *expr_end = p;
20430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020432 }
20433
20434 return p;
20435}
20436
20437/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020438 * Expands out the 'magic' {}'s in a variable/function name.
20439 * Note that this can call itself recursively, to deal with
20440 * constructs like foo{bar}{baz}{bam}
20441 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20442 * "in_start" ^
20443 * "expr_start" ^
20444 * "expr_end" ^
20445 * "in_end" ^
20446 *
20447 * Returns a new allocated string, which the caller must free.
20448 * Returns NULL for failure.
20449 */
20450 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020451make_expanded_name(
20452 char_u *in_start,
20453 char_u *expr_start,
20454 char_u *expr_end,
20455 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020456{
20457 char_u c1;
20458 char_u *retval = NULL;
20459 char_u *temp_result;
20460 char_u *nextcmd = NULL;
20461
20462 if (expr_end == NULL || in_end == NULL)
20463 return NULL;
20464 *expr_start = NUL;
20465 *expr_end = NUL;
20466 c1 = *in_end;
20467 *in_end = NUL;
20468
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020469 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020470 if (temp_result != NULL && nextcmd == NULL)
20471 {
20472 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20473 + (in_end - expr_end) + 1));
20474 if (retval != NULL)
20475 {
20476 STRCPY(retval, in_start);
20477 STRCAT(retval, temp_result);
20478 STRCAT(retval, expr_end + 1);
20479 }
20480 }
20481 vim_free(temp_result);
20482
20483 *in_end = c1; /* put char back for error messages */
20484 *expr_start = '{';
20485 *expr_end = '}';
20486
20487 if (retval != NULL)
20488 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020489 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020490 if (expr_start != NULL)
20491 {
20492 /* Further expansion! */
20493 temp_result = make_expanded_name(retval, expr_start,
20494 expr_end, temp_result);
20495 vim_free(retval);
20496 retval = temp_result;
20497 }
20498 }
20499
20500 return retval;
20501}
20502
20503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020504 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020505 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020506 */
20507 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020508eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020509{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020510 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20511}
20512
20513/*
20514 * Return TRUE if character "c" can be used as the first character in a
20515 * variable or function name (excluding '{' and '}').
20516 */
20517 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020518eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020519{
20520 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020521}
20522
20523/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020524 * Set number v: variable to "val".
20525 */
20526 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020527set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020528{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020529 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020530}
20531
20532/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020533 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020534 */
20535 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010020536get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020537{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020538 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020539}
20540
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020541/*
20542 * Get string v: variable value. Uses a static buffer, can only be used once.
20543 */
20544 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020545get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020546{
20547 return get_tv_string(&vimvars[idx].vv_tv);
20548}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020549
Bram Moolenaar071d4272004-06-13 20:20:40 +000020550/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020551 * Get List v: variable value. Caller must take care of reference count when
20552 * needed.
20553 */
20554 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020555get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000020556{
20557 return vimvars[idx].vv_list;
20558}
20559
20560/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020561 * Set v:char to character "c".
20562 */
20563 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020564set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020565{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020566 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020567
20568#ifdef FEAT_MBYTE
20569 if (has_mbyte)
20570 buf[(*mb_char2bytes)(c, buf)] = NUL;
20571 else
20572#endif
20573 {
20574 buf[0] = c;
20575 buf[1] = NUL;
20576 }
20577 set_vim_var_string(VV_CHAR, buf, -1);
20578}
20579
20580/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020581 * Set v:count to "count" and v:count1 to "count1".
20582 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020583 */
20584 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020585set_vcount(
20586 long count,
20587 long count1,
20588 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020589{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020590 if (set_prevcount)
20591 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020592 vimvars[VV_COUNT].vv_nr = count;
20593 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020594}
20595
20596/*
20597 * Set string v: variable to a copy of "val".
20598 */
20599 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020600set_vim_var_string(
20601 int idx,
20602 char_u *val,
20603 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020604{
Bram Moolenaara542c682016-01-31 16:28:04 +010020605 clear_tv(&vimvars[idx].vv_di.di_tv);
20606 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020607 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020608 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020609 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020610 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020612 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020613}
20614
20615/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020616 * Set List v: variable to "val".
20617 */
20618 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020619set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000020620{
Bram Moolenaara542c682016-01-31 16:28:04 +010020621 clear_tv(&vimvars[idx].vv_di.di_tv);
20622 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000020623 vimvars[idx].vv_list = val;
20624 if (val != NULL)
20625 ++val->lv_refcount;
20626}
20627
20628/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020629 * Set Dictionary v: variable to "val".
20630 */
20631 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020632set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020020633{
20634 int todo;
20635 hashitem_T *hi;
20636
Bram Moolenaara542c682016-01-31 16:28:04 +010020637 clear_tv(&vimvars[idx].vv_di.di_tv);
20638 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020020639 vimvars[idx].vv_dict = val;
20640 if (val != NULL)
20641 {
20642 ++val->dv_refcount;
20643
20644 /* Set readonly */
20645 todo = (int)val->dv_hashtab.ht_used;
20646 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20647 {
20648 if (HASHITEM_EMPTY(hi))
20649 continue;
20650 --todo;
20651 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20652 }
20653 }
20654}
20655
20656/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020657 * Set v:register if needed.
20658 */
20659 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020660set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661{
20662 char_u regname;
20663
20664 if (c == 0 || c == ' ')
20665 regname = '"';
20666 else
20667 regname = c;
20668 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020669 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020670 set_vim_var_string(VV_REG, &regname, 1);
20671}
20672
20673/*
20674 * Get or set v:exception. If "oldval" == NULL, return the current value.
20675 * Otherwise, restore the value to "oldval" and return NULL.
20676 * Must always be called in pairs to save and restore v:exception! Does not
20677 * take care of memory allocations.
20678 */
20679 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020680v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020681{
20682 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020683 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020684
Bram Moolenaare9a41262005-01-15 22:18:47 +000020685 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020686 return NULL;
20687}
20688
20689/*
20690 * Get or set v:throwpoint. 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:throwpoint! Does not
20693 * take care of memory allocations.
20694 */
20695 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020696v_throwpoint(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_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020700
Bram Moolenaare9a41262005-01-15 22:18:47 +000020701 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702 return NULL;
20703}
20704
20705#if defined(FEAT_AUTOCMD) || defined(PROTO)
20706/*
20707 * Set v:cmdarg.
20708 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20709 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20710 * Must always be called in pairs!
20711 */
20712 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020713set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020714{
20715 char_u *oldval;
20716 char_u *newval;
20717 unsigned len;
20718
Bram Moolenaare9a41262005-01-15 22:18:47 +000020719 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020720 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020721 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020722 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020723 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020724 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020725 }
20726
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020727 if (eap->force_bin == FORCE_BIN)
20728 len = 6;
20729 else if (eap->force_bin == FORCE_NOBIN)
20730 len = 8;
20731 else
20732 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020733
20734 if (eap->read_edit)
20735 len += 7;
20736
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020737 if (eap->force_ff != 0)
20738 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20739# ifdef FEAT_MBYTE
20740 if (eap->force_enc != 0)
20741 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020742 if (eap->bad_char != 0)
20743 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020744# endif
20745
20746 newval = alloc(len + 1);
20747 if (newval == NULL)
20748 return NULL;
20749
20750 if (eap->force_bin == FORCE_BIN)
20751 sprintf((char *)newval, " ++bin");
20752 else if (eap->force_bin == FORCE_NOBIN)
20753 sprintf((char *)newval, " ++nobin");
20754 else
20755 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020756
20757 if (eap->read_edit)
20758 STRCAT(newval, " ++edit");
20759
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020760 if (eap->force_ff != 0)
20761 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20762 eap->cmd + eap->force_ff);
20763# ifdef FEAT_MBYTE
20764 if (eap->force_enc != 0)
20765 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20766 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020767 if (eap->bad_char == BAD_KEEP)
20768 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20769 else if (eap->bad_char == BAD_DROP)
20770 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20771 else if (eap->bad_char != 0)
20772 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020773# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020774 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020775 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020776}
20777#endif
20778
20779/*
20780 * Get the value of internal variable "name".
20781 * Return OK or FAIL.
20782 */
20783 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020784get_var_tv(
20785 char_u *name,
20786 int len, /* length of "name" */
20787 typval_T *rettv, /* NULL when only checking existence */
20788 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
20789 int verbose, /* may give error message */
20790 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020791{
20792 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020793 typval_T *tv = NULL;
20794 typval_T atv;
20795 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020796 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020797
20798 /* truncate the name, so that we can use strcmp() */
20799 cc = name[len];
20800 name[len] = NUL;
20801
20802 /*
20803 * Check for "b:changedtick".
20804 */
20805 if (STRCMP(name, "b:changedtick") == 0)
20806 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020807 atv.v_type = VAR_NUMBER;
20808 atv.vval.v_number = curbuf->b_changedtick;
20809 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020810 }
20811
20812 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020813 * Check for user-defined variables.
20814 */
20815 else
20816 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020817 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020818 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020819 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020820 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020821 if (dip != NULL)
20822 *dip = v;
20823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020824 }
20825
Bram Moolenaare9a41262005-01-15 22:18:47 +000020826 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020827 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020828 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020829 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020830 ret = FAIL;
20831 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020832 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020833 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020834
20835 name[len] = cc;
20836
20837 return ret;
20838}
20839
20840/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020841 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20842 * Also handle function call with Funcref variable: func(expr)
20843 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20844 */
20845 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020846handle_subscript(
20847 char_u **arg,
20848 typval_T *rettv,
20849 int evaluate, /* do more than finding the end */
20850 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020851{
20852 int ret = OK;
20853 dict_T *selfdict = NULL;
20854 char_u *s;
20855 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020856 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020857
20858 while (ret == OK
20859 && (**arg == '['
20860 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020861 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020862 && !vim_iswhite(*(*arg - 1)))
20863 {
20864 if (**arg == '(')
20865 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020866 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020867 if (evaluate)
20868 {
20869 functv = *rettv;
20870 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020871
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020872 /* Invoke the function. Recursive! */
20873 s = functv.vval.v_string;
20874 }
20875 else
20876 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020877 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020878 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20879 &len, evaluate, selfdict);
20880
20881 /* Clear the funcref afterwards, so that deleting it while
20882 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020883 if (evaluate)
20884 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020885
20886 /* Stop the expression evaluation when immediately aborting on
20887 * error, or when an interrupt occurred or an exception was thrown
20888 * but not caught. */
20889 if (aborting())
20890 {
20891 if (ret == OK)
20892 clear_tv(rettv);
20893 ret = FAIL;
20894 }
20895 dict_unref(selfdict);
20896 selfdict = NULL;
20897 }
20898 else /* **arg == '[' || **arg == '.' */
20899 {
20900 dict_unref(selfdict);
20901 if (rettv->v_type == VAR_DICT)
20902 {
20903 selfdict = rettv->vval.v_dict;
20904 if (selfdict != NULL)
20905 ++selfdict->dv_refcount;
20906 }
20907 else
20908 selfdict = NULL;
20909 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20910 {
20911 clear_tv(rettv);
20912 ret = FAIL;
20913 }
20914 }
20915 }
20916 dict_unref(selfdict);
20917 return ret;
20918}
20919
20920/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020921 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020922 * value).
20923 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010020924 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020925alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020926{
Bram Moolenaar33570922005-01-25 22:26:29 +000020927 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020928}
20929
20930/*
20931 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020932 * The string "s" must have been allocated, it is consumed.
20933 * Return NULL for out of memory, the variable otherwise.
20934 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020935 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020936alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020937{
Bram Moolenaar33570922005-01-25 22:26:29 +000020938 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020939
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020940 rettv = alloc_tv();
20941 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020942 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020943 rettv->v_type = VAR_STRING;
20944 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020945 }
20946 else
20947 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020948 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020949}
20950
20951/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020952 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020953 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020954 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020955free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020956{
20957 if (varp != NULL)
20958 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020959 switch (varp->v_type)
20960 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020961 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020962 func_unref(varp->vval.v_string);
20963 /*FALLTHROUGH*/
20964 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020965 vim_free(varp->vval.v_string);
20966 break;
20967 case VAR_LIST:
20968 list_unref(varp->vval.v_list);
20969 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020970 case VAR_DICT:
20971 dict_unref(varp->vval.v_dict);
20972 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020973 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020974#ifdef FEAT_FLOAT
20975 case VAR_FLOAT:
20976#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020977 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010020978 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020979 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020980 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020981 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020982 break;
20983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020984 vim_free(varp);
20985 }
20986}
20987
20988/*
20989 * Free the memory for a variable value and set the value to NULL or 0.
20990 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020991 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020992clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020993{
20994 if (varp != NULL)
20995 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020996 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020997 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020998 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020999 func_unref(varp->vval.v_string);
21000 /*FALLTHROUGH*/
21001 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021002 vim_free(varp->vval.v_string);
21003 varp->vval.v_string = NULL;
21004 break;
21005 case VAR_LIST:
21006 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021007 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021008 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021009 case VAR_DICT:
21010 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021011 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021012 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021013 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021014 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021015 varp->vval.v_number = 0;
21016 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021017#ifdef FEAT_FLOAT
21018 case VAR_FLOAT:
21019 varp->vval.v_float = 0.0;
21020 break;
21021#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021022 case VAR_UNKNOWN:
21023 break;
21024 default:
21025 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021026 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021027 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021028 }
21029}
21030
21031/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021032 * Set the value of a variable to NULL without freeing items.
21033 */
21034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021035init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021036{
21037 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021038 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021039}
21040
21041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021042 * Get the number value of a variable.
21043 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021044 * For incompatible types, return 0.
21045 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21046 * caller of incompatible types: it sets *denote to TRUE if "denote"
21047 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021048 */
21049 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021050get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021051{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021052 int error = FALSE;
21053
21054 return get_tv_number_chk(varp, &error); /* return 0L on error */
21055}
21056
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021057 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021058get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021059{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021060 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021061
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021062 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021063 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021064 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021065 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021066#ifdef FEAT_FLOAT
21067 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021068 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021069 break;
21070#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021071 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021072 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021073 break;
21074 case VAR_STRING:
21075 if (varp->vval.v_string != NULL)
21076 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021077 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021078 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021079 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021080 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021081 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021082 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021083 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021084 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021085 case VAR_SPECIAL:
21086 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21087 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021088 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021089 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021090 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021091 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021092 if (denote == NULL) /* useful for values that must be unsigned */
21093 n = -1;
21094 else
21095 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021096 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097}
21098
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021099#ifdef FEAT_FLOAT
21100 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021101get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021102{
21103 switch (varp->v_type)
21104 {
21105 case VAR_NUMBER:
21106 return (float_T)(varp->vval.v_number);
21107#ifdef FEAT_FLOAT
21108 case VAR_FLOAT:
21109 return varp->vval.v_float;
21110 break;
21111#endif
21112 case VAR_FUNC:
21113 EMSG(_("E891: Using a Funcref as a Float"));
21114 break;
21115 case VAR_STRING:
21116 EMSG(_("E892: Using a String as a Float"));
21117 break;
21118 case VAR_LIST:
21119 EMSG(_("E893: Using a List as a Float"));
21120 break;
21121 case VAR_DICT:
21122 EMSG(_("E894: Using a Dictionary as a Float"));
21123 break;
21124 default:
21125 EMSG2(_(e_intern2), "get_tv_float()");
21126 break;
21127 }
21128 return 0;
21129}
21130#endif
21131
Bram Moolenaar071d4272004-06-13 20:20:40 +000021132/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021133 * Get the lnum from the first argument.
21134 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021135 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021136 */
21137 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021138get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139{
Bram Moolenaar33570922005-01-25 22:26:29 +000021140 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141 linenr_T lnum;
21142
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021143 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021144 if (lnum == 0) /* no valid number, try using line() */
21145 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021146 rettv.v_type = VAR_NUMBER;
21147 f_line(argvars, &rettv);
21148 lnum = rettv.vval.v_number;
21149 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021150 }
21151 return lnum;
21152}
21153
21154/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021155 * Get the lnum from the first argument.
21156 * Also accepts "$", then "buf" is used.
21157 * Returns 0 on error.
21158 */
21159 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021160get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021161{
21162 if (argvars[0].v_type == VAR_STRING
21163 && argvars[0].vval.v_string != NULL
21164 && argvars[0].vval.v_string[0] == '$'
21165 && buf != NULL)
21166 return buf->b_ml.ml_line_count;
21167 return get_tv_number_chk(&argvars[0], NULL);
21168}
21169
21170/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021171 * Get the string value of a variable.
21172 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021173 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21174 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021175 * If the String variable has never been set, return an empty string.
21176 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021177 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21178 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021179 */
21180 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021181get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021182{
21183 static char_u mybuf[NUMBUFLEN];
21184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021185 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021186}
21187
21188 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021189get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021190{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021191 char_u *res = get_tv_string_buf_chk(varp, buf);
21192
21193 return res != NULL ? res : (char_u *)"";
21194}
21195
Bram Moolenaar7d647822014-04-05 21:28:56 +020021196/*
21197 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21198 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021199 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021200get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021201{
21202 static char_u mybuf[NUMBUFLEN];
21203
21204 return get_tv_string_buf_chk(varp, mybuf);
21205}
21206
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021207 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021208get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021209{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021210 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021211 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021212 case VAR_NUMBER:
21213 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21214 return buf;
21215 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021216 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021217 break;
21218 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021219 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021220 break;
21221 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021222 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021223 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021224#ifdef FEAT_FLOAT
21225 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021226 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021227 break;
21228#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021229 case VAR_STRING:
21230 if (varp->vval.v_string != NULL)
21231 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021232 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021233 case VAR_SPECIAL:
21234 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21235 return buf;
21236
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021237 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021238 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021239 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021240 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021241 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021242}
21243
21244/*
21245 * Find variable "name" in the list of variables.
21246 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021247 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021248 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021249 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021250 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021251 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021252find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021253{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021254 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021255 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021256
Bram Moolenaara7043832005-01-21 11:56:39 +000021257 ht = find_var_ht(name, &varname);
21258 if (htp != NULL)
21259 *htp = ht;
21260 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021261 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021262 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263}
21264
21265/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021266 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021267 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021268 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021269 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021270find_var_in_ht(
21271 hashtab_T *ht,
21272 int htname,
21273 char_u *varname,
21274 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021275{
Bram Moolenaar33570922005-01-25 22:26:29 +000021276 hashitem_T *hi;
21277
21278 if (*varname == NUL)
21279 {
21280 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021281 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021282 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021283 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021284 case 'g': return &globvars_var;
21285 case 'v': return &vimvars_var;
21286 case 'b': return &curbuf->b_bufvar;
21287 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021288#ifdef FEAT_WINDOWS
21289 case 't': return &curtab->tp_winvar;
21290#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021291 case 'l': return current_funccal == NULL
21292 ? NULL : &current_funccal->l_vars_var;
21293 case 'a': return current_funccal == NULL
21294 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021295 }
21296 return NULL;
21297 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021298
21299 hi = hash_find(ht, varname);
21300 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021301 {
21302 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021303 * worked find the variable again. Don't auto-load a script if it was
21304 * loaded already, otherwise it would be loaded every time when
21305 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021306 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021307 {
21308 /* Note: script_autoload() may make "hi" invalid. It must either
21309 * be obtained again or not used. */
21310 if (!script_autoload(varname, FALSE) || aborting())
21311 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021312 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021313 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021314 if (HASHITEM_EMPTY(hi))
21315 return NULL;
21316 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021317 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021318}
21319
21320/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021321 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021322 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021323 * Set "varname" to the start of name without ':'.
21324 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021325 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021326find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021328 hashitem_T *hi;
21329
Bram Moolenaar73627d02015-08-11 15:46:09 +020021330 if (name[0] == NUL)
21331 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021332 if (name[1] != ':')
21333 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021334 /* The name must not start with a colon or #. */
21335 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336 return NULL;
21337 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021338
21339 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021340 hi = hash_find(&compat_hashtab, name);
21341 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021342 return &compat_hashtab;
21343
Bram Moolenaar071d4272004-06-13 20:20:40 +000021344 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021345 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021346 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021347 }
21348 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021349 if (*name == 'g') /* global variable */
21350 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021351 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21352 */
21353 if (vim_strchr(name + 2, ':') != NULL
21354 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021355 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021356 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021357 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021358 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021359 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021360#ifdef FEAT_WINDOWS
21361 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021362 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021363#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021364 if (*name == 'v') /* v: variable */
21365 return &vimvarht;
21366 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021367 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021368 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021369 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021370 if (*name == 's' /* script variable */
21371 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21372 return &SCRIPT_VARS(current_SID);
21373 return NULL;
21374}
21375
21376/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021377 * Get function call environment based on bactrace debug level
21378 */
21379 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021380get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021381{
21382 int i;
21383 funccall_T *funccal;
21384 funccall_T *temp_funccal;
21385
21386 funccal = current_funccal;
21387 if (debug_backtrace_level > 0)
21388 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021389 for (i = 0; i < debug_backtrace_level; i++)
21390 {
21391 temp_funccal = funccal->caller;
21392 if (temp_funccal)
21393 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021394 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021395 /* backtrace level overflow. reset to max */
21396 debug_backtrace_level = i;
21397 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021398 }
21399 return funccal;
21400}
21401
21402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021404 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021405 * Returns NULL when it doesn't exist.
21406 */
21407 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021408get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021409{
Bram Moolenaar33570922005-01-25 22:26:29 +000021410 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021411
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021412 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021413 if (v == NULL)
21414 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021415 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021416}
21417
21418/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021419 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021420 * sourcing this script and when executing functions defined in the script.
21421 */
21422 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021423new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021424{
Bram Moolenaara7043832005-01-21 11:56:39 +000021425 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021426 hashtab_T *ht;
21427 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021428
Bram Moolenaar071d4272004-06-13 20:20:40 +000021429 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21430 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021431 /* Re-allocating ga_data means that an ht_array pointing to
21432 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021433 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021434 for (i = 1; i <= ga_scripts.ga_len; ++i)
21435 {
21436 ht = &SCRIPT_VARS(i);
21437 if (ht->ht_mask == HT_INIT_SIZE - 1)
21438 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021439 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021440 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021441 }
21442
Bram Moolenaar071d4272004-06-13 20:20:40 +000021443 while (ga_scripts.ga_len < id)
21444 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021445 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021446 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021447 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021448 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021449 }
21450 }
21451}
21452
21453/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021454 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21455 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021456 */
21457 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021458init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021459{
Bram Moolenaar33570922005-01-25 22:26:29 +000021460 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021461 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021462 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021463 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021464 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021465 dict_var->di_tv.vval.v_dict = dict;
21466 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021467 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021468 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21469 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021470}
21471
21472/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021473 * Unreference a dictionary initialized by init_var_dict().
21474 */
21475 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021476unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020021477{
21478 /* Now the dict needs to be freed if no one else is using it, go back to
21479 * normal reference counting. */
21480 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21481 dict_unref(dict);
21482}
21483
21484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021486 * Frees all allocated variables and the value they contain.
21487 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021488 */
21489 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021490vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021491{
21492 vars_clear_ext(ht, TRUE);
21493}
21494
21495/*
21496 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21497 */
21498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021499vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021500{
Bram Moolenaara7043832005-01-21 11:56:39 +000021501 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021502 hashitem_T *hi;
21503 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021504
Bram Moolenaar33570922005-01-25 22:26:29 +000021505 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021506 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021507 for (hi = ht->ht_array; todo > 0; ++hi)
21508 {
21509 if (!HASHITEM_EMPTY(hi))
21510 {
21511 --todo;
21512
Bram Moolenaar33570922005-01-25 22:26:29 +000021513 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021514 * ht_array might change then. hash_clear() takes care of it
21515 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021516 v = HI2DI(hi);
21517 if (free_val)
21518 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021519 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021520 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021521 }
21522 }
21523 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021524 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021525}
21526
Bram Moolenaara7043832005-01-21 11:56:39 +000021527/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021528 * Delete a variable from hashtab "ht" at item "hi".
21529 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021532delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021533{
Bram Moolenaar33570922005-01-25 22:26:29 +000021534 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021535
21536 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021537 clear_tv(&di->di_tv);
21538 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021539}
21540
21541/*
21542 * List the value of one internal variable.
21543 */
21544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021545list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021546{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021547 char_u *tofree;
21548 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021549 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021550
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021551 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000021552 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021553 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021554 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021555}
21556
Bram Moolenaar071d4272004-06-13 20:20:40 +000021557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021558list_one_var_a(
21559 char_u *prefix,
21560 char_u *name,
21561 int type,
21562 char_u *string,
21563 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564{
Bram Moolenaar31859182007-08-14 20:41:13 +000021565 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21566 msg_start();
21567 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568 if (name != NULL) /* "a:" vars don't have a name stored */
21569 msg_puts(name);
21570 msg_putchar(' ');
21571 msg_advance(22);
21572 if (type == VAR_NUMBER)
21573 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021574 else if (type == VAR_FUNC)
21575 msg_putchar('*');
21576 else if (type == VAR_LIST)
21577 {
21578 msg_putchar('[');
21579 if (*string == '[')
21580 ++string;
21581 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021582 else if (type == VAR_DICT)
21583 {
21584 msg_putchar('{');
21585 if (*string == '{')
21586 ++string;
21587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021588 else
21589 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021590
Bram Moolenaar071d4272004-06-13 20:20:40 +000021591 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021592
21593 if (type == VAR_FUNC)
21594 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021595 if (*first)
21596 {
21597 msg_clr_eos();
21598 *first = FALSE;
21599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021600}
21601
21602/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021603 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604 * If the variable already exists, the value is updated.
21605 * Otherwise the variable is created.
21606 */
21607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021608set_var(
21609 char_u *name,
21610 typval_T *tv,
21611 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021612{
Bram Moolenaar33570922005-01-25 22:26:29 +000021613 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021615 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021617 ht = find_var_ht(name, &varname);
21618 if (ht == NULL || *varname == NUL)
21619 {
21620 EMSG2(_(e_illvar), name);
21621 return;
21622 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021623 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021624
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021625 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21626 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021627
Bram Moolenaar33570922005-01-25 22:26:29 +000021628 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021630 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021631 if (var_check_ro(v->di_flags, name, FALSE)
21632 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021633 return;
21634 if (v->di_tv.v_type != tv->v_type
21635 && !((v->di_tv.v_type == VAR_STRING
21636 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021637 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021638 || tv->v_type == VAR_NUMBER))
21639#ifdef FEAT_FLOAT
21640 && !((v->di_tv.v_type == VAR_NUMBER
21641 || v->di_tv.v_type == VAR_FLOAT)
21642 && (tv->v_type == VAR_NUMBER
21643 || tv->v_type == VAR_FLOAT))
21644#endif
21645 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021646 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021647 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021648 return;
21649 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021650
21651 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021652 * Handle setting internal v: variables separately where needed to
21653 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021654 */
21655 if (ht == &vimvarht)
21656 {
21657 if (v->di_tv.v_type == VAR_STRING)
21658 {
21659 vim_free(v->di_tv.vval.v_string);
21660 if (copy || tv->v_type != VAR_STRING)
21661 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21662 else
21663 {
21664 /* Take over the string to avoid an extra alloc/free. */
21665 v->di_tv.vval.v_string = tv->vval.v_string;
21666 tv->vval.v_string = NULL;
21667 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021668 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021669 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021670 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021671 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021672 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021673 if (STRCMP(varname, "searchforward") == 0)
21674 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021675#ifdef FEAT_SEARCH_EXTRA
21676 else if (STRCMP(varname, "hlsearch") == 0)
21677 {
21678 no_hlsearch = !v->di_tv.vval.v_number;
21679 redraw_all_later(SOME_VALID);
21680 }
21681#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021682 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021683 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021684 else if (v->di_tv.v_type != tv->v_type)
21685 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021686 }
21687
21688 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021689 }
21690 else /* add a new variable */
21691 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021692 /* Can't add "v:" variable. */
21693 if (ht == &vimvarht)
21694 {
21695 EMSG2(_(e_illvar), name);
21696 return;
21697 }
21698
Bram Moolenaar92124a32005-06-17 22:03:40 +000021699 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021700 if (!valid_varname(varname))
21701 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021702
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021703 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21704 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021705 if (v == NULL)
21706 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021707 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021708 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021709 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021710 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021711 return;
21712 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021713 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021714 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021715
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021716 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021717 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021718 else
21719 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021720 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021721 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021722 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021724}
21725
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021726/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021727 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021728 * Also give an error message.
21729 */
21730 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021731var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000021732{
21733 if (flags & DI_FLAGS_RO)
21734 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021735 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021736 return TRUE;
21737 }
21738 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21739 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021740 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021741 return TRUE;
21742 }
21743 return FALSE;
21744}
21745
21746/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021747 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21748 * Also give an error message.
21749 */
21750 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021751var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021752{
21753 if (flags & DI_FLAGS_FIX)
21754 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021755 EMSG2(_("E795: Cannot delete variable %s"),
21756 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021757 return TRUE;
21758 }
21759 return FALSE;
21760}
21761
21762/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021763 * Check if a funcref is assigned to a valid variable name.
21764 * Return TRUE and give an error if not.
21765 */
21766 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021767var_check_func_name(
21768 char_u *name, /* points to start of variable name */
21769 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021770{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021771 /* Allow for w: b: s: and t:. */
21772 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021773 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21774 ? name[2] : name[0]))
21775 {
21776 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21777 name);
21778 return TRUE;
21779 }
21780 /* Don't allow hiding a function. When "v" is not NULL we might be
21781 * assigning another function to the same var, the type is checked
21782 * below. */
21783 if (new_var && function_exists(name))
21784 {
21785 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21786 name);
21787 return TRUE;
21788 }
21789 return FALSE;
21790}
21791
21792/*
21793 * Check if a variable name is valid.
21794 * Return FALSE and give an error if not.
21795 */
21796 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021797valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021798{
21799 char_u *p;
21800
21801 for (p = varname; *p != NUL; ++p)
21802 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21803 && *p != AUTOLOAD_CHAR)
21804 {
21805 EMSG2(_(e_illvar), varname);
21806 return FALSE;
21807 }
21808 return TRUE;
21809}
21810
21811/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021812 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020021813 * Also give an error message, using "name" or _("name") when use_gettext is
21814 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021815 */
21816 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021817tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021818{
21819 if (lock & VAR_LOCKED)
21820 {
21821 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021822 name == NULL ? (char_u *)_("Unknown")
21823 : use_gettext ? (char_u *)_(name)
21824 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021825 return TRUE;
21826 }
21827 if (lock & VAR_FIXED)
21828 {
21829 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021830 name == NULL ? (char_u *)_("Unknown")
21831 : use_gettext ? (char_u *)_(name)
21832 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021833 return TRUE;
21834 }
21835 return FALSE;
21836}
21837
21838/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021839 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021840 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021841 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021842 * It is OK for "from" and "to" to point to the same item. This is used to
21843 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021844 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021845 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021846copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021847{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021848 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021849 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021850 switch (from->v_type)
21851 {
21852 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021853 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021854 to->vval.v_number = from->vval.v_number;
21855 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021856#ifdef FEAT_FLOAT
21857 case VAR_FLOAT:
21858 to->vval.v_float = from->vval.v_float;
21859 break;
21860#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021861 case VAR_STRING:
21862 case VAR_FUNC:
21863 if (from->vval.v_string == NULL)
21864 to->vval.v_string = NULL;
21865 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021866 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021867 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021868 if (from->v_type == VAR_FUNC)
21869 func_ref(to->vval.v_string);
21870 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021871 break;
21872 case VAR_LIST:
21873 if (from->vval.v_list == NULL)
21874 to->vval.v_list = NULL;
21875 else
21876 {
21877 to->vval.v_list = from->vval.v_list;
21878 ++to->vval.v_list->lv_refcount;
21879 }
21880 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021881 case VAR_DICT:
21882 if (from->vval.v_dict == NULL)
21883 to->vval.v_dict = NULL;
21884 else
21885 {
21886 to->vval.v_dict = from->vval.v_dict;
21887 ++to->vval.v_dict->dv_refcount;
21888 }
21889 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021890 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021891 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021892 break;
21893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021894}
21895
21896/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021897 * Make a copy of an item.
21898 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021899 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21900 * reference to an already copied list/dict can be used.
21901 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021902 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021903 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021904item_copy(
21905 typval_T *from,
21906 typval_T *to,
21907 int deep,
21908 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021909{
21910 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021911 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021912
Bram Moolenaar33570922005-01-25 22:26:29 +000021913 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021914 {
21915 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021916 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021917 }
21918 ++recurse;
21919
21920 switch (from->v_type)
21921 {
21922 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021923#ifdef FEAT_FLOAT
21924 case VAR_FLOAT:
21925#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021926 case VAR_STRING:
21927 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010021928 case VAR_SPECIAL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000021929 copy_tv(from, to);
21930 break;
21931 case VAR_LIST:
21932 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021933 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021934 if (from->vval.v_list == NULL)
21935 to->vval.v_list = NULL;
21936 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21937 {
21938 /* use the copy made earlier */
21939 to->vval.v_list = from->vval.v_list->lv_copylist;
21940 ++to->vval.v_list->lv_refcount;
21941 }
21942 else
21943 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21944 if (to->vval.v_list == NULL)
21945 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021946 break;
21947 case VAR_DICT:
21948 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021949 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021950 if (from->vval.v_dict == NULL)
21951 to->vval.v_dict = NULL;
21952 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21953 {
21954 /* use the copy made earlier */
21955 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21956 ++to->vval.v_dict->dv_refcount;
21957 }
21958 else
21959 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21960 if (to->vval.v_dict == NULL)
21961 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021962 break;
21963 default:
21964 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021965 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021966 }
21967 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021968 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021969}
21970
21971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021972 * ":echo expr1 ..." print each argument separated with a space, add a
21973 * newline at the end.
21974 * ":echon expr1 ..." print each argument plain.
21975 */
21976 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021977ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021978{
21979 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021980 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021981 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021982 char_u *p;
21983 int needclr = TRUE;
21984 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021985 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021986
21987 if (eap->skip)
21988 ++emsg_skip;
21989 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21990 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021991 /* If eval1() causes an error message the text from the command may
21992 * still need to be cleared. E.g., "echo 22,44". */
21993 need_clr_eos = needclr;
21994
Bram Moolenaar071d4272004-06-13 20:20:40 +000021995 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021996 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021997 {
21998 /*
21999 * Report the invalid expression unless the expression evaluation
22000 * has been cancelled due to an aborting error, an interrupt, or an
22001 * exception.
22002 */
22003 if (!aborting())
22004 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022005 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022006 break;
22007 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022008 need_clr_eos = FALSE;
22009
Bram Moolenaar071d4272004-06-13 20:20:40 +000022010 if (!eap->skip)
22011 {
22012 if (atstart)
22013 {
22014 atstart = FALSE;
22015 /* Call msg_start() after eval1(), evaluating the expression
22016 * may cause a message to appear. */
22017 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022018 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022019 /* Mark the saved text as finishing the line, so that what
22020 * follows is displayed on a new line when scrolling back
22021 * at the more prompt. */
22022 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022023 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022025 }
22026 else if (eap->cmdidx == CMD_echo)
22027 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022028 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022029 if (p != NULL)
22030 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022031 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022032 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022033 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022034 if (*p != TAB && needclr)
22035 {
22036 /* remove any text still there from the command */
22037 msg_clr_eos();
22038 needclr = FALSE;
22039 }
22040 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022041 }
22042 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022043 {
22044#ifdef FEAT_MBYTE
22045 if (has_mbyte)
22046 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022047 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022048
22049 (void)msg_outtrans_len_attr(p, i, echo_attr);
22050 p += i - 1;
22051 }
22052 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022053#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022054 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022056 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022057 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022058 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022059 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022060 arg = skipwhite(arg);
22061 }
22062 eap->nextcmd = check_nextcmd(arg);
22063
22064 if (eap->skip)
22065 --emsg_skip;
22066 else
22067 {
22068 /* remove text that may still be there from the command */
22069 if (needclr)
22070 msg_clr_eos();
22071 if (eap->cmdidx == CMD_echo)
22072 msg_end();
22073 }
22074}
22075
22076/*
22077 * ":echohl {name}".
22078 */
22079 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022080ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022081{
22082 int id;
22083
22084 id = syn_name2id(eap->arg);
22085 if (id == 0)
22086 echo_attr = 0;
22087 else
22088 echo_attr = syn_id2attr(id);
22089}
22090
22091/*
22092 * ":execute expr1 ..." execute the result of an expression.
22093 * ":echomsg expr1 ..." Print a message
22094 * ":echoerr expr1 ..." Print an error
22095 * Each gets spaces around each argument and a newline at the end for
22096 * echo commands
22097 */
22098 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022099ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100{
22101 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022102 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022103 int ret = OK;
22104 char_u *p;
22105 garray_T ga;
22106 int len;
22107 int save_did_emsg;
22108
22109 ga_init2(&ga, 1, 80);
22110
22111 if (eap->skip)
22112 ++emsg_skip;
22113 while (*arg != NUL && *arg != '|' && *arg != '\n')
22114 {
22115 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022116 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022117 {
22118 /*
22119 * Report the invalid expression unless the expression evaluation
22120 * has been cancelled due to an aborting error, an interrupt, or an
22121 * exception.
22122 */
22123 if (!aborting())
22124 EMSG2(_(e_invexpr2), p);
22125 ret = FAIL;
22126 break;
22127 }
22128
22129 if (!eap->skip)
22130 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022131 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022132 len = (int)STRLEN(p);
22133 if (ga_grow(&ga, len + 2) == FAIL)
22134 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022135 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022136 ret = FAIL;
22137 break;
22138 }
22139 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022140 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022141 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022142 ga.ga_len += len;
22143 }
22144
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022145 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146 arg = skipwhite(arg);
22147 }
22148
22149 if (ret != FAIL && ga.ga_data != NULL)
22150 {
22151 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022152 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022153 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022154 out_flush();
22155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022156 else if (eap->cmdidx == CMD_echoerr)
22157 {
22158 /* We don't want to abort following commands, restore did_emsg. */
22159 save_did_emsg = did_emsg;
22160 EMSG((char_u *)ga.ga_data);
22161 if (!force_abort)
22162 did_emsg = save_did_emsg;
22163 }
22164 else if (eap->cmdidx == CMD_execute)
22165 do_cmdline((char_u *)ga.ga_data,
22166 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22167 }
22168
22169 ga_clear(&ga);
22170
22171 if (eap->skip)
22172 --emsg_skip;
22173
22174 eap->nextcmd = check_nextcmd(arg);
22175}
22176
22177/*
22178 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22179 * "arg" points to the "&" or '+' when called, to "option" when returning.
22180 * Returns NULL when no option name found. Otherwise pointer to the char
22181 * after the option name.
22182 */
22183 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022184find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022185{
22186 char_u *p = *arg;
22187
22188 ++p;
22189 if (*p == 'g' && p[1] == ':')
22190 {
22191 *opt_flags = OPT_GLOBAL;
22192 p += 2;
22193 }
22194 else if (*p == 'l' && p[1] == ':')
22195 {
22196 *opt_flags = OPT_LOCAL;
22197 p += 2;
22198 }
22199 else
22200 *opt_flags = 0;
22201
22202 if (!ASCII_ISALPHA(*p))
22203 return NULL;
22204 *arg = p;
22205
22206 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22207 p += 4; /* termcap option */
22208 else
22209 while (ASCII_ISALPHA(*p))
22210 ++p;
22211 return p;
22212}
22213
22214/*
22215 * ":function"
22216 */
22217 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022218ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022219{
22220 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022221 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022222 int j;
22223 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022224 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022225 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022226 char_u *name = NULL;
22227 char_u *p;
22228 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022229 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022230 garray_T newargs;
22231 garray_T newlines;
22232 int varargs = FALSE;
22233 int mustend = FALSE;
22234 int flags = 0;
22235 ufunc_T *fp;
22236 int indent;
22237 int nesting;
22238 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022239 dictitem_T *v;
22240 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022241 static int func_nr = 0; /* number for nameless function */
22242 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022243 hashtab_T *ht;
22244 int todo;
22245 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022246 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022247
22248 /*
22249 * ":function" without argument: list functions.
22250 */
22251 if (ends_excmd(*eap->arg))
22252 {
22253 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022254 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022255 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022256 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022257 {
22258 if (!HASHITEM_EMPTY(hi))
22259 {
22260 --todo;
22261 fp = HI2UF(hi);
22262 if (!isdigit(*fp->uf_name))
22263 list_func_head(fp, FALSE);
22264 }
22265 }
22266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022267 eap->nextcmd = check_nextcmd(eap->arg);
22268 return;
22269 }
22270
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022271 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022272 * ":function /pat": list functions matching pattern.
22273 */
22274 if (*eap->arg == '/')
22275 {
22276 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22277 if (!eap->skip)
22278 {
22279 regmatch_T regmatch;
22280
22281 c = *p;
22282 *p = NUL;
22283 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22284 *p = c;
22285 if (regmatch.regprog != NULL)
22286 {
22287 regmatch.rm_ic = p_ic;
22288
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022289 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022290 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22291 {
22292 if (!HASHITEM_EMPTY(hi))
22293 {
22294 --todo;
22295 fp = HI2UF(hi);
22296 if (!isdigit(*fp->uf_name)
22297 && vim_regexec(&regmatch, fp->uf_name, 0))
22298 list_func_head(fp, FALSE);
22299 }
22300 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022301 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022302 }
22303 }
22304 if (*p == '/')
22305 ++p;
22306 eap->nextcmd = check_nextcmd(p);
22307 return;
22308 }
22309
22310 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022311 * Get the function name. There are these situations:
22312 * func normal function name
22313 * "name" == func, "fudi.fd_dict" == NULL
22314 * dict.func new dictionary entry
22315 * "name" == NULL, "fudi.fd_dict" set,
22316 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22317 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022318 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022319 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22320 * dict.func existing dict entry that's not a Funcref
22321 * "name" == NULL, "fudi.fd_dict" set,
22322 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022323 * s:func script-local function name
22324 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022325 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022326 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022327 name = trans_function_name(&p, eap->skip, 0, &fudi);
22328 paren = (vim_strchr(p, '(') != NULL);
22329 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330 {
22331 /*
22332 * Return on an invalid expression in braces, unless the expression
22333 * evaluation has been cancelled due to an aborting error, an
22334 * interrupt, or an exception.
22335 */
22336 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022337 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022338 if (!eap->skip && fudi.fd_newkey != NULL)
22339 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022340 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022341 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022343 else
22344 eap->skip = TRUE;
22345 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022346
Bram Moolenaar071d4272004-06-13 20:20:40 +000022347 /* An error in a function call during evaluation of an expression in magic
22348 * braces should not cause the function not to be defined. */
22349 saved_did_emsg = did_emsg;
22350 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022351
22352 /*
22353 * ":function func" with only function name: list function.
22354 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022355 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356 {
22357 if (!ends_excmd(*skipwhite(p)))
22358 {
22359 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022360 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022361 }
22362 eap->nextcmd = check_nextcmd(p);
22363 if (eap->nextcmd != NULL)
22364 *p = NUL;
22365 if (!eap->skip && !got_int)
22366 {
22367 fp = find_func(name);
22368 if (fp != NULL)
22369 {
22370 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022371 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022373 if (FUNCLINE(fp, j) == NULL)
22374 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022375 msg_putchar('\n');
22376 msg_outnum((long)(j + 1));
22377 if (j < 9)
22378 msg_putchar(' ');
22379 if (j < 99)
22380 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022381 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022382 out_flush(); /* show a line at a time */
22383 ui_breakcheck();
22384 }
22385 if (!got_int)
22386 {
22387 msg_putchar('\n');
22388 msg_puts((char_u *)" endfunction");
22389 }
22390 }
22391 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022392 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022393 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022394 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022395 }
22396
22397 /*
22398 * ":function name(arg1, arg2)" Define function.
22399 */
22400 p = skipwhite(p);
22401 if (*p != '(')
22402 {
22403 if (!eap->skip)
22404 {
22405 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022406 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022407 }
22408 /* attempt to continue by skipping some text */
22409 if (vim_strchr(p, '(') != NULL)
22410 p = vim_strchr(p, '(');
22411 }
22412 p = skipwhite(p + 1);
22413
22414 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22415 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22416
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022417 if (!eap->skip)
22418 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022419 /* Check the name of the function. Unless it's a dictionary function
22420 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022421 if (name != NULL)
22422 arg = name;
22423 else
22424 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022425 if (arg != NULL && (fudi.fd_di == NULL
22426 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022427 {
22428 if (*arg == K_SPECIAL)
22429 j = 3;
22430 else
22431 j = 0;
22432 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22433 : eval_isnamec(arg[j])))
22434 ++j;
22435 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022436 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022437 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022438 /* Disallow using the g: dict. */
22439 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22440 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022441 }
22442
Bram Moolenaar071d4272004-06-13 20:20:40 +000022443 /*
22444 * Isolate the arguments: "arg1, arg2, ...)"
22445 */
22446 while (*p != ')')
22447 {
22448 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22449 {
22450 varargs = TRUE;
22451 p += 3;
22452 mustend = TRUE;
22453 }
22454 else
22455 {
22456 arg = p;
22457 while (ASCII_ISALNUM(*p) || *p == '_')
22458 ++p;
22459 if (arg == p || isdigit(*arg)
22460 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22461 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22462 {
22463 if (!eap->skip)
22464 EMSG2(_("E125: Illegal argument: %s"), arg);
22465 break;
22466 }
22467 if (ga_grow(&newargs, 1) == FAIL)
22468 goto erret;
22469 c = *p;
22470 *p = NUL;
22471 arg = vim_strsave(arg);
22472 if (arg == NULL)
22473 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022474
22475 /* Check for duplicate argument name. */
22476 for (i = 0; i < newargs.ga_len; ++i)
22477 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22478 {
22479 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022480 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022481 goto erret;
22482 }
22483
Bram Moolenaar071d4272004-06-13 20:20:40 +000022484 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22485 *p = c;
22486 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022487 if (*p == ',')
22488 ++p;
22489 else
22490 mustend = TRUE;
22491 }
22492 p = skipwhite(p);
22493 if (mustend && *p != ')')
22494 {
22495 if (!eap->skip)
22496 EMSG2(_(e_invarg2), eap->arg);
22497 break;
22498 }
22499 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022500 if (*p != ')')
22501 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022502 ++p; /* skip the ')' */
22503
Bram Moolenaare9a41262005-01-15 22:18:47 +000022504 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022505 for (;;)
22506 {
22507 p = skipwhite(p);
22508 if (STRNCMP(p, "range", 5) == 0)
22509 {
22510 flags |= FC_RANGE;
22511 p += 5;
22512 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022513 else if (STRNCMP(p, "dict", 4) == 0)
22514 {
22515 flags |= FC_DICT;
22516 p += 4;
22517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022518 else if (STRNCMP(p, "abort", 5) == 0)
22519 {
22520 flags |= FC_ABORT;
22521 p += 5;
22522 }
22523 else
22524 break;
22525 }
22526
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022527 /* When there is a line break use what follows for the function body.
22528 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22529 if (*p == '\n')
22530 line_arg = p + 1;
22531 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022532 EMSG(_(e_trailing));
22533
22534 /*
22535 * Read the body of the function, until ":endfunction" is found.
22536 */
22537 if (KeyTyped)
22538 {
22539 /* Check if the function already exists, don't let the user type the
22540 * whole function before telling him it doesn't work! For a script we
22541 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022542 if (!eap->skip && !eap->forceit)
22543 {
22544 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22545 EMSG(_(e_funcdict));
22546 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022547 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022549
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022550 if (!eap->skip && did_emsg)
22551 goto erret;
22552
Bram Moolenaar071d4272004-06-13 20:20:40 +000022553 msg_putchar('\n'); /* don't overwrite the function name */
22554 cmdline_row = msg_row;
22555 }
22556
22557 indent = 2;
22558 nesting = 0;
22559 for (;;)
22560 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022561 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022562 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022563 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022564 saved_wait_return = FALSE;
22565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022566 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022567 sourcing_lnum_off = sourcing_lnum;
22568
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022569 if (line_arg != NULL)
22570 {
22571 /* Use eap->arg, split up in parts by line breaks. */
22572 theline = line_arg;
22573 p = vim_strchr(theline, '\n');
22574 if (p == NULL)
22575 line_arg += STRLEN(line_arg);
22576 else
22577 {
22578 *p = NUL;
22579 line_arg = p + 1;
22580 }
22581 }
22582 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022583 theline = getcmdline(':', 0L, indent);
22584 else
22585 theline = eap->getline(':', eap->cookie, indent);
22586 if (KeyTyped)
22587 lines_left = Rows - 1;
22588 if (theline == NULL)
22589 {
22590 EMSG(_("E126: Missing :endfunction"));
22591 goto erret;
22592 }
22593
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022594 /* Detect line continuation: sourcing_lnum increased more than one. */
22595 if (sourcing_lnum > sourcing_lnum_off + 1)
22596 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22597 else
22598 sourcing_lnum_off = 0;
22599
Bram Moolenaar071d4272004-06-13 20:20:40 +000022600 if (skip_until != NULL)
22601 {
22602 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22603 * don't check for ":endfunc". */
22604 if (STRCMP(theline, skip_until) == 0)
22605 {
22606 vim_free(skip_until);
22607 skip_until = NULL;
22608 }
22609 }
22610 else
22611 {
22612 /* skip ':' and blanks*/
22613 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22614 ;
22615
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022616 /* Check for "endfunction". */
22617 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022618 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022619 if (line_arg == NULL)
22620 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022621 break;
22622 }
22623
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022624 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022625 * at "end". */
22626 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22627 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022628 else if (STRNCMP(p, "if", 2) == 0
22629 || STRNCMP(p, "wh", 2) == 0
22630 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022631 || STRNCMP(p, "try", 3) == 0)
22632 indent += 2;
22633
22634 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022635 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022636 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022637 if (*p == '!')
22638 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022639 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022640 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22641 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022642 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022643 ++nesting;
22644 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645 }
22646 }
22647
22648 /* Check for ":append" or ":insert". */
22649 p = skip_range(p, NULL);
22650 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22651 || (p[0] == 'i'
22652 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22653 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22654 skip_until = vim_strsave((char_u *)".");
22655
22656 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22657 arg = skipwhite(skiptowhite(p));
22658 if (arg[0] == '<' && arg[1] =='<'
22659 && ((p[0] == 'p' && p[1] == 'y'
22660 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22661 || (p[0] == 'p' && p[1] == 'e'
22662 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22663 || (p[0] == 't' && p[1] == 'c'
22664 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022665 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22666 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022667 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22668 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022669 || (p[0] == 'm' && p[1] == 'z'
22670 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022671 ))
22672 {
22673 /* ":python <<" continues until a dot, like ":append" */
22674 p = skipwhite(arg + 2);
22675 if (*p == NUL)
22676 skip_until = vim_strsave((char_u *)".");
22677 else
22678 skip_until = vim_strsave(p);
22679 }
22680 }
22681
22682 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022683 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022684 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022685 if (line_arg == NULL)
22686 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022687 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022688 }
22689
22690 /* Copy the line to newly allocated memory. get_one_sourceline()
22691 * allocates 250 bytes per line, this saves 80% on average. The cost
22692 * is an extra alloc/free. */
22693 p = vim_strsave(theline);
22694 if (p != NULL)
22695 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022696 if (line_arg == NULL)
22697 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022698 theline = p;
22699 }
22700
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022701 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22702
22703 /* Add NULL lines for continuation lines, so that the line count is
22704 * equal to the index in the growarray. */
22705 while (sourcing_lnum_off-- > 0)
22706 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022707
22708 /* Check for end of eap->arg. */
22709 if (line_arg != NULL && *line_arg == NUL)
22710 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022711 }
22712
22713 /* Don't define the function when skipping commands or when an error was
22714 * detected. */
22715 if (eap->skip || did_emsg)
22716 goto erret;
22717
22718 /*
22719 * If there are no errors, add the function
22720 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022721 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022722 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022723 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022724 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022725 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022726 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022727 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022728 goto erret;
22729 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022730
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022731 fp = find_func(name);
22732 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022733 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022734 if (!eap->forceit)
22735 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022736 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022737 goto erret;
22738 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022739 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022740 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022741 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022742 name);
22743 goto erret;
22744 }
22745 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022746 ga_clear_strings(&(fp->uf_args));
22747 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022748 vim_free(name);
22749 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022751 }
22752 else
22753 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022754 char numbuf[20];
22755
22756 fp = NULL;
22757 if (fudi.fd_newkey == NULL && !eap->forceit)
22758 {
22759 EMSG(_(e_funcdict));
22760 goto erret;
22761 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022762 if (fudi.fd_di == NULL)
22763 {
22764 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022765 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022766 goto erret;
22767 }
22768 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022769 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022770 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022771
22772 /* Give the function a sequential number. Can only be used with a
22773 * Funcref! */
22774 vim_free(name);
22775 sprintf(numbuf, "%d", ++func_nr);
22776 name = vim_strsave((char_u *)numbuf);
22777 if (name == NULL)
22778 goto erret;
22779 }
22780
22781 if (fp == NULL)
22782 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022783 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022784 {
22785 int slen, plen;
22786 char_u *scriptname;
22787
22788 /* Check that the autoload name matches the script name. */
22789 j = FAIL;
22790 if (sourcing_name != NULL)
22791 {
22792 scriptname = autoload_name(name);
22793 if (scriptname != NULL)
22794 {
22795 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022796 plen = (int)STRLEN(p);
22797 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022798 if (slen > plen && fnamecmp(p,
22799 sourcing_name + slen - plen) == 0)
22800 j = OK;
22801 vim_free(scriptname);
22802 }
22803 }
22804 if (j == FAIL)
22805 {
22806 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22807 goto erret;
22808 }
22809 }
22810
22811 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022812 if (fp == NULL)
22813 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022814
22815 if (fudi.fd_dict != NULL)
22816 {
22817 if (fudi.fd_di == NULL)
22818 {
22819 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022820 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022821 if (fudi.fd_di == NULL)
22822 {
22823 vim_free(fp);
22824 goto erret;
22825 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022826 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22827 {
22828 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022829 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022830 goto erret;
22831 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022832 }
22833 else
22834 /* overwrite existing dict entry */
22835 clear_tv(&fudi.fd_di->di_tv);
22836 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022837 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022838 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022839 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022840
22841 /* behave like "dict" was used */
22842 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022843 }
22844
Bram Moolenaar071d4272004-06-13 20:20:40 +000022845 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022846 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010022847 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
22848 {
22849 vim_free(fp);
22850 goto erret;
22851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022852 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022853 fp->uf_args = newargs;
22854 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022855#ifdef FEAT_PROFILE
22856 fp->uf_tml_count = NULL;
22857 fp->uf_tml_total = NULL;
22858 fp->uf_tml_self = NULL;
22859 fp->uf_profiling = FALSE;
22860 if (prof_def_func())
22861 func_do_profile(fp);
22862#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022863 fp->uf_varargs = varargs;
22864 fp->uf_flags = flags;
22865 fp->uf_calls = 0;
22866 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022867 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868
22869erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022870 ga_clear_strings(&newargs);
22871 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022872ret_free:
22873 vim_free(skip_until);
22874 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022875 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022876 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022877 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022878}
22879
22880/*
22881 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022882 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022883 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022884 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010022885 * TFN_INT: internal function name OK
22886 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022887 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022888 * Advances "pp" to just after the function name (if no error).
22889 */
22890 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022891trans_function_name(
22892 char_u **pp,
22893 int skip, /* only find the end, don't evaluate */
22894 int flags,
22895 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022896{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022897 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022898 char_u *start;
22899 char_u *end;
22900 int lead;
22901 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022902 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022903 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022904
22905 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022906 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022907 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022908
22909 /* Check for hard coded <SNR>: already translated function ID (from a user
22910 * command). */
22911 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22912 && (*pp)[2] == (int)KE_SNR)
22913 {
22914 *pp += 3;
22915 len = get_id_len(pp) + 3;
22916 return vim_strnsave(start, len);
22917 }
22918
22919 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22920 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022922 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022923 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022924
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022925 /* Note that TFN_ flags use the same values as GLV_ flags. */
22926 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022927 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022928 if (end == start)
22929 {
22930 if (!skip)
22931 EMSG(_("E129: Function name required"));
22932 goto theend;
22933 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022934 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022935 {
22936 /*
22937 * Report an invalid expression in braces, unless the expression
22938 * evaluation has been cancelled due to an aborting error, an
22939 * interrupt, or an exception.
22940 */
22941 if (!aborting())
22942 {
22943 if (end != NULL)
22944 EMSG2(_(e_invarg2), start);
22945 }
22946 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022947 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022948 goto theend;
22949 }
22950
22951 if (lv.ll_tv != NULL)
22952 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022953 if (fdp != NULL)
22954 {
22955 fdp->fd_dict = lv.ll_dict;
22956 fdp->fd_newkey = lv.ll_newkey;
22957 lv.ll_newkey = NULL;
22958 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022959 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022960 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22961 {
22962 name = vim_strsave(lv.ll_tv->vval.v_string);
22963 *pp = end;
22964 }
22965 else
22966 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022967 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22968 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022969 EMSG(_(e_funcref));
22970 else
22971 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022972 name = NULL;
22973 }
22974 goto theend;
22975 }
22976
22977 if (lv.ll_name == NULL)
22978 {
22979 /* Error found, but continue after the function name. */
22980 *pp = end;
22981 goto theend;
22982 }
22983
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022984 /* Check if the name is a Funcref. If so, use the value. */
22985 if (lv.ll_exp_name != NULL)
22986 {
22987 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022988 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022989 if (name == lv.ll_exp_name)
22990 name = NULL;
22991 }
22992 else
22993 {
22994 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022995 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022996 if (name == *pp)
22997 name = NULL;
22998 }
22999 if (name != NULL)
23000 {
23001 name = vim_strsave(name);
23002 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023003 if (STRNCMP(name, "<SNR>", 5) == 0)
23004 {
23005 /* Change "<SNR>" to the byte sequence. */
23006 name[0] = K_SPECIAL;
23007 name[1] = KS_EXTRA;
23008 name[2] = (int)KE_SNR;
23009 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23010 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023011 goto theend;
23012 }
23013
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023014 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023015 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023016 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023017 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23018 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23019 {
23020 /* When there was "s:" already or the name expanded to get a
23021 * leading "s:" then remove it. */
23022 lv.ll_name += 2;
23023 len -= 2;
23024 lead = 2;
23025 }
23026 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023027 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023028 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023029 /* skip over "s:" and "g:" */
23030 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023031 lv.ll_name += 2;
23032 len = (int)(end - lv.ll_name);
23033 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023034
23035 /*
23036 * Copy the function name to allocated memory.
23037 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23038 * Accept <SNR>123_name() outside a script.
23039 */
23040 if (skip)
23041 lead = 0; /* do nothing */
23042 else if (lead > 0)
23043 {
23044 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023045 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23046 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023047 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023048 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023049 if (current_SID <= 0)
23050 {
23051 EMSG(_(e_usingsid));
23052 goto theend;
23053 }
23054 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23055 lead += (int)STRLEN(sid_buf);
23056 }
23057 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023058 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023059 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023060 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023061 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023062 goto theend;
23063 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023064 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023065 {
23066 char_u *cp = vim_strchr(lv.ll_name, ':');
23067
23068 if (cp != NULL && cp < end)
23069 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023070 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023071 goto theend;
23072 }
23073 }
23074
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023075 name = alloc((unsigned)(len + lead + 1));
23076 if (name != NULL)
23077 {
23078 if (lead > 0)
23079 {
23080 name[0] = K_SPECIAL;
23081 name[1] = KS_EXTRA;
23082 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023083 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023084 STRCPY(name + 3, sid_buf);
23085 }
23086 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023087 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023088 }
23089 *pp = end;
23090
23091theend:
23092 clear_lval(&lv);
23093 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023094}
23095
23096/*
23097 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23098 * Return 2 if "p" starts with "s:".
23099 * Return 0 otherwise.
23100 */
23101 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023102eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023103{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023104 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23105 * the standard library function. */
23106 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23107 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023108 return 5;
23109 if (p[0] == 's' && p[1] == ':')
23110 return 2;
23111 return 0;
23112}
23113
23114/*
23115 * Return TRUE if "p" starts with "<SID>" or "s:".
23116 * Only works if eval_fname_script() returned non-zero for "p"!
23117 */
23118 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023119eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023120{
23121 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23122}
23123
23124/*
23125 * List the head of the function: "name(arg1, arg2)".
23126 */
23127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023128list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023129{
23130 int j;
23131
23132 msg_start();
23133 if (indent)
23134 MSG_PUTS(" ");
23135 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023136 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023137 {
23138 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023139 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023140 }
23141 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023142 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023143 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023144 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145 {
23146 if (j)
23147 MSG_PUTS(", ");
23148 msg_puts(FUNCARG(fp, j));
23149 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023150 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023151 {
23152 if (j)
23153 MSG_PUTS(", ");
23154 MSG_PUTS("...");
23155 }
23156 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023157 if (fp->uf_flags & FC_ABORT)
23158 MSG_PUTS(" abort");
23159 if (fp->uf_flags & FC_RANGE)
23160 MSG_PUTS(" range");
23161 if (fp->uf_flags & FC_DICT)
23162 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023163 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023164 if (p_verbose > 0)
23165 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023166}
23167
23168/*
23169 * Find a function by name, return pointer to it in ufuncs.
23170 * Return NULL for unknown function.
23171 */
23172 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023173find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023174{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023175 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023176
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023177 hi = hash_find(&func_hashtab, name);
23178 if (!HASHITEM_EMPTY(hi))
23179 return HI2UF(hi);
23180 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023181}
23182
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023183#if defined(EXITFREE) || defined(PROTO)
23184 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023185free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023186{
23187 hashitem_T *hi;
23188
23189 /* Need to start all over every time, because func_free() may change the
23190 * hash table. */
23191 while (func_hashtab.ht_used > 0)
23192 for (hi = func_hashtab.ht_array; ; ++hi)
23193 if (!HASHITEM_EMPTY(hi))
23194 {
23195 func_free(HI2UF(hi));
23196 break;
23197 }
23198}
23199#endif
23200
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023201 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023202translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023203{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023204 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023205 return find_internal_func(name) >= 0;
23206 return find_func(name) != NULL;
23207}
23208
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023209/*
23210 * Return TRUE if a function "name" exists.
23211 */
23212 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023213function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023214{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023215 char_u *nm = name;
23216 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023217 int n = FALSE;
23218
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023219 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23220 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023221 nm = skipwhite(nm);
23222
23223 /* Only accept "funcname", "funcname ", "funcname (..." and
23224 * "funcname(...", not "funcname!...". */
23225 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023226 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023227 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023228 return n;
23229}
23230
Bram Moolenaara1544c02013-05-30 12:35:52 +020023231 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023232get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023233{
23234 char_u *nm = name;
23235 char_u *p;
23236
23237 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23238
23239 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023240 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023241 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023242
Bram Moolenaara1544c02013-05-30 12:35:52 +020023243 vim_free(p);
23244 return NULL;
23245}
23246
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023247/*
23248 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023249 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23250 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023251 */
23252 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023253builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023254{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023255 char_u *p;
23256
23257 if (!ASCII_ISLOWER(name[0]))
23258 return FALSE;
23259 p = vim_strchr(name, AUTOLOAD_CHAR);
23260 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023261}
23262
Bram Moolenaar05159a02005-02-26 23:04:13 +000023263#if defined(FEAT_PROFILE) || defined(PROTO)
23264/*
23265 * Start profiling function "fp".
23266 */
23267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023268func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023269{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023270 int len = fp->uf_lines.ga_len;
23271
23272 if (len == 0)
23273 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023274 fp->uf_tm_count = 0;
23275 profile_zero(&fp->uf_tm_self);
23276 profile_zero(&fp->uf_tm_total);
23277 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023278 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023279 if (fp->uf_tml_total == NULL)
23280 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023281 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023282 if (fp->uf_tml_self == NULL)
23283 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023284 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023285 fp->uf_tml_idx = -1;
23286 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23287 || fp->uf_tml_self == NULL)
23288 return; /* out of memory */
23289
23290 fp->uf_profiling = TRUE;
23291}
23292
23293/*
23294 * Dump the profiling results for all functions in file "fd".
23295 */
23296 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023297func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023298{
23299 hashitem_T *hi;
23300 int todo;
23301 ufunc_T *fp;
23302 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023303 ufunc_T **sorttab;
23304 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023305
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023306 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023307 if (todo == 0)
23308 return; /* nothing to dump */
23309
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023310 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023311
Bram Moolenaar05159a02005-02-26 23:04:13 +000023312 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23313 {
23314 if (!HASHITEM_EMPTY(hi))
23315 {
23316 --todo;
23317 fp = HI2UF(hi);
23318 if (fp->uf_profiling)
23319 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023320 if (sorttab != NULL)
23321 sorttab[st_len++] = fp;
23322
Bram Moolenaar05159a02005-02-26 23:04:13 +000023323 if (fp->uf_name[0] == K_SPECIAL)
23324 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23325 else
23326 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23327 if (fp->uf_tm_count == 1)
23328 fprintf(fd, "Called 1 time\n");
23329 else
23330 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23331 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23332 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23333 fprintf(fd, "\n");
23334 fprintf(fd, "count total (s) self (s)\n");
23335
23336 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23337 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023338 if (FUNCLINE(fp, i) == NULL)
23339 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023340 prof_func_line(fd, fp->uf_tml_count[i],
23341 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023342 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23343 }
23344 fprintf(fd, "\n");
23345 }
23346 }
23347 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023348
23349 if (sorttab != NULL && st_len > 0)
23350 {
23351 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23352 prof_total_cmp);
23353 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23354 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23355 prof_self_cmp);
23356 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23357 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023358
23359 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023360}
Bram Moolenaar73830342005-02-28 22:48:19 +000023361
23362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023363prof_sort_list(
23364 FILE *fd,
23365 ufunc_T **sorttab,
23366 int st_len,
23367 char *title,
23368 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023369{
23370 int i;
23371 ufunc_T *fp;
23372
23373 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23374 fprintf(fd, "count total (s) self (s) function\n");
23375 for (i = 0; i < 20 && i < st_len; ++i)
23376 {
23377 fp = sorttab[i];
23378 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23379 prefer_self);
23380 if (fp->uf_name[0] == K_SPECIAL)
23381 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23382 else
23383 fprintf(fd, " %s()\n", fp->uf_name);
23384 }
23385 fprintf(fd, "\n");
23386}
23387
23388/*
23389 * Print the count and times for one function or function line.
23390 */
23391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023392prof_func_line(
23393 FILE *fd,
23394 int count,
23395 proftime_T *total,
23396 proftime_T *self,
23397 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023398{
23399 if (count > 0)
23400 {
23401 fprintf(fd, "%5d ", count);
23402 if (prefer_self && profile_equal(total, self))
23403 fprintf(fd, " ");
23404 else
23405 fprintf(fd, "%s ", profile_msg(total));
23406 if (!prefer_self && profile_equal(total, self))
23407 fprintf(fd, " ");
23408 else
23409 fprintf(fd, "%s ", profile_msg(self));
23410 }
23411 else
23412 fprintf(fd, " ");
23413}
23414
23415/*
23416 * Compare function for total time sorting.
23417 */
23418 static int
23419#ifdef __BORLANDC__
23420_RTLENTRYF
23421#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023422prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023423{
23424 ufunc_T *p1, *p2;
23425
23426 p1 = *(ufunc_T **)s1;
23427 p2 = *(ufunc_T **)s2;
23428 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23429}
23430
23431/*
23432 * Compare function for self time sorting.
23433 */
23434 static int
23435#ifdef __BORLANDC__
23436_RTLENTRYF
23437#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023438prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023439{
23440 ufunc_T *p1, *p2;
23441
23442 p1 = *(ufunc_T **)s1;
23443 p2 = *(ufunc_T **)s2;
23444 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23445}
23446
Bram Moolenaar05159a02005-02-26 23:04:13 +000023447#endif
23448
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023449/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023450 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023451 * Return TRUE if a package was loaded.
23452 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023453 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023454script_autoload(
23455 char_u *name,
23456 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023457{
23458 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023459 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023460 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023461 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023462
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023463 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023464 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023465 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023466 return FALSE;
23467
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023468 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023469
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023470 /* Find the name in the list of previously loaded package names. Skip
23471 * "autoload/", it's always the same. */
23472 for (i = 0; i < ga_loaded.ga_len; ++i)
23473 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23474 break;
23475 if (!reload && i < ga_loaded.ga_len)
23476 ret = FALSE; /* was loaded already */
23477 else
23478 {
23479 /* Remember the name if it wasn't loaded already. */
23480 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23481 {
23482 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23483 tofree = NULL;
23484 }
23485
23486 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023487 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023488 ret = TRUE;
23489 }
23490
23491 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023492 return ret;
23493}
23494
23495/*
23496 * Return the autoload script name for a function or variable name.
23497 * Returns NULL when out of memory.
23498 */
23499 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023500autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023501{
23502 char_u *p;
23503 char_u *scriptname;
23504
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023505 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023506 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23507 if (scriptname == NULL)
23508 return FALSE;
23509 STRCPY(scriptname, "autoload/");
23510 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023511 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023512 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023513 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023514 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023515 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023516}
23517
Bram Moolenaar071d4272004-06-13 20:20:40 +000023518#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23519
23520/*
23521 * Function given to ExpandGeneric() to obtain the list of user defined
23522 * function names.
23523 */
23524 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023525get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023526{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023527 static long_u done;
23528 static hashitem_T *hi;
23529 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023530
23531 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023532 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023533 done = 0;
23534 hi = func_hashtab.ht_array;
23535 }
23536 if (done < func_hashtab.ht_used)
23537 {
23538 if (done++ > 0)
23539 ++hi;
23540 while (HASHITEM_EMPTY(hi))
23541 ++hi;
23542 fp = HI2UF(hi);
23543
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023544 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023545 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023546
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023547 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23548 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023549
23550 cat_func_name(IObuff, fp);
23551 if (xp->xp_context != EXPAND_USER_FUNC)
23552 {
23553 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023554 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023555 STRCAT(IObuff, ")");
23556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023557 return IObuff;
23558 }
23559 return NULL;
23560}
23561
23562#endif /* FEAT_CMDL_COMPL */
23563
23564/*
23565 * Copy the function name of "fp" to buffer "buf".
23566 * "buf" must be able to hold the function name plus three bytes.
23567 * Takes care of script-local function names.
23568 */
23569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023570cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023571{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023572 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023573 {
23574 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023575 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023576 }
23577 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023578 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023579}
23580
23581/*
23582 * ":delfunction {name}"
23583 */
23584 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023585ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023586{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023587 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023588 char_u *p;
23589 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023590 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023591
23592 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023593 name = trans_function_name(&p, eap->skip, 0, &fudi);
23594 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023595 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023596 {
23597 if (fudi.fd_dict != NULL && !eap->skip)
23598 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023599 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023601 if (!ends_excmd(*skipwhite(p)))
23602 {
23603 vim_free(name);
23604 EMSG(_(e_trailing));
23605 return;
23606 }
23607 eap->nextcmd = check_nextcmd(p);
23608 if (eap->nextcmd != NULL)
23609 *p = NUL;
23610
23611 if (!eap->skip)
23612 fp = find_func(name);
23613 vim_free(name);
23614
23615 if (!eap->skip)
23616 {
23617 if (fp == NULL)
23618 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023619 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023620 return;
23621 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023622 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023623 {
23624 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23625 return;
23626 }
23627
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023628 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023629 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023630 /* Delete the dict item that refers to the function, it will
23631 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023632 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023633 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023634 else
23635 func_free(fp);
23636 }
23637}
23638
23639/*
23640 * Free a function and remove it from the list of functions.
23641 */
23642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023643func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023644{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023645 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023646
23647 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023648 ga_clear_strings(&(fp->uf_args));
23649 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023650#ifdef FEAT_PROFILE
23651 vim_free(fp->uf_tml_count);
23652 vim_free(fp->uf_tml_total);
23653 vim_free(fp->uf_tml_self);
23654#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023655
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023656 /* remove the function from the function hashtable */
23657 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23658 if (HASHITEM_EMPTY(hi))
23659 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023660 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023661 hash_remove(&func_hashtab, hi);
23662
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023663 vim_free(fp);
23664}
23665
23666/*
23667 * Unreference a Function: decrement the reference count and free it when it
23668 * becomes zero. Only for numbered functions.
23669 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023670 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023671func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023672{
23673 ufunc_T *fp;
23674
23675 if (name != NULL && isdigit(*name))
23676 {
23677 fp = find_func(name);
23678 if (fp == NULL)
23679 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023680 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023681 {
23682 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023683 * when "uf_calls" becomes zero. */
23684 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023685 func_free(fp);
23686 }
23687 }
23688}
23689
23690/*
23691 * Count a reference to a Function.
23692 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023693 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023694func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023695{
23696 ufunc_T *fp;
23697
23698 if (name != NULL && isdigit(*name))
23699 {
23700 fp = find_func(name);
23701 if (fp == NULL)
23702 EMSG2(_(e_intern2), "func_ref()");
23703 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023704 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023705 }
23706}
23707
23708/*
23709 * Call a user function.
23710 */
23711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023712call_user_func(
23713 ufunc_T *fp, /* pointer to function */
23714 int argcount, /* nr of args */
23715 typval_T *argvars, /* arguments */
23716 typval_T *rettv, /* return value */
23717 linenr_T firstline, /* first line of range */
23718 linenr_T lastline, /* last line of range */
23719 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023720{
Bram Moolenaar33570922005-01-25 22:26:29 +000023721 char_u *save_sourcing_name;
23722 linenr_T save_sourcing_lnum;
23723 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023724 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023725 int save_did_emsg;
23726 static int depth = 0;
23727 dictitem_T *v;
23728 int fixvar_idx = 0; /* index in fixvar[] */
23729 int i;
23730 int ai;
23731 char_u numbuf[NUMBUFLEN];
23732 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023733 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023734#ifdef FEAT_PROFILE
23735 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023736 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023738
23739 /* If depth of calling is getting too high, don't execute the function */
23740 if (depth >= p_mfd)
23741 {
23742 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023743 rettv->v_type = VAR_NUMBER;
23744 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023745 return;
23746 }
23747 ++depth;
23748
23749 line_breakcheck(); /* check for CTRL-C hit */
23750
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023751 fc = (funccall_T *)alloc(sizeof(funccall_T));
23752 fc->caller = current_funccal;
23753 current_funccal = fc;
23754 fc->func = fp;
23755 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023756 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023757 fc->linenr = 0;
23758 fc->returned = FALSE;
23759 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023760 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023761 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23762 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023763
Bram Moolenaar33570922005-01-25 22:26:29 +000023764 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023765 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023766 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23767 * each argument variable and saves a lot of time.
23768 */
23769 /*
23770 * Init l: variables.
23771 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023772 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023773 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023774 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023775 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23776 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023777 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023778 name = v->di_key;
23779 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023780 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023781 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023782 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023783 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023784 v->di_tv.vval.v_dict = selfdict;
23785 ++selfdict->dv_refcount;
23786 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023787
Bram Moolenaar33570922005-01-25 22:26:29 +000023788 /*
23789 * Init a: variables.
23790 * Set a:0 to "argcount".
23791 * Set a:000 to a list with room for the "..." arguments.
23792 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023793 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023794 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023795 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023796 /* Use "name" to avoid a warning from some compiler that checks the
23797 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023798 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023799 name = v->di_key;
23800 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023801 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023802 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023803 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023804 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023805 v->di_tv.vval.v_list = &fc->l_varlist;
23806 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23807 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23808 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023809
23810 /*
23811 * Set a:firstline to "firstline" and a:lastline to "lastline".
23812 * Set a:name to named arguments.
23813 * Set a:N to the "..." arguments.
23814 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023815 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023816 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023817 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023818 (varnumber_T)lastline);
23819 for (i = 0; i < argcount; ++i)
23820 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023821 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023822 if (ai < 0)
23823 /* named argument a:name */
23824 name = FUNCARG(fp, i);
23825 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023826 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023827 /* "..." argument a:1, a:2, etc. */
23828 sprintf((char *)numbuf, "%d", ai + 1);
23829 name = numbuf;
23830 }
23831 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23832 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023833 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023834 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23835 }
23836 else
23837 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023838 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23839 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023840 if (v == NULL)
23841 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023842 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000023843 }
23844 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023845 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023846
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023847 /* Note: the values are copied directly to avoid alloc/free.
23848 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023849 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023850 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023851
23852 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23853 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023854 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23855 fc->l_listitems[ai].li_tv = argvars[i];
23856 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023857 }
23858 }
23859
Bram Moolenaar071d4272004-06-13 20:20:40 +000023860 /* Don't redraw while executing the function. */
23861 ++RedrawingDisabled;
23862 save_sourcing_name = sourcing_name;
23863 save_sourcing_lnum = sourcing_lnum;
23864 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023865 /* need space for function name + ("function " + 3) or "[number]" */
23866 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
23867 + STRLEN(fp->uf_name) + 20;
23868 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023869 if (sourcing_name != NULL)
23870 {
23871 if (save_sourcing_name != NULL
23872 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023873 sprintf((char *)sourcing_name, "%s[%d]..",
23874 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023875 else
23876 STRCPY(sourcing_name, "function ");
23877 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23878
23879 if (p_verbose >= 12)
23880 {
23881 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023882 verbose_enter_scroll();
23883
Bram Moolenaar555b2802005-05-19 21:08:39 +000023884 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023885 if (p_verbose >= 14)
23886 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023887 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023888 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023889 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023890 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023891
23892 msg_puts((char_u *)"(");
23893 for (i = 0; i < argcount; ++i)
23894 {
23895 if (i > 0)
23896 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023897 if (argvars[i].v_type == VAR_NUMBER)
23898 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023899 else
23900 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023901 /* Do not want errors such as E724 here. */
23902 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023903 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023904 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023905 if (s != NULL)
23906 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023907 if (vim_strsize(s) > MSG_BUF_CLEN)
23908 {
23909 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23910 s = buf;
23911 }
23912 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023913 vim_free(tofree);
23914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023915 }
23916 }
23917 msg_puts((char_u *)")");
23918 }
23919 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023920
23921 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023922 --no_wait_return;
23923 }
23924 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023925#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023926 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023927 {
23928 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23929 func_do_profile(fp);
23930 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023931 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023932 {
23933 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023934 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023935 profile_zero(&fp->uf_tm_children);
23936 }
23937 script_prof_save(&wait_start);
23938 }
23939#endif
23940
Bram Moolenaar071d4272004-06-13 20:20:40 +000023941 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023942 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023943 save_did_emsg = did_emsg;
23944 did_emsg = FALSE;
23945
23946 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023947 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023948 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23949
23950 --RedrawingDisabled;
23951
23952 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023953 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023954 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023955 clear_tv(rettv);
23956 rettv->v_type = VAR_NUMBER;
23957 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023958 }
23959
Bram Moolenaar05159a02005-02-26 23:04:13 +000023960#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023961 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023962 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023963 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023964 profile_end(&call_start);
23965 profile_sub_wait(&wait_start, &call_start);
23966 profile_add(&fp->uf_tm_total, &call_start);
23967 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023968 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023969 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023970 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23971 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023972 }
23973 }
23974#endif
23975
Bram Moolenaar071d4272004-06-13 20:20:40 +000023976 /* when being verbose, mention the return value */
23977 if (p_verbose >= 12)
23978 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023979 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023980 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023981
Bram Moolenaar071d4272004-06-13 20:20:40 +000023982 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023983 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023984 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023985 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023986 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023987 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023988 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023989 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023990 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023991 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023992 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023993
Bram Moolenaar555b2802005-05-19 21:08:39 +000023994 /* The value may be very long. Skip the middle part, so that we
23995 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020023996 * truncate it at the end. Don't want errors such as E724 here. */
23997 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023998 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023999 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024000 if (s != NULL)
24001 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024002 if (vim_strsize(s) > MSG_BUF_CLEN)
24003 {
24004 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24005 s = buf;
24006 }
24007 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024008 vim_free(tofree);
24009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024010 }
24011 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024012
24013 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024014 --no_wait_return;
24015 }
24016
24017 vim_free(sourcing_name);
24018 sourcing_name = save_sourcing_name;
24019 sourcing_lnum = save_sourcing_lnum;
24020 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024021#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024022 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024023 script_prof_restore(&wait_start);
24024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024025
24026 if (p_verbose >= 12 && sourcing_name != NULL)
24027 {
24028 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024029 verbose_enter_scroll();
24030
Bram Moolenaar555b2802005-05-19 21:08:39 +000024031 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024032 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024033
24034 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024035 --no_wait_return;
24036 }
24037
24038 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024039 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024040 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024041
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024042 /* If the a:000 list and the l: and a: dicts are not referenced we can
24043 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024044 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24045 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24046 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24047 {
24048 free_funccal(fc, FALSE);
24049 }
24050 else
24051 {
24052 hashitem_T *hi;
24053 listitem_T *li;
24054 int todo;
24055
24056 /* "fc" is still in use. This can happen when returning "a:000" or
24057 * assigning "l:" to a global variable.
24058 * Link "fc" in the list for garbage collection later. */
24059 fc->caller = previous_funccal;
24060 previous_funccal = fc;
24061
24062 /* Make a copy of the a: variables, since we didn't do that above. */
24063 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24064 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24065 {
24066 if (!HASHITEM_EMPTY(hi))
24067 {
24068 --todo;
24069 v = HI2DI(hi);
24070 copy_tv(&v->di_tv, &v->di_tv);
24071 }
24072 }
24073
24074 /* Make a copy of the a:000 items, since we didn't do that above. */
24075 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24076 copy_tv(&li->li_tv, &li->li_tv);
24077 }
24078}
24079
24080/*
24081 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024082 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024083 */
24084 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024085can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024086{
24087 return (fc->l_varlist.lv_copyID != copyID
24088 && fc->l_vars.dv_copyID != copyID
24089 && fc->l_avars.dv_copyID != copyID);
24090}
24091
24092/*
24093 * Free "fc" and what it contains.
24094 */
24095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024096free_funccal(
24097 funccall_T *fc,
24098 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024099{
24100 listitem_T *li;
24101
24102 /* The a: variables typevals may not have been allocated, only free the
24103 * allocated variables. */
24104 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24105
24106 /* free all l: variables */
24107 vars_clear(&fc->l_vars.dv_hashtab);
24108
24109 /* Free the a:000 variables if they were allocated. */
24110 if (free_val)
24111 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24112 clear_tv(&li->li_tv);
24113
24114 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024115}
24116
24117/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024118 * Add a number variable "name" to dict "dp" with value "nr".
24119 */
24120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024121add_nr_var(
24122 dict_T *dp,
24123 dictitem_T *v,
24124 char *name,
24125 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024126{
24127 STRCPY(v->di_key, name);
24128 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24129 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24130 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024131 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024132 v->di_tv.vval.v_number = nr;
24133}
24134
24135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024136 * ":return [expr]"
24137 */
24138 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024139ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024140{
24141 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024142 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024143 int returning = FALSE;
24144
24145 if (current_funccal == NULL)
24146 {
24147 EMSG(_("E133: :return not inside a function"));
24148 return;
24149 }
24150
24151 if (eap->skip)
24152 ++emsg_skip;
24153
24154 eap->nextcmd = NULL;
24155 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024156 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024157 {
24158 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024159 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024160 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024161 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024162 }
24163 /* It's safer to return also on error. */
24164 else if (!eap->skip)
24165 {
24166 /*
24167 * Return unless the expression evaluation has been cancelled due to an
24168 * aborting error, an interrupt, or an exception.
24169 */
24170 if (!aborting())
24171 returning = do_return(eap, FALSE, TRUE, NULL);
24172 }
24173
24174 /* When skipping or the return gets pending, advance to the next command
24175 * in this line (!returning). Otherwise, ignore the rest of the line.
24176 * Following lines will be ignored by get_func_line(). */
24177 if (returning)
24178 eap->nextcmd = NULL;
24179 else if (eap->nextcmd == NULL) /* no argument */
24180 eap->nextcmd = check_nextcmd(arg);
24181
24182 if (eap->skip)
24183 --emsg_skip;
24184}
24185
24186/*
24187 * Return from a function. Possibly makes the return pending. Also called
24188 * for a pending return at the ":endtry" or after returning from an extra
24189 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024190 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024191 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024192 * FALSE when the return gets pending.
24193 */
24194 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024195do_return(
24196 exarg_T *eap,
24197 int reanimate,
24198 int is_cmd,
24199 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024200{
24201 int idx;
24202 struct condstack *cstack = eap->cstack;
24203
24204 if (reanimate)
24205 /* Undo the return. */
24206 current_funccal->returned = FALSE;
24207
24208 /*
24209 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24210 * not in its finally clause (which then is to be executed next) is found.
24211 * In this case, make the ":return" pending for execution at the ":endtry".
24212 * Otherwise, return normally.
24213 */
24214 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24215 if (idx >= 0)
24216 {
24217 cstack->cs_pending[idx] = CSTP_RETURN;
24218
24219 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024220 /* A pending return again gets pending. "rettv" points to an
24221 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024222 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024223 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024224 else
24225 {
24226 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024227 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024228 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024229 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024230
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024231 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024232 {
24233 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024234 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024235 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024236 else
24237 EMSG(_(e_outofmem));
24238 }
24239 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024240 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024241
24242 if (reanimate)
24243 {
24244 /* The pending return value could be overwritten by a ":return"
24245 * without argument in a finally clause; reset the default
24246 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024247 current_funccal->rettv->v_type = VAR_NUMBER;
24248 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024249 }
24250 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024251 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024252 }
24253 else
24254 {
24255 current_funccal->returned = TRUE;
24256
24257 /* If the return is carried out now, store the return value. For
24258 * a return immediately after reanimation, the value is already
24259 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024260 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024261 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024262 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024263 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024264 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024265 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024266 }
24267 }
24268
24269 return idx < 0;
24270}
24271
24272/*
24273 * Free the variable with a pending return value.
24274 */
24275 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024276discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024277{
Bram Moolenaar33570922005-01-25 22:26:29 +000024278 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024279}
24280
24281/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024282 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024283 * is an allocated string. Used by report_pending() for verbose messages.
24284 */
24285 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024286get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024287{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024288 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024289 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024290 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024291
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024292 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024293 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024294 if (s == NULL)
24295 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024296
24297 STRCPY(IObuff, ":return ");
24298 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24299 if (STRLEN(s) + 8 >= IOSIZE)
24300 STRCPY(IObuff + IOSIZE - 4, "...");
24301 vim_free(tofree);
24302 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024303}
24304
24305/*
24306 * Get next function line.
24307 * Called by do_cmdline() to get the next line.
24308 * Returns allocated string, or NULL for end of function.
24309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024310 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024311get_func_line(
24312 int c UNUSED,
24313 void *cookie,
24314 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024315{
Bram Moolenaar33570922005-01-25 22:26:29 +000024316 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024317 ufunc_T *fp = fcp->func;
24318 char_u *retval;
24319 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024320
24321 /* If breakpoints have been added/deleted need to check for it. */
24322 if (fcp->dbg_tick != debug_tick)
24323 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024324 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024325 sourcing_lnum);
24326 fcp->dbg_tick = debug_tick;
24327 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024328#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024329 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024330 func_line_end(cookie);
24331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024332
Bram Moolenaar05159a02005-02-26 23:04:13 +000024333 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024334 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24335 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024336 retval = NULL;
24337 else
24338 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024339 /* Skip NULL lines (continuation lines). */
24340 while (fcp->linenr < gap->ga_len
24341 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24342 ++fcp->linenr;
24343 if (fcp->linenr >= gap->ga_len)
24344 retval = NULL;
24345 else
24346 {
24347 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24348 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024349#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024350 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024351 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024352#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024354 }
24355
24356 /* Did we encounter a breakpoint? */
24357 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24358 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024359 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024360 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024361 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024362 sourcing_lnum);
24363 fcp->dbg_tick = debug_tick;
24364 }
24365
24366 return retval;
24367}
24368
Bram Moolenaar05159a02005-02-26 23:04:13 +000024369#if defined(FEAT_PROFILE) || defined(PROTO)
24370/*
24371 * Called when starting to read a function line.
24372 * "sourcing_lnum" must be correct!
24373 * When skipping lines it may not actually be executed, but we won't find out
24374 * until later and we need to store the time now.
24375 */
24376 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024377func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024378{
24379 funccall_T *fcp = (funccall_T *)cookie;
24380 ufunc_T *fp = fcp->func;
24381
24382 if (fp->uf_profiling && sourcing_lnum >= 1
24383 && sourcing_lnum <= fp->uf_lines.ga_len)
24384 {
24385 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024386 /* Skip continuation lines. */
24387 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24388 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024389 fp->uf_tml_execed = FALSE;
24390 profile_start(&fp->uf_tml_start);
24391 profile_zero(&fp->uf_tml_children);
24392 profile_get_wait(&fp->uf_tml_wait);
24393 }
24394}
24395
24396/*
24397 * Called when actually executing a function line.
24398 */
24399 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024400func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024401{
24402 funccall_T *fcp = (funccall_T *)cookie;
24403 ufunc_T *fp = fcp->func;
24404
24405 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24406 fp->uf_tml_execed = TRUE;
24407}
24408
24409/*
24410 * Called when done with a function line.
24411 */
24412 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024413func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024414{
24415 funccall_T *fcp = (funccall_T *)cookie;
24416 ufunc_T *fp = fcp->func;
24417
24418 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24419 {
24420 if (fp->uf_tml_execed)
24421 {
24422 ++fp->uf_tml_count[fp->uf_tml_idx];
24423 profile_end(&fp->uf_tml_start);
24424 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024425 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024426 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24427 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024428 }
24429 fp->uf_tml_idx = -1;
24430 }
24431}
24432#endif
24433
Bram Moolenaar071d4272004-06-13 20:20:40 +000024434/*
24435 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024436 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024437 */
24438 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024439func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024440{
Bram Moolenaar33570922005-01-25 22:26:29 +000024441 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024442
24443 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24444 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024445 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024446 || fcp->returned);
24447}
24448
24449/*
24450 * return TRUE if cookie indicates a function which "abort"s on errors.
24451 */
24452 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024453func_has_abort(
24454 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024455{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024456 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024457}
24458
24459#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24460typedef enum
24461{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024462 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24463 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24464 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024465} var_flavour_T;
24466
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024467static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024468
24469 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010024470var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024471{
24472 char_u *p = varname;
24473
24474 if (ASCII_ISUPPER(*p))
24475 {
24476 while (*(++p))
24477 if (ASCII_ISLOWER(*p))
24478 return VAR_FLAVOUR_SESSION;
24479 return VAR_FLAVOUR_VIMINFO;
24480 }
24481 else
24482 return VAR_FLAVOUR_DEFAULT;
24483}
24484#endif
24485
24486#if defined(FEAT_VIMINFO) || defined(PROTO)
24487/*
24488 * Restore global vars that start with a capital from the viminfo file
24489 */
24490 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024491read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024492{
24493 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024494 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024495 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024496 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024497
24498 if (!writing && (find_viminfo_parameter('!') != NULL))
24499 {
24500 tab = vim_strchr(virp->vir_line + 1, '\t');
24501 if (tab != NULL)
24502 {
24503 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024504 switch (*tab)
24505 {
24506 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024507#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024508 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024509#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024510 case 'D': type = VAR_DICT; break;
24511 case 'L': type = VAR_LIST; break;
24512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024513
24514 tab = vim_strchr(tab, '\t');
24515 if (tab != NULL)
24516 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024517 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024518 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024519 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024520 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024521#ifdef FEAT_FLOAT
24522 else if (type == VAR_FLOAT)
24523 (void)string2float(tab + 1, &tv.vval.v_float);
24524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024525 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024526 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024527 if (type == VAR_DICT || type == VAR_LIST)
24528 {
24529 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24530
24531 if (etv == NULL)
24532 /* Failed to parse back the dict or list, use it as a
24533 * string. */
24534 tv.v_type = VAR_STRING;
24535 else
24536 {
24537 vim_free(tv.vval.v_string);
24538 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024539 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024540 }
24541 }
24542
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024543 /* when in a function use global variables */
24544 save_funccal = current_funccal;
24545 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024546 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024547 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024548
24549 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024550 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024551 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24552 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024553 }
24554 }
24555 }
24556
24557 return viminfo_readline(virp);
24558}
24559
24560/*
24561 * Write global vars that start with a capital to the viminfo file
24562 */
24563 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024564write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024565{
Bram Moolenaar33570922005-01-25 22:26:29 +000024566 hashitem_T *hi;
24567 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024568 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024569 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024570 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024571 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024572 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024573
24574 if (find_viminfo_parameter('!') == NULL)
24575 return;
24576
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024577 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024578
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024579 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024580 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024581 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024582 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024583 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024584 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024585 this_var = HI2DI(hi);
24586 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024587 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024588 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024589 {
24590 case VAR_STRING: s = "STR"; break;
24591 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024592#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024593 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024594#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024595 case VAR_DICT: s = "DIC"; break;
24596 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024597 default: continue;
24598 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024599 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024600 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024601 if (p != NULL)
24602 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024603 vim_free(tofree);
24604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024605 }
24606 }
24607}
24608#endif
24609
24610#if defined(FEAT_SESSION) || defined(PROTO)
24611 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024612store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024613{
Bram Moolenaar33570922005-01-25 22:26:29 +000024614 hashitem_T *hi;
24615 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024616 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024617 char_u *p, *t;
24618
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024619 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024620 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024621 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024622 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024623 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024624 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024625 this_var = HI2DI(hi);
24626 if ((this_var->di_tv.v_type == VAR_NUMBER
24627 || this_var->di_tv.v_type == VAR_STRING)
24628 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024629 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024630 /* Escape special characters with a backslash. Turn a LF and
24631 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024632 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024633 (char_u *)"\\\"\n\r");
24634 if (p == NULL) /* out of memory */
24635 break;
24636 for (t = p; *t != NUL; ++t)
24637 if (*t == '\n')
24638 *t = 'n';
24639 else if (*t == '\r')
24640 *t = 'r';
24641 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024642 this_var->di_key,
24643 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24644 : ' ',
24645 p,
24646 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24647 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024648 || put_eol(fd) == FAIL)
24649 {
24650 vim_free(p);
24651 return FAIL;
24652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024653 vim_free(p);
24654 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024655#ifdef FEAT_FLOAT
24656 else if (this_var->di_tv.v_type == VAR_FLOAT
24657 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24658 {
24659 float_T f = this_var->di_tv.vval.v_float;
24660 int sign = ' ';
24661
24662 if (f < 0)
24663 {
24664 f = -f;
24665 sign = '-';
24666 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024667 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024668 this_var->di_key, sign, f) < 0)
24669 || put_eol(fd) == FAIL)
24670 return FAIL;
24671 }
24672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024673 }
24674 }
24675 return OK;
24676}
24677#endif
24678
Bram Moolenaar661b1822005-07-28 22:36:45 +000024679/*
24680 * Display script name where an item was last set.
24681 * Should only be invoked when 'verbose' is non-zero.
24682 */
24683 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024684last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000024685{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024686 char_u *p;
24687
Bram Moolenaar661b1822005-07-28 22:36:45 +000024688 if (scriptID != 0)
24689 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024690 p = home_replace_save(NULL, get_scriptname(scriptID));
24691 if (p != NULL)
24692 {
24693 verbose_enter();
24694 MSG_PUTS(_("\n\tLast set from "));
24695 MSG_PUTS(p);
24696 vim_free(p);
24697 verbose_leave();
24698 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024699 }
24700}
24701
Bram Moolenaard812df62008-11-09 12:46:09 +000024702/*
24703 * List v:oldfiles in a nice way.
24704 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024705 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024706ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000024707{
24708 list_T *l = vimvars[VV_OLDFILES].vv_list;
24709 listitem_T *li;
24710 int nr = 0;
24711
24712 if (l == NULL)
24713 msg((char_u *)_("No old files"));
24714 else
24715 {
24716 msg_start();
24717 msg_scroll = TRUE;
24718 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24719 {
24720 msg_outnum((long)++nr);
24721 MSG_PUTS(": ");
24722 msg_outtrans(get_tv_string(&li->li_tv));
24723 msg_putchar('\n');
24724 out_flush(); /* output one line at a time */
24725 ui_breakcheck();
24726 }
24727 /* Assume "got_int" was set to truncate the listing. */
24728 got_int = FALSE;
24729
24730#ifdef FEAT_BROWSE_CMD
24731 if (cmdmod.browse)
24732 {
24733 quit_more = FALSE;
24734 nr = prompt_for_number(FALSE);
24735 msg_starthere();
24736 if (nr > 0)
24737 {
24738 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24739 (long)nr);
24740
24741 if (p != NULL)
24742 {
24743 p = expand_env_save(p);
24744 eap->arg = p;
24745 eap->cmdidx = CMD_edit;
24746 cmdmod.browse = FALSE;
24747 do_exedit(eap, NULL);
24748 vim_free(p);
24749 }
24750 }
24751 }
24752#endif
24753 }
24754}
24755
Bram Moolenaar53744302015-07-17 17:38:22 +020024756/* reset v:option_new, v:option_old and v:option_type */
24757 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024758reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020024759{
24760 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
24761 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
24762 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
24763}
24764
24765
Bram Moolenaar071d4272004-06-13 20:20:40 +000024766#endif /* FEAT_EVAL */
24767
Bram Moolenaar071d4272004-06-13 20:20:40 +000024768
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024769#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024770
24771#ifdef WIN3264
24772/*
24773 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24774 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024775static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
24776static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
24777static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024778
24779/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024780 * Get the short path (8.3) for the filename in "fnamep".
24781 * Only works for a valid file name.
24782 * When the path gets longer "fnamep" is changed and the allocated buffer
24783 * is put in "bufp".
24784 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24785 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024786 */
24787 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024788get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024789{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024790 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024791 char_u *newbuf;
24792
24793 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024794 l = GetShortPathName(*fnamep, *fnamep, len);
24795 if (l > len - 1)
24796 {
24797 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024798 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024799 newbuf = vim_strnsave(*fnamep, l);
24800 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024801 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024802
24803 vim_free(*bufp);
24804 *fnamep = *bufp = newbuf;
24805
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024806 /* Really should always succeed, as the buffer is big enough. */
24807 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024808 }
24809
24810 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024811 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024812}
24813
24814/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024815 * Get the short path (8.3) for the filename in "fname". The converted
24816 * path is returned in "bufp".
24817 *
24818 * Some of the directories specified in "fname" may not exist. This function
24819 * will shorten the existing directories at the beginning of the path and then
24820 * append the remaining non-existing path.
24821 *
24822 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024823 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024824 * bufp - Pointer to an allocated buffer for the filename.
24825 * fnamelen - Length of the filename pointed to by fname
24826 *
24827 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024828 */
24829 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024830shortpath_for_invalid_fname(
24831 char_u **fname,
24832 char_u **bufp,
24833 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024834{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024835 char_u *short_fname, *save_fname, *pbuf_unused;
24836 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024837 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024838 int old_len, len;
24839 int new_len, sfx_len;
24840 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024841
24842 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024843 old_len = *fnamelen;
24844 save_fname = vim_strnsave(*fname, old_len);
24845 pbuf_unused = NULL;
24846 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024847
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024848 endp = save_fname + old_len - 1; /* Find the end of the copy */
24849 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024850
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024851 /*
24852 * Try shortening the supplied path till it succeeds by removing one
24853 * directory at a time from the tail of the path.
24854 */
24855 len = 0;
24856 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024857 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024858 /* go back one path-separator */
24859 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24860 --endp;
24861 if (endp <= save_fname)
24862 break; /* processed the complete path */
24863
24864 /*
24865 * Replace the path separator with a NUL and try to shorten the
24866 * resulting path.
24867 */
24868 ch = *endp;
24869 *endp = 0;
24870 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024871 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024872 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24873 {
24874 retval = FAIL;
24875 goto theend;
24876 }
24877 *endp = ch; /* preserve the string */
24878
24879 if (len > 0)
24880 break; /* successfully shortened the path */
24881
24882 /* failed to shorten the path. Skip the path separator */
24883 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024884 }
24885
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024886 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024887 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024888 /*
24889 * Succeeded in shortening the path. Now concatenate the shortened
24890 * path with the remaining path at the tail.
24891 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024892
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024893 /* Compute the length of the new path. */
24894 sfx_len = (int)(save_endp - endp) + 1;
24895 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024896
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024897 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024898 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024899 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024900 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024901 /* There is not enough space in the currently allocated string,
24902 * copy it to a buffer big enough. */
24903 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024904 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024905 {
24906 retval = FAIL;
24907 goto theend;
24908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024909 }
24910 else
24911 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024912 /* Transfer short_fname to the main buffer (it's big enough),
24913 * unless get_short_pathname() did its work in-place. */
24914 *fname = *bufp = save_fname;
24915 if (short_fname != save_fname)
24916 vim_strncpy(save_fname, short_fname, len);
24917 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024918 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024919
24920 /* concat the not-shortened part of the path */
24921 vim_strncpy(*fname + len, endp, sfx_len);
24922 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024923 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024924
24925theend:
24926 vim_free(pbuf_unused);
24927 vim_free(save_fname);
24928
24929 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024930}
24931
24932/*
24933 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024934 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024935 */
24936 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024937shortpath_for_partial(
24938 char_u **fnamep,
24939 char_u **bufp,
24940 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024941{
24942 int sepcount, len, tflen;
24943 char_u *p;
24944 char_u *pbuf, *tfname;
24945 int hasTilde;
24946
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024947 /* Count up the path separators from the RHS.. so we know which part
24948 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024949 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024950 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024951 if (vim_ispathsep(*p))
24952 ++sepcount;
24953
24954 /* Need full path first (use expand_env() to remove a "~/") */
24955 hasTilde = (**fnamep == '~');
24956 if (hasTilde)
24957 pbuf = tfname = expand_env_save(*fnamep);
24958 else
24959 pbuf = tfname = FullName_save(*fnamep, FALSE);
24960
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024961 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024962
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024963 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24964 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024965
24966 if (len == 0)
24967 {
24968 /* Don't have a valid filename, so shorten the rest of the
24969 * path if we can. This CAN give us invalid 8.3 filenames, but
24970 * there's not a lot of point in guessing what it might be.
24971 */
24972 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024973 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24974 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024975 }
24976
24977 /* Count the paths backward to find the beginning of the desired string. */
24978 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024979 {
24980#ifdef FEAT_MBYTE
24981 if (has_mbyte)
24982 p -= mb_head_off(tfname, p);
24983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024984 if (vim_ispathsep(*p))
24985 {
24986 if (sepcount == 0 || (hasTilde && sepcount == 1))
24987 break;
24988 else
24989 sepcount --;
24990 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024992 if (hasTilde)
24993 {
24994 --p;
24995 if (p >= tfname)
24996 *p = '~';
24997 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024998 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024999 }
25000 else
25001 ++p;
25002
25003 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25004 vim_free(*bufp);
25005 *fnamelen = (int)STRLEN(p);
25006 *bufp = pbuf;
25007 *fnamep = p;
25008
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025009 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025010}
25011#endif /* WIN3264 */
25012
25013/*
25014 * Adjust a filename, according to a string of modifiers.
25015 * *fnamep must be NUL terminated when called. When returning, the length is
25016 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025017 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025018 * When there is an error, *fnamep is set to NULL.
25019 */
25020 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025021modify_fname(
25022 char_u *src, /* string with modifiers */
25023 int *usedlen, /* characters after src that are used */
25024 char_u **fnamep, /* file name so far */
25025 char_u **bufp, /* buffer for allocated file name or NULL */
25026 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025027{
25028 int valid = 0;
25029 char_u *tail;
25030 char_u *s, *p, *pbuf;
25031 char_u dirname[MAXPATHL];
25032 int c;
25033 int has_fullname = 0;
25034#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025035 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025036 int has_shortname = 0;
25037#endif
25038
25039repeat:
25040 /* ":p" - full path/file_name */
25041 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25042 {
25043 has_fullname = 1;
25044
25045 valid |= VALID_PATH;
25046 *usedlen += 2;
25047
25048 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25049 if ((*fnamep)[0] == '~'
25050#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25051 && ((*fnamep)[1] == '/'
25052# ifdef BACKSLASH_IN_FILENAME
25053 || (*fnamep)[1] == '\\'
25054# endif
25055 || (*fnamep)[1] == NUL)
25056
25057#endif
25058 )
25059 {
25060 *fnamep = expand_env_save(*fnamep);
25061 vim_free(*bufp); /* free any allocated file name */
25062 *bufp = *fnamep;
25063 if (*fnamep == NULL)
25064 return -1;
25065 }
25066
25067 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025068 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025069 {
25070 if (vim_ispathsep(*p)
25071 && p[1] == '.'
25072 && (p[2] == NUL
25073 || vim_ispathsep(p[2])
25074 || (p[2] == '.'
25075 && (p[3] == NUL || vim_ispathsep(p[3])))))
25076 break;
25077 }
25078
25079 /* FullName_save() is slow, don't use it when not needed. */
25080 if (*p != NUL || !vim_isAbsName(*fnamep))
25081 {
25082 *fnamep = FullName_save(*fnamep, *p != NUL);
25083 vim_free(*bufp); /* free any allocated file name */
25084 *bufp = *fnamep;
25085 if (*fnamep == NULL)
25086 return -1;
25087 }
25088
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025089#ifdef WIN3264
25090# if _WIN32_WINNT >= 0x0500
25091 if (vim_strchr(*fnamep, '~') != NULL)
25092 {
25093 /* Expand 8.3 filename to full path. Needed to make sure the same
25094 * file does not have two different names.
25095 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25096 p = alloc(_MAX_PATH + 1);
25097 if (p != NULL)
25098 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025099 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025100 {
25101 vim_free(*bufp);
25102 *bufp = *fnamep = p;
25103 }
25104 else
25105 vim_free(p);
25106 }
25107 }
25108# endif
25109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025110 /* Append a path separator to a directory. */
25111 if (mch_isdir(*fnamep))
25112 {
25113 /* Make room for one or two extra characters. */
25114 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25115 vim_free(*bufp); /* free any allocated file name */
25116 *bufp = *fnamep;
25117 if (*fnamep == NULL)
25118 return -1;
25119 add_pathsep(*fnamep);
25120 }
25121 }
25122
25123 /* ":." - path relative to the current directory */
25124 /* ":~" - path relative to the home directory */
25125 /* ":8" - shortname path - postponed till after */
25126 while (src[*usedlen] == ':'
25127 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25128 {
25129 *usedlen += 2;
25130 if (c == '8')
25131 {
25132#ifdef WIN3264
25133 has_shortname = 1; /* Postpone this. */
25134#endif
25135 continue;
25136 }
25137 pbuf = NULL;
25138 /* Need full path first (use expand_env() to remove a "~/") */
25139 if (!has_fullname)
25140 {
25141 if (c == '.' && **fnamep == '~')
25142 p = pbuf = expand_env_save(*fnamep);
25143 else
25144 p = pbuf = FullName_save(*fnamep, FALSE);
25145 }
25146 else
25147 p = *fnamep;
25148
25149 has_fullname = 0;
25150
25151 if (p != NULL)
25152 {
25153 if (c == '.')
25154 {
25155 mch_dirname(dirname, MAXPATHL);
25156 s = shorten_fname(p, dirname);
25157 if (s != NULL)
25158 {
25159 *fnamep = s;
25160 if (pbuf != NULL)
25161 {
25162 vim_free(*bufp); /* free any allocated file name */
25163 *bufp = pbuf;
25164 pbuf = NULL;
25165 }
25166 }
25167 }
25168 else
25169 {
25170 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25171 /* Only replace it when it starts with '~' */
25172 if (*dirname == '~')
25173 {
25174 s = vim_strsave(dirname);
25175 if (s != NULL)
25176 {
25177 *fnamep = s;
25178 vim_free(*bufp);
25179 *bufp = s;
25180 }
25181 }
25182 }
25183 vim_free(pbuf);
25184 }
25185 }
25186
25187 tail = gettail(*fnamep);
25188 *fnamelen = (int)STRLEN(*fnamep);
25189
25190 /* ":h" - head, remove "/file_name", can be repeated */
25191 /* Don't remove the first "/" or "c:\" */
25192 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25193 {
25194 valid |= VALID_HEAD;
25195 *usedlen += 2;
25196 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025197 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025198 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025199 *fnamelen = (int)(tail - *fnamep);
25200#ifdef VMS
25201 if (*fnamelen > 0)
25202 *fnamelen += 1; /* the path separator is part of the path */
25203#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025204 if (*fnamelen == 0)
25205 {
25206 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25207 p = vim_strsave((char_u *)".");
25208 if (p == NULL)
25209 return -1;
25210 vim_free(*bufp);
25211 *bufp = *fnamep = tail = p;
25212 *fnamelen = 1;
25213 }
25214 else
25215 {
25216 while (tail > s && !after_pathsep(s, tail))
25217 mb_ptr_back(*fnamep, tail);
25218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025219 }
25220
25221 /* ":8" - shortname */
25222 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25223 {
25224 *usedlen += 2;
25225#ifdef WIN3264
25226 has_shortname = 1;
25227#endif
25228 }
25229
25230#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025231 /*
25232 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025233 */
25234 if (has_shortname)
25235 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025236 /* Copy the string if it is shortened by :h and when it wasn't copied
25237 * yet, because we are going to change it in place. Avoids changing
25238 * the buffer name for "%:8". */
25239 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025240 {
25241 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025242 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025243 return -1;
25244 vim_free(*bufp);
25245 *bufp = *fnamep = p;
25246 }
25247
25248 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025249 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025250 if (!has_fullname && !vim_isAbsName(*fnamep))
25251 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025252 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025253 return -1;
25254 }
25255 else
25256 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025257 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025258
Bram Moolenaardc935552011-08-17 15:23:23 +020025259 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025260 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025261 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025262 return -1;
25263
25264 if (l == 0)
25265 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025266 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025267 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025268 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025269 return -1;
25270 }
25271 *fnamelen = l;
25272 }
25273 }
25274#endif /* WIN3264 */
25275
25276 /* ":t" - tail, just the basename */
25277 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25278 {
25279 *usedlen += 2;
25280 *fnamelen -= (int)(tail - *fnamep);
25281 *fnamep = tail;
25282 }
25283
25284 /* ":e" - extension, can be repeated */
25285 /* ":r" - root, without extension, can be repeated */
25286 while (src[*usedlen] == ':'
25287 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25288 {
25289 /* find a '.' in the tail:
25290 * - for second :e: before the current fname
25291 * - otherwise: The last '.'
25292 */
25293 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25294 s = *fnamep - 2;
25295 else
25296 s = *fnamep + *fnamelen - 1;
25297 for ( ; s > tail; --s)
25298 if (s[0] == '.')
25299 break;
25300 if (src[*usedlen + 1] == 'e') /* :e */
25301 {
25302 if (s > tail)
25303 {
25304 *fnamelen += (int)(*fnamep - (s + 1));
25305 *fnamep = s + 1;
25306#ifdef VMS
25307 /* cut version from the extension */
25308 s = *fnamep + *fnamelen - 1;
25309 for ( ; s > *fnamep; --s)
25310 if (s[0] == ';')
25311 break;
25312 if (s > *fnamep)
25313 *fnamelen = s - *fnamep;
25314#endif
25315 }
25316 else if (*fnamep <= tail)
25317 *fnamelen = 0;
25318 }
25319 else /* :r */
25320 {
25321 if (s > tail) /* remove one extension */
25322 *fnamelen = (int)(s - *fnamep);
25323 }
25324 *usedlen += 2;
25325 }
25326
25327 /* ":s?pat?foo?" - substitute */
25328 /* ":gs?pat?foo?" - global substitute */
25329 if (src[*usedlen] == ':'
25330 && (src[*usedlen + 1] == 's'
25331 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25332 {
25333 char_u *str;
25334 char_u *pat;
25335 char_u *sub;
25336 int sep;
25337 char_u *flags;
25338 int didit = FALSE;
25339
25340 flags = (char_u *)"";
25341 s = src + *usedlen + 2;
25342 if (src[*usedlen + 1] == 'g')
25343 {
25344 flags = (char_u *)"g";
25345 ++s;
25346 }
25347
25348 sep = *s++;
25349 if (sep)
25350 {
25351 /* find end of pattern */
25352 p = vim_strchr(s, sep);
25353 if (p != NULL)
25354 {
25355 pat = vim_strnsave(s, (int)(p - s));
25356 if (pat != NULL)
25357 {
25358 s = p + 1;
25359 /* find end of substitution */
25360 p = vim_strchr(s, sep);
25361 if (p != NULL)
25362 {
25363 sub = vim_strnsave(s, (int)(p - s));
25364 str = vim_strnsave(*fnamep, *fnamelen);
25365 if (sub != NULL && str != NULL)
25366 {
25367 *usedlen = (int)(p + 1 - src);
25368 s = do_string_sub(str, pat, sub, flags);
25369 if (s != NULL)
25370 {
25371 *fnamep = s;
25372 *fnamelen = (int)STRLEN(s);
25373 vim_free(*bufp);
25374 *bufp = s;
25375 didit = TRUE;
25376 }
25377 }
25378 vim_free(sub);
25379 vim_free(str);
25380 }
25381 vim_free(pat);
25382 }
25383 }
25384 /* after using ":s", repeat all the modifiers */
25385 if (didit)
25386 goto repeat;
25387 }
25388 }
25389
Bram Moolenaar26df0922014-02-23 23:39:13 +010025390 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25391 {
25392 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25393 if (p == NULL)
25394 return -1;
25395 vim_free(*bufp);
25396 *bufp = *fnamep = p;
25397 *fnamelen = (int)STRLEN(p);
25398 *usedlen += 2;
25399 }
25400
Bram Moolenaar071d4272004-06-13 20:20:40 +000025401 return valid;
25402}
25403
25404/*
25405 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25406 * "flags" can be "g" to do a global substitute.
25407 * Returns an allocated string, NULL for error.
25408 */
25409 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025410do_string_sub(
25411 char_u *str,
25412 char_u *pat,
25413 char_u *sub,
25414 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025415{
25416 int sublen;
25417 regmatch_T regmatch;
25418 int i;
25419 int do_all;
25420 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025421 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025422 garray_T ga;
25423 char_u *ret;
25424 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025425 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025426
25427 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25428 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025429 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025430
25431 ga_init2(&ga, 1, 200);
25432
25433 do_all = (flags[0] == 'g');
25434
25435 regmatch.rm_ic = p_ic;
25436 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25437 if (regmatch.regprog != NULL)
25438 {
25439 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025440 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025441 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25442 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025443 /* Skip empty match except for first match. */
25444 if (regmatch.startp[0] == regmatch.endp[0])
25445 {
25446 if (zero_width == regmatch.startp[0])
25447 {
25448 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025449 i = MB_PTR2LEN(tail);
25450 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25451 (size_t)i);
25452 ga.ga_len += i;
25453 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025454 continue;
25455 }
25456 zero_width = regmatch.startp[0];
25457 }
25458
Bram Moolenaar071d4272004-06-13 20:20:40 +000025459 /*
25460 * Get some space for a temporary buffer to do the substitution
25461 * into. It will contain:
25462 * - The text up to where the match is.
25463 * - The substituted text.
25464 * - The text after the match.
25465 */
25466 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025467 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025468 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25469 {
25470 ga_clear(&ga);
25471 break;
25472 }
25473
25474 /* copy the text up to where the match is */
25475 i = (int)(regmatch.startp[0] - tail);
25476 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25477 /* add the substituted text */
25478 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25479 + ga.ga_len + i, TRUE, TRUE, FALSE);
25480 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025481 tail = regmatch.endp[0];
25482 if (*tail == NUL)
25483 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025484 if (!do_all)
25485 break;
25486 }
25487
25488 if (ga.ga_data != NULL)
25489 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25490
Bram Moolenaar473de612013-06-08 18:19:48 +020025491 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025492 }
25493
25494 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25495 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025496 if (p_cpo == empty_option)
25497 p_cpo = save_cpo;
25498 else
25499 /* Darn, evaluating {sub} expression changed the value. */
25500 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025501
25502 return ret;
25503}
25504
25505#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */