blob: 767925193a8638a946c1313093f7c66dfb7b6082 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000102static char *e_listreq = N_("E714: List required");
103static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000104static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000105static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
106static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
107static char *e_funcdict = N_("E717: Dictionary entry already exists");
108static char *e_funcref = N_("E718: Funcref required");
109static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
110static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000111static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000112static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200113#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200114static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000116
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100117#define NAMESPACE_CHAR (char_u *)"abglstvw"
118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
Bram Moolenaar8502c702014-06-17 12:51:16 +0200137/* Abort conversion to string after a recursion error. */
138static int did_echo_string_emsg = FALSE;
139
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100159#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
160
161/* Values for get_lval() flags argument: */
162#define GLV_QUIET TFN_QUIET /* no error messages */
163#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000215/* list heads for garbage collection */
216static dict_T *first_dict = NULL; /* list of all dicts */
217static list_T *first_list = NULL; /* list of all lists */
218
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000219/* From user function to hashitem and back. */
220static ufunc_T dumuf;
221#define UF2HIKEY(fp) ((fp)->uf_name)
222#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
223#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
224
225#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
226#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
Bram Moolenaar33570922005-01-25 22:26:29 +0000228#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
229#define VAR_SHORT_LEN 20 /* short variable name length */
230#define FIXVAR_CNT 12 /* number of fixed variables */
231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000233typedef struct funccall_S funccall_T;
234
235struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
237 ufunc_T *func; /* function being called */
238 int linenr; /* next line to be executed */
239 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000240 struct /* fixed variables for arguments */
241 {
242 dictitem_T var; /* variable (without room for name) */
243 char_u room[VAR_SHORT_LEN]; /* room for the name */
244 } fixvar[FIXVAR_CNT];
245 dict_T l_vars; /* l: local function variables */
246 dictitem_T l_vars_var; /* variable for l: scope */
247 dict_T l_avars; /* a: argument variables */
248 dictitem_T l_avars_var; /* variable for a: scope */
249 list_T l_varlist; /* list for a:000 */
250 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
251 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 linenr_T breakpoint; /* next line with breakpoint or zero */
253 int dbg_tick; /* debug_tick when breakpoint was set */
254 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000255#ifdef FEAT_PROFILE
256 proftime_T prof_child; /* time spent in a child */
257#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000258 funccall_T *caller; /* calling function or NULL */
259};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
261/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262 * Info used by a ":for" loop.
263 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000264typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000265{
266 int fi_semicolon; /* TRUE if ending in '; var]' */
267 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000268 listwatch_T fi_lw; /* keep an eye on the item used. */
269 list_T *fi_list; /* list being used */
270} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000272/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000273 * Struct used by trans_function_name()
274 */
275typedef struct
276{
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000278 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dictitem_T *fd_di; /* Dictionary item used */
280} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000281
Bram Moolenaara7043832005-01-21 11:56:39 +0000282
283/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000284 * Array to hold the value of v: variables.
285 * The value is in a dictitem, so that it can also be used in the v: scope.
286 * The reason to use this table anyway is for very quick access to the
287 * variables with the VV_ defines.
288 */
289#include "version.h"
290
291/* values for vv_flags: */
292#define VV_COMPAT 1 /* compatible, also used without "v:" */
293#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000294#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000295
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000296#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
298static struct vimvar
299{
300 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301 dictitem_T vv_di; /* value and name for key */
302 char vv_filler[16]; /* space for LONGEST name below!!! */
303 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
304} vimvars[VV_LEN] =
305{
306 /*
307 * The order here must match the VV_ defines in vim.h!
308 * Initializing a union does not work, leave tv.vval empty to get zero's.
309 */
310 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
311 {VV_NAME("count1", VAR_NUMBER), VV_RO},
312 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
313 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
314 {VV_NAME("warningmsg", VAR_STRING), 0},
315 {VV_NAME("statusmsg", VAR_STRING), 0},
316 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
318 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
319 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("termresponse", VAR_STRING), VV_RO},
321 {VV_NAME("fname", VAR_STRING), VV_RO},
322 {VV_NAME("lang", VAR_STRING), VV_RO},
323 {VV_NAME("lc_time", VAR_STRING), VV_RO},
324 {VV_NAME("ctype", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
327 {VV_NAME("fname_in", VAR_STRING), VV_RO},
328 {VV_NAME("fname_out", VAR_STRING), VV_RO},
329 {VV_NAME("fname_new", VAR_STRING), VV_RO},
330 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
331 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
332 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
335 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
336 {VV_NAME("progname", VAR_STRING), VV_RO},
337 {VV_NAME("servername", VAR_STRING), VV_RO},
338 {VV_NAME("dying", VAR_NUMBER), VV_RO},
339 {VV_NAME("exception", VAR_STRING), VV_RO},
340 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
341 {VV_NAME("register", VAR_STRING), VV_RO},
342 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
343 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000344 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
345 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000346 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000347 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
348 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000349 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000354 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000355 {VV_NAME("swapname", VAR_STRING), VV_RO},
356 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000357 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200358 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000359 {VV_NAME("mouse_win", VAR_NUMBER), 0},
360 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
361 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000362 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000363 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100364 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000365 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200366 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200367 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200368 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200369 {VV_NAME("option_new", VAR_STRING), VV_RO},
370 {VV_NAME("option_old", VAR_STRING), VV_RO},
371 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100372 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100373 {VV_NAME("false", VAR_SPECIAL), VV_RO},
374 {VV_NAME("true", VAR_SPECIAL), VV_RO},
375 {VV_NAME("null", VAR_SPECIAL), VV_RO},
376 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000377};
378
379/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000380#define vv_type vv_di.di_tv.v_type
381#define vv_nr vv_di.di_tv.vval.v_number
382#define vv_float vv_di.di_tv.vval.v_float
383#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000384#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200385#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000386#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000387
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200388static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000389#define vimvarht vimvardict.dv_hashtab
390
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100391static void prepare_vimvar(int idx, typval_T *save_tv);
392static void restore_vimvar(int idx, typval_T *save_tv);
393static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
394static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
395static char_u *skip_var_one(char_u *arg);
396static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
397static void list_glob_vars(int *first);
398static void list_buf_vars(int *first);
399static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000400#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100401static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000402#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100403static void list_vim_vars(int *first);
404static void list_script_vars(int *first);
405static void list_func_vars(int *first);
406static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
407static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
408static int check_changedtick(char_u *arg);
409static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
410static void clear_lval(lval_T *lp);
411static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
412static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
413static void list_fix_watch(list_T *l, listitem_T *item);
414static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
415static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
416static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
417static void item_lock(typval_T *tv, int deep, int lock);
418static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000419
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100420static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
421static int eval1(char_u **arg, typval_T *rettv, int evaluate);
422static int eval2(char_u **arg, typval_T *rettv, int evaluate);
423static int eval3(char_u **arg, typval_T *rettv, int evaluate);
424static int eval4(char_u **arg, typval_T *rettv, int evaluate);
425static int eval5(char_u **arg, typval_T *rettv, int evaluate);
426static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
427static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000428
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100429static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
430static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
431static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
432static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
433static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
434static long list_len(list_T *l);
435static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
436static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
437static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
438static long list_find_nr(list_T *l, long idx, int *errorp);
439static long list_idx_of_item(list_T *l, listitem_T *item);
440static int list_append_number(list_T *l, varnumber_T n);
441static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
442static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
443static list_T *list_copy(list_T *orig, int deep, int copyID);
444static char_u *list2string(typval_T *tv, int copyID);
445static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
446static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
447static int free_unref_items(int copyID);
448static dictitem_T *dictitem_copy(dictitem_T *org);
449static void dictitem_remove(dict_T *dict, dictitem_T *item);
450static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
451static long dict_len(dict_T *d);
452static char_u *dict2string(typval_T *tv, int copyID);
453static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
454static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
455static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
456static char_u *string_quote(char_u *str, int function);
457static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
458static int find_internal_func(char_u *name);
459static char_u *deref_func_name(char_u *name, int *lenp, int no_autoload);
460static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100461static void emsg_funcname(char *ermsg, char_u *name);
462static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000463
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100465static void f_abs(typval_T *argvars, typval_T *rettv);
466static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100468static void f_add(typval_T *argvars, typval_T *rettv);
469static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
470static void f_and(typval_T *argvars, typval_T *rettv);
471static void f_append(typval_T *argvars, typval_T *rettv);
472static void f_argc(typval_T *argvars, typval_T *rettv);
473static void f_argidx(typval_T *argvars, typval_T *rettv);
474static void f_arglistid(typval_T *argvars, typval_T *rettv);
475static void f_argv(typval_T *argvars, typval_T *rettv);
476static void f_assert_equal(typval_T *argvars, typval_T *rettv);
477static void f_assert_exception(typval_T *argvars, typval_T *rettv);
478static void f_assert_fails(typval_T *argvars, typval_T *rettv);
479static void f_assert_false(typval_T *argvars, typval_T *rettv);
480static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000481#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100482static void f_asin(typval_T *argvars, typval_T *rettv);
483static void f_atan(typval_T *argvars, typval_T *rettv);
484static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100486static void f_browse(typval_T *argvars, typval_T *rettv);
487static void f_browsedir(typval_T *argvars, typval_T *rettv);
488static void f_bufexists(typval_T *argvars, typval_T *rettv);
489static void f_buflisted(typval_T *argvars, typval_T *rettv);
490static void f_bufloaded(typval_T *argvars, typval_T *rettv);
491static void f_bufname(typval_T *argvars, typval_T *rettv);
492static void f_bufnr(typval_T *argvars, typval_T *rettv);
493static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
494static void f_byte2line(typval_T *argvars, typval_T *rettv);
495static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
496static void f_byteidx(typval_T *argvars, typval_T *rettv);
497static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
498static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000499#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100500static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000501#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100502static void f_changenr(typval_T *argvars, typval_T *rettv);
503static void f_char2nr(typval_T *argvars, typval_T *rettv);
504static void f_cindent(typval_T *argvars, typval_T *rettv);
505static void f_clearmatches(typval_T *argvars, typval_T *rettv);
506static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000507#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100508static void f_complete(typval_T *argvars, typval_T *rettv);
509static void f_complete_add(typval_T *argvars, typval_T *rettv);
510static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000511#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100512static void f_confirm(typval_T *argvars, typval_T *rettv);
513static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000514#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100515static void f_cos(typval_T *argvars, typval_T *rettv);
516static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000517#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100518#ifdef FEAT_CHANNEL
519static void f_connect(typval_T *argvars, typval_T *rettv);
520#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100521static void f_count(typval_T *argvars, typval_T *rettv);
522static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
523static void f_cursor(typval_T *argsvars, typval_T *rettv);
524static void f_deepcopy(typval_T *argvars, typval_T *rettv);
525static void f_delete(typval_T *argvars, typval_T *rettv);
526static void f_did_filetype(typval_T *argvars, typval_T *rettv);
527static void f_diff_filler(typval_T *argvars, typval_T *rettv);
528static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100529#ifdef FEAT_CHANNEL
530static void f_disconnect(typval_T *argvars, typval_T *rettv);
531#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100532static void f_empty(typval_T *argvars, typval_T *rettv);
533static void f_escape(typval_T *argvars, typval_T *rettv);
534static void f_eval(typval_T *argvars, typval_T *rettv);
535static void f_eventhandler(typval_T *argvars, typval_T *rettv);
536static void f_executable(typval_T *argvars, typval_T *rettv);
537static void f_exepath(typval_T *argvars, typval_T *rettv);
538static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200539#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100540static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200541#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100542static void f_expand(typval_T *argvars, typval_T *rettv);
543static void f_extend(typval_T *argvars, typval_T *rettv);
544static void f_feedkeys(typval_T *argvars, typval_T *rettv);
545static void f_filereadable(typval_T *argvars, typval_T *rettv);
546static void f_filewritable(typval_T *argvars, typval_T *rettv);
547static void f_filter(typval_T *argvars, typval_T *rettv);
548static void f_finddir(typval_T *argvars, typval_T *rettv);
549static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000550#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100551static void f_float2nr(typval_T *argvars, typval_T *rettv);
552static void f_floor(typval_T *argvars, typval_T *rettv);
553static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000554#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100555static void f_fnameescape(typval_T *argvars, typval_T *rettv);
556static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
557static void f_foldclosed(typval_T *argvars, typval_T *rettv);
558static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
559static void f_foldlevel(typval_T *argvars, typval_T *rettv);
560static void f_foldtext(typval_T *argvars, typval_T *rettv);
561static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
562static void f_foreground(typval_T *argvars, typval_T *rettv);
563static void f_function(typval_T *argvars, typval_T *rettv);
564static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
565static void f_get(typval_T *argvars, typval_T *rettv);
566static void f_getbufline(typval_T *argvars, typval_T *rettv);
567static void f_getbufvar(typval_T *argvars, typval_T *rettv);
568static void f_getchar(typval_T *argvars, typval_T *rettv);
569static void f_getcharmod(typval_T *argvars, typval_T *rettv);
570static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
571static void f_getcmdline(typval_T *argvars, typval_T *rettv);
572static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
573static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
574static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
575static void f_getcwd(typval_T *argvars, typval_T *rettv);
576static void f_getfontname(typval_T *argvars, typval_T *rettv);
577static void f_getfperm(typval_T *argvars, typval_T *rettv);
578static void f_getfsize(typval_T *argvars, typval_T *rettv);
579static void f_getftime(typval_T *argvars, typval_T *rettv);
580static void f_getftype(typval_T *argvars, typval_T *rettv);
581static void f_getline(typval_T *argvars, typval_T *rettv);
582static void f_getmatches(typval_T *argvars, typval_T *rettv);
583static void f_getpid(typval_T *argvars, typval_T *rettv);
584static void f_getcurpos(typval_T *argvars, typval_T *rettv);
585static void f_getpos(typval_T *argvars, typval_T *rettv);
586static void f_getqflist(typval_T *argvars, typval_T *rettv);
587static void f_getreg(typval_T *argvars, typval_T *rettv);
588static void f_getregtype(typval_T *argvars, typval_T *rettv);
589static void f_gettabvar(typval_T *argvars, typval_T *rettv);
590static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
591static void f_getwinposx(typval_T *argvars, typval_T *rettv);
592static void f_getwinposy(typval_T *argvars, typval_T *rettv);
593static void f_getwinvar(typval_T *argvars, typval_T *rettv);
594static void f_glob(typval_T *argvars, typval_T *rettv);
595static void f_globpath(typval_T *argvars, typval_T *rettv);
596static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
597static void f_has(typval_T *argvars, typval_T *rettv);
598static void f_has_key(typval_T *argvars, typval_T *rettv);
599static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
600static void f_hasmapto(typval_T *argvars, typval_T *rettv);
601static void f_histadd(typval_T *argvars, typval_T *rettv);
602static void f_histdel(typval_T *argvars, typval_T *rettv);
603static void f_histget(typval_T *argvars, typval_T *rettv);
604static void f_histnr(typval_T *argvars, typval_T *rettv);
605static void f_hlID(typval_T *argvars, typval_T *rettv);
606static void f_hlexists(typval_T *argvars, typval_T *rettv);
607static void f_hostname(typval_T *argvars, typval_T *rettv);
608static void f_iconv(typval_T *argvars, typval_T *rettv);
609static void f_indent(typval_T *argvars, typval_T *rettv);
610static void f_index(typval_T *argvars, typval_T *rettv);
611static void f_input(typval_T *argvars, typval_T *rettv);
612static void f_inputdialog(typval_T *argvars, typval_T *rettv);
613static void f_inputlist(typval_T *argvars, typval_T *rettv);
614static void f_inputrestore(typval_T *argvars, typval_T *rettv);
615static void f_inputsave(typval_T *argvars, typval_T *rettv);
616static void f_inputsecret(typval_T *argvars, typval_T *rettv);
617static void f_insert(typval_T *argvars, typval_T *rettv);
618static void f_invert(typval_T *argvars, typval_T *rettv);
619static void f_isdirectory(typval_T *argvars, typval_T *rettv);
620static void f_islocked(typval_T *argvars, typval_T *rettv);
621static void f_items(typval_T *argvars, typval_T *rettv);
622static void f_join(typval_T *argvars, typval_T *rettv);
623static void f_jsondecode(typval_T *argvars, typval_T *rettv);
624static void f_jsonencode(typval_T *argvars, typval_T *rettv);
625static void f_keys(typval_T *argvars, typval_T *rettv);
626static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
627static void f_len(typval_T *argvars, typval_T *rettv);
628static void f_libcall(typval_T *argvars, typval_T *rettv);
629static void f_libcallnr(typval_T *argvars, typval_T *rettv);
630static void f_line(typval_T *argvars, typval_T *rettv);
631static void f_line2byte(typval_T *argvars, typval_T *rettv);
632static void f_lispindent(typval_T *argvars, typval_T *rettv);
633static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000634#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100635static void f_log(typval_T *argvars, typval_T *rettv);
636static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000637#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200638#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100639static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200640#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100641static void f_map(typval_T *argvars, typval_T *rettv);
642static void f_maparg(typval_T *argvars, typval_T *rettv);
643static void f_mapcheck(typval_T *argvars, typval_T *rettv);
644static void f_match(typval_T *argvars, typval_T *rettv);
645static void f_matchadd(typval_T *argvars, typval_T *rettv);
646static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
647static void f_matcharg(typval_T *argvars, typval_T *rettv);
648static void f_matchdelete(typval_T *argvars, typval_T *rettv);
649static void f_matchend(typval_T *argvars, typval_T *rettv);
650static void f_matchlist(typval_T *argvars, typval_T *rettv);
651static void f_matchstr(typval_T *argvars, typval_T *rettv);
652static void f_max(typval_T *argvars, typval_T *rettv);
653static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000654#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100655static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000656#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100657static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100658#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100659static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100660#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100661static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
662static void f_nr2char(typval_T *argvars, typval_T *rettv);
663static void f_or(typval_T *argvars, typval_T *rettv);
664static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100665#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100666static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100667#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000668#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100669static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000670#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100671static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
672static void f_printf(typval_T *argvars, typval_T *rettv);
673static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200674#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100675static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200676#endif
677#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100678static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200679#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100680static void f_range(typval_T *argvars, typval_T *rettv);
681static void f_readfile(typval_T *argvars, typval_T *rettv);
682static void f_reltime(typval_T *argvars, typval_T *rettv);
683static void f_reltimestr(typval_T *argvars, typval_T *rettv);
684static void f_remote_expr(typval_T *argvars, typval_T *rettv);
685static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
686static void f_remote_peek(typval_T *argvars, typval_T *rettv);
687static void f_remote_read(typval_T *argvars, typval_T *rettv);
688static void f_remote_send(typval_T *argvars, typval_T *rettv);
689static void f_remove(typval_T *argvars, typval_T *rettv);
690static void f_rename(typval_T *argvars, typval_T *rettv);
691static void f_repeat(typval_T *argvars, typval_T *rettv);
692static void f_resolve(typval_T *argvars, typval_T *rettv);
693static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000696#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100697static void f_screenattr(typval_T *argvars, typval_T *rettv);
698static void f_screenchar(typval_T *argvars, typval_T *rettv);
699static void f_screencol(typval_T *argvars, typval_T *rettv);
700static void f_screenrow(typval_T *argvars, typval_T *rettv);
701static void f_search(typval_T *argvars, typval_T *rettv);
702static void f_searchdecl(typval_T *argvars, typval_T *rettv);
703static void f_searchpair(typval_T *argvars, typval_T *rettv);
704static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
705static void f_searchpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100706#ifdef FEAT_CHANNEL
707static void f_sendexpr(typval_T *argvars, typval_T *rettv);
708static void f_sendraw(typval_T *argvars, typval_T *rettv);
709#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100710static void f_server2client(typval_T *argvars, typval_T *rettv);
711static void f_serverlist(typval_T *argvars, typval_T *rettv);
712static void f_setbufvar(typval_T *argvars, typval_T *rettv);
713static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
714static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
715static void f_setline(typval_T *argvars, typval_T *rettv);
716static void f_setloclist(typval_T *argvars, typval_T *rettv);
717static void f_setmatches(typval_T *argvars, typval_T *rettv);
718static void f_setpos(typval_T *argvars, typval_T *rettv);
719static void f_setqflist(typval_T *argvars, typval_T *rettv);
720static void f_setreg(typval_T *argvars, typval_T *rettv);
721static void f_settabvar(typval_T *argvars, typval_T *rettv);
722static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
723static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100724#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100725static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100726#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100727static void f_shellescape(typval_T *argvars, typval_T *rettv);
728static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
729static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000730#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100731static void f_sin(typval_T *argvars, typval_T *rettv);
732static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000733#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100734static void f_sort(typval_T *argvars, typval_T *rettv);
735static void f_soundfold(typval_T *argvars, typval_T *rettv);
736static void f_spellbadword(typval_T *argvars, typval_T *rettv);
737static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
738static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000739#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100740static void f_sqrt(typval_T *argvars, typval_T *rettv);
741static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000742#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100743static void f_str2nr(typval_T *argvars, typval_T *rettv);
744static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000745#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100746static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000747#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100748static void f_stridx(typval_T *argvars, typval_T *rettv);
749static void f_string(typval_T *argvars, typval_T *rettv);
750static void f_strlen(typval_T *argvars, typval_T *rettv);
751static void f_strpart(typval_T *argvars, typval_T *rettv);
752static void f_strridx(typval_T *argvars, typval_T *rettv);
753static void f_strtrans(typval_T *argvars, typval_T *rettv);
754static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
755static void f_strwidth(typval_T *argvars, typval_T *rettv);
756static void f_submatch(typval_T *argvars, typval_T *rettv);
757static void f_substitute(typval_T *argvars, typval_T *rettv);
758static void f_synID(typval_T *argvars, typval_T *rettv);
759static void f_synIDattr(typval_T *argvars, typval_T *rettv);
760static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
761static void f_synstack(typval_T *argvars, typval_T *rettv);
762static void f_synconcealed(typval_T *argvars, typval_T *rettv);
763static void f_system(typval_T *argvars, typval_T *rettv);
764static void f_systemlist(typval_T *argvars, typval_T *rettv);
765static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
766static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
767static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
768static void f_taglist(typval_T *argvars, typval_T *rettv);
769static void f_tagfiles(typval_T *argvars, typval_T *rettv);
770static void f_tempname(typval_T *argvars, typval_T *rettv);
771static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200772#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100773static void f_tan(typval_T *argvars, typval_T *rettv);
774static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200775#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100776static void f_tolower(typval_T *argvars, typval_T *rettv);
777static void f_toupper(typval_T *argvars, typval_T *rettv);
778static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000779#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100780static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000781#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100782static void f_type(typval_T *argvars, typval_T *rettv);
783static void f_undofile(typval_T *argvars, typval_T *rettv);
784static void f_undotree(typval_T *argvars, typval_T *rettv);
785static void f_uniq(typval_T *argvars, typval_T *rettv);
786static void f_values(typval_T *argvars, typval_T *rettv);
787static void f_virtcol(typval_T *argvars, typval_T *rettv);
788static void f_visualmode(typval_T *argvars, typval_T *rettv);
789static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
790static void f_winbufnr(typval_T *argvars, typval_T *rettv);
791static void f_wincol(typval_T *argvars, typval_T *rettv);
792static void f_winheight(typval_T *argvars, typval_T *rettv);
793static void f_winline(typval_T *argvars, typval_T *rettv);
794static void f_winnr(typval_T *argvars, typval_T *rettv);
795static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
796static void f_winrestview(typval_T *argvars, typval_T *rettv);
797static void f_winsaveview(typval_T *argvars, typval_T *rettv);
798static void f_winwidth(typval_T *argvars, typval_T *rettv);
799static void f_writefile(typval_T *argvars, typval_T *rettv);
800static void f_wordcount(typval_T *argvars, typval_T *rettv);
801static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000802
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100803static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
804static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
805static int get_env_len(char_u **arg);
806static int get_id_len(char_u **arg);
807static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
808static char_u *find_name_end(char_u *arg, char_u **expr_start, char_u **expr_end, int flags);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000809#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
810#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
811 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100812static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
813static int eval_isnamec(int c);
814static int eval_isnamec1(int c);
815static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
816static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
817static typval_T *alloc_tv(void);
818static typval_T *alloc_string_tv(char_u *string);
819static void init_tv(typval_T *varp);
820static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100821#ifdef FEAT_FLOAT
822static float_T get_tv_float(typval_T *varp);
823#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100824static linenr_T get_tv_lnum(typval_T *argvars);
825static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
826static char_u *get_tv_string(typval_T *varp);
827static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
828static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
829static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
830static hashtab_T *find_var_ht(char_u *name, char_u **varname);
831static funccall_T *get_funccal(void);
832static void vars_clear_ext(hashtab_T *ht, int free_val);
833static void delete_var(hashtab_T *ht, hashitem_T *hi);
834static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
835static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
836static void set_var(char_u *name, typval_T *varp, int copy);
837static int var_check_ro(int flags, char_u *name, int use_gettext);
838static int var_check_fixed(int flags, char_u *name, int use_gettext);
839static int var_check_func_name(char_u *name, int new_var);
840static int valid_varname(char_u *varname);
841static int tv_check_lock(int lock, char_u *name, int use_gettext);
842static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
843static char_u *find_option_end(char_u **arg, int *opt_flags);
844static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
845static int eval_fname_script(char_u *p);
846static int eval_fname_sid(char_u *p);
847static void list_func_head(ufunc_T *fp, int indent);
848static ufunc_T *find_func(char_u *name);
849static int function_exists(char_u *name);
850static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000851#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100852static void func_do_profile(ufunc_T *fp);
853static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
854static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000855static int
856# ifdef __BORLANDC__
857 _RTLENTRYF
858# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100859 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000860static int
861# ifdef __BORLANDC__
862 _RTLENTRYF
863# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100864 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000865#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100866static int script_autoload(char_u *name, int reload);
867static char_u *autoload_name(char_u *name);
868static void cat_func_name(char_u *buf, ufunc_T *fp);
869static void func_free(ufunc_T *fp);
870static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
871static int can_free_funccal(funccall_T *fc, int copyID) ;
872static void free_funccal(funccall_T *fc, int free_val);
873static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
874static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
875static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
876static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
877static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
878static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
879static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
880static int write_list(FILE *fd, list_T *list, int binary);
881static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000882
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200883
884#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100885static int compare_func_name(const void *s1, const void *s2);
886static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200887#endif
888
Bram Moolenaar33570922005-01-25 22:26:29 +0000889/*
890 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000891 */
892 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100893eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000894{
Bram Moolenaar33570922005-01-25 22:26:29 +0000895 int i;
896 struct vimvar *p;
897
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200898 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
899 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200900 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000901 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000902 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000903
904 for (i = 0; i < VV_LEN; ++i)
905 {
906 p = &vimvars[i];
907 STRCPY(p->vv_di.di_key, p->vv_name);
908 if (p->vv_flags & VV_RO)
909 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
910 else if (p->vv_flags & VV_RO_SBX)
911 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
912 else
913 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000914
915 /* add to v: scope dict, unless the value is not always available */
916 if (p->vv_type != VAR_UNKNOWN)
917 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000918 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000919 /* add to compat scope dict */
920 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000921 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000922 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100923 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200924 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100925 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100926
927 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
928 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
929 set_vim_var_nr(VV_NONE, VVAL_NONE);
930 set_vim_var_nr(VV_NULL, VVAL_NULL);
931
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200932 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200933
934#ifdef EBCDIC
935 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100936 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200937 */
938 sortFunctions();
939#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000940}
941
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000942#if defined(EXITFREE) || defined(PROTO)
943 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100944eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000945{
946 int i;
947 struct vimvar *p;
948
949 for (i = 0; i < VV_LEN; ++i)
950 {
951 p = &vimvars[i];
952 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000953 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000954 vim_free(p->vv_str);
955 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000956 }
957 else if (p->vv_di.di_tv.v_type == VAR_LIST)
958 {
959 list_unref(p->vv_list);
960 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000961 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000962 }
963 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000964 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000965 hash_clear(&compat_hashtab);
966
Bram Moolenaard9fba312005-06-26 22:34:35 +0000967 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100968# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200969 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100970# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000971
972 /* global variables */
973 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000974
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000975 /* autoloaded script names */
976 ga_clear_strings(&ga_loaded);
977
Bram Moolenaarcca74132013-09-25 21:00:28 +0200978 /* Script-local variables. First clear all the variables and in a second
979 * loop free the scriptvar_T, because a variable in one script might hold
980 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200981 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200982 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200983 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200984 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200985 ga_clear(&ga_scripts);
986
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000987 /* unreferenced lists and dicts */
988 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000989
990 /* functions */
991 free_all_functions();
992 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000993}
994#endif
995
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 * Return the name of the executed function.
998 */
999 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001000func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001002 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003}
1004
1005/*
1006 * Return the address holding the next breakpoint line for a funccall cookie.
1007 */
1008 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001009func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010{
Bram Moolenaar33570922005-01-25 22:26:29 +00001011 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012}
1013
1014/*
1015 * Return the address holding the debug tick for a funccall cookie.
1016 */
1017 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001018func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019{
Bram Moolenaar33570922005-01-25 22:26:29 +00001020 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021}
1022
1023/*
1024 * Return the nesting level for a funccall cookie.
1025 */
1026 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001027func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028{
Bram Moolenaar33570922005-01-25 22:26:29 +00001029 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030}
1031
1032/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001033funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001035/* pointer to list of previously used funccal, still around because some
1036 * item in it is still being used. */
1037funccall_T *previous_funccal = NULL;
1038
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039/*
1040 * Return TRUE when a function was ended by a ":return" command.
1041 */
1042 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001043current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044{
1045 return current_funccal->returned;
1046}
1047
1048
1049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 * Set an internal variable to a string value. Creates the variable if it does
1051 * not already exist.
1052 */
1053 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001054set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001056 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001057 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058
1059 val = vim_strsave(value);
1060 if (val != NULL)
1061 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001062 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001063 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001065 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001066 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 }
1068 }
1069}
1070
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001071static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001072static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001073static char_u *redir_endp = NULL;
1074static char_u *redir_varname = NULL;
1075
1076/*
1077 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001078 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001079 * Returns OK if successfully completed the setup. FAIL otherwise.
1080 */
1081 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001082var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083{
1084 int save_emsg;
1085 int err;
1086 typval_T tv;
1087
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001088 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001089 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 {
1091 EMSG(_(e_invarg));
1092 return FAIL;
1093 }
1094
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001095 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 redir_varname = vim_strsave(name);
1097 if (redir_varname == NULL)
1098 return FAIL;
1099
1100 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1101 if (redir_lval == NULL)
1102 {
1103 var_redir_stop();
1104 return FAIL;
1105 }
1106
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001107 /* The output is stored in growarray "redir_ga" until redirection ends. */
1108 ga_init2(&redir_ga, (int)sizeof(char), 500);
1109
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001111 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001112 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1114 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001115 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 if (redir_endp != NULL && *redir_endp != NUL)
1117 /* Trailing characters are present after the variable name */
1118 EMSG(_(e_trailing));
1119 else
1120 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001121 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001122 var_redir_stop();
1123 return FAIL;
1124 }
1125
1126 /* check if we can write to the variable: set it to or append an empty
1127 * string */
1128 save_emsg = did_emsg;
1129 did_emsg = FALSE;
1130 tv.v_type = VAR_STRING;
1131 tv.vval.v_string = (char_u *)"";
1132 if (append)
1133 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1134 else
1135 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001136 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001138 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 if (err)
1140 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001141 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142 var_redir_stop();
1143 return FAIL;
1144 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145
1146 return OK;
1147}
1148
1149/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001150 * Append "value[value_len]" to the variable set by var_redir_start().
1151 * The actual appending is postponed until redirection ends, because the value
1152 * appended may in fact be the string we write to, changing it may cause freed
1153 * memory to be used:
1154 * :redir => foo
1155 * :let foo
1156 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001157 */
1158 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001159var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001161 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162
1163 if (redir_lval == NULL)
1164 return;
1165
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001166 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001167 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001168 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001169 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001170
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001171 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001172 {
1173 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001174 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001175 }
1176 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001177 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001178}
1179
1180/*
1181 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001182 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001183 */
1184 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001185var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001186{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001187 typval_T tv;
1188
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001189 if (redir_lval != NULL)
1190 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001191 /* If there was no error: assign the text to the variable. */
1192 if (redir_endp != NULL)
1193 {
1194 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1195 tv.v_type = VAR_STRING;
1196 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001197 /* Call get_lval() again, if it's inside a Dict or List it may
1198 * have changed. */
1199 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001200 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001201 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1202 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1203 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001204 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001205
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001206 /* free the collected output */
1207 vim_free(redir_ga.ga_data);
1208 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001209
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001210 vim_free(redir_lval);
1211 redir_lval = NULL;
1212 }
1213 vim_free(redir_varname);
1214 redir_varname = NULL;
1215}
1216
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217# if defined(FEAT_MBYTE) || defined(PROTO)
1218 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001219eval_charconvert(
1220 char_u *enc_from,
1221 char_u *enc_to,
1222 char_u *fname_from,
1223 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224{
1225 int err = FALSE;
1226
1227 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1228 set_vim_var_string(VV_CC_TO, enc_to, -1);
1229 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1230 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1231 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1232 err = TRUE;
1233 set_vim_var_string(VV_CC_FROM, NULL, -1);
1234 set_vim_var_string(VV_CC_TO, NULL, -1);
1235 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1236 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1237
1238 if (err)
1239 return FAIL;
1240 return OK;
1241}
1242# endif
1243
1244# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1245 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001246eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247{
1248 int err = FALSE;
1249
1250 set_vim_var_string(VV_FNAME_IN, fname, -1);
1251 set_vim_var_string(VV_CMDARG, args, -1);
1252 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1253 err = TRUE;
1254 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1255 set_vim_var_string(VV_CMDARG, NULL, -1);
1256
1257 if (err)
1258 {
1259 mch_remove(fname);
1260 return FAIL;
1261 }
1262 return OK;
1263}
1264# endif
1265
1266# if defined(FEAT_DIFF) || defined(PROTO)
1267 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001268eval_diff(
1269 char_u *origfile,
1270 char_u *newfile,
1271 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272{
1273 int err = FALSE;
1274
1275 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1276 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1277 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1278 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1279 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1280 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1281 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1282}
1283
1284 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001285eval_patch(
1286 char_u *origfile,
1287 char_u *difffile,
1288 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289{
1290 int err;
1291
1292 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1293 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1294 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1295 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1296 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1297 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1298 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1299}
1300# endif
1301
1302/*
1303 * Top level evaluation function, returning a boolean.
1304 * Sets "error" to TRUE if there was an error.
1305 * Return TRUE or FALSE.
1306 */
1307 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001308eval_to_bool(
1309 char_u *arg,
1310 int *error,
1311 char_u **nextcmd,
1312 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313{
Bram Moolenaar33570922005-01-25 22:26:29 +00001314 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 int retval = FALSE;
1316
1317 if (skip)
1318 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001319 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 else
1322 {
1323 *error = FALSE;
1324 if (!skip)
1325 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001326 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001327 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
1329 }
1330 if (skip)
1331 --emsg_skip;
1332
1333 return retval;
1334}
1335
1336/*
1337 * Top level evaluation function, returning a string. If "skip" is TRUE,
1338 * only parsing to "nextcmd" is done, without reporting errors. Return
1339 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1340 */
1341 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001342eval_to_string_skip(
1343 char_u *arg,
1344 char_u **nextcmd,
1345 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346{
Bram Moolenaar33570922005-01-25 22:26:29 +00001347 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 char_u *retval;
1349
1350 if (skip)
1351 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001352 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 retval = NULL;
1354 else
1355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001356 retval = vim_strsave(get_tv_string(&tv));
1357 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 }
1359 if (skip)
1360 --emsg_skip;
1361
1362 return retval;
1363}
1364
1365/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001366 * Skip over an expression at "*pp".
1367 * Return FAIL for an error, OK otherwise.
1368 */
1369 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001370skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001371{
Bram Moolenaar33570922005-01-25 22:26:29 +00001372 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001373
1374 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001375 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001376}
1377
1378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001380 * When "convert" is TRUE convert a List into a sequence of lines and convert
1381 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 * Return pointer to allocated memory, or NULL for failure.
1383 */
1384 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001385eval_to_string(
1386 char_u *arg,
1387 char_u **nextcmd,
1388 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389{
Bram Moolenaar33570922005-01-25 22:26:29 +00001390 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001392 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001393#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001394 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001397 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 retval = NULL;
1399 else
1400 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001401 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001402 {
1403 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001404 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001405 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001406 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001407 if (tv.vval.v_list->lv_len > 0)
1408 ga_append(&ga, NL);
1409 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001410 ga_append(&ga, NUL);
1411 retval = (char_u *)ga.ga_data;
1412 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001413#ifdef FEAT_FLOAT
1414 else if (convert && tv.v_type == VAR_FLOAT)
1415 {
1416 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1417 retval = vim_strsave(numbuf);
1418 }
1419#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001420 else
1421 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001422 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 }
1424
1425 return retval;
1426}
1427
1428/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001429 * Call eval_to_string() without using current local variables and using
1430 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 */
1432 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001433eval_to_string_safe(
1434 char_u *arg,
1435 char_u **nextcmd,
1436 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437{
1438 char_u *retval;
1439 void *save_funccalp;
1440
1441 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001442 if (use_sandbox)
1443 ++sandbox;
1444 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001445 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001446 if (use_sandbox)
1447 --sandbox;
1448 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 restore_funccal(save_funccalp);
1450 return retval;
1451}
1452
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453/*
1454 * Top level evaluation function, returning a number.
1455 * Evaluates "expr" silently.
1456 * Returns -1 for an error.
1457 */
1458 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001459eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460{
Bram Moolenaar33570922005-01-25 22:26:29 +00001461 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001463 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464
1465 ++emsg_off;
1466
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001467 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 retval = -1;
1469 else
1470 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001471 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001472 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 }
1474 --emsg_off;
1475
1476 return retval;
1477}
1478
Bram Moolenaara40058a2005-07-11 22:42:07 +00001479/*
1480 * Prepare v: variable "idx" to be used.
1481 * Save the current typeval in "save_tv".
1482 * When not used yet add the variable to the v: hashtable.
1483 */
1484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001485prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001486{
1487 *save_tv = vimvars[idx].vv_tv;
1488 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1489 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1490}
1491
1492/*
1493 * Restore v: variable "idx" to typeval "save_tv".
1494 * When no longer defined, remove the variable from the v: hashtable.
1495 */
1496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001497restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001498{
1499 hashitem_T *hi;
1500
Bram Moolenaara40058a2005-07-11 22:42:07 +00001501 vimvars[idx].vv_tv = *save_tv;
1502 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1503 {
1504 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1505 if (HASHITEM_EMPTY(hi))
1506 EMSG2(_(e_intern2), "restore_vimvar()");
1507 else
1508 hash_remove(&vimvarht, hi);
1509 }
1510}
1511
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001512#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001513/*
1514 * Evaluate an expression to a list with suggestions.
1515 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001516 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001517 */
1518 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001519eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001520{
1521 typval_T save_val;
1522 typval_T rettv;
1523 list_T *list = NULL;
1524 char_u *p = skipwhite(expr);
1525
1526 /* Set "v:val" to the bad word. */
1527 prepare_vimvar(VV_VAL, &save_val);
1528 vimvars[VV_VAL].vv_type = VAR_STRING;
1529 vimvars[VV_VAL].vv_str = badword;
1530 if (p_verbose == 0)
1531 ++emsg_off;
1532
1533 if (eval1(&p, &rettv, TRUE) == OK)
1534 {
1535 if (rettv.v_type != VAR_LIST)
1536 clear_tv(&rettv);
1537 else
1538 list = rettv.vval.v_list;
1539 }
1540
1541 if (p_verbose == 0)
1542 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001543 restore_vimvar(VV_VAL, &save_val);
1544
1545 return list;
1546}
1547
1548/*
1549 * "list" is supposed to contain two items: a word and a number. Return the
1550 * word in "pp" and the number as the return value.
1551 * Return -1 if anything isn't right.
1552 * Used to get the good word and score from the eval_spell_expr() result.
1553 */
1554 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001555get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001556{
1557 listitem_T *li;
1558
1559 li = list->lv_first;
1560 if (li == NULL)
1561 return -1;
1562 *pp = get_tv_string(&li->li_tv);
1563
1564 li = li->li_next;
1565 if (li == NULL)
1566 return -1;
1567 return get_tv_number(&li->li_tv);
1568}
1569#endif
1570
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001571/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001572 * Top level evaluation function.
1573 * Returns an allocated typval_T with the result.
1574 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001575 */
1576 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001577eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001578{
1579 typval_T *tv;
1580
1581 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001582 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001583 {
1584 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001585 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001586 }
1587
1588 return tv;
1589}
1590
1591
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001593 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001594 * Uses argv[argc] for the function arguments. Only Number and String
1595 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001596 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001598 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001599call_vim_function(
1600 char_u *func,
1601 int argc,
1602 char_u **argv,
1603 int safe, /* use the sandbox */
1604 int str_arg_only, /* all arguments are strings */
1605 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606{
Bram Moolenaar33570922005-01-25 22:26:29 +00001607 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 long n;
1609 int len;
1610 int i;
1611 int doesrange;
1612 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001613 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001615 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001617 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618
1619 for (i = 0; i < argc; i++)
1620 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001621 /* Pass a NULL or empty argument as an empty string */
1622 if (argv[i] == NULL || *argv[i] == NUL)
1623 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001624 argvars[i].v_type = VAR_STRING;
1625 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001626 continue;
1627 }
1628
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001629 if (str_arg_only)
1630 len = 0;
1631 else
1632 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001633 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 if (len != 0 && len == (int)STRLEN(argv[i]))
1635 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001636 argvars[i].v_type = VAR_NUMBER;
1637 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 }
1639 else
1640 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001641 argvars[i].v_type = VAR_STRING;
1642 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 }
1644 }
1645
1646 if (safe)
1647 {
1648 save_funccalp = save_funccal();
1649 ++sandbox;
1650 }
1651
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001652 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1653 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001655 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 if (safe)
1657 {
1658 --sandbox;
1659 restore_funccal(save_funccalp);
1660 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001661 vim_free(argvars);
1662
1663 if (ret == FAIL)
1664 clear_tv(rettv);
1665
1666 return ret;
1667}
1668
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001669/*
1670 * Call vimL function "func" and return the result as a number.
1671 * Returns -1 when calling the function fails.
1672 * Uses argv[argc] for the function arguments.
1673 */
1674 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001675call_func_retnr(
1676 char_u *func,
1677 int argc,
1678 char_u **argv,
1679 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001680{
1681 typval_T rettv;
1682 long retval;
1683
1684 /* All arguments are passed as strings, no conversion to number. */
1685 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1686 return -1;
1687
1688 retval = get_tv_number_chk(&rettv, NULL);
1689 clear_tv(&rettv);
1690 return retval;
1691}
1692
1693#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1694 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1695
Bram Moolenaar4f688582007-07-24 12:34:30 +00001696# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001697/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001698 * Call vimL function "func" and return the result as a string.
1699 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001700 * Uses argv[argc] for the function arguments.
1701 */
1702 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001703call_func_retstr(
1704 char_u *func,
1705 int argc,
1706 char_u **argv,
1707 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001708{
1709 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001710 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001711
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001712 /* All arguments are passed as strings, no conversion to number. */
1713 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001714 return NULL;
1715
1716 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001717 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 return retval;
1719}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001720# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001721
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001722/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001723 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001724 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001725 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001726 */
1727 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001728call_func_retlist(
1729 char_u *func,
1730 int argc,
1731 char_u **argv,
1732 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001733{
1734 typval_T rettv;
1735
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001736 /* All arguments are passed as strings, no conversion to number. */
1737 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001738 return NULL;
1739
1740 if (rettv.v_type != VAR_LIST)
1741 {
1742 clear_tv(&rettv);
1743 return NULL;
1744 }
1745
1746 return rettv.vval.v_list;
1747}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748#endif
1749
1750/*
1751 * Save the current function call pointer, and set it to NULL.
1752 * Used when executing autocommands and for ":source".
1753 */
1754 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001755save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001757 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 current_funccal = NULL;
1760 return (void *)fc;
1761}
1762
1763 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001764restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001766 funccall_T *fc = (funccall_T *)vfc;
1767
1768 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769}
1770
Bram Moolenaar05159a02005-02-26 23:04:13 +00001771#if defined(FEAT_PROFILE) || defined(PROTO)
1772/*
1773 * Prepare profiling for entering a child or something else that is not
1774 * counted for the script/function itself.
1775 * Should always be called in pair with prof_child_exit().
1776 */
1777 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001778prof_child_enter(
1779 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001780{
1781 funccall_T *fc = current_funccal;
1782
1783 if (fc != NULL && fc->func->uf_profiling)
1784 profile_start(&fc->prof_child);
1785 script_prof_save(tm);
1786}
1787
1788/*
1789 * Take care of time spent in a child.
1790 * Should always be called after prof_child_enter().
1791 */
1792 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001793prof_child_exit(
1794 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001795{
1796 funccall_T *fc = current_funccal;
1797
1798 if (fc != NULL && fc->func->uf_profiling)
1799 {
1800 profile_end(&fc->prof_child);
1801 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1802 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1803 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1804 }
1805 script_prof_restore(tm);
1806}
1807#endif
1808
1809
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810#ifdef FEAT_FOLDING
1811/*
1812 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1813 * it in "*cp". Doesn't give error messages.
1814 */
1815 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001816eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817{
Bram Moolenaar33570922005-01-25 22:26:29 +00001818 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 int retval;
1820 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001821 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1822 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823
1824 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001825 if (use_sandbox)
1826 ++sandbox;
1827 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001829 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 retval = 0;
1831 else
1832 {
1833 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001834 if (tv.v_type == VAR_NUMBER)
1835 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001836 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 retval = 0;
1838 else
1839 {
1840 /* If the result is a string, check if there is a non-digit before
1841 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001842 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 if (!VIM_ISDIGIT(*s) && *s != '-')
1844 *cp = *s++;
1845 retval = atol((char *)s);
1846 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 }
1849 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001850 if (use_sandbox)
1851 --sandbox;
1852 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853
1854 return retval;
1855}
1856#endif
1857
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 * ":let" list all variable values
1860 * ":let var1 var2" list variable values
1861 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001862 * ":let var += expr" assignment command.
1863 * ":let var -= expr" assignment command.
1864 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001865 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 */
1867 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001868ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869{
1870 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001872 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 int var_count = 0;
1875 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001876 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001877 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001878 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879
Bram Moolenaardb552d602006-03-23 22:59:57 +00001880 argend = skip_var_list(arg, &var_count, &semicolon);
1881 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001882 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001883 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1884 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001885 expr = skipwhite(argend);
1886 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1887 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001889 /*
1890 * ":let" without "=": list variables
1891 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892 if (*arg == '[')
1893 EMSG(_(e_invarg));
1894 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001896 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001898 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001900 list_glob_vars(&first);
1901 list_buf_vars(&first);
1902 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001903#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001904 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001905#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001906 list_script_vars(&first);
1907 list_func_vars(&first);
1908 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 eap->nextcmd = check_nextcmd(arg);
1911 }
1912 else
1913 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001914 op[0] = '=';
1915 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001916 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001917 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001918 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1919 op[0] = *expr; /* +=, -= or .= */
1920 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001921 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001922 else
1923 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 if (eap->skip)
1926 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001927 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 if (eap->skip)
1929 {
1930 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001931 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 --emsg_skip;
1933 }
1934 else if (i != FAIL)
1935 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001936 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001937 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001938 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 }
1940 }
1941}
1942
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001943/*
1944 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1945 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001946 * When "nextchars" is not NULL it points to a string with characters that
1947 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1948 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001949 * Returns OK or FAIL;
1950 */
1951 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001952ex_let_vars(
1953 char_u *arg_start,
1954 typval_T *tv,
1955 int copy, /* copy values from "tv", don't move */
1956 int semicolon, /* from skip_var_list() */
1957 int var_count, /* from skip_var_list() */
1958 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959{
1960 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001961 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001962 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001963 listitem_T *item;
1964 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965
1966 if (*arg != '[')
1967 {
1968 /*
1969 * ":let var = expr" or ":for var in list"
1970 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001971 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972 return FAIL;
1973 return OK;
1974 }
1975
1976 /*
1977 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1978 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001979 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980 {
1981 EMSG(_(e_listreq));
1982 return FAIL;
1983 }
1984
1985 i = list_len(l);
1986 if (semicolon == 0 && var_count < i)
1987 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001988 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989 return FAIL;
1990 }
1991 if (var_count - semicolon > i)
1992 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001993 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001994 return FAIL;
1995 }
1996
1997 item = l->lv_first;
1998 while (*arg != ']')
1999 {
2000 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002001 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002 item = item->li_next;
2003 if (arg == NULL)
2004 return FAIL;
2005
2006 arg = skipwhite(arg);
2007 if (*arg == ';')
2008 {
2009 /* Put the rest of the list (may be empty) in the var after ';'.
2010 * Create a new list for this. */
2011 l = list_alloc();
2012 if (l == NULL)
2013 return FAIL;
2014 while (item != NULL)
2015 {
2016 list_append_tv(l, &item->li_tv);
2017 item = item->li_next;
2018 }
2019
2020 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002021 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002022 ltv.vval.v_list = l;
2023 l->lv_refcount = 1;
2024
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002025 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2026 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002027 clear_tv(&ltv);
2028 if (arg == NULL)
2029 return FAIL;
2030 break;
2031 }
2032 else if (*arg != ',' && *arg != ']')
2033 {
2034 EMSG2(_(e_intern2), "ex_let_vars()");
2035 return FAIL;
2036 }
2037 }
2038
2039 return OK;
2040}
2041
2042/*
2043 * Skip over assignable variable "var" or list of variables "[var, var]".
2044 * Used for ":let varvar = expr" and ":for varvar in expr".
2045 * For "[var, var]" increment "*var_count" for each variable.
2046 * for "[var, var; var]" set "semicolon".
2047 * Return NULL for an error.
2048 */
2049 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002050skip_var_list(
2051 char_u *arg,
2052 int *var_count,
2053 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002054{
2055 char_u *p, *s;
2056
2057 if (*arg == '[')
2058 {
2059 /* "[var, var]": find the matching ']'. */
2060 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002061 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002062 {
2063 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2064 s = skip_var_one(p);
2065 if (s == p)
2066 {
2067 EMSG2(_(e_invarg2), p);
2068 return NULL;
2069 }
2070 ++*var_count;
2071
2072 p = skipwhite(s);
2073 if (*p == ']')
2074 break;
2075 else if (*p == ';')
2076 {
2077 if (*semicolon == 1)
2078 {
2079 EMSG(_("Double ; in list of variables"));
2080 return NULL;
2081 }
2082 *semicolon = 1;
2083 }
2084 else if (*p != ',')
2085 {
2086 EMSG2(_(e_invarg2), p);
2087 return NULL;
2088 }
2089 }
2090 return p + 1;
2091 }
2092 else
2093 return skip_var_one(arg);
2094}
2095
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002096/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002097 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002098 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002099 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002100 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002101skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002102{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002103 if (*arg == '@' && arg[1] != NUL)
2104 return arg + 2;
2105 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2106 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002107}
2108
Bram Moolenaara7043832005-01-21 11:56:39 +00002109/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002110 * List variables for hashtab "ht" with prefix "prefix".
2111 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002112 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002114list_hashtable_vars(
2115 hashtab_T *ht,
2116 char_u *prefix,
2117 int empty,
2118 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002119{
Bram Moolenaar33570922005-01-25 22:26:29 +00002120 hashitem_T *hi;
2121 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002122 int todo;
2123
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002124 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002125 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2126 {
2127 if (!HASHITEM_EMPTY(hi))
2128 {
2129 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002130 di = HI2DI(hi);
2131 if (empty || di->di_tv.v_type != VAR_STRING
2132 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002133 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002134 }
2135 }
2136}
2137
2138/*
2139 * List global variables.
2140 */
2141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002142list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002143{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002145}
2146
2147/*
2148 * List buffer variables.
2149 */
2150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002151list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002152{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002153 char_u numbuf[NUMBUFLEN];
2154
Bram Moolenaar429fa852013-04-15 12:27:36 +02002155 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002157
2158 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002159 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2160 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002161}
2162
2163/*
2164 * List window variables.
2165 */
2166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002167list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002168{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002169 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002171}
2172
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002173#ifdef FEAT_WINDOWS
2174/*
2175 * List tab page variables.
2176 */
2177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002178list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002179{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002180 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002181 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002182}
2183#endif
2184
Bram Moolenaara7043832005-01-21 11:56:39 +00002185/*
2186 * List Vim variables.
2187 */
2188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002189list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192}
2193
2194/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002195 * List script-local variables, if there is a script.
2196 */
2197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002198list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002199{
2200 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002201 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2202 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002203}
2204
2205/*
2206 * List function variables, if there is a function.
2207 */
2208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002209list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002210{
2211 if (current_funccal != NULL)
2212 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002213 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002214}
2215
2216/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 * List variables in "arg".
2218 */
2219 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002220list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221{
2222 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002223 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225 char_u *name_start;
2226 char_u *arg_subsc;
2227 char_u *tofree;
2228 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229
2230 while (!ends_excmd(*arg) && !got_int)
2231 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002234 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002235 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2236 {
2237 emsg_severe = TRUE;
2238 EMSG(_(e_trailing));
2239 break;
2240 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002241 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 /* get_name_len() takes care of expanding curly braces */
2245 name_start = name = arg;
2246 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2247 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 /* This is mainly to keep test 49 working: when expanding
2250 * curly braces fails overrule the exception error message. */
2251 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 emsg_severe = TRUE;
2254 EMSG2(_(e_invarg2), arg);
2255 break;
2256 }
2257 error = TRUE;
2258 }
2259 else
2260 {
2261 if (tofree != NULL)
2262 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002263 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 else
2266 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 /* handle d.key, l[idx], f(expr) */
2268 arg_subsc = arg;
2269 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002270 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002272 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002274 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002275 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002276 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002277 case 'g': list_glob_vars(first); break;
2278 case 'b': list_buf_vars(first); break;
2279 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002280#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002281 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002282#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002283 case 'v': list_vim_vars(first); break;
2284 case 's': list_script_vars(first); break;
2285 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286 default:
2287 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002288 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002289 }
2290 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002291 {
2292 char_u numbuf[NUMBUFLEN];
2293 char_u *tf;
2294 int c;
2295 char_u *s;
2296
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002297 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002298 c = *arg;
2299 *arg = NUL;
2300 list_one_var_a((char_u *)"",
2301 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002302 tv.v_type,
2303 s == NULL ? (char_u *)"" : s,
2304 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002305 *arg = c;
2306 vim_free(tf);
2307 }
2308 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002309 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310 }
2311 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312
2313 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002315
2316 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317 }
2318
2319 return arg;
2320}
2321
2322/*
2323 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2324 * Returns a pointer to the char just after the var name.
2325 * Returns NULL if there is an error.
2326 */
2327 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002328ex_let_one(
2329 char_u *arg, /* points to variable name */
2330 typval_T *tv, /* value to assign to variable */
2331 int copy, /* copy value from "tv" */
2332 char_u *endchars, /* valid chars after variable name or NULL */
2333 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334{
2335 int c1;
2336 char_u *name;
2337 char_u *p;
2338 char_u *arg_end = NULL;
2339 int len;
2340 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002341 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002342
2343 /*
2344 * ":let $VAR = expr": Set environment variable.
2345 */
2346 if (*arg == '$')
2347 {
2348 /* Find the end of the name. */
2349 ++arg;
2350 name = arg;
2351 len = get_env_len(&arg);
2352 if (len == 0)
2353 EMSG2(_(e_invarg2), name - 1);
2354 else
2355 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002356 if (op != NULL && (*op == '+' || *op == '-'))
2357 EMSG2(_(e_letwrong), op);
2358 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002359 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002360 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002361 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002362 {
2363 c1 = name[len];
2364 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002365 p = get_tv_string_chk(tv);
2366 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002367 {
2368 int mustfree = FALSE;
2369 char_u *s = vim_getenv(name, &mustfree);
2370
2371 if (s != NULL)
2372 {
2373 p = tofree = concat_str(s, p);
2374 if (mustfree)
2375 vim_free(s);
2376 }
2377 }
2378 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002379 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002380 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002381 if (STRICMP(name, "HOME") == 0)
2382 init_homedir();
2383 else if (didset_vim && STRICMP(name, "VIM") == 0)
2384 didset_vim = FALSE;
2385 else if (didset_vimruntime
2386 && STRICMP(name, "VIMRUNTIME") == 0)
2387 didset_vimruntime = FALSE;
2388 arg_end = arg;
2389 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002392 }
2393 }
2394 }
2395
2396 /*
2397 * ":let &option = expr": Set option value.
2398 * ":let &l:option = expr": Set local option value.
2399 * ":let &g:option = expr": Set global option value.
2400 */
2401 else if (*arg == '&')
2402 {
2403 /* Find the end of the name. */
2404 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002405 if (p == NULL || (endchars != NULL
2406 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002407 EMSG(_(e_letunexp));
2408 else
2409 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002410 long n;
2411 int opt_type;
2412 long numval;
2413 char_u *stringval = NULL;
2414 char_u *s;
2415
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002416 c1 = *p;
2417 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002418
2419 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002420 s = get_tv_string_chk(tv); /* != NULL if number or string */
2421 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002422 {
2423 opt_type = get_option_value(arg, &numval,
2424 &stringval, opt_flags);
2425 if ((opt_type == 1 && *op == '.')
2426 || (opt_type == 0 && *op != '.'))
2427 EMSG2(_(e_letwrong), op);
2428 else
2429 {
2430 if (opt_type == 1) /* number */
2431 {
2432 if (*op == '+')
2433 n = numval + n;
2434 else
2435 n = numval - n;
2436 }
2437 else if (opt_type == 0 && stringval != NULL) /* string */
2438 {
2439 s = concat_str(stringval, s);
2440 vim_free(stringval);
2441 stringval = s;
2442 }
2443 }
2444 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002445 if (s != NULL)
2446 {
2447 set_option_value(arg, n, s, opt_flags);
2448 arg_end = p;
2449 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002450 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002452 }
2453 }
2454
2455 /*
2456 * ":let @r = expr": Set register contents.
2457 */
2458 else if (*arg == '@')
2459 {
2460 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002461 if (op != NULL && (*op == '+' || *op == '-'))
2462 EMSG2(_(e_letwrong), op);
2463 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002464 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465 EMSG(_(e_letunexp));
2466 else
2467 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002468 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002469 char_u *s;
2470
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002471 p = get_tv_string_chk(tv);
2472 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002473 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002474 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002475 if (s != NULL)
2476 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002477 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 vim_free(s);
2479 }
2480 }
2481 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002482 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002483 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002484 arg_end = arg + 1;
2485 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002486 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002487 }
2488 }
2489
2490 /*
2491 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002493 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002494 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002495 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002496 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002498 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2502 EMSG(_(e_letunexp));
2503 else
2504 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002505 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 arg_end = p;
2507 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002508 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510 }
2511
2512 else
2513 EMSG2(_(e_invarg2), arg);
2514
2515 return arg_end;
2516}
2517
2518/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002519 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2520 */
2521 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002522check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002523{
2524 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2525 {
2526 EMSG2(_(e_readonlyvar), arg);
2527 return TRUE;
2528 }
2529 return FALSE;
2530}
2531
2532/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 * Get an lval: variable, Dict item or List item that can be assigned a value
2534 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2535 * "name.key", "name.key[expr]" etc.
2536 * Indexing only works if "name" is an existing List or Dictionary.
2537 * "name" points to the start of the name.
2538 * If "rettv" is not NULL it points to the value to be assigned.
2539 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2540 * wrong; must end in space or cmd separator.
2541 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002542 * flags:
2543 * GLV_QUIET: do not give error messages
2544 * GLV_NO_AUTOLOAD: do not use script autoloading
2545 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002547 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002549 */
2550 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002551get_lval(
2552 char_u *name,
2553 typval_T *rettv,
2554 lval_T *lp,
2555 int unlet,
2556 int skip,
2557 int flags, /* GLV_ values */
2558 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002559{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002560 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 char_u *expr_start, *expr_end;
2562 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002563 dictitem_T *v;
2564 typval_T var1;
2565 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002567 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002568 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002569 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002570 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002571 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002572
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002574 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575
2576 if (skip)
2577 {
2578 /* When skipping just find the end of the name. */
2579 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002580 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 }
2582
2583 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002584 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 if (expr_start != NULL)
2586 {
2587 /* Don't expand the name when we already know there is an error. */
2588 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2589 && *p != '[' && *p != '.')
2590 {
2591 EMSG(_(e_trailing));
2592 return NULL;
2593 }
2594
2595 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2596 if (lp->ll_exp_name == NULL)
2597 {
2598 /* Report an invalid expression in braces, unless the
2599 * expression evaluation has been cancelled due to an
2600 * aborting error, an interrupt, or an exception. */
2601 if (!aborting() && !quiet)
2602 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002603 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 EMSG2(_(e_invarg2), name);
2605 return NULL;
2606 }
2607 }
2608 lp->ll_name = lp->ll_exp_name;
2609 }
2610 else
2611 lp->ll_name = name;
2612
2613 /* Without [idx] or .key we are done. */
2614 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2615 return p;
2616
2617 cc = *p;
2618 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002619 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 if (v == NULL && !quiet)
2621 EMSG2(_(e_undefvar), lp->ll_name);
2622 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002623 if (v == NULL)
2624 return NULL;
2625
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 /*
2627 * Loop until no more [idx] or .key is following.
2628 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002629 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002631 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2633 && !(lp->ll_tv->v_type == VAR_DICT
2634 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002635 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 if (!quiet)
2637 EMSG(_("E689: Can only index a List or Dictionary"));
2638 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002639 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002641 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 if (!quiet)
2643 EMSG(_("E708: [:] must come last"));
2644 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002645 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002646
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 len = -1;
2648 if (*p == '.')
2649 {
2650 key = p + 1;
2651 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2652 ;
2653 if (len == 0)
2654 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 if (!quiet)
2656 EMSG(_(e_emptykey));
2657 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 }
2659 p = key + len;
2660 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002661 else
2662 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002664 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002665 if (*p == ':')
2666 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002667 else
2668 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 empty1 = FALSE;
2670 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002672 if (get_tv_string_chk(&var1) == NULL)
2673 {
2674 /* not a number or string */
2675 clear_tv(&var1);
2676 return NULL;
2677 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 }
2679
2680 /* Optionally get the second index [ :expr]. */
2681 if (*p == ':')
2682 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002686 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002687 if (!empty1)
2688 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002690 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 if (rettv != NULL && (rettv->v_type != VAR_LIST
2692 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 if (!quiet)
2695 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 if (!empty1)
2697 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 }
2700 p = skipwhite(p + 1);
2701 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 else
2704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2707 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 if (!empty1)
2709 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002712 if (get_tv_string_chk(&var2) == NULL)
2713 {
2714 /* not a number or string */
2715 if (!empty1)
2716 clear_tv(&var1);
2717 clear_tv(&var2);
2718 return NULL;
2719 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002722 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002725
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002727 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 if (!quiet)
2729 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 if (!empty1)
2731 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 }
2736
2737 /* Skip to past ']'. */
2738 ++p;
2739 }
2740
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 {
2743 if (len == -1)
2744 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002746 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 if (*key == NUL)
2748 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 if (!quiet)
2750 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 }
2754 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002755 lp->ll_list = NULL;
2756 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002757 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002758
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002759 /* When assigning to a scope dictionary check that a function and
2760 * variable name is valid (only variable name unless it is l: or
2761 * g: dictionary). Disallow overwriting a builtin function. */
2762 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002763 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002764 int prevval;
2765 int wrong;
2766
2767 if (len != -1)
2768 {
2769 prevval = key[len];
2770 key[len] = NUL;
2771 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002772 else
2773 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002774 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2775 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002776 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002777 || !valid_varname(key);
2778 if (len != -1)
2779 key[len] = prevval;
2780 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002781 return NULL;
2782 }
2783
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002786 /* Can't add "v:" variable. */
2787 if (lp->ll_dict == &vimvardict)
2788 {
2789 EMSG2(_(e_illvar), name);
2790 return NULL;
2791 }
2792
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002793 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002797 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 if (len == -1)
2799 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 }
2802 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 if (len == -1)
2807 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 p = NULL;
2810 break;
2811 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002812 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002813 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002814 return NULL;
2815
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 if (len == -1)
2817 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002819 }
2820 else
2821 {
2822 /*
2823 * Get the number and item for the only or first index of the List.
2824 */
2825 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 else
2828 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002829 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 clear_tv(&var1);
2831 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002832 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 lp->ll_list = lp->ll_tv->vval.v_list;
2834 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2835 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002836 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002837 if (lp->ll_n1 < 0)
2838 {
2839 lp->ll_n1 = 0;
2840 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2841 }
2842 }
2843 if (lp->ll_li == NULL)
2844 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002847 if (!quiet)
2848 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002850 }
2851
2852 /*
2853 * May need to find the item or absolute index for the second
2854 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 * When no index given: "lp->ll_empty2" is TRUE.
2856 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002860 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002865 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002866 {
2867 if (!quiet)
2868 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002870 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002872 }
2873
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2875 if (lp->ll_n1 < 0)
2876 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2877 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002878 {
2879 if (!quiet)
2880 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002882 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002883 }
2884
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002886 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002887 }
2888
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 return p;
2890}
2891
2892/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002893 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 */
2895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002896clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897{
2898 vim_free(lp->ll_exp_name);
2899 vim_free(lp->ll_newkey);
2900}
2901
2902/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002903 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002905 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 */
2907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002908set_var_lval(
2909 lval_T *lp,
2910 char_u *endp,
2911 typval_T *rettv,
2912 int copy,
2913 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914{
2915 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002916 listitem_T *ri;
2917 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918
2919 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002920 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002922 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 cc = *endp;
2924 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002925 if (op != NULL && *op != '=')
2926 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002927 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002928
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002929 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002930 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002931 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002932 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002933 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002934 if ((di == NULL
2935 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2936 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2937 FALSE)))
2938 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002939 set_var(lp->ll_name, &tv, FALSE);
2940 clear_tv(&tv);
2941 }
2942 }
2943 else
2944 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002946 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002948 else if (tv_check_lock(lp->ll_newkey == NULL
2949 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002950 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002951 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 else if (lp->ll_range)
2953 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002954 listitem_T *ll_li = lp->ll_li;
2955 int ll_n1 = lp->ll_n1;
2956
2957 /*
2958 * Check whether any of the list items is locked
2959 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002960 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002961 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002962 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002963 return;
2964 ri = ri->li_next;
2965 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2966 break;
2967 ll_li = ll_li->li_next;
2968 ++ll_n1;
2969 }
2970
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002971 /*
2972 * Assign the List values to the list items.
2973 */
2974 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002975 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002976 if (op != NULL && *op != '=')
2977 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2978 else
2979 {
2980 clear_tv(&lp->ll_li->li_tv);
2981 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2982 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002983 ri = ri->li_next;
2984 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2985 break;
2986 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002987 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002989 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002990 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002991 ri = NULL;
2992 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002993 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002994 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 lp->ll_li = lp->ll_li->li_next;
2996 ++lp->ll_n1;
2997 }
2998 if (ri != NULL)
2999 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003000 else if (lp->ll_empty2
3001 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 : lp->ll_n1 != lp->ll_n2)
3003 EMSG(_("E711: List value has not enough items"));
3004 }
3005 else
3006 {
3007 /*
3008 * Assign to a List or Dictionary item.
3009 */
3010 if (lp->ll_newkey != NULL)
3011 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003012 if (op != NULL && *op != '=')
3013 {
3014 EMSG2(_(e_letwrong), op);
3015 return;
3016 }
3017
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003018 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003019 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003020 if (di == NULL)
3021 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003022 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3023 {
3024 vim_free(di);
3025 return;
3026 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003027 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003028 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003029 else if (op != NULL && *op != '=')
3030 {
3031 tv_op(lp->ll_tv, rettv, op);
3032 return;
3033 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003034 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003035 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003036
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003037 /*
3038 * Assign the value to the variable or list item.
3039 */
3040 if (copy)
3041 copy_tv(rettv, lp->ll_tv);
3042 else
3043 {
3044 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003045 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003046 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003047 }
3048 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003049}
3050
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003051/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003052 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3053 * Returns OK or FAIL.
3054 */
3055 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003056tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003057{
3058 long n;
3059 char_u numbuf[NUMBUFLEN];
3060 char_u *s;
3061
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003062 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3063 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3064 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003065 {
3066 switch (tv1->v_type)
3067 {
3068 case VAR_DICT:
3069 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003070 case VAR_SPECIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003071 break;
3072
3073 case VAR_LIST:
3074 if (*op != '+' || tv2->v_type != VAR_LIST)
3075 break;
3076 /* List += List */
3077 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3078 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3079 return OK;
3080
3081 case VAR_NUMBER:
3082 case VAR_STRING:
3083 if (tv2->v_type == VAR_LIST)
3084 break;
3085 if (*op == '+' || *op == '-')
3086 {
3087 /* nr += nr or nr -= nr*/
3088 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003089#ifdef FEAT_FLOAT
3090 if (tv2->v_type == VAR_FLOAT)
3091 {
3092 float_T f = n;
3093
3094 if (*op == '+')
3095 f += tv2->vval.v_float;
3096 else
3097 f -= tv2->vval.v_float;
3098 clear_tv(tv1);
3099 tv1->v_type = VAR_FLOAT;
3100 tv1->vval.v_float = f;
3101 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003102 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003103#endif
3104 {
3105 if (*op == '+')
3106 n += get_tv_number(tv2);
3107 else
3108 n -= get_tv_number(tv2);
3109 clear_tv(tv1);
3110 tv1->v_type = VAR_NUMBER;
3111 tv1->vval.v_number = n;
3112 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003113 }
3114 else
3115 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003116 if (tv2->v_type == VAR_FLOAT)
3117 break;
3118
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003119 /* str .= str */
3120 s = get_tv_string(tv1);
3121 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3122 clear_tv(tv1);
3123 tv1->v_type = VAR_STRING;
3124 tv1->vval.v_string = s;
3125 }
3126 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003127
3128#ifdef FEAT_FLOAT
3129 case VAR_FLOAT:
3130 {
3131 float_T f;
3132
3133 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3134 && tv2->v_type != VAR_NUMBER
3135 && tv2->v_type != VAR_STRING))
3136 break;
3137 if (tv2->v_type == VAR_FLOAT)
3138 f = tv2->vval.v_float;
3139 else
3140 f = get_tv_number(tv2);
3141 if (*op == '+')
3142 tv1->vval.v_float += f;
3143 else
3144 tv1->vval.v_float -= f;
3145 }
3146 return OK;
3147#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003148 }
3149 }
3150
3151 EMSG2(_(e_letwrong), op);
3152 return FAIL;
3153}
3154
3155/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003156 * Add a watcher to a list.
3157 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003158 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003159list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003160{
3161 lw->lw_next = l->lv_watch;
3162 l->lv_watch = lw;
3163}
3164
3165/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003166 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003167 * No warning when it isn't found...
3168 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003169 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003170list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003171{
Bram Moolenaar33570922005-01-25 22:26:29 +00003172 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003173
3174 lwp = &l->lv_watch;
3175 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3176 {
3177 if (lw == lwrem)
3178 {
3179 *lwp = lw->lw_next;
3180 break;
3181 }
3182 lwp = &lw->lw_next;
3183 }
3184}
3185
3186/*
3187 * Just before removing an item from a list: advance watchers to the next
3188 * item.
3189 */
3190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003191list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003192{
Bram Moolenaar33570922005-01-25 22:26:29 +00003193 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194
3195 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3196 if (lw->lw_item == item)
3197 lw->lw_item = item->li_next;
3198}
3199
3200/*
3201 * Evaluate the expression used in a ":for var in expr" command.
3202 * "arg" points to "var".
3203 * Set "*errp" to TRUE for an error, FALSE otherwise;
3204 * Return a pointer that holds the info. Null when there is an error.
3205 */
3206 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003207eval_for_line(
3208 char_u *arg,
3209 int *errp,
3210 char_u **nextcmdp,
3211 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003212{
Bram Moolenaar33570922005-01-25 22:26:29 +00003213 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003215 typval_T tv;
3216 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003217
3218 *errp = TRUE; /* default: there is an error */
3219
Bram Moolenaar33570922005-01-25 22:26:29 +00003220 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003221 if (fi == NULL)
3222 return NULL;
3223
3224 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3225 if (expr == NULL)
3226 return fi;
3227
3228 expr = skipwhite(expr);
3229 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3230 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003231 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232 return fi;
3233 }
3234
3235 if (skip)
3236 ++emsg_skip;
3237 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3238 {
3239 *errp = FALSE;
3240 if (!skip)
3241 {
3242 l = tv.vval.v_list;
3243 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003244 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003245 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003246 clear_tv(&tv);
3247 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003248 else
3249 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003250 /* No need to increment the refcount, it's already set for the
3251 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003252 fi->fi_list = l;
3253 list_add_watch(l, &fi->fi_lw);
3254 fi->fi_lw.lw_item = l->lv_first;
3255 }
3256 }
3257 }
3258 if (skip)
3259 --emsg_skip;
3260
3261 return fi;
3262}
3263
3264/*
3265 * Use the first item in a ":for" list. Advance to the next.
3266 * Assign the values to the variable (list). "arg" points to the first one.
3267 * Return TRUE when a valid item was found, FALSE when at end of list or
3268 * something wrong.
3269 */
3270 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003271next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003273 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003274 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003275 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276
3277 item = fi->fi_lw.lw_item;
3278 if (item == NULL)
3279 result = FALSE;
3280 else
3281 {
3282 fi->fi_lw.lw_item = item->li_next;
3283 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3284 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3285 }
3286 return result;
3287}
3288
3289/*
3290 * Free the structure used to store info used by ":for".
3291 */
3292 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003293free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294{
Bram Moolenaar33570922005-01-25 22:26:29 +00003295 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003296
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003297 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003298 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003299 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003300 list_unref(fi->fi_list);
3301 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003302 vim_free(fi);
3303}
3304
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3306
3307 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003308set_context_for_expression(
3309 expand_T *xp,
3310 char_u *arg,
3311 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312{
3313 int got_eq = FALSE;
3314 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003315 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003317 if (cmdidx == CMD_let)
3318 {
3319 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003320 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321 {
3322 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003323 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324 {
3325 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003326 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003327 if (vim_iswhite(*p))
3328 break;
3329 }
3330 return;
3331 }
3332 }
3333 else
3334 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3335 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 while ((xp->xp_pattern = vim_strpbrk(arg,
3337 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3338 {
3339 c = *xp->xp_pattern;
3340 if (c == '&')
3341 {
3342 c = xp->xp_pattern[1];
3343 if (c == '&')
3344 {
3345 ++xp->xp_pattern;
3346 xp->xp_context = cmdidx != CMD_let || got_eq
3347 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3348 }
3349 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003350 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003352 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3353 xp->xp_pattern += 2;
3354
3355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 }
3357 else if (c == '$')
3358 {
3359 /* environment variable */
3360 xp->xp_context = EXPAND_ENV_VARS;
3361 }
3362 else if (c == '=')
3363 {
3364 got_eq = TRUE;
3365 xp->xp_context = EXPAND_EXPRESSION;
3366 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003367 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 && xp->xp_context == EXPAND_FUNCTIONS
3369 && vim_strchr(xp->xp_pattern, '(') == NULL)
3370 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003371 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 break;
3373 }
3374 else if (cmdidx != CMD_let || got_eq)
3375 {
3376 if (c == '"') /* string */
3377 {
3378 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3379 if (c == '\\' && xp->xp_pattern[1] != NUL)
3380 ++xp->xp_pattern;
3381 xp->xp_context = EXPAND_NOTHING;
3382 }
3383 else if (c == '\'') /* literal string */
3384 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003385 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3387 /* skip */ ;
3388 xp->xp_context = EXPAND_NOTHING;
3389 }
3390 else if (c == '|')
3391 {
3392 if (xp->xp_pattern[1] == '|')
3393 {
3394 ++xp->xp_pattern;
3395 xp->xp_context = EXPAND_EXPRESSION;
3396 }
3397 else
3398 xp->xp_context = EXPAND_COMMANDS;
3399 }
3400 else
3401 xp->xp_context = EXPAND_EXPRESSION;
3402 }
3403 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003404 /* Doesn't look like something valid, expand as an expression
3405 * anyway. */
3406 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 arg = xp->xp_pattern;
3408 if (*arg != NUL)
3409 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3410 /* skip */ ;
3411 }
3412 xp->xp_pattern = arg;
3413}
3414
3415#endif /* FEAT_CMDL_COMPL */
3416
3417/*
3418 * ":1,25call func(arg1, arg2)" function call.
3419 */
3420 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003421ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422{
3423 char_u *arg = eap->arg;
3424 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003426 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003428 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 linenr_T lnum;
3430 int doesrange;
3431 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003432 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003434 if (eap->skip)
3435 {
3436 /* trans_function_name() doesn't work well when skipping, use eval0()
3437 * instead to skip to any following command, e.g. for:
3438 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003439 ++emsg_skip;
3440 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3441 clear_tv(&rettv);
3442 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003443 return;
3444 }
3445
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003446 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003447 if (fudi.fd_newkey != NULL)
3448 {
3449 /* Still need to give an error message for missing key. */
3450 EMSG2(_(e_dictkey), fudi.fd_newkey);
3451 vim_free(fudi.fd_newkey);
3452 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003453 if (tofree == NULL)
3454 return;
3455
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003456 /* Increase refcount on dictionary, it could get deleted when evaluating
3457 * the arguments. */
3458 if (fudi.fd_dict != NULL)
3459 ++fudi.fd_dict->dv_refcount;
3460
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003461 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003462 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003463 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464
Bram Moolenaar532c7802005-01-27 14:44:31 +00003465 /* Skip white space to allow ":call func ()". Not good, but required for
3466 * backward compatibility. */
3467 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003468 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469
3470 if (*startarg != '(')
3471 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003472 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 goto end;
3474 }
3475
3476 /*
3477 * When skipping, evaluate the function once, to find the end of the
3478 * arguments.
3479 * When the function takes a range, this is discovered after the first
3480 * call, and the loop is broken.
3481 */
3482 if (eap->skip)
3483 {
3484 ++emsg_skip;
3485 lnum = eap->line2; /* do it once, also with an invalid range */
3486 }
3487 else
3488 lnum = eap->line1;
3489 for ( ; lnum <= eap->line2; ++lnum)
3490 {
3491 if (!eap->skip && eap->addr_count > 0)
3492 {
3493 curwin->w_cursor.lnum = lnum;
3494 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003495#ifdef FEAT_VIRTUALEDIT
3496 curwin->w_cursor.coladd = 0;
3497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 }
3499 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003500 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003501 eap->line1, eap->line2, &doesrange,
3502 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 {
3504 failed = TRUE;
3505 break;
3506 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003507
3508 /* Handle a function returning a Funcref, Dictionary or List. */
3509 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3510 {
3511 failed = TRUE;
3512 break;
3513 }
3514
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003515 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 if (doesrange || eap->skip)
3517 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003518
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003520 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003521 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003522 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 if (aborting())
3524 break;
3525 }
3526 if (eap->skip)
3527 --emsg_skip;
3528
3529 if (!failed)
3530 {
3531 /* Check for trailing illegal characters and a following command. */
3532 if (!ends_excmd(*arg))
3533 {
3534 emsg_severe = TRUE;
3535 EMSG(_(e_trailing));
3536 }
3537 else
3538 eap->nextcmd = check_nextcmd(arg);
3539 }
3540
3541end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003542 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003543 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544}
3545
3546/*
3547 * ":unlet[!] var1 ... " command.
3548 */
3549 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003550ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003552 ex_unletlock(eap, eap->arg, 0);
3553}
3554
3555/*
3556 * ":lockvar" and ":unlockvar" commands
3557 */
3558 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003559ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003560{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003562 int deep = 2;
3563
3564 if (eap->forceit)
3565 deep = -1;
3566 else if (vim_isdigit(*arg))
3567 {
3568 deep = getdigits(&arg);
3569 arg = skipwhite(arg);
3570 }
3571
3572 ex_unletlock(eap, arg, deep);
3573}
3574
3575/*
3576 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3577 */
3578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003579ex_unletlock(
3580 exarg_T *eap,
3581 char_u *argstart,
3582 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003583{
3584 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003587 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588
3589 do
3590 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003591 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003592 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003593 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003594 if (lv.ll_name == NULL)
3595 error = TRUE; /* error but continue parsing */
3596 if (name_end == NULL || (!vim_iswhite(*name_end)
3597 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003599 if (name_end != NULL)
3600 {
3601 emsg_severe = TRUE;
3602 EMSG(_(e_trailing));
3603 }
3604 if (!(eap->skip || error))
3605 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 break;
3607 }
3608
3609 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003610 {
3611 if (eap->cmdidx == CMD_unlet)
3612 {
3613 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3614 error = TRUE;
3615 }
3616 else
3617 {
3618 if (do_lock_var(&lv, name_end, deep,
3619 eap->cmdidx == CMD_lockvar) == FAIL)
3620 error = TRUE;
3621 }
3622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003624 if (!eap->skip)
3625 clear_lval(&lv);
3626
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 arg = skipwhite(name_end);
3628 } while (!ends_excmd(*arg));
3629
3630 eap->nextcmd = check_nextcmd(arg);
3631}
3632
Bram Moolenaar8c711452005-01-14 21:53:12 +00003633 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003634do_unlet_var(
3635 lval_T *lp,
3636 char_u *name_end,
3637 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003638{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003639 int ret = OK;
3640 int cc;
3641
3642 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003643 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003644 cc = *name_end;
3645 *name_end = NUL;
3646
3647 /* Normal name or expanded name. */
3648 if (check_changedtick(lp->ll_name))
3649 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003650 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003652 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003653 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003654 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003655 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003656 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003657 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003658 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003659 else if (lp->ll_range)
3660 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003661 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003662 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003663 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003664
3665 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3666 {
3667 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003668 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003669 return FAIL;
3670 ll_li = li;
3671 ++ll_n1;
3672 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003673
3674 /* Delete a range of List items. */
3675 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3676 {
3677 li = lp->ll_li->li_next;
3678 listitem_remove(lp->ll_list, lp->ll_li);
3679 lp->ll_li = li;
3680 ++lp->ll_n1;
3681 }
3682 }
3683 else
3684 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003685 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003686 /* unlet a List item. */
3687 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003688 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003689 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003690 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003691 }
3692
3693 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003694}
3695
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696/*
3697 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003698 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 */
3700 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003701do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702{
Bram Moolenaar33570922005-01-25 22:26:29 +00003703 hashtab_T *ht;
3704 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003705 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003706 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003707 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708
Bram Moolenaar33570922005-01-25 22:26:29 +00003709 ht = find_var_ht(name, &varname);
3710 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003712 if (ht == &globvarht)
3713 d = &globvardict;
3714 else if (current_funccal != NULL
3715 && ht == &current_funccal->l_vars.dv_hashtab)
3716 d = &current_funccal->l_vars;
3717 else if (ht == &compat_hashtab)
3718 d = &vimvardict;
3719 else
3720 {
3721 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3722 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3723 }
3724 if (d == NULL)
3725 {
3726 EMSG2(_(e_intern2), "do_unlet()");
3727 return FAIL;
3728 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003729 hi = hash_find(ht, varname);
3730 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003731 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003732 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003733 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003734 || var_check_ro(di->di_flags, name, FALSE)
3735 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003736 return FAIL;
3737
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003738 delete_var(ht, hi);
3739 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003742 if (forceit)
3743 return OK;
3744 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 return FAIL;
3746}
3747
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003748/*
3749 * Lock or unlock variable indicated by "lp".
3750 * "deep" is the levels to go (-1 for unlimited);
3751 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3752 */
3753 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003754do_lock_var(
3755 lval_T *lp,
3756 char_u *name_end,
3757 int deep,
3758 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003759{
3760 int ret = OK;
3761 int cc;
3762 dictitem_T *di;
3763
3764 if (deep == 0) /* nothing to do */
3765 return OK;
3766
3767 if (lp->ll_tv == NULL)
3768 {
3769 cc = *name_end;
3770 *name_end = NUL;
3771
3772 /* Normal name or expanded name. */
3773 if (check_changedtick(lp->ll_name))
3774 ret = FAIL;
3775 else
3776 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003777 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003778 if (di == NULL)
3779 ret = FAIL;
3780 else
3781 {
3782 if (lock)
3783 di->di_flags |= DI_FLAGS_LOCK;
3784 else
3785 di->di_flags &= ~DI_FLAGS_LOCK;
3786 item_lock(&di->di_tv, deep, lock);
3787 }
3788 }
3789 *name_end = cc;
3790 }
3791 else if (lp->ll_range)
3792 {
3793 listitem_T *li = lp->ll_li;
3794
3795 /* (un)lock a range of List items. */
3796 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3797 {
3798 item_lock(&li->li_tv, deep, lock);
3799 li = li->li_next;
3800 ++lp->ll_n1;
3801 }
3802 }
3803 else if (lp->ll_list != NULL)
3804 /* (un)lock a List item. */
3805 item_lock(&lp->ll_li->li_tv, deep, lock);
3806 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003807 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003808 item_lock(&lp->ll_di->di_tv, deep, lock);
3809
3810 return ret;
3811}
3812
3813/*
3814 * Lock or unlock an item. "deep" is nr of levels to go.
3815 */
3816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003817item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003818{
3819 static int recurse = 0;
3820 list_T *l;
3821 listitem_T *li;
3822 dict_T *d;
3823 hashitem_T *hi;
3824 int todo;
3825
3826 if (recurse >= DICT_MAXNEST)
3827 {
3828 EMSG(_("E743: variable nested too deep for (un)lock"));
3829 return;
3830 }
3831 if (deep == 0)
3832 return;
3833 ++recurse;
3834
3835 /* lock/unlock the item itself */
3836 if (lock)
3837 tv->v_lock |= VAR_LOCKED;
3838 else
3839 tv->v_lock &= ~VAR_LOCKED;
3840
3841 switch (tv->v_type)
3842 {
3843 case VAR_LIST:
3844 if ((l = tv->vval.v_list) != NULL)
3845 {
3846 if (lock)
3847 l->lv_lock |= VAR_LOCKED;
3848 else
3849 l->lv_lock &= ~VAR_LOCKED;
3850 if (deep < 0 || deep > 1)
3851 /* recursive: lock/unlock the items the List contains */
3852 for (li = l->lv_first; li != NULL; li = li->li_next)
3853 item_lock(&li->li_tv, deep - 1, lock);
3854 }
3855 break;
3856 case VAR_DICT:
3857 if ((d = tv->vval.v_dict) != NULL)
3858 {
3859 if (lock)
3860 d->dv_lock |= VAR_LOCKED;
3861 else
3862 d->dv_lock &= ~VAR_LOCKED;
3863 if (deep < 0 || deep > 1)
3864 {
3865 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003866 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003867 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3868 {
3869 if (!HASHITEM_EMPTY(hi))
3870 {
3871 --todo;
3872 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3873 }
3874 }
3875 }
3876 }
3877 }
3878 --recurse;
3879}
3880
Bram Moolenaara40058a2005-07-11 22:42:07 +00003881/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003882 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3883 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003884 */
3885 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003886tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003887{
3888 return (tv->v_lock & VAR_LOCKED)
3889 || (tv->v_type == VAR_LIST
3890 && tv->vval.v_list != NULL
3891 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3892 || (tv->v_type == VAR_DICT
3893 && tv->vval.v_dict != NULL
3894 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3895}
3896
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3898/*
3899 * Delete all "menutrans_" variables.
3900 */
3901 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003902del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903{
Bram Moolenaar33570922005-01-25 22:26:29 +00003904 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003905 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906
Bram Moolenaar33570922005-01-25 22:26:29 +00003907 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003908 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003909 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003910 {
3911 if (!HASHITEM_EMPTY(hi))
3912 {
3913 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003914 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3915 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003916 }
3917 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003918 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919}
3920#endif
3921
3922#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3923
3924/*
3925 * Local string buffer for the next two functions to store a variable name
3926 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3927 * get_user_var_name().
3928 */
3929
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003930static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931
3932static char_u *varnamebuf = NULL;
3933static int varnamebuflen = 0;
3934
3935/*
3936 * Function to concatenate a prefix and a variable name.
3937 */
3938 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003939cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940{
3941 int len;
3942
3943 len = (int)STRLEN(name) + 3;
3944 if (len > varnamebuflen)
3945 {
3946 vim_free(varnamebuf);
3947 len += 10; /* some additional space */
3948 varnamebuf = alloc(len);
3949 if (varnamebuf == NULL)
3950 {
3951 varnamebuflen = 0;
3952 return NULL;
3953 }
3954 varnamebuflen = len;
3955 }
3956 *varnamebuf = prefix;
3957 varnamebuf[1] = ':';
3958 STRCPY(varnamebuf + 2, name);
3959 return varnamebuf;
3960}
3961
3962/*
3963 * Function given to ExpandGeneric() to obtain the list of user defined
3964 * (global/buffer/window/built-in) variable names.
3965 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003967get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003969 static long_u gdone;
3970 static long_u bdone;
3971 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003972#ifdef FEAT_WINDOWS
3973 static long_u tdone;
3974#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003975 static int vidx;
3976 static hashitem_T *hi;
3977 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978
3979 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003980 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003981 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003982#ifdef FEAT_WINDOWS
3983 tdone = 0;
3984#endif
3985 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003986
3987 /* Global variables */
3988 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003990 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003991 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003992 else
3993 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003994 while (HASHITEM_EMPTY(hi))
3995 ++hi;
3996 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3997 return cat_prefix_varname('g', hi->hi_key);
3998 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004000
4001 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004002 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004003 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004005 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004006 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004007 else
4008 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004009 while (HASHITEM_EMPTY(hi))
4010 ++hi;
4011 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004013 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004015 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 return (char_u *)"b:changedtick";
4017 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004018
4019 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004020 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004021 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004023 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004024 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004025 else
4026 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004027 while (HASHITEM_EMPTY(hi))
4028 ++hi;
4029 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004031
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004032#ifdef FEAT_WINDOWS
4033 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004034 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004035 if (tdone < ht->ht_used)
4036 {
4037 if (tdone++ == 0)
4038 hi = ht->ht_array;
4039 else
4040 ++hi;
4041 while (HASHITEM_EMPTY(hi))
4042 ++hi;
4043 return cat_prefix_varname('t', hi->hi_key);
4044 }
4045#endif
4046
Bram Moolenaar33570922005-01-25 22:26:29 +00004047 /* v: variables */
4048 if (vidx < VV_LEN)
4049 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050
4051 vim_free(varnamebuf);
4052 varnamebuf = NULL;
4053 varnamebuflen = 0;
4054 return NULL;
4055}
4056
4057#endif /* FEAT_CMDL_COMPL */
4058
4059/*
4060 * types for expressions.
4061 */
4062typedef enum
4063{
4064 TYPE_UNKNOWN = 0
4065 , TYPE_EQUAL /* == */
4066 , TYPE_NEQUAL /* != */
4067 , TYPE_GREATER /* > */
4068 , TYPE_GEQUAL /* >= */
4069 , TYPE_SMALLER /* < */
4070 , TYPE_SEQUAL /* <= */
4071 , TYPE_MATCH /* =~ */
4072 , TYPE_NOMATCH /* !~ */
4073} exptype_T;
4074
4075/*
4076 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004077 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4079 */
4080
4081/*
4082 * Handle zero level expression.
4083 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004084 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004085 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 * Return OK or FAIL.
4087 */
4088 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004089eval0(
4090 char_u *arg,
4091 typval_T *rettv,
4092 char_u **nextcmd,
4093 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094{
4095 int ret;
4096 char_u *p;
4097
4098 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004099 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 if (ret == FAIL || !ends_excmd(*p))
4101 {
4102 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004103 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 /*
4105 * Report the invalid expression unless the expression evaluation has
4106 * been cancelled due to an aborting error, an interrupt, or an
4107 * exception.
4108 */
4109 if (!aborting())
4110 EMSG2(_(e_invexpr2), arg);
4111 ret = FAIL;
4112 }
4113 if (nextcmd != NULL)
4114 *nextcmd = check_nextcmd(p);
4115
4116 return ret;
4117}
4118
4119/*
4120 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004121 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 *
4123 * "arg" must point to the first non-white of the expression.
4124 * "arg" is advanced to the next non-white after the recognized expression.
4125 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004126 * Note: "rettv.v_lock" is not set.
4127 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 * Return OK or FAIL.
4129 */
4130 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004131eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132{
4133 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004134 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135
4136 /*
4137 * Get the first variable.
4138 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004139 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 return FAIL;
4141
4142 if ((*arg)[0] == '?')
4143 {
4144 result = FALSE;
4145 if (evaluate)
4146 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004147 int error = FALSE;
4148
4149 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004151 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004152 if (error)
4153 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 }
4155
4156 /*
4157 * Get the second variable.
4158 */
4159 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004160 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 return FAIL;
4162
4163 /*
4164 * Check for the ":".
4165 */
4166 if ((*arg)[0] != ':')
4167 {
4168 EMSG(_("E109: Missing ':' after '?'"));
4169 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004170 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 return FAIL;
4172 }
4173
4174 /*
4175 * Get the third variable.
4176 */
4177 *arg = skipwhite(*arg + 1);
4178 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4179 {
4180 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004181 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 return FAIL;
4183 }
4184 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004185 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
4187
4188 return OK;
4189}
4190
4191/*
4192 * Handle first level expression:
4193 * expr2 || expr2 || expr2 logical OR
4194 *
4195 * "arg" must point to the first non-white of the expression.
4196 * "arg" is advanced to the next non-white after the recognized expression.
4197 *
4198 * Return OK or FAIL.
4199 */
4200 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004201eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202{
Bram Moolenaar33570922005-01-25 22:26:29 +00004203 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 long result;
4205 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004206 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207
4208 /*
4209 * Get the first variable.
4210 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 return FAIL;
4213
4214 /*
4215 * Repeat until there is no following "||".
4216 */
4217 first = TRUE;
4218 result = FALSE;
4219 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4220 {
4221 if (evaluate && first)
4222 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004223 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004225 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004226 if (error)
4227 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 first = FALSE;
4229 }
4230
4231 /*
4232 * Get the second variable.
4233 */
4234 *arg = skipwhite(*arg + 2);
4235 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4236 return FAIL;
4237
4238 /*
4239 * Compute the result.
4240 */
4241 if (evaluate && !result)
4242 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004246 if (error)
4247 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 }
4249 if (evaluate)
4250 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004251 rettv->v_type = VAR_NUMBER;
4252 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 }
4254 }
4255
4256 return OK;
4257}
4258
4259/*
4260 * Handle second level expression:
4261 * expr3 && expr3 && expr3 logical AND
4262 *
4263 * "arg" must point to the first non-white of the expression.
4264 * "arg" is advanced to the next non-white after the recognized expression.
4265 *
4266 * Return OK or FAIL.
4267 */
4268 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004269eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270{
Bram Moolenaar33570922005-01-25 22:26:29 +00004271 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 long result;
4273 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004274 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275
4276 /*
4277 * Get the first variable.
4278 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004279 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 return FAIL;
4281
4282 /*
4283 * Repeat until there is no following "&&".
4284 */
4285 first = TRUE;
4286 result = TRUE;
4287 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4288 {
4289 if (evaluate && first)
4290 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004291 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004293 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004294 if (error)
4295 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 first = FALSE;
4297 }
4298
4299 /*
4300 * Get the second variable.
4301 */
4302 *arg = skipwhite(*arg + 2);
4303 if (eval4(arg, &var2, evaluate && result) == FAIL)
4304 return FAIL;
4305
4306 /*
4307 * Compute the result.
4308 */
4309 if (evaluate && result)
4310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004311 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004313 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004314 if (error)
4315 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 }
4317 if (evaluate)
4318 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004319 rettv->v_type = VAR_NUMBER;
4320 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 }
4322 }
4323
4324 return OK;
4325}
4326
4327/*
4328 * Handle third level expression:
4329 * var1 == var2
4330 * var1 =~ var2
4331 * var1 != var2
4332 * var1 !~ var2
4333 * var1 > var2
4334 * var1 >= var2
4335 * var1 < var2
4336 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004337 * var1 is var2
4338 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 *
4340 * "arg" must point to the first non-white of the expression.
4341 * "arg" is advanced to the next non-white after the recognized expression.
4342 *
4343 * Return OK or FAIL.
4344 */
4345 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004346eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347{
Bram Moolenaar33570922005-01-25 22:26:29 +00004348 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 char_u *p;
4350 int i;
4351 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004352 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 int len = 2;
4354 long n1, n2;
4355 char_u *s1, *s2;
4356 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4357 regmatch_T regmatch;
4358 int ic;
4359 char_u *save_cpo;
4360
4361 /*
4362 * Get the first variable.
4363 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004364 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 return FAIL;
4366
4367 p = *arg;
4368 switch (p[0])
4369 {
4370 case '=': if (p[1] == '=')
4371 type = TYPE_EQUAL;
4372 else if (p[1] == '~')
4373 type = TYPE_MATCH;
4374 break;
4375 case '!': if (p[1] == '=')
4376 type = TYPE_NEQUAL;
4377 else if (p[1] == '~')
4378 type = TYPE_NOMATCH;
4379 break;
4380 case '>': if (p[1] != '=')
4381 {
4382 type = TYPE_GREATER;
4383 len = 1;
4384 }
4385 else
4386 type = TYPE_GEQUAL;
4387 break;
4388 case '<': if (p[1] != '=')
4389 {
4390 type = TYPE_SMALLER;
4391 len = 1;
4392 }
4393 else
4394 type = TYPE_SEQUAL;
4395 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004396 case 'i': if (p[1] == 's')
4397 {
4398 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4399 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004400 i = p[len];
4401 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004402 {
4403 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4404 type_is = TRUE;
4405 }
4406 }
4407 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 }
4409
4410 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004411 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 */
4413 if (type != TYPE_UNKNOWN)
4414 {
4415 /* extra question mark appended: ignore case */
4416 if (p[len] == '?')
4417 {
4418 ic = TRUE;
4419 ++len;
4420 }
4421 /* extra '#' appended: match case */
4422 else if (p[len] == '#')
4423 {
4424 ic = FALSE;
4425 ++len;
4426 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004427 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 else
4429 ic = p_ic;
4430
4431 /*
4432 * Get the second variable.
4433 */
4434 *arg = skipwhite(p + len);
4435 if (eval5(arg, &var2, evaluate) == FAIL)
4436 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004437 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 return FAIL;
4439 }
4440
4441 if (evaluate)
4442 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004443 if (type_is && rettv->v_type != var2.v_type)
4444 {
4445 /* For "is" a different type always means FALSE, for "notis"
4446 * it means TRUE. */
4447 n1 = (type == TYPE_NEQUAL);
4448 }
4449 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4450 {
4451 if (type_is)
4452 {
4453 n1 = (rettv->v_type == var2.v_type
4454 && rettv->vval.v_list == var2.vval.v_list);
4455 if (type == TYPE_NEQUAL)
4456 n1 = !n1;
4457 }
4458 else if (rettv->v_type != var2.v_type
4459 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4460 {
4461 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004462 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004463 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004464 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004465 clear_tv(rettv);
4466 clear_tv(&var2);
4467 return FAIL;
4468 }
4469 else
4470 {
4471 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004472 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4473 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004474 if (type == TYPE_NEQUAL)
4475 n1 = !n1;
4476 }
4477 }
4478
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004479 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4480 {
4481 if (type_is)
4482 {
4483 n1 = (rettv->v_type == var2.v_type
4484 && rettv->vval.v_dict == var2.vval.v_dict);
4485 if (type == TYPE_NEQUAL)
4486 n1 = !n1;
4487 }
4488 else if (rettv->v_type != var2.v_type
4489 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4490 {
4491 if (rettv->v_type != var2.v_type)
4492 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4493 else
4494 EMSG(_("E736: Invalid operation for Dictionary"));
4495 clear_tv(rettv);
4496 clear_tv(&var2);
4497 return FAIL;
4498 }
4499 else
4500 {
4501 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004502 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4503 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004504 if (type == TYPE_NEQUAL)
4505 n1 = !n1;
4506 }
4507 }
4508
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004509 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4510 {
4511 if (rettv->v_type != var2.v_type
4512 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4513 {
4514 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004515 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004516 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004517 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004518 clear_tv(rettv);
4519 clear_tv(&var2);
4520 return FAIL;
4521 }
4522 else
4523 {
4524 /* Compare two Funcrefs for being equal or unequal. */
4525 if (rettv->vval.v_string == NULL
4526 || var2.vval.v_string == NULL)
4527 n1 = FALSE;
4528 else
4529 n1 = STRCMP(rettv->vval.v_string,
4530 var2.vval.v_string) == 0;
4531 if (type == TYPE_NEQUAL)
4532 n1 = !n1;
4533 }
4534 }
4535
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004536#ifdef FEAT_FLOAT
4537 /*
4538 * If one of the two variables is a float, compare as a float.
4539 * When using "=~" or "!~", always compare as string.
4540 */
4541 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4542 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4543 {
4544 float_T f1, f2;
4545
4546 if (rettv->v_type == VAR_FLOAT)
4547 f1 = rettv->vval.v_float;
4548 else
4549 f1 = get_tv_number(rettv);
4550 if (var2.v_type == VAR_FLOAT)
4551 f2 = var2.vval.v_float;
4552 else
4553 f2 = get_tv_number(&var2);
4554 n1 = FALSE;
4555 switch (type)
4556 {
4557 case TYPE_EQUAL: n1 = (f1 == f2); break;
4558 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4559 case TYPE_GREATER: n1 = (f1 > f2); break;
4560 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4561 case TYPE_SMALLER: n1 = (f1 < f2); break;
4562 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4563 case TYPE_UNKNOWN:
4564 case TYPE_MATCH:
4565 case TYPE_NOMATCH: break; /* avoid gcc warning */
4566 }
4567 }
4568#endif
4569
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 /*
4571 * If one of the two variables is a number, compare as a number.
4572 * When using "=~" or "!~", always compare as string.
4573 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004574 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4576 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004577 n1 = get_tv_number(rettv);
4578 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 switch (type)
4580 {
4581 case TYPE_EQUAL: n1 = (n1 == n2); break;
4582 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4583 case TYPE_GREATER: n1 = (n1 > n2); break;
4584 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4585 case TYPE_SMALLER: n1 = (n1 < n2); break;
4586 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4587 case TYPE_UNKNOWN:
4588 case TYPE_MATCH:
4589 case TYPE_NOMATCH: break; /* avoid gcc warning */
4590 }
4591 }
4592 else
4593 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004594 s1 = get_tv_string_buf(rettv, buf1);
4595 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4597 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4598 else
4599 i = 0;
4600 n1 = FALSE;
4601 switch (type)
4602 {
4603 case TYPE_EQUAL: n1 = (i == 0); break;
4604 case TYPE_NEQUAL: n1 = (i != 0); break;
4605 case TYPE_GREATER: n1 = (i > 0); break;
4606 case TYPE_GEQUAL: n1 = (i >= 0); break;
4607 case TYPE_SMALLER: n1 = (i < 0); break;
4608 case TYPE_SEQUAL: n1 = (i <= 0); break;
4609
4610 case TYPE_MATCH:
4611 case TYPE_NOMATCH:
4612 /* avoid 'l' flag in 'cpoptions' */
4613 save_cpo = p_cpo;
4614 p_cpo = (char_u *)"";
4615 regmatch.regprog = vim_regcomp(s2,
4616 RE_MAGIC + RE_STRING);
4617 regmatch.rm_ic = ic;
4618 if (regmatch.regprog != NULL)
4619 {
4620 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004621 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 if (type == TYPE_NOMATCH)
4623 n1 = !n1;
4624 }
4625 p_cpo = save_cpo;
4626 break;
4627
4628 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4629 }
4630 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004631 clear_tv(rettv);
4632 clear_tv(&var2);
4633 rettv->v_type = VAR_NUMBER;
4634 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 }
4636 }
4637
4638 return OK;
4639}
4640
4641/*
4642 * Handle fourth level expression:
4643 * + number addition
4644 * - number subtraction
4645 * . string concatenation
4646 *
4647 * "arg" must point to the first non-white of the expression.
4648 * "arg" is advanced to the next non-white after the recognized expression.
4649 *
4650 * Return OK or FAIL.
4651 */
4652 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004653eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654{
Bram Moolenaar33570922005-01-25 22:26:29 +00004655 typval_T var2;
4656 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 int op;
4658 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004659#ifdef FEAT_FLOAT
4660 float_T f1 = 0, f2 = 0;
4661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 char_u *s1, *s2;
4663 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4664 char_u *p;
4665
4666 /*
4667 * Get the first variable.
4668 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004669 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 return FAIL;
4671
4672 /*
4673 * Repeat computing, until no '+', '-' or '.' is following.
4674 */
4675 for (;;)
4676 {
4677 op = **arg;
4678 if (op != '+' && op != '-' && op != '.')
4679 break;
4680
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004681 if ((op != '+' || rettv->v_type != VAR_LIST)
4682#ifdef FEAT_FLOAT
4683 && (op == '.' || rettv->v_type != VAR_FLOAT)
4684#endif
4685 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004686 {
4687 /* For "list + ...", an illegal use of the first operand as
4688 * a number cannot be determined before evaluating the 2nd
4689 * operand: if this is also a list, all is ok.
4690 * For "something . ...", "something - ..." or "non-list + ...",
4691 * we know that the first operand needs to be a string or number
4692 * without evaluating the 2nd operand. So check before to avoid
4693 * side effects after an error. */
4694 if (evaluate && get_tv_string_chk(rettv) == NULL)
4695 {
4696 clear_tv(rettv);
4697 return FAIL;
4698 }
4699 }
4700
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 /*
4702 * Get the second variable.
4703 */
4704 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004705 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004707 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 return FAIL;
4709 }
4710
4711 if (evaluate)
4712 {
4713 /*
4714 * Compute the result.
4715 */
4716 if (op == '.')
4717 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004718 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4719 s2 = get_tv_string_buf_chk(&var2, buf2);
4720 if (s2 == NULL) /* type error ? */
4721 {
4722 clear_tv(rettv);
4723 clear_tv(&var2);
4724 return FAIL;
4725 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004726 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004727 clear_tv(rettv);
4728 rettv->v_type = VAR_STRING;
4729 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004731 else if (op == '+' && rettv->v_type == VAR_LIST
4732 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004733 {
4734 /* concatenate Lists */
4735 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4736 &var3) == FAIL)
4737 {
4738 clear_tv(rettv);
4739 clear_tv(&var2);
4740 return FAIL;
4741 }
4742 clear_tv(rettv);
4743 *rettv = var3;
4744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 else
4746 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004747 int error = FALSE;
4748
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004749#ifdef FEAT_FLOAT
4750 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004751 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004752 f1 = rettv->vval.v_float;
4753 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004754 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004755 else
4756#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004757 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004758 n1 = get_tv_number_chk(rettv, &error);
4759 if (error)
4760 {
4761 /* This can only happen for "list + non-list". For
4762 * "non-list + ..." or "something - ...", we returned
4763 * before evaluating the 2nd operand. */
4764 clear_tv(rettv);
4765 return FAIL;
4766 }
4767#ifdef FEAT_FLOAT
4768 if (var2.v_type == VAR_FLOAT)
4769 f1 = n1;
4770#endif
4771 }
4772#ifdef FEAT_FLOAT
4773 if (var2.v_type == VAR_FLOAT)
4774 {
4775 f2 = var2.vval.v_float;
4776 n2 = 0;
4777 }
4778 else
4779#endif
4780 {
4781 n2 = get_tv_number_chk(&var2, &error);
4782 if (error)
4783 {
4784 clear_tv(rettv);
4785 clear_tv(&var2);
4786 return FAIL;
4787 }
4788#ifdef FEAT_FLOAT
4789 if (rettv->v_type == VAR_FLOAT)
4790 f2 = n2;
4791#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004792 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004793 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004794
4795#ifdef FEAT_FLOAT
4796 /* If there is a float on either side the result is a float. */
4797 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4798 {
4799 if (op == '+')
4800 f1 = f1 + f2;
4801 else
4802 f1 = f1 - f2;
4803 rettv->v_type = VAR_FLOAT;
4804 rettv->vval.v_float = f1;
4805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004807#endif
4808 {
4809 if (op == '+')
4810 n1 = n1 + n2;
4811 else
4812 n1 = n1 - n2;
4813 rettv->v_type = VAR_NUMBER;
4814 rettv->vval.v_number = n1;
4815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004817 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 }
4819 }
4820 return OK;
4821}
4822
4823/*
4824 * Handle fifth level expression:
4825 * * number multiplication
4826 * / number division
4827 * % number modulo
4828 *
4829 * "arg" must point to the first non-white of the expression.
4830 * "arg" is advanced to the next non-white after the recognized expression.
4831 *
4832 * Return OK or FAIL.
4833 */
4834 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004835eval6(
4836 char_u **arg,
4837 typval_T *rettv,
4838 int evaluate,
4839 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840{
Bram Moolenaar33570922005-01-25 22:26:29 +00004841 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 int op;
4843 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004844#ifdef FEAT_FLOAT
4845 int use_float = FALSE;
4846 float_T f1 = 0, f2;
4847#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004848 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849
4850 /*
4851 * Get the first variable.
4852 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004853 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 return FAIL;
4855
4856 /*
4857 * Repeat computing, until no '*', '/' or '%' is following.
4858 */
4859 for (;;)
4860 {
4861 op = **arg;
4862 if (op != '*' && op != '/' && op != '%')
4863 break;
4864
4865 if (evaluate)
4866 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004867#ifdef FEAT_FLOAT
4868 if (rettv->v_type == VAR_FLOAT)
4869 {
4870 f1 = rettv->vval.v_float;
4871 use_float = TRUE;
4872 n1 = 0;
4873 }
4874 else
4875#endif
4876 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004877 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004878 if (error)
4879 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 }
4881 else
4882 n1 = 0;
4883
4884 /*
4885 * Get the second variable.
4886 */
4887 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004888 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 return FAIL;
4890
4891 if (evaluate)
4892 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004893#ifdef FEAT_FLOAT
4894 if (var2.v_type == VAR_FLOAT)
4895 {
4896 if (!use_float)
4897 {
4898 f1 = n1;
4899 use_float = TRUE;
4900 }
4901 f2 = var2.vval.v_float;
4902 n2 = 0;
4903 }
4904 else
4905#endif
4906 {
4907 n2 = get_tv_number_chk(&var2, &error);
4908 clear_tv(&var2);
4909 if (error)
4910 return FAIL;
4911#ifdef FEAT_FLOAT
4912 if (use_float)
4913 f2 = n2;
4914#endif
4915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916
4917 /*
4918 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004919 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004921#ifdef FEAT_FLOAT
4922 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004924 if (op == '*')
4925 f1 = f1 * f2;
4926 else if (op == '/')
4927 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004928# ifdef VMS
4929 /* VMS crashes on divide by zero, work around it */
4930 if (f2 == 0.0)
4931 {
4932 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004933 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004934 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004935 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004936 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004937 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004938 }
4939 else
4940 f1 = f1 / f2;
4941# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004942 /* We rely on the floating point library to handle divide
4943 * by zero to result in "inf" and not a crash. */
4944 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004945# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004948 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004949 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004950 return FAIL;
4951 }
4952 rettv->v_type = VAR_FLOAT;
4953 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 }
4955 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004958 if (op == '*')
4959 n1 = n1 * n2;
4960 else if (op == '/')
4961 {
4962 if (n2 == 0) /* give an error message? */
4963 {
4964 if (n1 == 0)
4965 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4966 else if (n1 < 0)
4967 n1 = -0x7fffffffL;
4968 else
4969 n1 = 0x7fffffffL;
4970 }
4971 else
4972 n1 = n1 / n2;
4973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004975 {
4976 if (n2 == 0) /* give an error message? */
4977 n1 = 0;
4978 else
4979 n1 = n1 % n2;
4980 }
4981 rettv->v_type = VAR_NUMBER;
4982 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 }
4985 }
4986
4987 return OK;
4988}
4989
4990/*
4991 * Handle sixth level expression:
4992 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004993 * "string" string constant
4994 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 * &option-name option value
4996 * @r register contents
4997 * identifier variable value
4998 * function() function call
4999 * $VAR environment variable
5000 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005001 * [expr, expr] List
5002 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 *
5004 * Also handle:
5005 * ! in front logical NOT
5006 * - in front unary minus
5007 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005008 * trailing [] subscript in String or List
5009 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 *
5011 * "arg" must point to the first non-white of the expression.
5012 * "arg" is advanced to the next non-white after the recognized expression.
5013 *
5014 * Return OK or FAIL.
5015 */
5016 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005017eval7(
5018 char_u **arg,
5019 typval_T *rettv,
5020 int evaluate,
5021 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 long n;
5024 int len;
5025 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 char_u *start_leader, *end_leader;
5027 int ret = OK;
5028 char_u *alias;
5029
5030 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005031 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005032 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005034 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035
5036 /*
5037 * Skip '!' and '-' characters. They are handled later.
5038 */
5039 start_leader = *arg;
5040 while (**arg == '!' || **arg == '-' || **arg == '+')
5041 *arg = skipwhite(*arg + 1);
5042 end_leader = *arg;
5043
5044 switch (**arg)
5045 {
5046 /*
5047 * Number constant.
5048 */
5049 case '0':
5050 case '1':
5051 case '2':
5052 case '3':
5053 case '4':
5054 case '5':
5055 case '6':
5056 case '7':
5057 case '8':
5058 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005059 {
5060#ifdef FEAT_FLOAT
5061 char_u *p = skipdigits(*arg + 1);
5062 int get_float = FALSE;
5063
5064 /* We accept a float when the format matches
5065 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005066 * strict to avoid backwards compatibility problems.
5067 * Don't look for a float after the "." operator, so that
5068 * ":let vers = 1.2.3" doesn't fail. */
5069 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005071 get_float = TRUE;
5072 p = skipdigits(p + 2);
5073 if (*p == 'e' || *p == 'E')
5074 {
5075 ++p;
5076 if (*p == '-' || *p == '+')
5077 ++p;
5078 if (!vim_isdigit(*p))
5079 get_float = FALSE;
5080 else
5081 p = skipdigits(p + 1);
5082 }
5083 if (ASCII_ISALPHA(*p) || *p == '.')
5084 get_float = FALSE;
5085 }
5086 if (get_float)
5087 {
5088 float_T f;
5089
5090 *arg += string2float(*arg, &f);
5091 if (evaluate)
5092 {
5093 rettv->v_type = VAR_FLOAT;
5094 rettv->vval.v_float = f;
5095 }
5096 }
5097 else
5098#endif
5099 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005100 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005101 *arg += len;
5102 if (evaluate)
5103 {
5104 rettv->v_type = VAR_NUMBER;
5105 rettv->vval.v_number = n;
5106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 }
5108 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110
5111 /*
5112 * String constant: "string".
5113 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005114 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 break;
5116
5117 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005118 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005120 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005121 break;
5122
5123 /*
5124 * List: [expr, expr]
5125 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005126 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 break;
5128
5129 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005130 * Dictionary: {key: val, key: val}
5131 */
5132 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5133 break;
5134
5135 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005136 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005138 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 break;
5140
5141 /*
5142 * Environment variable: $VAR.
5143 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005144 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 break;
5146
5147 /*
5148 * Register contents: @r.
5149 */
5150 case '@': ++*arg;
5151 if (evaluate)
5152 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005153 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005154 rettv->vval.v_string = get_reg_contents(**arg,
5155 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 }
5157 if (**arg != NUL)
5158 ++*arg;
5159 break;
5160
5161 /*
5162 * nested expression: (expression).
5163 */
5164 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005165 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 if (**arg == ')')
5167 ++*arg;
5168 else if (ret == OK)
5169 {
5170 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005171 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 ret = FAIL;
5173 }
5174 break;
5175
Bram Moolenaar8c711452005-01-14 21:53:12 +00005176 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 break;
5178 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005179
5180 if (ret == NOTDONE)
5181 {
5182 /*
5183 * Must be a variable or function name.
5184 * Can also be a curly-braces kind of name: {expr}.
5185 */
5186 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005187 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005188 if (alias != NULL)
5189 s = alias;
5190
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005191 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005192 ret = FAIL;
5193 else
5194 {
5195 if (**arg == '(') /* recursive! */
5196 {
5197 /* If "s" is the name of a variable of type VAR_FUNC
5198 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005199 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005200
5201 /* Invoke the function. */
5202 ret = get_func_tv(s, len, rettv, arg,
5203 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005204 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005205
5206 /* If evaluate is FALSE rettv->v_type was not set in
5207 * get_func_tv, but it's needed in handle_subscript() to parse
5208 * what follows. So set it here. */
5209 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5210 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005211 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005212 rettv->v_type = VAR_FUNC;
5213 }
5214
Bram Moolenaar8c711452005-01-14 21:53:12 +00005215 /* Stop the expression evaluation when immediately
5216 * aborting on error, or when an interrupt occurred or
5217 * an exception was thrown but not caught. */
5218 if (aborting())
5219 {
5220 if (ret == OK)
5221 clear_tv(rettv);
5222 ret = FAIL;
5223 }
5224 }
5225 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005226 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005227 else
5228 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005230 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005231 }
5232
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 *arg = skipwhite(*arg);
5234
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005235 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5236 * expr(expr). */
5237 if (ret == OK)
5238 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239
5240 /*
5241 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5242 */
5243 if (ret == OK && evaluate && end_leader > start_leader)
5244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005245 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005246 int val = 0;
5247#ifdef FEAT_FLOAT
5248 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005249
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005250 if (rettv->v_type == VAR_FLOAT)
5251 f = rettv->vval.v_float;
5252 else
5253#endif
5254 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005255 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005257 clear_tv(rettv);
5258 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005260 else
5261 {
5262 while (end_leader > start_leader)
5263 {
5264 --end_leader;
5265 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005266 {
5267#ifdef FEAT_FLOAT
5268 if (rettv->v_type == VAR_FLOAT)
5269 f = !f;
5270 else
5271#endif
5272 val = !val;
5273 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005274 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005275 {
5276#ifdef FEAT_FLOAT
5277 if (rettv->v_type == VAR_FLOAT)
5278 f = -f;
5279 else
5280#endif
5281 val = -val;
5282 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005283 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005284#ifdef FEAT_FLOAT
5285 if (rettv->v_type == VAR_FLOAT)
5286 {
5287 clear_tv(rettv);
5288 rettv->vval.v_float = f;
5289 }
5290 else
5291#endif
5292 {
5293 clear_tv(rettv);
5294 rettv->v_type = VAR_NUMBER;
5295 rettv->vval.v_number = val;
5296 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 }
5299
5300 return ret;
5301}
5302
5303/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005304 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5305 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005306 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5307 */
5308 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005309eval_index(
5310 char_u **arg,
5311 typval_T *rettv,
5312 int evaluate,
5313 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005314{
5315 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005316 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005317 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005318 long len = -1;
5319 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005321 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005322
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005323 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005324 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005325 if (verbose)
5326 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005327 return FAIL;
5328 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005329#ifdef FEAT_FLOAT
5330 else if (rettv->v_type == VAR_FLOAT)
5331 {
5332 if (verbose)
5333 EMSG(_(e_float_as_string));
5334 return FAIL;
5335 }
5336#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005337 else if (rettv->v_type == VAR_SPECIAL)
5338 {
5339 return FAIL;
5340 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005341
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005342 init_tv(&var1);
5343 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005344 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005346 /*
5347 * dict.name
5348 */
5349 key = *arg + 1;
5350 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5351 ;
5352 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005354 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 }
5356 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005358 /*
5359 * something[idx]
5360 *
5361 * Get the (first) variable from inside the [].
5362 */
5363 *arg = skipwhite(*arg + 1);
5364 if (**arg == ':')
5365 empty1 = TRUE;
5366 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5367 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005368 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5369 {
5370 /* not a number or string */
5371 clear_tv(&var1);
5372 return FAIL;
5373 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005374
5375 /*
5376 * Get the second variable from inside the [:].
5377 */
5378 if (**arg == ':')
5379 {
5380 range = TRUE;
5381 *arg = skipwhite(*arg + 1);
5382 if (**arg == ']')
5383 empty2 = TRUE;
5384 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005386 if (!empty1)
5387 clear_tv(&var1);
5388 return FAIL;
5389 }
5390 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5391 {
5392 /* not a number or string */
5393 if (!empty1)
5394 clear_tv(&var1);
5395 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005396 return FAIL;
5397 }
5398 }
5399
5400 /* Check for the ']'. */
5401 if (**arg != ']')
5402 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005403 if (verbose)
5404 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005405 clear_tv(&var1);
5406 if (range)
5407 clear_tv(&var2);
5408 return FAIL;
5409 }
5410 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411 }
5412
5413 if (evaluate)
5414 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005415 n1 = 0;
5416 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005417 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005418 n1 = get_tv_number(&var1);
5419 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005420 }
5421 if (range)
5422 {
5423 if (empty2)
5424 n2 = -1;
5425 else
5426 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005427 n2 = get_tv_number(&var2);
5428 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429 }
5430 }
5431
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005432 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433 {
5434 case VAR_NUMBER:
5435 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005436 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 len = (long)STRLEN(s);
5438 if (range)
5439 {
5440 /* The resulting variable is a substring. If the indexes
5441 * are out of range the result is empty. */
5442 if (n1 < 0)
5443 {
5444 n1 = len + n1;
5445 if (n1 < 0)
5446 n1 = 0;
5447 }
5448 if (n2 < 0)
5449 n2 = len + n2;
5450 else if (n2 >= len)
5451 n2 = len;
5452 if (n1 >= len || n2 < 0 || n1 > n2)
5453 s = NULL;
5454 else
5455 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5456 }
5457 else
5458 {
5459 /* The resulting variable is a string of a single
5460 * character. If the index is too big or negative the
5461 * result is empty. */
5462 if (n1 >= len || n1 < 0)
5463 s = NULL;
5464 else
5465 s = vim_strnsave(s + n1, 1);
5466 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005467 clear_tv(rettv);
5468 rettv->v_type = VAR_STRING;
5469 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 break;
5471
5472 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005473 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005474 if (n1 < 0)
5475 n1 = len + n1;
5476 if (!empty1 && (n1 < 0 || n1 >= len))
5477 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005478 /* For a range we allow invalid values and return an empty
5479 * list. A list index out of range is an error. */
5480 if (!range)
5481 {
5482 if (verbose)
5483 EMSGN(_(e_listidx), n1);
5484 return FAIL;
5485 }
5486 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487 }
5488 if (range)
5489 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005490 list_T *l;
5491 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005492
5493 if (n2 < 0)
5494 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005495 else if (n2 >= len)
5496 n2 = len - 1;
5497 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005498 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005499 l = list_alloc();
5500 if (l == NULL)
5501 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005502 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005503 n1 <= n2; ++n1)
5504 {
5505 if (list_append_tv(l, &item->li_tv) == FAIL)
5506 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005507 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005508 return FAIL;
5509 }
5510 item = item->li_next;
5511 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005512 clear_tv(rettv);
5513 rettv->v_type = VAR_LIST;
5514 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005515 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005516 }
5517 else
5518 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005519 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005520 clear_tv(rettv);
5521 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005522 }
5523 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005524
5525 case VAR_DICT:
5526 if (range)
5527 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005528 if (verbose)
5529 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005530 if (len == -1)
5531 clear_tv(&var1);
5532 return FAIL;
5533 }
5534 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005535 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005536
5537 if (len == -1)
5538 {
5539 key = get_tv_string(&var1);
5540 if (*key == NUL)
5541 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005542 if (verbose)
5543 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005544 clear_tv(&var1);
5545 return FAIL;
5546 }
5547 }
5548
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005549 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005550
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005551 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005552 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005553 if (len == -1)
5554 clear_tv(&var1);
5555 if (item == NULL)
5556 return FAIL;
5557
5558 copy_tv(&item->di_tv, &var1);
5559 clear_tv(rettv);
5560 *rettv = var1;
5561 }
5562 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005563 }
5564 }
5565
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005566 return OK;
5567}
5568
5569/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 * Get an option value.
5571 * "arg" points to the '&' or '+' before the option name.
5572 * "arg" is advanced to character after the option name.
5573 * Return OK or FAIL.
5574 */
5575 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005576get_option_tv(
5577 char_u **arg,
5578 typval_T *rettv, /* when NULL, only check if option exists */
5579 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580{
5581 char_u *option_end;
5582 long numval;
5583 char_u *stringval;
5584 int opt_type;
5585 int c;
5586 int working = (**arg == '+'); /* has("+option") */
5587 int ret = OK;
5588 int opt_flags;
5589
5590 /*
5591 * Isolate the option name and find its value.
5592 */
5593 option_end = find_option_end(arg, &opt_flags);
5594 if (option_end == NULL)
5595 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005596 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 EMSG2(_("E112: Option name missing: %s"), *arg);
5598 return FAIL;
5599 }
5600
5601 if (!evaluate)
5602 {
5603 *arg = option_end;
5604 return OK;
5605 }
5606
5607 c = *option_end;
5608 *option_end = NUL;
5609 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005610 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611
5612 if (opt_type == -3) /* invalid name */
5613 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005614 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 EMSG2(_("E113: Unknown option: %s"), *arg);
5616 ret = FAIL;
5617 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005618 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 {
5620 if (opt_type == -2) /* hidden string option */
5621 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005622 rettv->v_type = VAR_STRING;
5623 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 }
5625 else if (opt_type == -1) /* hidden number option */
5626 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005627 rettv->v_type = VAR_NUMBER;
5628 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 }
5630 else if (opt_type == 1) /* number option */
5631 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005632 rettv->v_type = VAR_NUMBER;
5633 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 }
5635 else /* string option */
5636 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005637 rettv->v_type = VAR_STRING;
5638 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 }
5640 }
5641 else if (working && (opt_type == -2 || opt_type == -1))
5642 ret = FAIL;
5643
5644 *option_end = c; /* put back for error messages */
5645 *arg = option_end;
5646
5647 return ret;
5648}
5649
5650/*
5651 * Allocate a variable for a string constant.
5652 * Return OK or FAIL.
5653 */
5654 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005655get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656{
5657 char_u *p;
5658 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 int extra = 0;
5660
5661 /*
5662 * Find the end of the string, skipping backslashed characters.
5663 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005664 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 {
5666 if (*p == '\\' && p[1] != NUL)
5667 {
5668 ++p;
5669 /* A "\<x>" form occupies at least 4 characters, and produces up
5670 * to 6 characters: reserve space for 2 extra */
5671 if (*p == '<')
5672 extra += 2;
5673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 }
5675
5676 if (*p != '"')
5677 {
5678 EMSG2(_("E114: Missing quote: %s"), *arg);
5679 return FAIL;
5680 }
5681
5682 /* If only parsing, set *arg and return here */
5683 if (!evaluate)
5684 {
5685 *arg = p + 1;
5686 return OK;
5687 }
5688
5689 /*
5690 * Copy the string into allocated memory, handling backslashed
5691 * characters.
5692 */
5693 name = alloc((unsigned)(p - *arg + extra));
5694 if (name == NULL)
5695 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005696 rettv->v_type = VAR_STRING;
5697 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 {
5701 if (*p == '\\')
5702 {
5703 switch (*++p)
5704 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005705 case 'b': *name++ = BS; ++p; break;
5706 case 'e': *name++ = ESC; ++p; break;
5707 case 'f': *name++ = FF; ++p; break;
5708 case 'n': *name++ = NL; ++p; break;
5709 case 'r': *name++ = CAR; ++p; break;
5710 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711
5712 case 'X': /* hex: "\x1", "\x12" */
5713 case 'x':
5714 case 'u': /* Unicode: "\u0023" */
5715 case 'U':
5716 if (vim_isxdigit(p[1]))
5717 {
5718 int n, nr;
5719 int c = toupper(*p);
5720
5721 if (c == 'X')
5722 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005723 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005725 else
5726 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 nr = 0;
5728 while (--n >= 0 && vim_isxdigit(p[1]))
5729 {
5730 ++p;
5731 nr = (nr << 4) + hex2nr(*p);
5732 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005733 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734#ifdef FEAT_MBYTE
5735 /* For "\u" store the number according to
5736 * 'encoding'. */
5737 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 else
5740#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005741 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 break;
5744
5745 /* octal: "\1", "\12", "\123" */
5746 case '0':
5747 case '1':
5748 case '2':
5749 case '3':
5750 case '4':
5751 case '5':
5752 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 case '7': *name = *p++ - '0';
5754 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 *name = (*name << 3) + *p++ - '0';
5757 if (*p >= '0' && *p <= '7')
5758 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005760 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 break;
5762
5763 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005764 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 if (extra != 0)
5766 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005767 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 break;
5769 }
5770 /* FALLTHROUGH */
5771
Bram Moolenaar8c711452005-01-14 21:53:12 +00005772 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 break;
5774 }
5775 }
5776 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005777 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005780 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 *arg = p + 1;
5782
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 return OK;
5784}
5785
5786/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 * Return OK or FAIL.
5789 */
5790 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005791get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792{
5793 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005794 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005795 int reduce = 0;
5796
5797 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005799 */
5800 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5801 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005803 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005805 break;
5806 ++reduce;
5807 ++p;
5808 }
5809 }
5810
Bram Moolenaar8c711452005-01-14 21:53:12 +00005811 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005812 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005814 return FAIL;
5815 }
5816
Bram Moolenaar8c711452005-01-14 21:53:12 +00005817 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005818 if (!evaluate)
5819 {
5820 *arg = p + 1;
5821 return OK;
5822 }
5823
5824 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005825 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005826 */
5827 str = alloc((unsigned)((p - *arg) - reduce));
5828 if (str == NULL)
5829 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005830 rettv->v_type = VAR_STRING;
5831 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005832
Bram Moolenaar8c711452005-01-14 21:53:12 +00005833 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005834 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005835 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005836 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005837 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005838 break;
5839 ++p;
5840 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005842 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005844 *arg = p + 1;
5845
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005846 return OK;
5847}
5848
5849/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005850 * Allocate a variable for a List and fill it from "*arg".
5851 * Return OK or FAIL.
5852 */
5853 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005854get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005855{
Bram Moolenaar33570922005-01-25 22:26:29 +00005856 list_T *l = NULL;
5857 typval_T tv;
5858 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005859
5860 if (evaluate)
5861 {
5862 l = list_alloc();
5863 if (l == NULL)
5864 return FAIL;
5865 }
5866
5867 *arg = skipwhite(*arg + 1);
5868 while (**arg != ']' && **arg != NUL)
5869 {
5870 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5871 goto failret;
5872 if (evaluate)
5873 {
5874 item = listitem_alloc();
5875 if (item != NULL)
5876 {
5877 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005878 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005879 list_append(l, item);
5880 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005881 else
5882 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005883 }
5884
5885 if (**arg == ']')
5886 break;
5887 if (**arg != ',')
5888 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005889 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890 goto failret;
5891 }
5892 *arg = skipwhite(*arg + 1);
5893 }
5894
5895 if (**arg != ']')
5896 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005897 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005898failret:
5899 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005900 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901 return FAIL;
5902 }
5903
5904 *arg = skipwhite(*arg + 1);
5905 if (evaluate)
5906 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005907 rettv->v_type = VAR_LIST;
5908 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909 ++l->lv_refcount;
5910 }
5911
5912 return OK;
5913}
5914
5915/*
5916 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005917 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005919 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005920list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005922 list_T *l;
5923
5924 l = (list_T *)alloc_clear(sizeof(list_T));
5925 if (l != NULL)
5926 {
5927 /* Prepend the list to the list of lists for garbage collection. */
5928 if (first_list != NULL)
5929 first_list->lv_used_prev = l;
5930 l->lv_used_prev = NULL;
5931 l->lv_used_next = first_list;
5932 first_list = l;
5933 }
5934 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935}
5936
5937/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005938 * Allocate an empty list for a return value.
5939 * Returns OK or FAIL.
5940 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005941 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005942rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005943{
5944 list_T *l = list_alloc();
5945
5946 if (l == NULL)
5947 return FAIL;
5948
5949 rettv->vval.v_list = l;
5950 rettv->v_type = VAR_LIST;
5951 ++l->lv_refcount;
5952 return OK;
5953}
5954
5955/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956 * Unreference a list: decrement the reference count and free it when it
5957 * becomes zero.
5958 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005959 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005960list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005961{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005962 if (l != NULL && --l->lv_refcount <= 0)
5963 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005964}
5965
5966/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01005967 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005968 * Ignores the reference count.
5969 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005970 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005971list_free(
5972 list_T *l,
5973 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974{
Bram Moolenaar33570922005-01-25 22:26:29 +00005975 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005976
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005977 /* Remove the list from the list of lists for garbage collection. */
5978 if (l->lv_used_prev == NULL)
5979 first_list = l->lv_used_next;
5980 else
5981 l->lv_used_prev->lv_used_next = l->lv_used_next;
5982 if (l->lv_used_next != NULL)
5983 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5984
Bram Moolenaard9fba312005-06-26 22:34:35 +00005985 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005987 /* Remove the item before deleting it. */
5988 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005989 if (recurse || (item->li_tv.v_type != VAR_LIST
5990 && item->li_tv.v_type != VAR_DICT))
5991 clear_tv(&item->li_tv);
5992 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005993 }
5994 vim_free(l);
5995}
5996
5997/*
5998 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01005999 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006000 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006001 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006002listitem_alloc()
6003{
Bram Moolenaar33570922005-01-25 22:26:29 +00006004 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006005}
6006
6007/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006008 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006010 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006011listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006013 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006014 vim_free(item);
6015}
6016
6017/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006018 * Remove a list item from a List and free it. Also clears the value.
6019 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006020 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006021listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006022{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006023 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006024 listitem_free(item);
6025}
6026
6027/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006028 * Get the number of items in a list.
6029 */
6030 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006031list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006032{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033 if (l == NULL)
6034 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006035 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036}
6037
6038/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006039 * Return TRUE when two lists have exactly the same values.
6040 */
6041 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006042list_equal(
6043 list_T *l1,
6044 list_T *l2,
6045 int ic, /* ignore case for strings */
6046 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006047{
Bram Moolenaar33570922005-01-25 22:26:29 +00006048 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006049
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006050 if (l1 == NULL || l2 == NULL)
6051 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006052 if (l1 == l2)
6053 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006054 if (list_len(l1) != list_len(l2))
6055 return FALSE;
6056
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006057 for (item1 = l1->lv_first, item2 = l2->lv_first;
6058 item1 != NULL && item2 != NULL;
6059 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006060 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006061 return FALSE;
6062 return item1 == NULL && item2 == NULL;
6063}
6064
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006065/*
6066 * Return the dictitem that an entry in a hashtable points to.
6067 */
6068 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006069dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006070{
6071 return HI2DI(hi);
6072}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006073
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006074/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006075 * Return TRUE when two dictionaries have exactly the same key/values.
6076 */
6077 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006078dict_equal(
6079 dict_T *d1,
6080 dict_T *d2,
6081 int ic, /* ignore case for strings */
6082 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006083{
Bram Moolenaar33570922005-01-25 22:26:29 +00006084 hashitem_T *hi;
6085 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006086 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006087
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006088 if (d1 == NULL || d2 == NULL)
6089 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006090 if (d1 == d2)
6091 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006092 if (dict_len(d1) != dict_len(d2))
6093 return FALSE;
6094
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006095 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006096 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006097 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006098 if (!HASHITEM_EMPTY(hi))
6099 {
6100 item2 = dict_find(d2, hi->hi_key, -1);
6101 if (item2 == NULL)
6102 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006103 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006104 return FALSE;
6105 --todo;
6106 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006107 }
6108 return TRUE;
6109}
6110
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006111static int tv_equal_recurse_limit;
6112
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006113/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006114 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006115 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006116 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006117 */
6118 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006119tv_equal(
6120 typval_T *tv1,
6121 typval_T *tv2,
6122 int ic, /* ignore case */
6123 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006124{
6125 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006126 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006127 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006128 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006129
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006130 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006131 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006132
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006133 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006134 * recursiveness to a limit. We guess they are equal then.
6135 * A fixed limit has the problem of still taking an awful long time.
6136 * Reduce the limit every time running into it. That should work fine for
6137 * deeply linked structures that are not recursively linked and catch
6138 * recursiveness quickly. */
6139 if (!recursive)
6140 tv_equal_recurse_limit = 1000;
6141 if (recursive_cnt >= tv_equal_recurse_limit)
6142 {
6143 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006144 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006145 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006146
6147 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006148 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006149 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006150 ++recursive_cnt;
6151 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6152 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006153 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006154
6155 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006156 ++recursive_cnt;
6157 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6158 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006159 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006160
6161 case VAR_FUNC:
6162 return (tv1->vval.v_string != NULL
6163 && tv2->vval.v_string != NULL
6164 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6165
6166 case VAR_NUMBER:
6167 return tv1->vval.v_number == tv2->vval.v_number;
6168
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006169#ifdef FEAT_FLOAT
6170 case VAR_FLOAT:
6171 return tv1->vval.v_float == tv2->vval.v_float;
6172#endif
6173
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006174 case VAR_STRING:
6175 s1 = get_tv_string_buf(tv1, buf1);
6176 s2 = get_tv_string_buf(tv2, buf2);
6177 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006178
6179 case VAR_SPECIAL:
6180 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006181 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006182
6183 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006184 return TRUE;
6185}
6186
6187/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006188 * Locate item with index "n" in list "l" and return it.
6189 * A negative index is counted from the end; -1 is the last item.
6190 * Returns NULL when "n" is out of range.
6191 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006192 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006193list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006194{
Bram Moolenaar33570922005-01-25 22:26:29 +00006195 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006196 long idx;
6197
6198 if (l == NULL)
6199 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006200
6201 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006202 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006203 n = l->lv_len + n;
6204
6205 /* Check for index out of range. */
6206 if (n < 0 || n >= l->lv_len)
6207 return NULL;
6208
6209 /* When there is a cached index may start search from there. */
6210 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006211 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006212 if (n < l->lv_idx / 2)
6213 {
6214 /* closest to the start of the list */
6215 item = l->lv_first;
6216 idx = 0;
6217 }
6218 else if (n > (l->lv_idx + l->lv_len) / 2)
6219 {
6220 /* closest to the end of the list */
6221 item = l->lv_last;
6222 idx = l->lv_len - 1;
6223 }
6224 else
6225 {
6226 /* closest to the cached index */
6227 item = l->lv_idx_item;
6228 idx = l->lv_idx;
6229 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006230 }
6231 else
6232 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006233 if (n < l->lv_len / 2)
6234 {
6235 /* closest to the start of the list */
6236 item = l->lv_first;
6237 idx = 0;
6238 }
6239 else
6240 {
6241 /* closest to the end of the list */
6242 item = l->lv_last;
6243 idx = l->lv_len - 1;
6244 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006245 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006246
6247 while (n > idx)
6248 {
6249 /* search forward */
6250 item = item->li_next;
6251 ++idx;
6252 }
6253 while (n < idx)
6254 {
6255 /* search backward */
6256 item = item->li_prev;
6257 --idx;
6258 }
6259
6260 /* cache the used index */
6261 l->lv_idx = idx;
6262 l->lv_idx_item = item;
6263
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006264 return item;
6265}
6266
6267/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006268 * Get list item "l[idx]" as a number.
6269 */
6270 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006271list_find_nr(
6272 list_T *l,
6273 long idx,
6274 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006275{
6276 listitem_T *li;
6277
6278 li = list_find(l, idx);
6279 if (li == NULL)
6280 {
6281 if (errorp != NULL)
6282 *errorp = TRUE;
6283 return -1L;
6284 }
6285 return get_tv_number_chk(&li->li_tv, errorp);
6286}
6287
6288/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006289 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6290 */
6291 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006292list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006293{
6294 listitem_T *li;
6295
6296 li = list_find(l, idx - 1);
6297 if (li == NULL)
6298 {
6299 EMSGN(_(e_listidx), idx);
6300 return NULL;
6301 }
6302 return get_tv_string(&li->li_tv);
6303}
6304
6305/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006306 * Locate "item" list "l" and return its index.
6307 * Returns -1 when "item" is not in the list.
6308 */
6309 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006310list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006311{
6312 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006313 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006314
6315 if (l == NULL)
6316 return -1;
6317 idx = 0;
6318 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6319 ++idx;
6320 if (li == NULL)
6321 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006322 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006323}
6324
6325/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006326 * Append item "item" to the end of list "l".
6327 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006328 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006329list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006330{
6331 if (l->lv_last == NULL)
6332 {
6333 /* empty list */
6334 l->lv_first = item;
6335 l->lv_last = item;
6336 item->li_prev = NULL;
6337 }
6338 else
6339 {
6340 l->lv_last->li_next = item;
6341 item->li_prev = l->lv_last;
6342 l->lv_last = item;
6343 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006344 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006345 item->li_next = NULL;
6346}
6347
6348/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006349 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006350 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006351 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006352 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006353list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006354{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006355 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006356
Bram Moolenaar05159a02005-02-26 23:04:13 +00006357 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006358 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006359 copy_tv(tv, &li->li_tv);
6360 list_append(l, li);
6361 return OK;
6362}
6363
6364/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006365 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006366 * Return FAIL when out of memory.
6367 */
6368 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006369list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006370{
6371 listitem_T *li = listitem_alloc();
6372
6373 if (li == NULL)
6374 return FAIL;
6375 li->li_tv.v_type = VAR_DICT;
6376 li->li_tv.v_lock = 0;
6377 li->li_tv.vval.v_dict = dict;
6378 list_append(list, li);
6379 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006380 return OK;
6381}
6382
6383/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006384 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006385 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006386 * Returns FAIL when out of memory.
6387 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006388 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006389list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006390{
6391 listitem_T *li = listitem_alloc();
6392
6393 if (li == NULL)
6394 return FAIL;
6395 list_append(l, li);
6396 li->li_tv.v_type = VAR_STRING;
6397 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006398 if (str == NULL)
6399 li->li_tv.vval.v_string = NULL;
6400 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006401 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006402 return FAIL;
6403 return OK;
6404}
6405
6406/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006407 * Append "n" to list "l".
6408 * Returns FAIL when out of memory.
6409 */
6410 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006411list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006412{
6413 listitem_T *li;
6414
6415 li = listitem_alloc();
6416 if (li == NULL)
6417 return FAIL;
6418 li->li_tv.v_type = VAR_NUMBER;
6419 li->li_tv.v_lock = 0;
6420 li->li_tv.vval.v_number = n;
6421 list_append(l, li);
6422 return OK;
6423}
6424
6425/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006426 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006427 * If "item" is NULL append at the end.
6428 * Return FAIL when out of memory.
6429 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006430 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006431list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006432{
Bram Moolenaar33570922005-01-25 22:26:29 +00006433 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006434
6435 if (ni == NULL)
6436 return FAIL;
6437 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006438 list_insert(l, ni, item);
6439 return OK;
6440}
6441
6442 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006443list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006444{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006445 if (item == NULL)
6446 /* Append new item at end of list. */
6447 list_append(l, ni);
6448 else
6449 {
6450 /* Insert new item before existing item. */
6451 ni->li_prev = item->li_prev;
6452 ni->li_next = item;
6453 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006454 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006455 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006456 ++l->lv_idx;
6457 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006458 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006459 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006460 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006461 l->lv_idx_item = NULL;
6462 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006463 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006464 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006465 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006466}
6467
6468/*
6469 * Extend "l1" with "l2".
6470 * If "bef" is NULL append at the end, otherwise insert before this item.
6471 * Returns FAIL when out of memory.
6472 */
6473 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006474list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006475{
Bram Moolenaar33570922005-01-25 22:26:29 +00006476 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006477 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006478
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006479 /* We also quit the loop when we have inserted the original item count of
6480 * the list, avoid a hang when we extend a list with itself. */
6481 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006482 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6483 return FAIL;
6484 return OK;
6485}
6486
6487/*
6488 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6489 * Return FAIL when out of memory.
6490 */
6491 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006492list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006493{
Bram Moolenaar33570922005-01-25 22:26:29 +00006494 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006495
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006496 if (l1 == NULL || l2 == NULL)
6497 return FAIL;
6498
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006499 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006500 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006501 if (l == NULL)
6502 return FAIL;
6503 tv->v_type = VAR_LIST;
6504 tv->vval.v_list = l;
6505
6506 /* append all items from the second list */
6507 return list_extend(l, l2, NULL);
6508}
6509
6510/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006511 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006512 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006513 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006514 * Returns NULL when out of memory.
6515 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006516 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006517list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006518{
Bram Moolenaar33570922005-01-25 22:26:29 +00006519 list_T *copy;
6520 listitem_T *item;
6521 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006522
6523 if (orig == NULL)
6524 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006525
6526 copy = list_alloc();
6527 if (copy != NULL)
6528 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006529 if (copyID != 0)
6530 {
6531 /* Do this before adding the items, because one of the items may
6532 * refer back to this list. */
6533 orig->lv_copyID = copyID;
6534 orig->lv_copylist = copy;
6535 }
6536 for (item = orig->lv_first; item != NULL && !got_int;
6537 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006538 {
6539 ni = listitem_alloc();
6540 if (ni == NULL)
6541 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006542 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006543 {
6544 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6545 {
6546 vim_free(ni);
6547 break;
6548 }
6549 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006550 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006551 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006552 list_append(copy, ni);
6553 }
6554 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006555 if (item != NULL)
6556 {
6557 list_unref(copy);
6558 copy = NULL;
6559 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006560 }
6561
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006562 return copy;
6563}
6564
6565/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006567 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006568 * This used to be called list_remove, but that conflicts with a Sun header
6569 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006570 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006571 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006572vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006573{
Bram Moolenaar33570922005-01-25 22:26:29 +00006574 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006575
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006576 /* notify watchers */
6577 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006578 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006579 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006580 list_fix_watch(l, ip);
6581 if (ip == item2)
6582 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006583 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006584
6585 if (item2->li_next == NULL)
6586 l->lv_last = item->li_prev;
6587 else
6588 item2->li_next->li_prev = item->li_prev;
6589 if (item->li_prev == NULL)
6590 l->lv_first = item2->li_next;
6591 else
6592 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006593 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006594}
6595
6596/*
6597 * Return an allocated string with the string representation of a list.
6598 * May return NULL.
6599 */
6600 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006601list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006602{
6603 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006604
6605 if (tv->vval.v_list == NULL)
6606 return NULL;
6607 ga_init2(&ga, (int)sizeof(char), 80);
6608 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006609 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006610 {
6611 vim_free(ga.ga_data);
6612 return NULL;
6613 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006614 ga_append(&ga, ']');
6615 ga_append(&ga, NUL);
6616 return (char_u *)ga.ga_data;
6617}
6618
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006619typedef struct join_S {
6620 char_u *s;
6621 char_u *tofree;
6622} join_T;
6623
6624 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006625list_join_inner(
6626 garray_T *gap, /* to store the result in */
6627 list_T *l,
6628 char_u *sep,
6629 int echo_style,
6630 int copyID,
6631 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006632{
6633 int i;
6634 join_T *p;
6635 int len;
6636 int sumlen = 0;
6637 int first = TRUE;
6638 char_u *tofree;
6639 char_u numbuf[NUMBUFLEN];
6640 listitem_T *item;
6641 char_u *s;
6642
6643 /* Stringify each item in the list. */
6644 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6645 {
6646 if (echo_style)
6647 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6648 else
6649 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6650 if (s == NULL)
6651 return FAIL;
6652
6653 len = (int)STRLEN(s);
6654 sumlen += len;
6655
Bram Moolenaarcde88542015-08-11 19:14:00 +02006656 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006657 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6658 if (tofree != NULL || s != numbuf)
6659 {
6660 p->s = s;
6661 p->tofree = tofree;
6662 }
6663 else
6664 {
6665 p->s = vim_strnsave(s, len);
6666 p->tofree = p->s;
6667 }
6668
6669 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006670 if (did_echo_string_emsg) /* recursion error, bail out */
6671 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006672 }
6673
6674 /* Allocate result buffer with its total size, avoid re-allocation and
6675 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6676 if (join_gap->ga_len >= 2)
6677 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6678 if (ga_grow(gap, sumlen + 2) == FAIL)
6679 return FAIL;
6680
6681 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6682 {
6683 if (first)
6684 first = FALSE;
6685 else
6686 ga_concat(gap, sep);
6687 p = ((join_T *)join_gap->ga_data) + i;
6688
6689 if (p->s != NULL)
6690 ga_concat(gap, p->s);
6691 line_breakcheck();
6692 }
6693
6694 return OK;
6695}
6696
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006697/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006698 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006699 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006700 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006701 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006702 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006703list_join(
6704 garray_T *gap,
6705 list_T *l,
6706 char_u *sep,
6707 int echo_style,
6708 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006709{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006710 garray_T join_ga;
6711 int retval;
6712 join_T *p;
6713 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006714
Bram Moolenaard39a7512015-04-16 22:51:22 +02006715 if (l->lv_len < 1)
6716 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006717 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6718 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6719
6720 /* Dispose each item in join_ga. */
6721 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006722 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006723 p = (join_T *)join_ga.ga_data;
6724 for (i = 0; i < join_ga.ga_len; ++i)
6725 {
6726 vim_free(p->tofree);
6727 ++p;
6728 }
6729 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006730 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006731
6732 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006733}
6734
6735/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006736 * Return the next (unique) copy ID.
6737 * Used for serializing nested structures.
6738 */
6739 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006740get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006741{
6742 current_copyID += COPYID_INC;
6743 return current_copyID;
6744}
6745
6746/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006747 * Garbage collection for lists and dictionaries.
6748 *
6749 * We use reference counts to be able to free most items right away when they
6750 * are no longer used. But for composite items it's possible that it becomes
6751 * unused while the reference count is > 0: When there is a recursive
6752 * reference. Example:
6753 * :let l = [1, 2, 3]
6754 * :let d = {9: l}
6755 * :let l[1] = d
6756 *
6757 * Since this is quite unusual we handle this with garbage collection: every
6758 * once in a while find out which lists and dicts are not referenced from any
6759 * variable.
6760 *
6761 * Here is a good reference text about garbage collection (refers to Python
6762 * but it applies to all reference-counting mechanisms):
6763 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006764 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006765
6766/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006767 * Do garbage collection for lists and dicts.
6768 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006769 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006770 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006771garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006772{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006773 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006774 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006775 buf_T *buf;
6776 win_T *wp;
6777 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006778 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006779 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006780 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006781#ifdef FEAT_WINDOWS
6782 tabpage_T *tp;
6783#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006784
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006785 /* Only do this once. */
6786 want_garbage_collect = FALSE;
6787 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006788 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006789
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006790 /* We advance by two because we add one for items referenced through
6791 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006792 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006793
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006794 /*
6795 * 1. Go through all accessible variables and mark all lists and dicts
6796 * with copyID.
6797 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006798
6799 /* Don't free variables in the previous_funccal list unless they are only
6800 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006801 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006802 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6803 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006804 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6805 NULL);
6806 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6807 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006808 }
6809
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006810 /* script-local variables */
6811 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006812 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006813
6814 /* buffer-local variables */
6815 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006816 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6817 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006818
6819 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006820 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006821 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6822 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006823#ifdef FEAT_AUTOCMD
6824 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006825 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6826 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006827#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006828
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006829#ifdef FEAT_WINDOWS
6830 /* tabpage-local variables */
6831 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006832 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6833 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006834#endif
6835
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006836 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006837 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006838
6839 /* function-local variables */
6840 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6841 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006842 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6843 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006844 }
6845
Bram Moolenaard812df62008-11-09 12:46:09 +00006846 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006847 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006848
Bram Moolenaar1dced572012-04-05 16:54:08 +02006849#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006850 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006851#endif
6852
Bram Moolenaardb913952012-06-29 12:54:53 +02006853#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006854 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006855#endif
6856
6857#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006858 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006859#endif
6860
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006861 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006862 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006863 /*
6864 * 2. Free lists and dictionaries that are not referenced.
6865 */
6866 did_free = free_unref_items(copyID);
6867
6868 /*
6869 * 3. Check if any funccal can be freed now.
6870 */
6871 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006872 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006873 if (can_free_funccal(*pfc, copyID))
6874 {
6875 fc = *pfc;
6876 *pfc = fc->caller;
6877 free_funccal(fc, TRUE);
6878 did_free = TRUE;
6879 did_free_funccal = TRUE;
6880 }
6881 else
6882 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006883 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006884 if (did_free_funccal)
6885 /* When a funccal was freed some more items might be garbage
6886 * collected, so run again. */
6887 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006888 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006889 else if (p_verbose > 0)
6890 {
6891 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6892 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006893
6894 return did_free;
6895}
6896
6897/*
6898 * Free lists and dictionaries that are no longer referenced.
6899 */
6900 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006901free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006902{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006903 dict_T *dd, *dd_next;
6904 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006905 int did_free = FALSE;
6906
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006907 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006908 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006909 */
6910 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006911 {
6912 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006913 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006914 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006915 /* Free the Dictionary and ordinary items it contains, but don't
6916 * recurse into Lists and Dictionaries, they will be in the list
6917 * of dicts or list of lists. */
6918 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006920 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006921 dd = dd_next;
6922 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006923
6924 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006925 * Go through the list of lists and free items without the copyID.
6926 * But don't free a list that has a watcher (used in a for loop), these
6927 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006928 */
6929 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006930 {
6931 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006932 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6933 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006934 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006935 /* Free the List and ordinary items it contains, but don't recurse
6936 * into Lists and Dictionaries, they will be in the list of dicts
6937 * or list of lists. */
6938 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006939 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006940 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006941 ll = ll_next;
6942 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006943 return did_free;
6944}
6945
6946/*
6947 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006948 * "list_stack" is used to add lists to be marked. Can be NULL.
6949 *
6950 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006951 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006952 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006953set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006954{
6955 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006956 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006957 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006958 hashtab_T *cur_ht;
6959 ht_stack_T *ht_stack = NULL;
6960 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006961
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006962 cur_ht = ht;
6963 for (;;)
6964 {
6965 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006966 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006967 /* Mark each item in the hashtab. If the item contains a hashtab
6968 * it is added to ht_stack, if it contains a list it is added to
6969 * list_stack. */
6970 todo = (int)cur_ht->ht_used;
6971 for (hi = cur_ht->ht_array; todo > 0; ++hi)
6972 if (!HASHITEM_EMPTY(hi))
6973 {
6974 --todo;
6975 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
6976 &ht_stack, list_stack);
6977 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006978 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006979
6980 if (ht_stack == NULL)
6981 break;
6982
6983 /* take an item from the stack */
6984 cur_ht = ht_stack->ht;
6985 tempitem = ht_stack;
6986 ht_stack = ht_stack->prev;
6987 free(tempitem);
6988 }
6989
6990 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991}
6992
6993/*
6994 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006995 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6996 *
6997 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006998 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006999 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007000set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007001{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007002 listitem_T *li;
7003 int abort = FALSE;
7004 list_T *cur_l;
7005 list_stack_T *list_stack = NULL;
7006 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007007
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007008 cur_l = l;
7009 for (;;)
7010 {
7011 if (!abort)
7012 /* Mark each item in the list. If the item contains a hashtab
7013 * it is added to ht_stack, if it contains a list it is added to
7014 * list_stack. */
7015 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7016 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7017 ht_stack, &list_stack);
7018 if (list_stack == NULL)
7019 break;
7020
7021 /* take an item from the stack */
7022 cur_l = list_stack->list;
7023 tempitem = list_stack;
7024 list_stack = list_stack->prev;
7025 free(tempitem);
7026 }
7027
7028 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029}
7030
7031/*
7032 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007033 * "list_stack" is used to add lists to be marked. Can be NULL.
7034 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7035 *
7036 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007037 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007038 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007039set_ref_in_item(
7040 typval_T *tv,
7041 int copyID,
7042 ht_stack_T **ht_stack,
7043 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007044{
7045 dict_T *dd;
7046 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007047 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007048
7049 switch (tv->v_type)
7050 {
7051 case VAR_DICT:
7052 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007053 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007054 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007055 /* Didn't see this dict yet. */
7056 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007057 if (ht_stack == NULL)
7058 {
7059 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7060 }
7061 else
7062 {
7063 ht_stack_T *newitem = (ht_stack_T*)malloc(
7064 sizeof(ht_stack_T));
7065 if (newitem == NULL)
7066 abort = TRUE;
7067 else
7068 {
7069 newitem->ht = &dd->dv_hashtab;
7070 newitem->prev = *ht_stack;
7071 *ht_stack = newitem;
7072 }
7073 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007074 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007075 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007076
7077 case VAR_LIST:
7078 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007079 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007080 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007081 /* Didn't see this list yet. */
7082 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007083 if (list_stack == NULL)
7084 {
7085 abort = set_ref_in_list(ll, copyID, ht_stack);
7086 }
7087 else
7088 {
7089 list_stack_T *newitem = (list_stack_T*)malloc(
7090 sizeof(list_stack_T));
7091 if (newitem == NULL)
7092 abort = TRUE;
7093 else
7094 {
7095 newitem->list = ll;
7096 newitem->prev = *list_stack;
7097 *list_stack = newitem;
7098 }
7099 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007100 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007101 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007102 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007103 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007104}
7105
7106/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007107 * Allocate an empty header for a dictionary.
7108 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007109 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007110dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007111{
Bram Moolenaar33570922005-01-25 22:26:29 +00007112 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007113
Bram Moolenaar33570922005-01-25 22:26:29 +00007114 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007115 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007116 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007117 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007118 if (first_dict != NULL)
7119 first_dict->dv_used_prev = d;
7120 d->dv_used_next = first_dict;
7121 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007122 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007123
Bram Moolenaar33570922005-01-25 22:26:29 +00007124 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007125 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007126 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007127 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007128 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007129 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007130 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007131}
7132
7133/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007134 * Allocate an empty dict for a return value.
7135 * Returns OK or FAIL.
7136 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007137 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007138rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007139{
7140 dict_T *d = dict_alloc();
7141
7142 if (d == NULL)
7143 return FAIL;
7144
7145 rettv->vval.v_dict = d;
7146 rettv->v_type = VAR_DICT;
7147 ++d->dv_refcount;
7148 return OK;
7149}
7150
7151
7152/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007153 * Unreference a Dictionary: decrement the reference count and free it when it
7154 * becomes zero.
7155 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007156 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007157dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007158{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007159 if (d != NULL && --d->dv_refcount <= 0)
7160 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007161}
7162
7163/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007164 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007165 * Ignores the reference count.
7166 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007167 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007168dict_free(
7169 dict_T *d,
7170 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007171{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007172 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007173 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007174 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007175
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007176 /* Remove the dict from the list of dicts for garbage collection. */
7177 if (d->dv_used_prev == NULL)
7178 first_dict = d->dv_used_next;
7179 else
7180 d->dv_used_prev->dv_used_next = d->dv_used_next;
7181 if (d->dv_used_next != NULL)
7182 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7183
7184 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007185 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007186 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007187 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007188 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007189 if (!HASHITEM_EMPTY(hi))
7190 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007191 /* Remove the item before deleting it, just in case there is
7192 * something recursive causing trouble. */
7193 di = HI2DI(hi);
7194 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007195 if (recurse || (di->di_tv.v_type != VAR_LIST
7196 && di->di_tv.v_type != VAR_DICT))
7197 clear_tv(&di->di_tv);
7198 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007199 --todo;
7200 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007201 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007202 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007203 vim_free(d);
7204}
7205
7206/*
7207 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007208 * The "key" is copied to the new item.
7209 * Note that the value of the item "di_tv" still needs to be initialized!
7210 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007211 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007212 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007213dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007214{
Bram Moolenaar33570922005-01-25 22:26:29 +00007215 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007216
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007217 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007218 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007219 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007220 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007221 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007222 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007223 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007224}
7225
7226/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007227 * Make a copy of a Dictionary item.
7228 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007229 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007230dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007231{
Bram Moolenaar33570922005-01-25 22:26:29 +00007232 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007233
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007234 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7235 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007236 if (di != NULL)
7237 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007238 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007239 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007240 copy_tv(&org->di_tv, &di->di_tv);
7241 }
7242 return di;
7243}
7244
7245/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007246 * Remove item "item" from Dictionary "dict" and free it.
7247 */
7248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007249dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007250{
Bram Moolenaar33570922005-01-25 22:26:29 +00007251 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007252
Bram Moolenaar33570922005-01-25 22:26:29 +00007253 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007254 if (HASHITEM_EMPTY(hi))
7255 EMSG2(_(e_intern2), "dictitem_remove()");
7256 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007257 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007258 dictitem_free(item);
7259}
7260
7261/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007262 * Free a dict item. Also clears the value.
7263 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007264 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007265dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007266{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007268 if (item->di_flags & DI_FLAGS_ALLOC)
7269 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007270}
7271
7272/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007273 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7274 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007275 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007276 * Returns NULL when out of memory.
7277 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007278 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007279dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007280{
Bram Moolenaar33570922005-01-25 22:26:29 +00007281 dict_T *copy;
7282 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007283 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007284 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007285
7286 if (orig == NULL)
7287 return NULL;
7288
7289 copy = dict_alloc();
7290 if (copy != NULL)
7291 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007292 if (copyID != 0)
7293 {
7294 orig->dv_copyID = copyID;
7295 orig->dv_copydict = copy;
7296 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007297 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007298 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007299 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007300 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007301 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007302 --todo;
7303
7304 di = dictitem_alloc(hi->hi_key);
7305 if (di == NULL)
7306 break;
7307 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007308 {
7309 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7310 copyID) == FAIL)
7311 {
7312 vim_free(di);
7313 break;
7314 }
7315 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007316 else
7317 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7318 if (dict_add(copy, di) == FAIL)
7319 {
7320 dictitem_free(di);
7321 break;
7322 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007323 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007324 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325
Bram Moolenaare9a41262005-01-15 22:18:47 +00007326 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007327 if (todo > 0)
7328 {
7329 dict_unref(copy);
7330 copy = NULL;
7331 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007332 }
7333
7334 return copy;
7335}
7336
7337/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007338 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007339 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007340 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007341 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007342dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007343{
Bram Moolenaar33570922005-01-25 22:26:29 +00007344 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007345}
7346
Bram Moolenaar8c711452005-01-14 21:53:12 +00007347/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007348 * Add a number or string entry to dictionary "d".
7349 * When "str" is NULL use number "nr", otherwise use "str".
7350 * Returns FAIL when out of memory and when key already exists.
7351 */
7352 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007353dict_add_nr_str(
7354 dict_T *d,
7355 char *key,
7356 long nr,
7357 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007358{
7359 dictitem_T *item;
7360
7361 item = dictitem_alloc((char_u *)key);
7362 if (item == NULL)
7363 return FAIL;
7364 item->di_tv.v_lock = 0;
7365 if (str == NULL)
7366 {
7367 item->di_tv.v_type = VAR_NUMBER;
7368 item->di_tv.vval.v_number = nr;
7369 }
7370 else
7371 {
7372 item->di_tv.v_type = VAR_STRING;
7373 item->di_tv.vval.v_string = vim_strsave(str);
7374 }
7375 if (dict_add(d, item) == FAIL)
7376 {
7377 dictitem_free(item);
7378 return FAIL;
7379 }
7380 return OK;
7381}
7382
7383/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007384 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007385 * Returns FAIL when out of memory and when key already exists.
7386 */
7387 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007388dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007389{
7390 dictitem_T *item;
7391
7392 item = dictitem_alloc((char_u *)key);
7393 if (item == NULL)
7394 return FAIL;
7395 item->di_tv.v_lock = 0;
7396 item->di_tv.v_type = VAR_LIST;
7397 item->di_tv.vval.v_list = list;
7398 if (dict_add(d, item) == FAIL)
7399 {
7400 dictitem_free(item);
7401 return FAIL;
7402 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007403 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007404 return OK;
7405}
7406
7407/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007408 * Get the number of items in a Dictionary.
7409 */
7410 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007411dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007412{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007413 if (d == NULL)
7414 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007415 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007416}
7417
7418/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007419 * Find item "key[len]" in Dictionary "d".
7420 * If "len" is negative use strlen(key).
7421 * Returns NULL when not found.
7422 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007423 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007424dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007425{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007426#define AKEYLEN 200
7427 char_u buf[AKEYLEN];
7428 char_u *akey;
7429 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007430 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007431
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007432 if (len < 0)
7433 akey = key;
7434 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007435 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007436 tofree = akey = vim_strnsave(key, len);
7437 if (akey == NULL)
7438 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007439 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007440 else
7441 {
7442 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007443 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007444 akey = buf;
7445 }
7446
Bram Moolenaar33570922005-01-25 22:26:29 +00007447 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007448 vim_free(tofree);
7449 if (HASHITEM_EMPTY(hi))
7450 return NULL;
7451 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007452}
7453
7454/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007455 * Get a string item from a dictionary.
7456 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007457 * Returns NULL if the entry doesn't exist or out of memory.
7458 */
7459 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007460get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007461{
7462 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007463 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007464
7465 di = dict_find(d, key, -1);
7466 if (di == NULL)
7467 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007468 s = get_tv_string(&di->di_tv);
7469 if (save && s != NULL)
7470 s = vim_strsave(s);
7471 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007472}
7473
7474/*
7475 * Get a number item from a dictionary.
7476 * Returns 0 if the entry doesn't exist or out of memory.
7477 */
7478 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007479get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007480{
7481 dictitem_T *di;
7482
7483 di = dict_find(d, key, -1);
7484 if (di == NULL)
7485 return 0;
7486 return get_tv_number(&di->di_tv);
7487}
7488
7489/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007490 * Return an allocated string with the string representation of a Dictionary.
7491 * May return NULL.
7492 */
7493 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007494dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495{
7496 garray_T ga;
7497 int first = TRUE;
7498 char_u *tofree;
7499 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007500 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007501 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007502 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007503 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007504
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007505 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007506 return NULL;
7507 ga_init2(&ga, (int)sizeof(char), 80);
7508 ga_append(&ga, '{');
7509
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007510 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007511 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007512 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007513 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007514 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007515 --todo;
7516
7517 if (first)
7518 first = FALSE;
7519 else
7520 ga_concat(&ga, (char_u *)", ");
7521
7522 tofree = string_quote(hi->hi_key, FALSE);
7523 if (tofree != NULL)
7524 {
7525 ga_concat(&ga, tofree);
7526 vim_free(tofree);
7527 }
7528 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007529 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007530 if (s != NULL)
7531 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007532 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007533 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007534 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007535 line_breakcheck();
7536
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007538 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007539 if (todo > 0)
7540 {
7541 vim_free(ga.ga_data);
7542 return NULL;
7543 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007544
7545 ga_append(&ga, '}');
7546 ga_append(&ga, NUL);
7547 return (char_u *)ga.ga_data;
7548}
7549
7550/*
7551 * Allocate a variable for a Dictionary and fill it from "*arg".
7552 * Return OK or FAIL. Returns NOTDONE for {expr}.
7553 */
7554 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007555get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556{
Bram Moolenaar33570922005-01-25 22:26:29 +00007557 dict_T *d = NULL;
7558 typval_T tvkey;
7559 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007560 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007561 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007562 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007563 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007564
7565 /*
7566 * First check if it's not a curly-braces thing: {expr}.
7567 * Must do this without evaluating, otherwise a function may be called
7568 * twice. Unfortunately this means we need to call eval1() twice for the
7569 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007570 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007571 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007572 if (*start != '}')
7573 {
7574 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7575 return FAIL;
7576 if (*start == '}')
7577 return NOTDONE;
7578 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007579
7580 if (evaluate)
7581 {
7582 d = dict_alloc();
7583 if (d == NULL)
7584 return FAIL;
7585 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007586 tvkey.v_type = VAR_UNKNOWN;
7587 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007588
7589 *arg = skipwhite(*arg + 1);
7590 while (**arg != '}' && **arg != NUL)
7591 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007592 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007593 goto failret;
7594 if (**arg != ':')
7595 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007596 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007597 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007598 goto failret;
7599 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007600 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007601 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007602 key = get_tv_string_buf_chk(&tvkey, buf);
7603 if (key == NULL || *key == NUL)
7604 {
7605 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7606 if (key != NULL)
7607 EMSG(_(e_emptykey));
7608 clear_tv(&tvkey);
7609 goto failret;
7610 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007611 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007612
7613 *arg = skipwhite(*arg + 1);
7614 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7615 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007616 if (evaluate)
7617 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007618 goto failret;
7619 }
7620 if (evaluate)
7621 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007622 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007623 if (item != NULL)
7624 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007625 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007626 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007627 clear_tv(&tv);
7628 goto failret;
7629 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007630 item = dictitem_alloc(key);
7631 clear_tv(&tvkey);
7632 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007633 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007634 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007635 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007636 if (dict_add(d, item) == FAIL)
7637 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007638 }
7639 }
7640
7641 if (**arg == '}')
7642 break;
7643 if (**arg != ',')
7644 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007645 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007646 goto failret;
7647 }
7648 *arg = skipwhite(*arg + 1);
7649 }
7650
7651 if (**arg != '}')
7652 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007653 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007654failret:
7655 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007656 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657 return FAIL;
7658 }
7659
7660 *arg = skipwhite(*arg + 1);
7661 if (evaluate)
7662 {
7663 rettv->v_type = VAR_DICT;
7664 rettv->vval.v_dict = d;
7665 ++d->dv_refcount;
7666 }
7667
7668 return OK;
7669}
7670
Bram Moolenaar17a13432016-01-24 14:22:10 +01007671 static char *
7672get_var_special_name(int nr)
7673{
7674 switch (nr)
7675 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007676 case VVAL_FALSE: return "v:false";
7677 case VVAL_TRUE: return "v:true";
7678 case VVAL_NONE: return "v:none";
7679 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007680 }
7681 EMSG2(_(e_intern2), "get_var_special_name()");
7682 return "42";
7683}
7684
Bram Moolenaar8c711452005-01-14 21:53:12 +00007685/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007686 * Return a string with the string representation of a variable.
7687 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007688 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007689 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007690 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007691 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007692 */
7693 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007694echo_string(
7695 typval_T *tv,
7696 char_u **tofree,
7697 char_u *numbuf,
7698 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007699{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007700 static int recurse = 0;
7701 char_u *r = NULL;
7702
Bram Moolenaar33570922005-01-25 22:26:29 +00007703 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007704 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007705 if (!did_echo_string_emsg)
7706 {
7707 /* Only give this message once for a recursive call to avoid
7708 * flooding the user with errors. And stop iterating over lists
7709 * and dicts. */
7710 did_echo_string_emsg = TRUE;
7711 EMSG(_("E724: variable nested too deep for displaying"));
7712 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007713 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007714 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007715 }
7716 ++recurse;
7717
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007718 switch (tv->v_type)
7719 {
7720 case VAR_FUNC:
7721 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007722 r = tv->vval.v_string;
7723 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007724
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007725 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007726 if (tv->vval.v_list == NULL)
7727 {
7728 *tofree = NULL;
7729 r = NULL;
7730 }
7731 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7732 {
7733 *tofree = NULL;
7734 r = (char_u *)"[...]";
7735 }
7736 else
7737 {
7738 tv->vval.v_list->lv_copyID = copyID;
7739 *tofree = list2string(tv, copyID);
7740 r = *tofree;
7741 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007742 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007743
Bram Moolenaar8c711452005-01-14 21:53:12 +00007744 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007745 if (tv->vval.v_dict == NULL)
7746 {
7747 *tofree = NULL;
7748 r = NULL;
7749 }
7750 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7751 {
7752 *tofree = NULL;
7753 r = (char_u *)"{...}";
7754 }
7755 else
7756 {
7757 tv->vval.v_dict->dv_copyID = copyID;
7758 *tofree = dict2string(tv, copyID);
7759 r = *tofree;
7760 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007761 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007762
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007763 case VAR_STRING:
7764 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007765 *tofree = NULL;
7766 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007767 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007768
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007769#ifdef FEAT_FLOAT
7770 case VAR_FLOAT:
7771 *tofree = NULL;
7772 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7773 r = numbuf;
7774 break;
7775#endif
7776
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007777 case VAR_SPECIAL:
7778 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007779 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007780 break;
7781
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007782 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007783 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007784 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007785 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007786
Bram Moolenaar8502c702014-06-17 12:51:16 +02007787 if (--recurse == 0)
7788 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007789 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007790}
7791
7792/*
7793 * Return a string with the string representation of a variable.
7794 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7795 * "numbuf" is used for a number.
7796 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007797 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007798 */
7799 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007800tv2string(
7801 typval_T *tv,
7802 char_u **tofree,
7803 char_u *numbuf,
7804 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007805{
7806 switch (tv->v_type)
7807 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007808 case VAR_FUNC:
7809 *tofree = string_quote(tv->vval.v_string, TRUE);
7810 return *tofree;
7811 case VAR_STRING:
7812 *tofree = string_quote(tv->vval.v_string, FALSE);
7813 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007814#ifdef FEAT_FLOAT
7815 case VAR_FLOAT:
7816 *tofree = NULL;
7817 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7818 return numbuf;
7819#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007820 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007821 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007822 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007823 case VAR_SPECIAL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007824 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007825 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007826 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007827 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007828 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007829}
7830
7831/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007832 * Return string "str" in ' quotes, doubling ' characters.
7833 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007834 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007835 */
7836 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007837string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007838{
Bram Moolenaar33570922005-01-25 22:26:29 +00007839 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007840 char_u *p, *r, *s;
7841
Bram Moolenaar33570922005-01-25 22:26:29 +00007842 len = (function ? 13 : 3);
7843 if (str != NULL)
7844 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007845 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007846 for (p = str; *p != NUL; mb_ptr_adv(p))
7847 if (*p == '\'')
7848 ++len;
7849 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007850 s = r = alloc(len);
7851 if (r != NULL)
7852 {
7853 if (function)
7854 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007855 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007856 r += 10;
7857 }
7858 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007859 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007860 if (str != NULL)
7861 for (p = str; *p != NUL; )
7862 {
7863 if (*p == '\'')
7864 *r++ = '\'';
7865 MB_COPY_CHAR(p, r);
7866 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007867 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007868 if (function)
7869 *r++ = ')';
7870 *r++ = NUL;
7871 }
7872 return s;
7873}
7874
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007875#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007876/*
7877 * Convert the string "text" to a floating point number.
7878 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7879 * this always uses a decimal point.
7880 * Returns the length of the text that was consumed.
7881 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007882 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007883string2float(
7884 char_u *text,
7885 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007886{
7887 char *s = (char *)text;
7888 float_T f;
7889
7890 f = strtod(s, &s);
7891 *value = f;
7892 return (int)((char_u *)s - text);
7893}
7894#endif
7895
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007896/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 * Get the value of an environment variable.
7898 * "arg" is pointing to the '$'. It is advanced to after the name.
7899 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007900 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 */
7902 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007903get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904{
7905 char_u *string = NULL;
7906 int len;
7907 int cc;
7908 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007909 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910
7911 ++*arg;
7912 name = *arg;
7913 len = get_env_len(arg);
7914 if (evaluate)
7915 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007916 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01007917 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007918
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007919 cc = name[len];
7920 name[len] = NUL;
7921 /* first try vim_getenv(), fast for normal environment vars */
7922 string = vim_getenv(name, &mustfree);
7923 if (string != NULL && *string != NUL)
7924 {
7925 if (!mustfree)
7926 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007928 else
7929 {
7930 if (mustfree)
7931 vim_free(string);
7932
7933 /* next try expanding things like $VIM and ${HOME} */
7934 string = expand_env_save(name - 1);
7935 if (string != NULL && *string == '$')
7936 {
7937 vim_free(string);
7938 string = NULL;
7939 }
7940 }
7941 name[len] = cc;
7942
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007943 rettv->v_type = VAR_STRING;
7944 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 }
7946
7947 return OK;
7948}
7949
7950/*
7951 * Array with names and number of arguments of all internal functions
7952 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7953 */
7954static struct fst
7955{
7956 char *f_name; /* function name */
7957 char f_min_argc; /* minimal number of arguments */
7958 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01007959 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007960 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961} functions[] =
7962{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007963#ifdef FEAT_FLOAT
7964 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007965 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007966#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007967 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01007968 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007969 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 {"append", 2, 2, f_append},
7971 {"argc", 0, 0, f_argc},
7972 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007973 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007974 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01007975#ifdef FEAT_FLOAT
7976 {"asin", 1, 1, f_asin}, /* WJMc */
7977#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01007978 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01007979 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01007980 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01007981 {"assert_false", 1, 2, f_assert_false},
7982 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007983#ifdef FEAT_FLOAT
7984 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007985 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007988 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 {"bufexists", 1, 1, f_bufexists},
7990 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7991 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7992 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7993 {"buflisted", 1, 1, f_buflisted},
7994 {"bufloaded", 1, 1, f_bufloaded},
7995 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007996 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 {"bufwinnr", 1, 1, f_bufwinnr},
7998 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007999 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008000 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008001 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008002#ifdef FEAT_FLOAT
8003 {"ceil", 1, 1, f_ceil},
8004#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008005 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008006 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008008 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008010#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008011 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008012 {"complete_add", 1, 1, f_complete_add},
8013 {"complete_check", 0, 0, f_complete_check},
8014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 {"confirm", 1, 4, f_confirm},
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008016#ifdef FEAT_CHANNEL
8017 {"connect", 2, 3, f_connect},
8018#endif
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 Moolenaar3b5f9292016-01-28 22:37:01 +01008032#ifdef FEAT_CHANNEL
8033 {"disconnect", 1, 1, f_disconnect},
8034#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008035 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008037 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 {"eventhandler", 0, 0, f_eventhandler},
8039 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008040 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008042#ifdef FEAT_FLOAT
8043 {"exp", 1, 1, f_exp},
8044#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008045 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008046 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008047 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8049 {"filereadable", 1, 1, f_filereadable},
8050 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008051 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008052 {"finddir", 1, 3, f_finddir},
8053 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008054#ifdef FEAT_FLOAT
8055 {"float2nr", 1, 1, f_float2nr},
8056 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008057 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008058#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008059 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 {"fnamemodify", 2, 2, f_fnamemodify},
8061 {"foldclosed", 1, 1, f_foldclosed},
8062 {"foldclosedend", 1, 1, f_foldclosedend},
8063 {"foldlevel", 1, 1, f_foldlevel},
8064 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008065 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008067 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008068 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008069 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008070 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008071 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 {"getchar", 0, 1, f_getchar},
8073 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008074 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 {"getcmdline", 0, 0, f_getcmdline},
8076 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008077 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008078 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008079 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008080 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008081 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008082 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 {"getfsize", 1, 1, f_getfsize},
8084 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008085 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008086 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008087 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008088 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008089 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008090 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008091 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008092 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008094 {"gettabvar", 2, 3, f_gettabvar},
8095 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 {"getwinposx", 0, 0, f_getwinposx},
8097 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008098 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008099 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008100 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008101 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008103 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008104 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008105 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8107 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8108 {"histadd", 2, 2, f_histadd},
8109 {"histdel", 1, 2, f_histdel},
8110 {"histget", 1, 2, f_histget},
8111 {"histnr", 1, 1, f_histnr},
8112 {"hlID", 1, 1, f_hlID},
8113 {"hlexists", 1, 1, f_hlexists},
8114 {"hostname", 0, 0, f_hostname},
8115 {"iconv", 3, 3, f_iconv},
8116 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008117 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008118 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008120 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {"inputrestore", 0, 0, f_inputrestore},
8122 {"inputsave", 0, 0, f_inputsave},
8123 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008124 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008125 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008127 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008128 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008129 {"join", 1, 2, f_join},
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008130 {"jsondecode", 1, 1, f_jsondecode},
8131 {"jsonencode", 1, 1, f_jsonencode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008132 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008134 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"libcall", 3, 3, f_libcall},
8136 {"libcallnr", 3, 3, f_libcallnr},
8137 {"line", 1, 1, f_line},
8138 {"line2byte", 1, 1, f_line2byte},
8139 {"lispindent", 1, 1, f_lispindent},
8140 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008141#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008142 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008143 {"log10", 1, 1, f_log10},
8144#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008145#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008146 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008147#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008148 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008149 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008150 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008151 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008152 {"matchadd", 2, 5, f_matchadd},
8153 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008154 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008155 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008156 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008157 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008158 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008159 {"max", 1, 1, f_max},
8160 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008161#ifdef vim_mkdir
8162 {"mkdir", 1, 3, f_mkdir},
8163#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008164 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008165#ifdef FEAT_MZSCHEME
8166 {"mzeval", 1, 1, f_mzeval},
8167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008169 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008170 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008171 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008172#ifdef FEAT_PERL
8173 {"perleval", 1, 1, f_perleval},
8174#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008175#ifdef FEAT_FLOAT
8176 {"pow", 2, 2, f_pow},
8177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008179 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008180 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008181#ifdef FEAT_PYTHON3
8182 {"py3eval", 1, 1, f_py3eval},
8183#endif
8184#ifdef FEAT_PYTHON
8185 {"pyeval", 1, 1, f_pyeval},
8186#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008187 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008188 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008189 {"reltime", 0, 2, f_reltime},
8190 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 {"remote_expr", 2, 3, f_remote_expr},
8192 {"remote_foreground", 1, 1, f_remote_foreground},
8193 {"remote_peek", 1, 2, f_remote_peek},
8194 {"remote_read", 1, 1, f_remote_read},
8195 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008196 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008198 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008200 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008201#ifdef FEAT_FLOAT
8202 {"round", 1, 1, f_round},
8203#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008204 {"screenattr", 2, 2, f_screenattr},
8205 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008206 {"screencol", 0, 0, f_screencol},
8207 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008208 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008209 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008210 {"searchpair", 3, 7, f_searchpair},
8211 {"searchpairpos", 3, 7, f_searchpairpos},
8212 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008213#ifdef FEAT_CHANNEL
8214 {"sendexpr", 2, 3, f_sendexpr},
8215 {"sendraw", 2, 3, f_sendraw},
8216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {"server2client", 2, 2, f_server2client},
8218 {"serverlist", 0, 0, f_serverlist},
8219 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008220 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 {"setcmdpos", 1, 1, f_setcmdpos},
8222 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008223 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008224 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008225 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008226 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008228 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008229 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008231#ifdef FEAT_CRYPT
8232 {"sha256", 1, 1, f_sha256},
8233#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008234 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008235 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008237#ifdef FEAT_FLOAT
8238 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008239 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008240#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008241 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008242 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008243 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008244 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008245 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008246#ifdef FEAT_FLOAT
8247 {"sqrt", 1, 1, f_sqrt},
8248 {"str2float", 1, 1, f_str2float},
8249#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008250 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008251 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008252 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253#ifdef HAVE_STRFTIME
8254 {"strftime", 1, 2, f_strftime},
8255#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008256 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008257 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 {"strlen", 1, 1, f_strlen},
8259 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008260 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008262 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008263 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 {"substitute", 4, 4, f_substitute},
8265 {"synID", 3, 3, f_synID},
8266 {"synIDattr", 2, 3, f_synIDattr},
8267 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008268 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008269 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008270 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008271 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008272 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008273 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008274 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008275 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008276 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008277#ifdef FEAT_FLOAT
8278 {"tan", 1, 1, f_tan},
8279 {"tanh", 1, 1, f_tanh},
8280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008282 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 {"tolower", 1, 1, f_tolower},
8284 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008285 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008286#ifdef FEAT_FLOAT
8287 {"trunc", 1, 1, f_trunc},
8288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008290 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008291 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008292 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008293 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 {"virtcol", 1, 1, f_virtcol},
8295 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008296 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 {"winbufnr", 1, 1, f_winbufnr},
8298 {"wincol", 0, 0, f_wincol},
8299 {"winheight", 1, 1, f_winheight},
8300 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008301 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008303 {"winrestview", 1, 1, f_winrestview},
8304 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008306 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008307 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008308 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309};
8310
8311#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8312
8313/*
8314 * Function given to ExpandGeneric() to obtain the list of internal
8315 * or user defined function names.
8316 */
8317 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008318get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319{
8320 static int intidx = -1;
8321 char_u *name;
8322
8323 if (idx == 0)
8324 intidx = -1;
8325 if (intidx < 0)
8326 {
8327 name = get_user_func_name(xp, idx);
8328 if (name != NULL)
8329 return name;
8330 }
8331 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8332 {
8333 STRCPY(IObuff, functions[intidx].f_name);
8334 STRCAT(IObuff, "(");
8335 if (functions[intidx].f_max_argc == 0)
8336 STRCAT(IObuff, ")");
8337 return IObuff;
8338 }
8339
8340 return NULL;
8341}
8342
8343/*
8344 * Function given to ExpandGeneric() to obtain the list of internal or
8345 * user defined variable or function names.
8346 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008348get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349{
8350 static int intidx = -1;
8351 char_u *name;
8352
8353 if (idx == 0)
8354 intidx = -1;
8355 if (intidx < 0)
8356 {
8357 name = get_function_name(xp, idx);
8358 if (name != NULL)
8359 return name;
8360 }
8361 return get_user_var_name(xp, ++intidx);
8362}
8363
8364#endif /* FEAT_CMDL_COMPL */
8365
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008366#if defined(EBCDIC) || defined(PROTO)
8367/*
8368 * Compare struct fst by function name.
8369 */
8370 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008371compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008372{
8373 struct fst *p1 = (struct fst *)s1;
8374 struct fst *p2 = (struct fst *)s2;
8375
8376 return STRCMP(p1->f_name, p2->f_name);
8377}
8378
8379/*
8380 * Sort the function table by function name.
8381 * The sorting of the table above is ASCII dependant.
8382 * On machines using EBCDIC we have to sort it.
8383 */
8384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008385sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008386{
8387 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8388
8389 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8390}
8391#endif
8392
8393
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394/*
8395 * Find internal function in table above.
8396 * Return index, or -1 if not found
8397 */
8398 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008399find_internal_func(
8400 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401{
8402 int first = 0;
8403 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8404 int cmp;
8405 int x;
8406
8407 /*
8408 * Find the function name in the table. Binary search.
8409 */
8410 while (first <= last)
8411 {
8412 x = first + ((unsigned)(last - first) >> 1);
8413 cmp = STRCMP(name, functions[x].f_name);
8414 if (cmp < 0)
8415 last = x - 1;
8416 else if (cmp > 0)
8417 first = x + 1;
8418 else
8419 return x;
8420 }
8421 return -1;
8422}
8423
8424/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008425 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8426 * name it contains, otherwise return "name".
8427 */
8428 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008429deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008430{
Bram Moolenaar33570922005-01-25 22:26:29 +00008431 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008432 int cc;
8433
8434 cc = name[*lenp];
8435 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008436 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008437 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008438 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008439 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008440 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008441 {
8442 *lenp = 0;
8443 return (char_u *)""; /* just in case */
8444 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008445 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008446 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008447 }
8448
8449 return name;
8450}
8451
8452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 * Allocate a variable for the result of a function.
8454 * Return OK or FAIL.
8455 */
8456 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008457get_func_tv(
8458 char_u *name, /* name of the function */
8459 int len, /* length of "name" */
8460 typval_T *rettv,
8461 char_u **arg, /* argument, pointing to the '(' */
8462 linenr_T firstline, /* first line of range */
8463 linenr_T lastline, /* last line of range */
8464 int *doesrange, /* return: function handled range */
8465 int evaluate,
8466 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467{
8468 char_u *argp;
8469 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008470 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 int argcount = 0; /* number of arguments found */
8472
8473 /*
8474 * Get the arguments.
8475 */
8476 argp = *arg;
8477 while (argcount < MAX_FUNC_ARGS)
8478 {
8479 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8480 if (*argp == ')' || *argp == ',' || *argp == NUL)
8481 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8483 {
8484 ret = FAIL;
8485 break;
8486 }
8487 ++argcount;
8488 if (*argp != ',')
8489 break;
8490 }
8491 if (*argp == ')')
8492 ++argp;
8493 else
8494 ret = FAIL;
8495
8496 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008497 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008498 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008500 {
8501 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008502 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008503 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008504 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506
8507 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008508 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509
8510 *arg = skipwhite(argp);
8511 return ret;
8512}
8513
8514
8515/*
8516 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008517 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008518 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008520 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008521call_func(
8522 char_u *funcname, /* name of the function */
8523 int len, /* length of "name" */
8524 typval_T *rettv, /* return value goes here */
8525 int argcount, /* number of "argvars" */
8526 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008527 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008528 linenr_T firstline, /* first line of range */
8529 linenr_T lastline, /* last line of range */
8530 int *doesrange, /* return: function handled range */
8531 int evaluate,
8532 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533{
8534 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535#define ERROR_UNKNOWN 0
8536#define ERROR_TOOMANY 1
8537#define ERROR_TOOFEW 2
8538#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008539#define ERROR_DICT 4
8540#define ERROR_NONE 5
8541#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 int error = ERROR_NONE;
8543 int i;
8544 int llen;
8545 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546#define FLEN_FIXED 40
8547 char_u fname_buf[FLEN_FIXED + 1];
8548 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008549 char_u *name;
8550
8551 /* Make a copy of the name, if it comes from a funcref variable it could
8552 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008553 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008554 if (name == NULL)
8555 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556
8557 /*
8558 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8559 * Change <SNR>123_name() to K_SNR 123_name().
8560 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8561 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 llen = eval_fname_script(name);
8563 if (llen > 0)
8564 {
8565 fname_buf[0] = K_SPECIAL;
8566 fname_buf[1] = KS_EXTRA;
8567 fname_buf[2] = (int)KE_SNR;
8568 i = 3;
8569 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8570 {
8571 if (current_SID <= 0)
8572 error = ERROR_SCRIPT;
8573 else
8574 {
8575 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8576 i = (int)STRLEN(fname_buf);
8577 }
8578 }
8579 if (i + STRLEN(name + llen) < FLEN_FIXED)
8580 {
8581 STRCPY(fname_buf + i, name + llen);
8582 fname = fname_buf;
8583 }
8584 else
8585 {
8586 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8587 if (fname == NULL)
8588 error = ERROR_OTHER;
8589 else
8590 {
8591 mch_memmove(fname, fname_buf, (size_t)i);
8592 STRCPY(fname + i, name + llen);
8593 }
8594 }
8595 }
8596 else
8597 fname = name;
8598
8599 *doesrange = FALSE;
8600
8601
8602 /* execute the function if no errors detected and executing */
8603 if (evaluate && error == ERROR_NONE)
8604 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008605 char_u *rfname = fname;
8606
8607 /* Ignore "g:" before a function name. */
8608 if (fname[0] == 'g' && fname[1] == ':')
8609 rfname = fname + 2;
8610
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008611 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8612 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 error = ERROR_UNKNOWN;
8614
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008615 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 {
8617 /*
8618 * User defined function.
8619 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008620 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008621
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008623 /* Trigger FuncUndefined event, may load the function. */
8624 if (fp == NULL
8625 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008626 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008627 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008629 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008630 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 }
8632#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008633 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008634 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008635 {
8636 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008637 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008638 }
8639
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 if (fp != NULL)
8641 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008642 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008644 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008646 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008648 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008649 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 else
8651 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008652 int did_save_redo = FALSE;
8653
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 /*
8655 * Call the user function.
8656 * Save and restore search patterns, script variables and
8657 * redo buffer.
8658 */
8659 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008660#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008661 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008662#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008663 {
8664 saveRedobuff();
8665 did_save_redo = TRUE;
8666 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008667 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008668 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008669 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008670 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8671 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8672 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008673 /* Function was unreferenced while being used, free it
8674 * now. */
8675 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008676 if (did_save_redo)
8677 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 restore_search_patterns();
8679 error = ERROR_NONE;
8680 }
8681 }
8682 }
8683 else
8684 {
8685 /*
8686 * Find the function name in the table, call its implementation.
8687 */
8688 i = find_internal_func(fname);
8689 if (i >= 0)
8690 {
8691 if (argcount < functions[i].f_min_argc)
8692 error = ERROR_TOOFEW;
8693 else if (argcount > functions[i].f_max_argc)
8694 error = ERROR_TOOMANY;
8695 else
8696 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008697 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008698 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 error = ERROR_NONE;
8700 }
8701 }
8702 }
8703 /*
8704 * The function call (or "FuncUndefined" autocommand sequence) might
8705 * have been aborted by an error, an interrupt, or an explicitly thrown
8706 * exception that has not been caught so far. This situation can be
8707 * tested for by calling aborting(). For an error in an internal
8708 * function or for the "E132" error in call_user_func(), however, the
8709 * throw point at which the "force_abort" flag (temporarily reset by
8710 * emsg()) is normally updated has not been reached yet. We need to
8711 * update that flag first to make aborting() reliable.
8712 */
8713 update_force_abort();
8714 }
8715 if (error == ERROR_NONE)
8716 ret = OK;
8717
8718 /*
8719 * Report an error unless the argument evaluation or function call has been
8720 * cancelled due to an aborting error, an interrupt, or an exception.
8721 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008722 if (!aborting())
8723 {
8724 switch (error)
8725 {
8726 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008727 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008728 break;
8729 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008730 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008731 break;
8732 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008733 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008734 name);
8735 break;
8736 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008737 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008738 name);
8739 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008740 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008741 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008742 name);
8743 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008744 }
8745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 if (fname != name && fname != fname_buf)
8748 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008749 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750
8751 return ret;
8752}
8753
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008754/*
8755 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008756 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008757 */
8758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008759emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008760{
8761 char_u *p;
8762
8763 if (*name == K_SPECIAL)
8764 p = concat_str((char_u *)"<SNR>", name + 3);
8765 else
8766 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008767 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008768 if (p != name)
8769 vim_free(p);
8770}
8771
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008772/*
8773 * Return TRUE for a non-zero Number and a non-empty String.
8774 */
8775 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008776non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008777{
8778 return ((argvars[0].v_type == VAR_NUMBER
8779 && argvars[0].vval.v_number != 0)
8780 || (argvars[0].v_type == VAR_STRING
8781 && argvars[0].vval.v_string != NULL
8782 && *argvars[0].vval.v_string != NUL));
8783}
8784
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785/*********************************************
8786 * Implementation of the built-in functions
8787 */
8788
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008789#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008790static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008791
8792/*
8793 * Get the float value of "argvars[0]" into "f".
8794 * Returns FAIL when the argument is not a Number or Float.
8795 */
8796 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008797get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008798{
8799 if (argvars[0].v_type == VAR_FLOAT)
8800 {
8801 *f = argvars[0].vval.v_float;
8802 return OK;
8803 }
8804 if (argvars[0].v_type == VAR_NUMBER)
8805 {
8806 *f = (float_T)argvars[0].vval.v_number;
8807 return OK;
8808 }
8809 EMSG(_("E808: Number or Float required"));
8810 return FAIL;
8811}
8812
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008813/*
8814 * "abs(expr)" function
8815 */
8816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008817f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008818{
8819 if (argvars[0].v_type == VAR_FLOAT)
8820 {
8821 rettv->v_type = VAR_FLOAT;
8822 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8823 }
8824 else
8825 {
8826 varnumber_T n;
8827 int error = FALSE;
8828
8829 n = get_tv_number_chk(&argvars[0], &error);
8830 if (error)
8831 rettv->vval.v_number = -1;
8832 else if (n > 0)
8833 rettv->vval.v_number = n;
8834 else
8835 rettv->vval.v_number = -n;
8836 }
8837}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008838
8839/*
8840 * "acos()" function
8841 */
8842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008843f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008844{
8845 float_T f;
8846
8847 rettv->v_type = VAR_FLOAT;
8848 if (get_float_arg(argvars, &f) == OK)
8849 rettv->vval.v_float = acos(f);
8850 else
8851 rettv->vval.v_float = 0.0;
8852}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008853#endif
8854
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008856 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 */
8858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008859f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860{
Bram Moolenaar33570922005-01-25 22:26:29 +00008861 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008863 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008864 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008866 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008867 && !tv_check_lock(l->lv_lock,
8868 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008869 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008870 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008871 }
8872 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008873 EMSG(_(e_listreq));
8874}
8875
8876/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008877 * "alloc_fail(id, countdown, repeat)" function
8878 */
8879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008880f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008881{
8882 if (argvars[0].v_type != VAR_NUMBER
8883 || argvars[0].vval.v_number <= 0
8884 || argvars[1].v_type != VAR_NUMBER
8885 || argvars[1].vval.v_number < 0
8886 || argvars[2].v_type != VAR_NUMBER)
8887 EMSG(_(e_invarg));
8888 else
8889 {
8890 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008891 if (alloc_fail_id >= aid_last)
8892 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008893 alloc_fail_countdown = argvars[1].vval.v_number;
8894 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008895 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008896 }
8897}
8898
8899/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008900 * "and(expr, expr)" function
8901 */
8902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008903f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008904{
8905 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8906 & get_tv_number_chk(&argvars[1], NULL);
8907}
8908
8909/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008910 * "append(lnum, string/list)" function
8911 */
8912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008913f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008914{
8915 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008916 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008917 list_T *l = NULL;
8918 listitem_T *li = NULL;
8919 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008920 long added = 0;
8921
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008922 /* When coming here from Insert mode, sync undo, so that this can be
8923 * undone separately from what was previously inserted. */
8924 if (u_sync_once == 2)
8925 {
8926 u_sync_once = 1; /* notify that u_sync() was called */
8927 u_sync(TRUE);
8928 }
8929
Bram Moolenaar0d660222005-01-07 21:51:51 +00008930 lnum = get_tv_lnum(argvars);
8931 if (lnum >= 0
8932 && lnum <= curbuf->b_ml.ml_line_count
8933 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008934 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008935 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008936 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008937 l = argvars[1].vval.v_list;
8938 if (l == NULL)
8939 return;
8940 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008941 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008942 for (;;)
8943 {
8944 if (l == NULL)
8945 tv = &argvars[1]; /* append a string */
8946 else if (li == NULL)
8947 break; /* end of list */
8948 else
8949 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008950 line = get_tv_string_chk(tv);
8951 if (line == NULL) /* type error */
8952 {
8953 rettv->vval.v_number = 1; /* Failed */
8954 break;
8955 }
8956 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008957 ++added;
8958 if (l == NULL)
8959 break;
8960 li = li->li_next;
8961 }
8962
8963 appended_lines_mark(lnum, added);
8964 if (curwin->w_cursor.lnum > lnum)
8965 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008967 else
8968 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969}
8970
8971/*
8972 * "argc()" function
8973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008975f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008977 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978}
8979
8980/*
8981 * "argidx()" function
8982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008984f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008986 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987}
8988
8989/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008990 * "arglistid()" function
8991 */
8992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008993f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008994{
8995 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008996
8997 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01008998 wp = find_tabwin(&argvars[0], &argvars[1]);
8999 if (wp != NULL)
9000 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009001}
9002
9003/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 * "argv(nr)" function
9005 */
9006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009007f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008{
9009 int idx;
9010
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009011 if (argvars[0].v_type != VAR_UNKNOWN)
9012 {
9013 idx = get_tv_number_chk(&argvars[0], NULL);
9014 if (idx >= 0 && idx < ARGCOUNT)
9015 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9016 else
9017 rettv->vval.v_string = NULL;
9018 rettv->v_type = VAR_STRING;
9019 }
9020 else if (rettv_list_alloc(rettv) == OK)
9021 for (idx = 0; idx < ARGCOUNT; ++idx)
9022 list_append_string(rettv->vval.v_list,
9023 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024}
9025
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009026static void prepare_assert_error(garray_T*gap);
9027static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9028static void assert_error(garray_T *gap);
9029static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009030
9031/*
9032 * Prepare "gap" for an assert error and add the sourcing position.
9033 */
9034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009035prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009036{
9037 char buf[NUMBUFLEN];
9038
9039 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009040 if (sourcing_name != NULL)
9041 {
9042 ga_concat(gap, sourcing_name);
9043 if (sourcing_lnum > 0)
9044 ga_concat(gap, (char_u *)" ");
9045 }
9046 if (sourcing_lnum > 0)
9047 {
9048 sprintf(buf, "line %ld", (long)sourcing_lnum);
9049 ga_concat(gap, (char_u *)buf);
9050 }
9051 if (sourcing_name != NULL || sourcing_lnum > 0)
9052 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009053}
9054
9055/*
9056 * Fill "gap" with information about an assert error.
9057 */
9058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009059fill_assert_error(
9060 garray_T *gap,
9061 typval_T *opt_msg_tv,
9062 char_u *exp_str,
9063 typval_T *exp_tv,
9064 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009065{
9066 char_u numbuf[NUMBUFLEN];
9067 char_u *tofree;
9068
9069 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9070 {
9071 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9072 vim_free(tofree);
9073 }
9074 else
9075 {
9076 ga_concat(gap, (char_u *)"Expected ");
9077 if (exp_str == NULL)
9078 {
9079 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9080 vim_free(tofree);
9081 }
9082 else
9083 ga_concat(gap, exp_str);
9084 ga_concat(gap, (char_u *)" but got ");
9085 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9086 vim_free(tofree);
9087 }
9088}
Bram Moolenaar43345542015-11-29 17:35:35 +01009089
9090/*
9091 * Add an assert error to v:errors.
9092 */
9093 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009094assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009095{
9096 struct vimvar *vp = &vimvars[VV_ERRORS];
9097
9098 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9099 /* Make sure v:errors is a list. */
9100 set_vim_var_list(VV_ERRORS, list_alloc());
9101 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9102}
9103
Bram Moolenaar43345542015-11-29 17:35:35 +01009104/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009105 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009106 */
9107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009108f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009109{
9110 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009111
9112 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9113 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009114 prepare_assert_error(&ga);
9115 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9116 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009117 ga_clear(&ga);
9118 }
9119}
9120
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009121/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009122 * "assert_exception(string[, msg])" function
9123 */
9124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009125f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009126{
9127 garray_T ga;
9128 char *error;
9129
9130 error = (char *)get_tv_string_chk(&argvars[0]);
9131 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9132 {
9133 prepare_assert_error(&ga);
9134 ga_concat(&ga, (char_u *)"v:exception is not set");
9135 assert_error(&ga);
9136 ga_clear(&ga);
9137 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009138 else if (error != NULL
9139 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009140 {
9141 prepare_assert_error(&ga);
9142 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9143 &vimvars[VV_EXCEPTION].vv_tv);
9144 assert_error(&ga);
9145 ga_clear(&ga);
9146 }
9147}
9148
9149/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009150 * "assert_fails(cmd [, error])" function
9151 */
9152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009153f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009154{
9155 char_u *cmd = get_tv_string_chk(&argvars[0]);
9156 garray_T ga;
9157
9158 called_emsg = FALSE;
9159 suppress_errthrow = TRUE;
9160 emsg_silent = TRUE;
9161 do_cmdline_cmd(cmd);
9162 if (!called_emsg)
9163 {
9164 prepare_assert_error(&ga);
9165 ga_concat(&ga, (char_u *)"command did not fail: ");
9166 ga_concat(&ga, cmd);
9167 assert_error(&ga);
9168 ga_clear(&ga);
9169 }
9170 else if (argvars[1].v_type != VAR_UNKNOWN)
9171 {
9172 char_u buf[NUMBUFLEN];
9173 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9174
9175 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9176 {
9177 prepare_assert_error(&ga);
9178 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9179 &vimvars[VV_ERRMSG].vv_tv);
9180 assert_error(&ga);
9181 ga_clear(&ga);
9182 }
9183 }
9184
9185 called_emsg = FALSE;
9186 suppress_errthrow = FALSE;
9187 emsg_silent = FALSE;
9188 emsg_on_display = FALSE;
9189 set_vim_var_string(VV_ERRMSG, NULL, 0);
9190}
9191
9192/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009193 * Common for assert_true() and assert_false().
9194 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009196assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009197{
9198 int error = FALSE;
9199 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009200
9201 if (argvars[0].v_type != VAR_NUMBER
9202 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9203 || error)
9204 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009205 prepare_assert_error(&ga);
9206 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009207 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009208 NULL, &argvars[0]);
9209 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009210 ga_clear(&ga);
9211 }
9212}
9213
9214/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009215 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009216 */
9217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009218f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009219{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009220 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009221}
9222
9223/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009224 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009225 */
9226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009227f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009228{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009229 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009230}
9231
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009232#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009233/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009234 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009235 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009237f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009238{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009239 float_T f;
9240
9241 rettv->v_type = VAR_FLOAT;
9242 if (get_float_arg(argvars, &f) == OK)
9243 rettv->vval.v_float = asin(f);
9244 else
9245 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009246}
9247
9248/*
9249 * "atan()" function
9250 */
9251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009252f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009253{
9254 float_T f;
9255
9256 rettv->v_type = VAR_FLOAT;
9257 if (get_float_arg(argvars, &f) == OK)
9258 rettv->vval.v_float = atan(f);
9259 else
9260 rettv->vval.v_float = 0.0;
9261}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009262
9263/*
9264 * "atan2()" function
9265 */
9266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009267f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009268{
9269 float_T fx, fy;
9270
9271 rettv->v_type = VAR_FLOAT;
9272 if (get_float_arg(argvars, &fx) == OK
9273 && get_float_arg(&argvars[1], &fy) == OK)
9274 rettv->vval.v_float = atan2(fx, fy);
9275 else
9276 rettv->vval.v_float = 0.0;
9277}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009278#endif
9279
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280/*
9281 * "browse(save, title, initdir, default)" function
9282 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009284f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285{
9286#ifdef FEAT_BROWSE
9287 int save;
9288 char_u *title;
9289 char_u *initdir;
9290 char_u *defname;
9291 char_u buf[NUMBUFLEN];
9292 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009293 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009295 save = get_tv_number_chk(&argvars[0], &error);
9296 title = get_tv_string_chk(&argvars[1]);
9297 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9298 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009300 if (error || title == NULL || initdir == NULL || defname == NULL)
9301 rettv->vval.v_string = NULL;
9302 else
9303 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009304 do_browse(save ? BROWSE_SAVE : 0,
9305 title, defname, NULL, initdir, NULL, curbuf);
9306#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009307 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009308#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009309 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009310}
9311
9312/*
9313 * "browsedir(title, initdir)" function
9314 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009316f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009317{
9318#ifdef FEAT_BROWSE
9319 char_u *title;
9320 char_u *initdir;
9321 char_u buf[NUMBUFLEN];
9322
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009323 title = get_tv_string_chk(&argvars[0]);
9324 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009325
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009326 if (title == NULL || initdir == NULL)
9327 rettv->vval.v_string = NULL;
9328 else
9329 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009330 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009332 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009334 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335}
9336
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009337static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009338
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339/*
9340 * Find a buffer by number or exact name.
9341 */
9342 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009343find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344{
9345 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009347 if (avar->v_type == VAR_NUMBER)
9348 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009349 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009351 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009352 if (buf == NULL)
9353 {
9354 /* No full path name match, try a match with a URL or a "nofile"
9355 * buffer, these don't use the full path. */
9356 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9357 if (buf->b_fname != NULL
9358 && (path_with_url(buf->b_fname)
9359#ifdef FEAT_QUICKFIX
9360 || bt_nofile(buf)
9361#endif
9362 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009363 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009364 break;
9365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 }
9367 return buf;
9368}
9369
9370/*
9371 * "bufexists(expr)" function
9372 */
9373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009374f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009376 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377}
9378
9379/*
9380 * "buflisted(expr)" function
9381 */
9382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009383f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384{
9385 buf_T *buf;
9386
9387 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009388 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389}
9390
9391/*
9392 * "bufloaded(expr)" function
9393 */
9394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009395f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396{
9397 buf_T *buf;
9398
9399 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009400 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401}
9402
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009403static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009404
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405/*
9406 * Get buffer by number or pattern.
9407 */
9408 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009409get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009411 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 int save_magic;
9413 char_u *save_cpo;
9414 buf_T *buf;
9415
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009416 if (tv->v_type == VAR_NUMBER)
9417 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009418 if (tv->v_type != VAR_STRING)
9419 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 if (name == NULL || *name == NUL)
9421 return curbuf;
9422 if (name[0] == '$' && name[1] == NUL)
9423 return lastbuf;
9424
9425 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9426 save_magic = p_magic;
9427 p_magic = TRUE;
9428 save_cpo = p_cpo;
9429 p_cpo = (char_u *)"";
9430
9431 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009432 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433
9434 p_magic = save_magic;
9435 p_cpo = save_cpo;
9436
9437 /* If not found, try expanding the name, like done for bufexists(). */
9438 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009439 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440
9441 return buf;
9442}
9443
9444/*
9445 * "bufname(expr)" function
9446 */
9447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009448f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449{
9450 buf_T *buf;
9451
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009452 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009454 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009455 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009457 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009459 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 --emsg_off;
9461}
9462
9463/*
9464 * "bufnr(expr)" function
9465 */
9466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009467f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468{
9469 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009470 int error = FALSE;
9471 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009473 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009475 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009476 --emsg_off;
9477
9478 /* If the buffer isn't found and the second argument is not zero create a
9479 * new buffer. */
9480 if (buf == NULL
9481 && argvars[1].v_type != VAR_UNKNOWN
9482 && get_tv_number_chk(&argvars[1], &error) != 0
9483 && !error
9484 && (name = get_tv_string_chk(&argvars[0])) != NULL
9485 && !error)
9486 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9487
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009489 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009491 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492}
9493
9494/*
9495 * "bufwinnr(nr)" function
9496 */
9497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009498f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499{
9500#ifdef FEAT_WINDOWS
9501 win_T *wp;
9502 int winnr = 0;
9503#endif
9504 buf_T *buf;
9505
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009506 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009508 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509#ifdef FEAT_WINDOWS
9510 for (wp = firstwin; wp; wp = wp->w_next)
9511 {
9512 ++winnr;
9513 if (wp->w_buffer == buf)
9514 break;
9515 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009516 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009518 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519#endif
9520 --emsg_off;
9521}
9522
9523/*
9524 * "byte2line(byte)" function
9525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009527f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528{
9529#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009530 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531#else
9532 long boff = 0;
9533
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009534 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009536 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009538 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 (linenr_T)0, &boff);
9540#endif
9541}
9542
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009544byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009545{
9546#ifdef FEAT_MBYTE
9547 char_u *t;
9548#endif
9549 char_u *str;
9550 long idx;
9551
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009552 str = get_tv_string_chk(&argvars[0]);
9553 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009554 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009555 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009556 return;
9557
9558#ifdef FEAT_MBYTE
9559 t = str;
9560 for ( ; idx > 0; idx--)
9561 {
9562 if (*t == NUL) /* EOL reached */
9563 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009564 if (enc_utf8 && comp)
9565 t += utf_ptr2len(t);
9566 else
9567 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009568 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009569 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009570#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009571 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009572 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009573#endif
9574}
9575
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009576/*
9577 * "byteidx()" function
9578 */
9579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009580f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009581{
9582 byteidx(argvars, rettv, FALSE);
9583}
9584
9585/*
9586 * "byteidxcomp()" function
9587 */
9588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009589f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009590{
9591 byteidx(argvars, rettv, TRUE);
9592}
9593
Bram Moolenaardb913952012-06-29 12:54:53 +02009594 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009595func_call(
9596 char_u *name,
9597 typval_T *args,
9598 dict_T *selfdict,
9599 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009600{
9601 listitem_T *item;
9602 typval_T argv[MAX_FUNC_ARGS + 1];
9603 int argc = 0;
9604 int dummy;
9605 int r = 0;
9606
9607 for (item = args->vval.v_list->lv_first; item != NULL;
9608 item = item->li_next)
9609 {
9610 if (argc == MAX_FUNC_ARGS)
9611 {
9612 EMSG(_("E699: Too many arguments"));
9613 break;
9614 }
9615 /* Make a copy of each argument. This is needed to be able to set
9616 * v_lock to VAR_FIXED in the copy without changing the original list.
9617 */
9618 copy_tv(&item->li_tv, &argv[argc++]);
9619 }
9620
9621 if (item == NULL)
9622 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9623 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9624 &dummy, TRUE, selfdict);
9625
9626 /* Free the arguments. */
9627 while (argc > 0)
9628 clear_tv(&argv[--argc]);
9629
9630 return r;
9631}
9632
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009633/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009634 * "call(func, arglist)" function
9635 */
9636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009637f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009638{
9639 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009640 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009641
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009642 if (argvars[1].v_type != VAR_LIST)
9643 {
9644 EMSG(_(e_listreq));
9645 return;
9646 }
9647 if (argvars[1].vval.v_list == NULL)
9648 return;
9649
9650 if (argvars[0].v_type == VAR_FUNC)
9651 func = argvars[0].vval.v_string;
9652 else
9653 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009654 if (*func == NUL)
9655 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009656
Bram Moolenaare9a41262005-01-15 22:18:47 +00009657 if (argvars[2].v_type != VAR_UNKNOWN)
9658 {
9659 if (argvars[2].v_type != VAR_DICT)
9660 {
9661 EMSG(_(e_dictreq));
9662 return;
9663 }
9664 selfdict = argvars[2].vval.v_dict;
9665 }
9666
Bram Moolenaardb913952012-06-29 12:54:53 +02009667 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009668}
9669
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009670#ifdef FEAT_FLOAT
9671/*
9672 * "ceil({float})" function
9673 */
9674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009675f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009676{
9677 float_T f;
9678
9679 rettv->v_type = VAR_FLOAT;
9680 if (get_float_arg(argvars, &f) == OK)
9681 rettv->vval.v_float = ceil(f);
9682 else
9683 rettv->vval.v_float = 0.0;
9684}
9685#endif
9686
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009687/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009688 * "changenr()" function
9689 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009691f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009692{
9693 rettv->vval.v_number = curbuf->b_u_seq_cur;
9694}
9695
9696/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 * "char2nr(string)" function
9698 */
9699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009700f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701{
9702#ifdef FEAT_MBYTE
9703 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009704 {
9705 int utf8 = 0;
9706
9707 if (argvars[1].v_type != VAR_UNKNOWN)
9708 utf8 = get_tv_number_chk(&argvars[1], NULL);
9709
9710 if (utf8)
9711 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9712 else
9713 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 else
9716#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009717 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718}
9719
9720/*
9721 * "cindent(lnum)" function
9722 */
9723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009724f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725{
9726#ifdef FEAT_CINDENT
9727 pos_T pos;
9728 linenr_T lnum;
9729
9730 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009731 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9733 {
9734 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009735 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 curwin->w_cursor = pos;
9737 }
9738 else
9739#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009740 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741}
9742
9743/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009744 * "clearmatches()" function
9745 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009747f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009748{
9749#ifdef FEAT_SEARCH_EXTRA
9750 clear_matches(curwin);
9751#endif
9752}
9753
9754/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 * "col(string)" function
9756 */
9757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009758f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759{
9760 colnr_T col = 0;
9761 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009762 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009764 fp = var2fpos(&argvars[0], FALSE, &fnum);
9765 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 {
9767 if (fp->col == MAXCOL)
9768 {
9769 /* '> can be MAXCOL, get the length of the line then */
9770 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009771 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 else
9773 col = MAXCOL;
9774 }
9775 else
9776 {
9777 col = fp->col + 1;
9778#ifdef FEAT_VIRTUALEDIT
9779 /* col(".") when the cursor is on the NUL at the end of the line
9780 * because of "coladd" can be seen as an extra column. */
9781 if (virtual_active() && fp == &curwin->w_cursor)
9782 {
9783 char_u *p = ml_get_cursor();
9784
9785 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9786 curwin->w_virtcol - curwin->w_cursor.coladd))
9787 {
9788# ifdef FEAT_MBYTE
9789 int l;
9790
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009791 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 col += l;
9793# else
9794 if (*p != NUL && p[1] == NUL)
9795 ++col;
9796# endif
9797 }
9798 }
9799#endif
9800 }
9801 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009802 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803}
9804
Bram Moolenaar572cb562005-08-05 21:35:02 +00009805#if defined(FEAT_INS_EXPAND)
9806/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009807 * "complete()" function
9808 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009810f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +00009811{
9812 int startcol;
9813
9814 if ((State & INSERT) == 0)
9815 {
9816 EMSG(_("E785: complete() can only be used in Insert mode"));
9817 return;
9818 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009819
9820 /* Check for undo allowed here, because if something was already inserted
9821 * the line was already saved for undo and this check isn't done. */
9822 if (!undo_allowed())
9823 return;
9824
Bram Moolenaarade00832006-03-10 21:46:58 +00009825 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9826 {
9827 EMSG(_(e_invarg));
9828 return;
9829 }
9830
9831 startcol = get_tv_number_chk(&argvars[0], NULL);
9832 if (startcol <= 0)
9833 return;
9834
9835 set_completion(startcol - 1, argvars[1].vval.v_list);
9836}
9837
9838/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009839 * "complete_add()" function
9840 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009842f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +00009843{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009844 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009845}
9846
9847/*
9848 * "complete_check()" function
9849 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009851f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +00009852{
9853 int saved = RedrawingDisabled;
9854
9855 RedrawingDisabled = 0;
9856 ins_compl_check_keys(0);
9857 rettv->vval.v_number = compl_interrupted;
9858 RedrawingDisabled = saved;
9859}
9860#endif
9861
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862/*
9863 * "confirm(message, buttons[, default [, type]])" function
9864 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009866f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867{
9868#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9869 char_u *message;
9870 char_u *buttons = NULL;
9871 char_u buf[NUMBUFLEN];
9872 char_u buf2[NUMBUFLEN];
9873 int def = 1;
9874 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009875 char_u *typestr;
9876 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009878 message = get_tv_string_chk(&argvars[0]);
9879 if (message == NULL)
9880 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009881 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009883 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9884 if (buttons == NULL)
9885 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009886 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009888 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009889 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009891 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9892 if (typestr == NULL)
9893 error = TRUE;
9894 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009896 switch (TOUPPER_ASC(*typestr))
9897 {
9898 case 'E': type = VIM_ERROR; break;
9899 case 'Q': type = VIM_QUESTION; break;
9900 case 'I': type = VIM_INFO; break;
9901 case 'W': type = VIM_WARNING; break;
9902 case 'G': type = VIM_GENERIC; break;
9903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 }
9905 }
9906 }
9907 }
9908
9909 if (buttons == NULL || *buttons == NUL)
9910 buttons = (char_u *)_("&Ok");
9911
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009912 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009913 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009914 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915#endif
9916}
9917
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009918/*
9919 * "copy()" function
9920 */
9921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009922f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009923{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009924 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009925}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009927#ifdef FEAT_FLOAT
9928/*
9929 * "cos()" function
9930 */
9931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009932f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009933{
9934 float_T f;
9935
9936 rettv->v_type = VAR_FLOAT;
9937 if (get_float_arg(argvars, &f) == OK)
9938 rettv->vval.v_float = cos(f);
9939 else
9940 rettv->vval.v_float = 0.0;
9941}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009942
9943/*
9944 * "cosh()" function
9945 */
9946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009947f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009948{
9949 float_T f;
9950
9951 rettv->v_type = VAR_FLOAT;
9952 if (get_float_arg(argvars, &f) == OK)
9953 rettv->vval.v_float = cosh(f);
9954 else
9955 rettv->vval.v_float = 0.0;
9956}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009957#endif
9958
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009960 * "count()" function
9961 */
9962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009963f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009964{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009965 long n = 0;
9966 int ic = FALSE;
9967
Bram Moolenaare9a41262005-01-15 22:18:47 +00009968 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009969 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009970 listitem_T *li;
9971 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009972 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009973
Bram Moolenaare9a41262005-01-15 22:18:47 +00009974 if ((l = argvars[0].vval.v_list) != NULL)
9975 {
9976 li = l->lv_first;
9977 if (argvars[2].v_type != VAR_UNKNOWN)
9978 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009979 int error = FALSE;
9980
9981 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009982 if (argvars[3].v_type != VAR_UNKNOWN)
9983 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009984 idx = get_tv_number_chk(&argvars[3], &error);
9985 if (!error)
9986 {
9987 li = list_find(l, idx);
9988 if (li == NULL)
9989 EMSGN(_(e_listidx), idx);
9990 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009991 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009992 if (error)
9993 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009994 }
9995
9996 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009997 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009998 ++n;
9999 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010000 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010001 else if (argvars[0].v_type == VAR_DICT)
10002 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010003 int todo;
10004 dict_T *d;
10005 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010006
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010007 if ((d = argvars[0].vval.v_dict) != NULL)
10008 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010009 int error = FALSE;
10010
Bram Moolenaare9a41262005-01-15 22:18:47 +000010011 if (argvars[2].v_type != VAR_UNKNOWN)
10012 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010013 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010014 if (argvars[3].v_type != VAR_UNKNOWN)
10015 EMSG(_(e_invarg));
10016 }
10017
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010018 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010019 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010020 {
10021 if (!HASHITEM_EMPTY(hi))
10022 {
10023 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010024 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010025 ++n;
10026 }
10027 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010028 }
10029 }
10030 else
10031 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010032 rettv->vval.v_number = n;
10033}
10034
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010010035#ifdef FEAT_CHANNEL
10036/*
10037 * Get a callback from "arg". It can be a Funcref or a function name.
10038 * When "arg" is zero return an empty string.
10039 * Return NULL for an invalid argument.
10040 */
10041 static char_u *
10042get_callback(typval_T *arg)
10043{
10044 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
10045 return arg->vval.v_string;
10046 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
10047 return (char_u *)"";
10048 EMSG(_("E999: Invalid callback argument"));
10049 return NULL;
10050}
10051
10052/*
10053 * "connect()" function
10054 */
10055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010056f_connect(typval_T *argvars, typval_T *rettv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010010057{
10058 char_u *address;
10059 char_u *mode;
10060 char_u *callback = NULL;
10061 char_u buf1[NUMBUFLEN];
10062 char_u *p;
10063 int port;
10064 int json_mode = FALSE;
10065
10066 address = get_tv_string(&argvars[0]);
10067 mode = get_tv_string_buf(&argvars[1], buf1);
10068 if (argvars[2].v_type != VAR_UNKNOWN)
10069 {
10070 callback = get_callback(&argvars[2]);
10071 if (callback == NULL)
10072 return;
10073 }
10074
10075 /* parse address */
10076 p = vim_strchr(address, ':');
10077 if (p == NULL)
10078 {
10079 EMSG2(_(e_invarg2), address);
10080 return;
10081 }
10082 *p++ = NUL;
10083 port = atoi((char *)p);
10084 if (*address == NUL || port <= 0)
10085 {
10086 p[-1] = ':';
10087 EMSG2(_(e_invarg2), address);
10088 return;
10089 }
10090
10091 /* parse mode */
10092 if (STRCMP(mode, "json") == 0)
10093 json_mode = TRUE;
10094 else if (STRCMP(mode, "raw") != 0)
10095 {
10096 EMSG2(_(e_invarg2), mode);
10097 return;
10098 }
10099
10100 rettv->vval.v_number = channel_open((char *)address, port, NULL);
10101 if (rettv->vval.v_number >= 0)
10102 {
10103 channel_set_json_mode(rettv->vval.v_number, json_mode);
10104 if (callback != NULL && *callback != NUL)
10105 channel_set_callback(rettv->vval.v_number, callback);
10106 }
10107}
10108#endif
10109
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010110/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10112 *
10113 * Checks the existence of a cscope connection.
10114 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010116f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117{
10118#ifdef FEAT_CSCOPE
10119 int num = 0;
10120 char_u *dbpath = NULL;
10121 char_u *prepend = NULL;
10122 char_u buf[NUMBUFLEN];
10123
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010124 if (argvars[0].v_type != VAR_UNKNOWN
10125 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010127 num = (int)get_tv_number(&argvars[0]);
10128 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010129 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010130 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 }
10132
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010133 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134#endif
10135}
10136
10137/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010138 * "cursor(lnum, col)" function, or
10139 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010141 * Moves the cursor to the specified line and column.
10142 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010145f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146{
10147 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010148#ifdef FEAT_VIRTUALEDIT
10149 long coladd = 0;
10150#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010151 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010153 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010154 if (argvars[1].v_type == VAR_UNKNOWN)
10155 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010156 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010157 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010158
Bram Moolenaar493c1782014-05-28 14:34:46 +020010159 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010160 {
10161 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010162 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010163 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010164 line = pos.lnum;
10165 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010166#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010167 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010168#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010169 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010170 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010171 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010172 set_curswant = FALSE;
10173 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010174 }
10175 else
10176 {
10177 line = get_tv_lnum(argvars);
10178 col = get_tv_number_chk(&argvars[1], NULL);
10179#ifdef FEAT_VIRTUALEDIT
10180 if (argvars[2].v_type != VAR_UNKNOWN)
10181 coladd = get_tv_number_chk(&argvars[2], NULL);
10182#endif
10183 }
10184 if (line < 0 || col < 0
10185#ifdef FEAT_VIRTUALEDIT
10186 || coladd < 0
10187#endif
10188 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010189 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190 if (line > 0)
10191 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192 if (col > 0)
10193 curwin->w_cursor.col = col - 1;
10194#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010195 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196#endif
10197
10198 /* Make sure the cursor is in a valid position. */
10199 check_cursor();
10200#ifdef FEAT_MBYTE
10201 /* Correct cursor for multi-byte character. */
10202 if (has_mbyte)
10203 mb_adjust_cursor();
10204#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010205
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010206 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010207 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208}
10209
10210/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010211 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 */
10213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010214f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010216 int noref = 0;
10217
10218 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010219 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010220 if (noref < 0 || noref > 1)
10221 EMSG(_(e_invarg));
10222 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010223 {
10224 current_copyID += COPYID_INC;
10225 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227}
10228
10229/*
10230 * "delete()" function
10231 */
10232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010233f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010235 char_u nbuf[NUMBUFLEN];
10236 char_u *name;
10237 char_u *flags;
10238
10239 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010241 return;
10242
10243 name = get_tv_string(&argvars[0]);
10244 if (name == NULL || *name == NUL)
10245 {
10246 EMSG(_(e_invarg));
10247 return;
10248 }
10249
10250 if (argvars[1].v_type != VAR_UNKNOWN)
10251 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010253 flags = (char_u *)"";
10254
10255 if (*flags == NUL)
10256 /* delete a file */
10257 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10258 else if (STRCMP(flags, "d") == 0)
10259 /* delete an empty directory */
10260 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10261 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010262 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010263 rettv->vval.v_number = delete_recursive(name);
10264 else
10265 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266}
10267
10268/*
10269 * "did_filetype()" function
10270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010272f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273{
10274#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010275 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276#endif
10277}
10278
10279/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010280 * "diff_filler()" function
10281 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010283f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010284{
10285#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010286 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010287#endif
10288}
10289
10290/*
10291 * "diff_hlID()" function
10292 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010294f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010295{
10296#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010297 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010298 static linenr_T prev_lnum = 0;
10299 static int changedtick = 0;
10300 static int fnum = 0;
10301 static int change_start = 0;
10302 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010303 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010304 int filler_lines;
10305 int col;
10306
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010307 if (lnum < 0) /* ignore type error in {lnum} arg */
10308 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010309 if (lnum != prev_lnum
10310 || changedtick != curbuf->b_changedtick
10311 || fnum != curbuf->b_fnum)
10312 {
10313 /* New line, buffer, change: need to get the values. */
10314 filler_lines = diff_check(curwin, lnum);
10315 if (filler_lines < 0)
10316 {
10317 if (filler_lines == -1)
10318 {
10319 change_start = MAXCOL;
10320 change_end = -1;
10321 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10322 hlID = HLF_ADD; /* added line */
10323 else
10324 hlID = HLF_CHD; /* changed line */
10325 }
10326 else
10327 hlID = HLF_ADD; /* added line */
10328 }
10329 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010330 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010331 prev_lnum = lnum;
10332 changedtick = curbuf->b_changedtick;
10333 fnum = curbuf->b_fnum;
10334 }
10335
10336 if (hlID == HLF_CHD || hlID == HLF_TXD)
10337 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010338 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010339 if (col >= change_start && col <= change_end)
10340 hlID = HLF_TXD; /* changed text */
10341 else
10342 hlID = HLF_CHD; /* changed line */
10343 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010344 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010345#endif
10346}
10347
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010010348#ifdef FEAT_CHANNEL
10349/*
10350 * Get the channel index from the handle argument.
10351 * Returns -1 if the handle is invalid or the channel is closed.
10352 */
10353 static int
10354get_channel_arg(typval_T *tv)
10355{
10356 int ch_idx;
10357
10358 if (tv->v_type != VAR_NUMBER)
10359 {
10360 EMSG2(_(e_invarg2), get_tv_string(tv));
10361 return -1;
10362 }
10363 ch_idx = tv->vval.v_number;
10364
10365 if (!channel_is_open(ch_idx))
10366 {
10367 EMSGN(_("E999: not an open channel"), ch_idx);
10368 return -1;
10369 }
10370 return ch_idx;
10371}
10372
10373/*
10374 * "disconnect()" function
10375 */
10376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010377f_disconnect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010010378{
10379 int ch_idx = get_channel_arg(&argvars[0]);
10380
10381 if (ch_idx >= 0)
10382 channel_close(ch_idx);
10383}
10384#endif
10385
Bram Moolenaar47136d72004-10-12 20:02:24 +000010386/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010387 * "empty({expr})" function
10388 */
10389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010390f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010391{
10392 int n;
10393
10394 switch (argvars[0].v_type)
10395 {
10396 case VAR_STRING:
10397 case VAR_FUNC:
10398 n = argvars[0].vval.v_string == NULL
10399 || *argvars[0].vval.v_string == NUL;
10400 break;
10401 case VAR_NUMBER:
10402 n = argvars[0].vval.v_number == 0;
10403 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010404#ifdef FEAT_FLOAT
10405 case VAR_FLOAT:
10406 n = argvars[0].vval.v_float == 0.0;
10407 break;
10408#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010409 case VAR_LIST:
10410 n = argvars[0].vval.v_list == NULL
10411 || argvars[0].vval.v_list->lv_first == NULL;
10412 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010413 case VAR_DICT:
10414 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010415 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010416 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010417 case VAR_SPECIAL:
10418 n = argvars[0].vval.v_number != VVAL_TRUE;
10419 break;
10420
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010421 default:
10422 EMSG2(_(e_intern2), "f_empty()");
10423 n = 0;
10424 }
10425
10426 rettv->vval.v_number = n;
10427}
10428
10429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 * "escape({string}, {chars})" function
10431 */
10432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010433f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434{
10435 char_u buf[NUMBUFLEN];
10436
Bram Moolenaar758711c2005-02-02 23:11:38 +000010437 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10438 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010439 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440}
10441
10442/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010443 * "eval()" function
10444 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010446f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010447{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010448 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010449
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010450 s = get_tv_string_chk(&argvars[0]);
10451 if (s != NULL)
10452 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010453
Bram Moolenaar615b9972015-01-14 17:15:05 +010010454 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010455 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10456 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010457 if (p != NULL && !aborting())
10458 EMSG2(_(e_invexpr2), p);
10459 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010460 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010461 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010462 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010463 else if (*s != NUL)
10464 EMSG(_(e_trailing));
10465}
10466
10467/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 * "eventhandler()" function
10469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010471f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010473 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474}
10475
10476/*
10477 * "executable()" function
10478 */
10479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010480f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010482 char_u *name = get_tv_string(&argvars[0]);
10483
10484 /* Check in $PATH and also check directly if there is a directory name. */
10485 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10486 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010487}
10488
10489/*
10490 * "exepath()" function
10491 */
10492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010493f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010494{
10495 char_u *p = NULL;
10496
Bram Moolenaarb5971142015-03-21 17:32:19 +010010497 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010498 rettv->v_type = VAR_STRING;
10499 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500}
10501
10502/*
10503 * "exists()" function
10504 */
10505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010506f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507{
10508 char_u *p;
10509 char_u *name;
10510 int n = FALSE;
10511 int len = 0;
10512
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010513 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514 if (*p == '$') /* environment variable */
10515 {
10516 /* first try "normal" environment variables (fast) */
10517 if (mch_getenv(p + 1) != NULL)
10518 n = TRUE;
10519 else
10520 {
10521 /* try expanding things like $VIM and ${HOME} */
10522 p = expand_env_save(p);
10523 if (p != NULL && *p != '$')
10524 n = TRUE;
10525 vim_free(p);
10526 }
10527 }
10528 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010529 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010530 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010531 if (*skipwhite(p) != NUL)
10532 n = FALSE; /* trailing garbage */
10533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534 else if (*p == '*') /* internal or user defined function */
10535 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010536 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 }
10538 else if (*p == ':')
10539 {
10540 n = cmd_exists(p + 1);
10541 }
10542 else if (*p == '#')
10543 {
10544#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010545 if (p[1] == '#')
10546 n = autocmd_supported(p + 2);
10547 else
10548 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549#endif
10550 }
10551 else /* internal variable */
10552 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010553 char_u *tofree;
10554 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010556 /* get_name_len() takes care of expanding curly braces */
10557 name = p;
10558 len = get_name_len(&p, &tofree, TRUE, FALSE);
10559 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010561 if (tofree != NULL)
10562 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010563 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010564 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010566 /* handle d.key, l[idx], f(expr) */
10567 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10568 if (n)
10569 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 }
10571 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010572 if (*p != NUL)
10573 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010575 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576 }
10577
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010578 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579}
10580
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010581#ifdef FEAT_FLOAT
10582/*
10583 * "exp()" function
10584 */
10585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010586f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010587{
10588 float_T f;
10589
10590 rettv->v_type = VAR_FLOAT;
10591 if (get_float_arg(argvars, &f) == OK)
10592 rettv->vval.v_float = exp(f);
10593 else
10594 rettv->vval.v_float = 0.0;
10595}
10596#endif
10597
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598/*
10599 * "expand()" function
10600 */
10601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010602f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603{
10604 char_u *s;
10605 int len;
10606 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010607 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010609 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010610 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010612 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010613 if (argvars[1].v_type != VAR_UNKNOWN
10614 && argvars[2].v_type != VAR_UNKNOWN
10615 && get_tv_number_chk(&argvars[2], &error)
10616 && !error)
10617 {
10618 rettv->v_type = VAR_LIST;
10619 rettv->vval.v_list = NULL;
10620 }
10621
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010622 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623 if (*s == '%' || *s == '#' || *s == '<')
10624 {
10625 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010626 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010628 if (rettv->v_type == VAR_LIST)
10629 {
10630 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10631 list_append_string(rettv->vval.v_list, result, -1);
10632 else
10633 vim_free(result);
10634 }
10635 else
10636 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637 }
10638 else
10639 {
10640 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010641 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010642 if (argvars[1].v_type != VAR_UNKNOWN
10643 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010644 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010645 if (!error)
10646 {
10647 ExpandInit(&xpc);
10648 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010649 if (p_wic)
10650 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010651 if (rettv->v_type == VAR_STRING)
10652 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10653 options, WILD_ALL);
10654 else if (rettv_list_alloc(rettv) != FAIL)
10655 {
10656 int i;
10657
10658 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10659 for (i = 0; i < xpc.xp_numfiles; i++)
10660 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10661 ExpandCleanup(&xpc);
10662 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010663 }
10664 else
10665 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 }
10667}
10668
10669/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010670 * Go over all entries in "d2" and add them to "d1".
10671 * When "action" is "error" then a duplicate key is an error.
10672 * When "action" is "force" then a duplicate key is overwritten.
10673 * Otherwise duplicate keys are ignored ("action" is "keep").
10674 */
10675 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010676dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020010677{
10678 dictitem_T *di1;
10679 hashitem_T *hi2;
10680 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010681 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010682
10683 todo = (int)d2->dv_hashtab.ht_used;
10684 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10685 {
10686 if (!HASHITEM_EMPTY(hi2))
10687 {
10688 --todo;
10689 di1 = dict_find(d1, hi2->hi_key, -1);
10690 if (d1->dv_scope != 0)
10691 {
10692 /* Disallow replacing a builtin function in l: and g:.
10693 * Check the key to be valid when adding to any
10694 * scope. */
10695 if (d1->dv_scope == VAR_DEF_SCOPE
10696 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10697 && var_check_func_name(hi2->hi_key,
10698 di1 == NULL))
10699 break;
10700 if (!valid_varname(hi2->hi_key))
10701 break;
10702 }
10703 if (di1 == NULL)
10704 {
10705 di1 = dictitem_copy(HI2DI(hi2));
10706 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10707 dictitem_free(di1);
10708 }
10709 else if (*action == 'e')
10710 {
10711 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10712 break;
10713 }
10714 else if (*action == 'f' && HI2DI(hi2) != di1)
10715 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010716 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10717 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010718 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010719 clear_tv(&di1->di_tv);
10720 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10721 }
10722 }
10723 }
10724}
10725
10726/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010727 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010728 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010729 */
10730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010731f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010732{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010733 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010734
Bram Moolenaare9a41262005-01-15 22:18:47 +000010735 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010736 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010737 list_T *l1, *l2;
10738 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010739 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010740 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010741
Bram Moolenaare9a41262005-01-15 22:18:47 +000010742 l1 = argvars[0].vval.v_list;
10743 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010744 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010745 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010746 {
10747 if (argvars[2].v_type != VAR_UNKNOWN)
10748 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010749 before = get_tv_number_chk(&argvars[2], &error);
10750 if (error)
10751 return; /* type error; errmsg already given */
10752
Bram Moolenaar758711c2005-02-02 23:11:38 +000010753 if (before == l1->lv_len)
10754 item = NULL;
10755 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010756 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010757 item = list_find(l1, before);
10758 if (item == NULL)
10759 {
10760 EMSGN(_(e_listidx), before);
10761 return;
10762 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010763 }
10764 }
10765 else
10766 item = NULL;
10767 list_extend(l1, l2, item);
10768
Bram Moolenaare9a41262005-01-15 22:18:47 +000010769 copy_tv(&argvars[0], rettv);
10770 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010771 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010772 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10773 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010774 dict_T *d1, *d2;
10775 char_u *action;
10776 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010777
10778 d1 = argvars[0].vval.v_dict;
10779 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010780 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010781 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010782 {
10783 /* Check the third argument. */
10784 if (argvars[2].v_type != VAR_UNKNOWN)
10785 {
10786 static char *(av[]) = {"keep", "force", "error"};
10787
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010788 action = get_tv_string_chk(&argvars[2]);
10789 if (action == NULL)
10790 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010791 for (i = 0; i < 3; ++i)
10792 if (STRCMP(action, av[i]) == 0)
10793 break;
10794 if (i == 3)
10795 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010796 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010797 return;
10798 }
10799 }
10800 else
10801 action = (char_u *)"force";
10802
Bram Moolenaara9922d62013-05-30 13:01:18 +020010803 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010804
Bram Moolenaare9a41262005-01-15 22:18:47 +000010805 copy_tv(&argvars[0], rettv);
10806 }
10807 }
10808 else
10809 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010810}
10811
10812/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010813 * "feedkeys()" function
10814 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010816f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010817{
10818 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010819 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010820 char_u *keys, *flags;
10821 char_u nbuf[NUMBUFLEN];
10822 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010823 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010824 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010825
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010826 /* This is not allowed in the sandbox. If the commands would still be
10827 * executed in the sandbox it would be OK, but it probably happens later,
10828 * when "sandbox" is no longer set. */
10829 if (check_secure())
10830 return;
10831
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010832 keys = get_tv_string(&argvars[0]);
10833 if (*keys != NUL)
10834 {
10835 if (argvars[1].v_type != VAR_UNKNOWN)
10836 {
10837 flags = get_tv_string_buf(&argvars[1], nbuf);
10838 for ( ; *flags != NUL; ++flags)
10839 {
10840 switch (*flags)
10841 {
10842 case 'n': remap = FALSE; break;
10843 case 'm': remap = TRUE; break;
10844 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010845 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010846 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010847 }
10848 }
10849 }
10850
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010851 /* Need to escape K_SPECIAL and CSI before putting the string in the
10852 * typeahead buffer. */
10853 keys_esc = vim_strsave_escape_csi(keys);
10854 if (keys_esc != NULL)
10855 {
10856 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010857 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010858 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010859 if (vgetc_busy)
10860 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010861 if (execute)
10862 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010863 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010864 }
10865}
10866
10867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 * "filereadable()" function
10869 */
10870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010871f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010873 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 char_u *p;
10875 int n;
10876
Bram Moolenaarc236c162008-07-13 17:41:49 +000010877#ifndef O_NONBLOCK
10878# define O_NONBLOCK 0
10879#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010880 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010881 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10882 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883 {
10884 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010885 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886 }
10887 else
10888 n = FALSE;
10889
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010890 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891}
10892
10893/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010894 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 * rights to write into.
10896 */
10897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010898f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010900 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901}
10902
Bram Moolenaar48e697e2016-01-23 22:17:30 +010010903static void findfilendir(typval_T *argvars, typval_T *rettv, int find_what);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010904
10905 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010906findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010907 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010908 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010909 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010910{
10911#ifdef FEAT_SEARCHPATH
10912 char_u *fname;
10913 char_u *fresult = NULL;
10914 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10915 char_u *p;
10916 char_u pathbuf[NUMBUFLEN];
10917 int count = 1;
10918 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010919 int error = FALSE;
10920#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010921
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010922 rettv->vval.v_string = NULL;
10923 rettv->v_type = VAR_STRING;
10924
10925#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010926 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010927
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010928 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010929 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010930 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10931 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010932 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010933 else
10934 {
10935 if (*p != NUL)
10936 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010937
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010938 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010939 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010940 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010941 }
10942
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010943 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10944 error = TRUE;
10945
10946 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010947 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010948 do
10949 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010950 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010951 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010952 fresult = find_file_in_path_option(first ? fname : NULL,
10953 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010954 0, first, path,
10955 find_what,
10956 curbuf->b_ffname,
10957 find_what == FINDFILE_DIR
10958 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010959 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010960
10961 if (fresult != NULL && rettv->v_type == VAR_LIST)
10962 list_append_string(rettv->vval.v_list, fresult, -1);
10963
10964 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010965 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010966
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010967 if (rettv->v_type == VAR_STRING)
10968 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010969#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010970}
10971
Bram Moolenaar48e697e2016-01-23 22:17:30 +010010972static void filter_map(typval_T *argvars, typval_T *rettv, int map);
10973static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010974
10975/*
10976 * Implementation of map() and filter().
10977 */
10978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010979filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010980{
10981 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010982 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010983 listitem_T *li, *nli;
10984 list_T *l = NULL;
10985 dictitem_T *di;
10986 hashtab_T *ht;
10987 hashitem_T *hi;
10988 dict_T *d = NULL;
10989 typval_T save_val;
10990 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010991 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010992 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010993 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020010994 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010995 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010996 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010997 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010998
Bram Moolenaare9a41262005-01-15 22:18:47 +000010999 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011000 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011001 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011002 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011003 return;
11004 }
11005 else if (argvars[0].v_type == VAR_DICT)
11006 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011007 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011008 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011009 return;
11010 }
11011 else
11012 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011013 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011014 return;
11015 }
11016
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011017 expr = get_tv_string_buf_chk(&argvars[1], buf);
11018 /* On type errors, the preceding call has already displayed an error
11019 * message. Avoid a misleading error message for an empty string that
11020 * was not passed as argument. */
11021 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011022 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011023 prepare_vimvar(VV_VAL, &save_val);
11024 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011025
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011026 /* We reset "did_emsg" to be able to detect whether an error
11027 * occurred during evaluation of the expression. */
11028 save_did_emsg = did_emsg;
11029 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011030
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011031 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011032 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011033 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011034 vimvars[VV_KEY].vv_type = VAR_STRING;
11035
11036 ht = &d->dv_hashtab;
11037 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011038 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011039 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011040 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011041 if (!HASHITEM_EMPTY(hi))
11042 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011043 int r;
11044
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011045 --todo;
11046 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011047 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011048 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11049 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011050 break;
11051 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011052 r = filter_map_one(&di->di_tv, expr, map, &rem);
11053 clear_tv(&vimvars[VV_KEY].vv_tv);
11054 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011055 break;
11056 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011057 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011058 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11059 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011060 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011061 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011062 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011063 }
11064 }
11065 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011066 }
11067 else
11068 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011069 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11070
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011071 for (li = l->lv_first; li != NULL; li = nli)
11072 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011073 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011074 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011075 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011076 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011077 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011078 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011079 break;
11080 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011081 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011082 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011083 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011084 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011085
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011086 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011087 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011088
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011089 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011090 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011091
11092 copy_tv(&argvars[0], rettv);
11093}
11094
11095 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011096filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011097{
Bram Moolenaar33570922005-01-25 22:26:29 +000011098 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011099 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011100 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011101
Bram Moolenaar33570922005-01-25 22:26:29 +000011102 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011103 s = expr;
11104 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011105 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011106 if (*s != NUL) /* check for trailing chars after expr */
11107 {
11108 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011109 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011110 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011111 }
11112 if (map)
11113 {
11114 /* map(): replace the list item value */
11115 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011116 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011117 *tv = rettv;
11118 }
11119 else
11120 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011121 int error = FALSE;
11122
Bram Moolenaare9a41262005-01-15 22:18:47 +000011123 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011124 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011125 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011126 /* On type error, nothing has been removed; return FAIL to stop the
11127 * loop. The error message was given by get_tv_number_chk(). */
11128 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011129 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011130 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011131 retval = OK;
11132theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011133 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011134 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011135}
11136
11137/*
11138 * "filter()" function
11139 */
11140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011141f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011142{
11143 filter_map(argvars, rettv, FALSE);
11144}
11145
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011146/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011147 * "finddir({fname}[, {path}[, {count}]])" function
11148 */
11149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011150f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011151{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011152 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011153}
11154
11155/*
11156 * "findfile({fname}[, {path}[, {count}]])" function
11157 */
11158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011159f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011160{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011161 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011162}
11163
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011164#ifdef FEAT_FLOAT
11165/*
11166 * "float2nr({float})" function
11167 */
11168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011169f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011170{
11171 float_T f;
11172
11173 if (get_float_arg(argvars, &f) == OK)
11174 {
11175 if (f < -0x7fffffff)
11176 rettv->vval.v_number = -0x7fffffff;
11177 else if (f > 0x7fffffff)
11178 rettv->vval.v_number = 0x7fffffff;
11179 else
11180 rettv->vval.v_number = (varnumber_T)f;
11181 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011182}
11183
11184/*
11185 * "floor({float})" function
11186 */
11187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011188f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011189{
11190 float_T f;
11191
11192 rettv->v_type = VAR_FLOAT;
11193 if (get_float_arg(argvars, &f) == OK)
11194 rettv->vval.v_float = floor(f);
11195 else
11196 rettv->vval.v_float = 0.0;
11197}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011198
11199/*
11200 * "fmod()" function
11201 */
11202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011203f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011204{
11205 float_T fx, fy;
11206
11207 rettv->v_type = VAR_FLOAT;
11208 if (get_float_arg(argvars, &fx) == OK
11209 && get_float_arg(&argvars[1], &fy) == OK)
11210 rettv->vval.v_float = fmod(fx, fy);
11211 else
11212 rettv->vval.v_float = 0.0;
11213}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011214#endif
11215
Bram Moolenaar0d660222005-01-07 21:51:51 +000011216/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011217 * "fnameescape({string})" function
11218 */
11219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011220f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011221{
11222 rettv->vval.v_string = vim_strsave_fnameescape(
11223 get_tv_string(&argvars[0]), FALSE);
11224 rettv->v_type = VAR_STRING;
11225}
11226
11227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228 * "fnamemodify({fname}, {mods})" function
11229 */
11230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011231f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232{
11233 char_u *fname;
11234 char_u *mods;
11235 int usedlen = 0;
11236 int len;
11237 char_u *fbuf = NULL;
11238 char_u buf[NUMBUFLEN];
11239
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011240 fname = get_tv_string_chk(&argvars[0]);
11241 mods = get_tv_string_buf_chk(&argvars[1], buf);
11242 if (fname == NULL || mods == NULL)
11243 fname = NULL;
11244 else
11245 {
11246 len = (int)STRLEN(fname);
11247 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011250 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011252 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011254 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255 vim_free(fbuf);
11256}
11257
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011258static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259
11260/*
11261 * "foldclosed()" function
11262 */
11263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011264foldclosed_both(
11265 typval_T *argvars UNUSED,
11266 typval_T *rettv,
11267 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268{
11269#ifdef FEAT_FOLDING
11270 linenr_T lnum;
11271 linenr_T first, last;
11272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011273 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11275 {
11276 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11277 {
11278 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011279 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011281 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282 return;
11283 }
11284 }
11285#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011286 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287}
11288
11289/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011290 * "foldclosed()" function
11291 */
11292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011293f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011294{
11295 foldclosed_both(argvars, rettv, FALSE);
11296}
11297
11298/*
11299 * "foldclosedend()" function
11300 */
11301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011302f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011303{
11304 foldclosed_both(argvars, rettv, TRUE);
11305}
11306
11307/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308 * "foldlevel()" function
11309 */
11310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011311f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312{
11313#ifdef FEAT_FOLDING
11314 linenr_T lnum;
11315
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011316 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011318 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011320}
11321
11322/*
11323 * "foldtext()" function
11324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011326f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327{
11328#ifdef FEAT_FOLDING
11329 linenr_T lnum;
11330 char_u *s;
11331 char_u *r;
11332 int len;
11333 char *txt;
11334#endif
11335
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011336 rettv->v_type = VAR_STRING;
11337 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011339 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11340 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11341 <= curbuf->b_ml.ml_line_count
11342 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343 {
11344 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011345 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11346 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347 {
11348 if (!linewhite(lnum))
11349 break;
11350 ++lnum;
11351 }
11352
11353 /* Find interesting text in this line. */
11354 s = skipwhite(ml_get(lnum));
11355 /* skip C comment-start */
11356 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011357 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011359 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011360 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011361 {
11362 s = skipwhite(ml_get(lnum + 1));
11363 if (*s == '*')
11364 s = skipwhite(s + 1);
11365 }
11366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367 txt = _("+-%s%3ld lines: ");
11368 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011369 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370 + 20 /* for %3ld */
11371 + STRLEN(s))); /* concatenated */
11372 if (r != NULL)
11373 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011374 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11375 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11376 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377 len = (int)STRLEN(r);
11378 STRCAT(r, s);
11379 /* remove 'foldmarker' and 'commentstring' */
11380 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011381 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382 }
11383 }
11384#endif
11385}
11386
11387/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011388 * "foldtextresult(lnum)" function
11389 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011391f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011392{
11393#ifdef FEAT_FOLDING
11394 linenr_T lnum;
11395 char_u *text;
11396 char_u buf[51];
11397 foldinfo_T foldinfo;
11398 int fold_count;
11399#endif
11400
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011401 rettv->v_type = VAR_STRING;
11402 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011403#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011404 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011405 /* treat illegal types and illegal string values for {lnum} the same */
11406 if (lnum < 0)
11407 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011408 fold_count = foldedCount(curwin, lnum, &foldinfo);
11409 if (fold_count > 0)
11410 {
11411 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11412 &foldinfo, buf);
11413 if (text == buf)
11414 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011415 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011416 }
11417#endif
11418}
11419
11420/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421 * "foreground()" function
11422 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011424f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426#ifdef FEAT_GUI
11427 if (gui.in_use)
11428 gui_mch_set_foreground();
11429#else
11430# ifdef WIN32
11431 win32_set_foreground();
11432# endif
11433#endif
11434}
11435
11436/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011437 * "function()" function
11438 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011440f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011441{
11442 char_u *s;
11443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011444 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011445 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011446 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011447 /* Don't check an autoload name for existence here. */
11448 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011449 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011450 else
11451 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011452 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011453 {
11454 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011455 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011456
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011457 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11458 * also be called from another script. Using trans_function_name()
11459 * would also work, but some plugins depend on the name being
11460 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011461 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011462 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011463 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011464 if (rettv->vval.v_string != NULL)
11465 {
11466 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011467 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011468 }
11469 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011470 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011471 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011472 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011473 }
11474}
11475
11476/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011477 * "garbagecollect()" function
11478 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011480f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011481{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011482 /* This is postponed until we are back at the toplevel, because we may be
11483 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11484 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011485
11486 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11487 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011488}
11489
11490/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011491 * "get()" function
11492 */
11493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011494f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011495{
Bram Moolenaar33570922005-01-25 22:26:29 +000011496 listitem_T *li;
11497 list_T *l;
11498 dictitem_T *di;
11499 dict_T *d;
11500 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011501
Bram Moolenaare9a41262005-01-15 22:18:47 +000011502 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011503 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011504 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011505 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011506 int error = FALSE;
11507
11508 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11509 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011510 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011511 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011512 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011513 else if (argvars[0].v_type == VAR_DICT)
11514 {
11515 if ((d = argvars[0].vval.v_dict) != NULL)
11516 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011517 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011518 if (di != NULL)
11519 tv = &di->di_tv;
11520 }
11521 }
11522 else
11523 EMSG2(_(e_listdictarg), "get()");
11524
11525 if (tv == NULL)
11526 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011527 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011528 copy_tv(&argvars[2], rettv);
11529 }
11530 else
11531 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011532}
11533
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011534static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011535
11536/*
11537 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011538 * Return a range (from start to end) of lines in rettv from the specified
11539 * buffer.
11540 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011541 */
11542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011543get_buffer_lines(
11544 buf_T *buf,
11545 linenr_T start,
11546 linenr_T end,
11547 int retlist,
11548 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011549{
11550 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011551
Bram Moolenaar959a1432013-12-14 12:17:38 +010011552 rettv->v_type = VAR_STRING;
11553 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011554 if (retlist && rettv_list_alloc(rettv) == FAIL)
11555 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011556
11557 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11558 return;
11559
11560 if (!retlist)
11561 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011562 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11563 p = ml_get_buf(buf, start, FALSE);
11564 else
11565 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011566 rettv->vval.v_string = vim_strsave(p);
11567 }
11568 else
11569 {
11570 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011571 return;
11572
11573 if (start < 1)
11574 start = 1;
11575 if (end > buf->b_ml.ml_line_count)
11576 end = buf->b_ml.ml_line_count;
11577 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011578 if (list_append_string(rettv->vval.v_list,
11579 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011580 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011581 }
11582}
11583
11584/*
11585 * "getbufline()" function
11586 */
11587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011588f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011589{
11590 linenr_T lnum;
11591 linenr_T end;
11592 buf_T *buf;
11593
11594 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11595 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011596 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011597 --emsg_off;
11598
Bram Moolenaar661b1822005-07-28 22:36:45 +000011599 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011600 if (argvars[2].v_type == VAR_UNKNOWN)
11601 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011602 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011603 end = get_tv_lnum_buf(&argvars[2], buf);
11604
Bram Moolenaar342337a2005-07-21 21:11:17 +000011605 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011606}
11607
Bram Moolenaar0d660222005-01-07 21:51:51 +000011608/*
11609 * "getbufvar()" function
11610 */
11611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011612f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011613{
11614 buf_T *buf;
11615 buf_T *save_curbuf;
11616 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011617 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011618 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011620 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11621 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011622 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011623 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011624
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011625 rettv->v_type = VAR_STRING;
11626 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011627
11628 if (buf != NULL && varname != NULL)
11629 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011630 /* set curbuf to be our buf, temporarily */
11631 save_curbuf = curbuf;
11632 curbuf = buf;
11633
Bram Moolenaar0d660222005-01-07 21:51:51 +000011634 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011635 {
11636 if (get_option_tv(&varname, rettv, TRUE) == OK)
11637 done = TRUE;
11638 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011639 else if (STRCMP(varname, "changedtick") == 0)
11640 {
11641 rettv->v_type = VAR_NUMBER;
11642 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011643 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011644 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011645 else
11646 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011647 /* Look up the variable. */
11648 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11649 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11650 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011651 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011652 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011653 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011654 done = TRUE;
11655 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011656 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011657
11658 /* restore previous notion of curbuf */
11659 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011660 }
11661
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011662 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11663 /* use the default value */
11664 copy_tv(&argvars[2], rettv);
11665
Bram Moolenaar0d660222005-01-07 21:51:51 +000011666 --emsg_off;
11667}
11668
11669/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670 * "getchar()" function
11671 */
11672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011673f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674{
11675 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011676 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011678 /* Position the cursor. Needed after a message that ends in a space. */
11679 windgoto(msg_row, msg_col);
11680
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681 ++no_mapping;
11682 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011683 for (;;)
11684 {
11685 if (argvars[0].v_type == VAR_UNKNOWN)
11686 /* getchar(): blocking wait. */
11687 n = safe_vgetc();
11688 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11689 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011690 n = vpeekc_any();
11691 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011692 /* illegal argument or getchar(0) and no char avail: return zero */
11693 n = 0;
11694 else
11695 /* getchar(0) and char avail: return char */
11696 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011697
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011698 if (n == K_IGNORE)
11699 continue;
11700 break;
11701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702 --no_mapping;
11703 --allow_keys;
11704
Bram Moolenaar219b8702006-11-01 14:32:36 +000011705 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11706 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11707 vimvars[VV_MOUSE_COL].vv_nr = 0;
11708
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011709 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710 if (IS_SPECIAL(n) || mod_mask != 0)
11711 {
11712 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11713 int i = 0;
11714
11715 /* Turn a special key into three bytes, plus modifier. */
11716 if (mod_mask != 0)
11717 {
11718 temp[i++] = K_SPECIAL;
11719 temp[i++] = KS_MODIFIER;
11720 temp[i++] = mod_mask;
11721 }
11722 if (IS_SPECIAL(n))
11723 {
11724 temp[i++] = K_SPECIAL;
11725 temp[i++] = K_SECOND(n);
11726 temp[i++] = K_THIRD(n);
11727 }
11728#ifdef FEAT_MBYTE
11729 else if (has_mbyte)
11730 i += (*mb_char2bytes)(n, temp + i);
11731#endif
11732 else
11733 temp[i++] = n;
11734 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011735 rettv->v_type = VAR_STRING;
11736 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011737
11738#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011739 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011740 {
11741 int row = mouse_row;
11742 int col = mouse_col;
11743 win_T *win;
11744 linenr_T lnum;
11745# ifdef FEAT_WINDOWS
11746 win_T *wp;
11747# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011748 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011749
11750 if (row >= 0 && col >= 0)
11751 {
11752 /* Find the window at the mouse coordinates and compute the
11753 * text position. */
11754 win = mouse_find_win(&row, &col);
11755 (void)mouse_comp_pos(win, &row, &col, &lnum);
11756# ifdef FEAT_WINDOWS
11757 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011758 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011759# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011760 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011761 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11762 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11763 }
11764 }
11765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766 }
11767}
11768
11769/*
11770 * "getcharmod()" function
11771 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011773f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011775 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011776}
11777
11778/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011779 * "getcharsearch()" function
11780 */
11781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011782f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011783{
11784 if (rettv_dict_alloc(rettv) != FAIL)
11785 {
11786 dict_T *dict = rettv->vval.v_dict;
11787
11788 dict_add_nr_str(dict, "char", 0L, last_csearch());
11789 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11790 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11791 }
11792}
11793
11794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795 * "getcmdline()" function
11796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011798f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011800 rettv->v_type = VAR_STRING;
11801 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011802}
11803
11804/*
11805 * "getcmdpos()" function
11806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011808f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011810 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811}
11812
11813/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011814 * "getcmdtype()" function
11815 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011817f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011818{
11819 rettv->v_type = VAR_STRING;
11820 rettv->vval.v_string = alloc(2);
11821 if (rettv->vval.v_string != NULL)
11822 {
11823 rettv->vval.v_string[0] = get_cmdline_type();
11824 rettv->vval.v_string[1] = NUL;
11825 }
11826}
11827
11828/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011829 * "getcmdwintype()" function
11830 */
11831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011832f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011833{
11834 rettv->v_type = VAR_STRING;
11835 rettv->vval.v_string = NULL;
11836#ifdef FEAT_CMDWIN
11837 rettv->vval.v_string = alloc(2);
11838 if (rettv->vval.v_string != NULL)
11839 {
11840 rettv->vval.v_string[0] = cmdwin_type;
11841 rettv->vval.v_string[1] = NUL;
11842 }
11843#endif
11844}
11845
11846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847 * "getcwd()" function
11848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011850f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851{
Bram Moolenaarc9703302016-01-17 21:49:33 +010011852 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011853 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011855 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011856 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010011857
11858 wp = find_tabwin(&argvars[0], &argvars[1]);
11859 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010011861 if (wp->w_localdir != NULL)
11862 rettv->vval.v_string = vim_strsave(wp->w_localdir);
11863 else if(globaldir != NULL)
11864 rettv->vval.v_string = vim_strsave(globaldir);
11865 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020011866 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010011867 cwd = alloc(MAXPATHL);
11868 if (cwd != NULL)
11869 {
11870 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11871 rettv->vval.v_string = vim_strsave(cwd);
11872 vim_free(cwd);
11873 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020011874 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010011875#ifdef BACKSLASH_IN_FILENAME
11876 if (rettv->vval.v_string != NULL)
11877 slash_adjust(rettv->vval.v_string);
11878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011879 }
11880}
11881
11882/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011883 * "getfontname()" function
11884 */
11885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011886f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011887{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011888 rettv->v_type = VAR_STRING;
11889 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011890#ifdef FEAT_GUI
11891 if (gui.in_use)
11892 {
11893 GuiFont font;
11894 char_u *name = NULL;
11895
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011896 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011897 {
11898 /* Get the "Normal" font. Either the name saved by
11899 * hl_set_font_name() or from the font ID. */
11900 font = gui.norm_font;
11901 name = hl_get_font_name();
11902 }
11903 else
11904 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011905 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011906 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11907 return;
11908 font = gui_mch_get_font(name, FALSE);
11909 if (font == NOFONT)
11910 return; /* Invalid font name, return empty string. */
11911 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011912 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011913 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011914 gui_mch_free_font(font);
11915 }
11916#endif
11917}
11918
11919/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011920 * "getfperm({fname})" function
11921 */
11922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011923f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011924{
11925 char_u *fname;
11926 struct stat st;
11927 char_u *perm = NULL;
11928 char_u flags[] = "rwx";
11929 int i;
11930
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011931 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011932
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011933 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011934 if (mch_stat((char *)fname, &st) >= 0)
11935 {
11936 perm = vim_strsave((char_u *)"---------");
11937 if (perm != NULL)
11938 {
11939 for (i = 0; i < 9; i++)
11940 {
11941 if (st.st_mode & (1 << (8 - i)))
11942 perm[i] = flags[i % 3];
11943 }
11944 }
11945 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011946 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011947}
11948
11949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950 * "getfsize({fname})" function
11951 */
11952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011953f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954{
11955 char_u *fname;
11956 struct stat st;
11957
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011958 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011960 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961
11962 if (mch_stat((char *)fname, &st) >= 0)
11963 {
11964 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011965 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011967 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011968 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011969
11970 /* non-perfect check for overflow */
11971 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11972 rettv->vval.v_number = -2;
11973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974 }
11975 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011976 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977}
11978
11979/*
11980 * "getftime({fname})" function
11981 */
11982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011983f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984{
11985 char_u *fname;
11986 struct stat st;
11987
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011988 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011989
11990 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011991 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011993 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994}
11995
11996/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011997 * "getftype({fname})" function
11998 */
11999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012000f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012001{
12002 char_u *fname;
12003 struct stat st;
12004 char_u *type = NULL;
12005 char *t;
12006
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012007 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012008
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012009 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012010 if (mch_lstat((char *)fname, &st) >= 0)
12011 {
12012#ifdef S_ISREG
12013 if (S_ISREG(st.st_mode))
12014 t = "file";
12015 else if (S_ISDIR(st.st_mode))
12016 t = "dir";
12017# ifdef S_ISLNK
12018 else if (S_ISLNK(st.st_mode))
12019 t = "link";
12020# endif
12021# ifdef S_ISBLK
12022 else if (S_ISBLK(st.st_mode))
12023 t = "bdev";
12024# endif
12025# ifdef S_ISCHR
12026 else if (S_ISCHR(st.st_mode))
12027 t = "cdev";
12028# endif
12029# ifdef S_ISFIFO
12030 else if (S_ISFIFO(st.st_mode))
12031 t = "fifo";
12032# endif
12033# ifdef S_ISSOCK
12034 else if (S_ISSOCK(st.st_mode))
12035 t = "fifo";
12036# endif
12037 else
12038 t = "other";
12039#else
12040# ifdef S_IFMT
12041 switch (st.st_mode & S_IFMT)
12042 {
12043 case S_IFREG: t = "file"; break;
12044 case S_IFDIR: t = "dir"; break;
12045# ifdef S_IFLNK
12046 case S_IFLNK: t = "link"; break;
12047# endif
12048# ifdef S_IFBLK
12049 case S_IFBLK: t = "bdev"; break;
12050# endif
12051# ifdef S_IFCHR
12052 case S_IFCHR: t = "cdev"; break;
12053# endif
12054# ifdef S_IFIFO
12055 case S_IFIFO: t = "fifo"; break;
12056# endif
12057# ifdef S_IFSOCK
12058 case S_IFSOCK: t = "socket"; break;
12059# endif
12060 default: t = "other";
12061 }
12062# else
12063 if (mch_isdir(fname))
12064 t = "dir";
12065 else
12066 t = "file";
12067# endif
12068#endif
12069 type = vim_strsave((char_u *)t);
12070 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012071 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012072}
12073
12074/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012075 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012076 */
12077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012078f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012079{
12080 linenr_T lnum;
12081 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012082 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012083
12084 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012085 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012086 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012087 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012088 retlist = FALSE;
12089 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012090 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012091 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012092 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012093 retlist = TRUE;
12094 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012095
Bram Moolenaar342337a2005-07-21 21:11:17 +000012096 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012097}
12098
12099/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012100 * "getmatches()" function
12101 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012103f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012104{
12105#ifdef FEAT_SEARCH_EXTRA
12106 dict_T *dict;
12107 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012108 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012109
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012110 if (rettv_list_alloc(rettv) == OK)
12111 {
12112 while (cur != NULL)
12113 {
12114 dict = dict_alloc();
12115 if (dict == NULL)
12116 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012117 if (cur->match.regprog == NULL)
12118 {
12119 /* match added with matchaddpos() */
12120 for (i = 0; i < MAXPOSMATCH; ++i)
12121 {
12122 llpos_T *llpos;
12123 char buf[6];
12124 list_T *l;
12125
12126 llpos = &cur->pos.pos[i];
12127 if (llpos->lnum == 0)
12128 break;
12129 l = list_alloc();
12130 if (l == NULL)
12131 break;
12132 list_append_number(l, (varnumber_T)llpos->lnum);
12133 if (llpos->col > 0)
12134 {
12135 list_append_number(l, (varnumber_T)llpos->col);
12136 list_append_number(l, (varnumber_T)llpos->len);
12137 }
12138 sprintf(buf, "pos%d", i + 1);
12139 dict_add_list(dict, buf, l);
12140 }
12141 }
12142 else
12143 {
12144 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12145 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012146 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012147 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12148 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012149# ifdef FEAT_CONCEAL
12150 if (cur->conceal_char)
12151 {
12152 char_u buf[MB_MAXBYTES + 1];
12153
12154 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12155 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12156 }
12157# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012158 list_append_dict(rettv->vval.v_list, dict);
12159 cur = cur->next;
12160 }
12161 }
12162#endif
12163}
12164
12165/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012166 * "getpid()" function
12167 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012169f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012170{
12171 rettv->vval.v_number = mch_get_pid();
12172}
12173
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012174static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012175
12176/*
12177 * "getcurpos()" function
12178 */
12179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012180f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012181{
12182 getpos_both(argvars, rettv, TRUE);
12183}
12184
Bram Moolenaar18081e32008-02-20 19:11:07 +000012185/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012186 * "getpos(string)" function
12187 */
12188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012189f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012190{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012191 getpos_both(argvars, rettv, FALSE);
12192}
12193
12194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012195getpos_both(
12196 typval_T *argvars,
12197 typval_T *rettv,
12198 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012199{
Bram Moolenaara5525202006-03-02 22:52:09 +000012200 pos_T *fp;
12201 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012202 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012203
12204 if (rettv_list_alloc(rettv) == OK)
12205 {
12206 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012207 if (getcurpos)
12208 fp = &curwin->w_cursor;
12209 else
12210 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012211 if (fnum != -1)
12212 list_append_number(l, (varnumber_T)fnum);
12213 else
12214 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012215 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12216 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012217 list_append_number(l, (fp != NULL)
12218 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012219 : (varnumber_T)0);
12220 list_append_number(l,
12221#ifdef FEAT_VIRTUALEDIT
12222 (fp != NULL) ? (varnumber_T)fp->coladd :
12223#endif
12224 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012225 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012226 list_append_number(l, curwin->w_curswant == MAXCOL ?
12227 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012228 }
12229 else
12230 rettv->vval.v_number = FALSE;
12231}
12232
12233/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012234 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012235 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012237f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012238{
12239#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012240 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012241#endif
12242
Bram Moolenaar2641f772005-03-25 21:58:17 +000012243#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012244 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012245 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012246 wp = NULL;
12247 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12248 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012249 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012250 if (wp == NULL)
12251 return;
12252 }
12253
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012254 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012255 }
12256#endif
12257}
12258
12259/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260 * "getreg()" function
12261 */
12262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012263f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012264{
12265 char_u *strregname;
12266 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012267 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012268 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012269 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012271 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012272 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012273 strregname = get_tv_string_chk(&argvars[0]);
12274 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012275 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012277 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012278 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12279 return_list = get_tv_number_chk(&argvars[2], &error);
12280 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012282 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012283 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012284
12285 if (error)
12286 return;
12287
Bram Moolenaar071d4272004-06-13 20:20:40 +000012288 regname = (strregname == NULL ? '"' : *strregname);
12289 if (regname == 0)
12290 regname = '"';
12291
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012292 if (return_list)
12293 {
12294 rettv->v_type = VAR_LIST;
12295 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12296 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012297 if (rettv->vval.v_list != NULL)
12298 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012299 }
12300 else
12301 {
12302 rettv->v_type = VAR_STRING;
12303 rettv->vval.v_string = get_reg_contents(regname,
12304 arg2 ? GREG_EXPR_SRC : 0);
12305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306}
12307
12308/*
12309 * "getregtype()" function
12310 */
12311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012312f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313{
12314 char_u *strregname;
12315 int regname;
12316 char_u buf[NUMBUFLEN + 2];
12317 long reglen = 0;
12318
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012319 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012320 {
12321 strregname = get_tv_string_chk(&argvars[0]);
12322 if (strregname == NULL) /* type error; errmsg already given */
12323 {
12324 rettv->v_type = VAR_STRING;
12325 rettv->vval.v_string = NULL;
12326 return;
12327 }
12328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329 else
12330 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012331 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012332
12333 regname = (strregname == NULL ? '"' : *strregname);
12334 if (regname == 0)
12335 regname = '"';
12336
12337 buf[0] = NUL;
12338 buf[1] = NUL;
12339 switch (get_reg_type(regname, &reglen))
12340 {
12341 case MLINE: buf[0] = 'V'; break;
12342 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343 case MBLOCK:
12344 buf[0] = Ctrl_V;
12345 sprintf((char *)buf + 1, "%ld", reglen + 1);
12346 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012348 rettv->v_type = VAR_STRING;
12349 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350}
12351
12352/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012353 * "gettabvar()" function
12354 */
12355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012356f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012357{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012358 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012359 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012360 dictitem_T *v;
12361 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012362 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012363
12364 rettv->v_type = VAR_STRING;
12365 rettv->vval.v_string = NULL;
12366
12367 varname = get_tv_string_chk(&argvars[1]);
12368 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12369 if (tp != NULL && varname != NULL)
12370 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012371 /* Set tp to be our tabpage, temporarily. Also set the window to the
12372 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012373 if (switch_win(&oldcurwin, &oldtabpage,
12374 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012375 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012376 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012377 /* look up the variable */
12378 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12379 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12380 if (v != NULL)
12381 {
12382 copy_tv(&v->di_tv, rettv);
12383 done = TRUE;
12384 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012385 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012386
12387 /* restore previous notion of curwin */
12388 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012389 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012390
12391 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012392 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012393}
12394
12395/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012396 * "gettabwinvar()" function
12397 */
12398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012399f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012400{
12401 getwinvar(argvars, rettv, 1);
12402}
12403
12404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405 * "getwinposx()" function
12406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012408f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012409{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012410 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012411#ifdef FEAT_GUI
12412 if (gui.in_use)
12413 {
12414 int x, y;
12415
12416 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012417 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012418 }
12419#endif
12420}
12421
12422/*
12423 * "getwinposy()" function
12424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012426f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012428 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429#ifdef FEAT_GUI
12430 if (gui.in_use)
12431 {
12432 int x, y;
12433
12434 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012435 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436 }
12437#endif
12438}
12439
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012440/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012441 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012442 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012443 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012444find_win_by_nr(
12445 typval_T *vp,
12446 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012447{
12448#ifdef FEAT_WINDOWS
12449 win_T *wp;
12450#endif
12451 int nr;
12452
12453 nr = get_tv_number_chk(vp, NULL);
12454
12455#ifdef FEAT_WINDOWS
12456 if (nr < 0)
12457 return NULL;
12458 if (nr == 0)
12459 return curwin;
12460
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012461 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12462 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012463 if (--nr <= 0)
12464 break;
12465 return wp;
12466#else
12467 if (nr == 0 || nr == 1)
12468 return curwin;
12469 return NULL;
12470#endif
12471}
12472
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012474 * Find window specified by "wvp" in tabpage "tvp".
12475 */
12476 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012477find_tabwin(
12478 typval_T *wvp, /* VAR_UNKNOWN for current window */
12479 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012480{
12481 win_T *wp = NULL;
12482 tabpage_T *tp = NULL;
12483 long n;
12484
12485 if (wvp->v_type != VAR_UNKNOWN)
12486 {
12487 if (tvp->v_type != VAR_UNKNOWN)
12488 {
12489 n = get_tv_number(tvp);
12490 if (n >= 0)
12491 tp = find_tabpage(n);
12492 }
12493 else
12494 tp = curtab;
12495
12496 if (tp != NULL)
12497 wp = find_win_by_nr(wvp, tp);
12498 }
12499 else
12500 wp = curwin;
12501
12502 return wp;
12503}
12504
12505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012506 * "getwinvar()" function
12507 */
12508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012509f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012511 getwinvar(argvars, rettv, 0);
12512}
12513
12514/*
12515 * getwinvar() and gettabwinvar()
12516 */
12517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012518getwinvar(
12519 typval_T *argvars,
12520 typval_T *rettv,
12521 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012522{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012523 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012524 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012525 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012526 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012527 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012528#ifdef FEAT_WINDOWS
12529 win_T *oldcurwin;
12530 tabpage_T *oldtabpage;
12531 int need_switch_win;
12532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012534#ifdef FEAT_WINDOWS
12535 if (off == 1)
12536 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12537 else
12538 tp = curtab;
12539#endif
12540 win = find_win_by_nr(&argvars[off], tp);
12541 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012542 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012543
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012544 rettv->v_type = VAR_STRING;
12545 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012546
12547 if (win != NULL && varname != NULL)
12548 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012549#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012550 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012551 * otherwise the window is not valid. Only do this when needed,
12552 * autocommands get blocked. */
12553 need_switch_win = !(tp == curtab && win == curwin);
12554 if (!need_switch_win
12555 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12556#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012557 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012558 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012559 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012560 if (get_option_tv(&varname, rettv, 1) == OK)
12561 done = TRUE;
12562 }
12563 else
12564 {
12565 /* Look up the variable. */
12566 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12567 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12568 varname, FALSE);
12569 if (v != NULL)
12570 {
12571 copy_tv(&v->di_tv, rettv);
12572 done = TRUE;
12573 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012575 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012576
Bram Moolenaarba117c22015-09-29 16:53:22 +020012577#ifdef FEAT_WINDOWS
12578 if (need_switch_win)
12579 /* restore previous notion of curwin */
12580 restore_win(oldcurwin, oldtabpage, TRUE);
12581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012582 }
12583
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012584 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12585 /* use the default return value */
12586 copy_tv(&argvars[off + 2], rettv);
12587
Bram Moolenaar071d4272004-06-13 20:20:40 +000012588 --emsg_off;
12589}
12590
12591/*
12592 * "glob()" function
12593 */
12594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012595f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012596{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012597 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012598 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012599 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012601 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012602 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012603 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012604 if (argvars[1].v_type != VAR_UNKNOWN)
12605 {
12606 if (get_tv_number_chk(&argvars[1], &error))
12607 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012608 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012609 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012610 if (get_tv_number_chk(&argvars[2], &error))
12611 {
12612 rettv->v_type = VAR_LIST;
12613 rettv->vval.v_list = NULL;
12614 }
12615 if (argvars[3].v_type != VAR_UNKNOWN
12616 && get_tv_number_chk(&argvars[3], &error))
12617 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012618 }
12619 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012620 if (!error)
12621 {
12622 ExpandInit(&xpc);
12623 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012624 if (p_wic)
12625 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012626 if (rettv->v_type == VAR_STRING)
12627 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012628 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012629 else if (rettv_list_alloc(rettv) != FAIL)
12630 {
12631 int i;
12632
12633 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12634 NULL, options, WILD_ALL_KEEP);
12635 for (i = 0; i < xpc.xp_numfiles; i++)
12636 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12637
12638 ExpandCleanup(&xpc);
12639 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012640 }
12641 else
12642 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012643}
12644
12645/*
12646 * "globpath()" function
12647 */
12648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012649f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012651 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012653 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012654 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012655 garray_T ga;
12656 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012657
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012658 /* When the optional second argument is non-zero, don't remove matches
12659 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012660 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012661 if (argvars[2].v_type != VAR_UNKNOWN)
12662 {
12663 if (get_tv_number_chk(&argvars[2], &error))
12664 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012665 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012666 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012667 if (get_tv_number_chk(&argvars[3], &error))
12668 {
12669 rettv->v_type = VAR_LIST;
12670 rettv->vval.v_list = NULL;
12671 }
12672 if (argvars[4].v_type != VAR_UNKNOWN
12673 && get_tv_number_chk(&argvars[4], &error))
12674 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012675 }
12676 }
12677 if (file != NULL && !error)
12678 {
12679 ga_init2(&ga, (int)sizeof(char_u *), 10);
12680 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12681 if (rettv->v_type == VAR_STRING)
12682 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12683 else if (rettv_list_alloc(rettv) != FAIL)
12684 for (i = 0; i < ga.ga_len; ++i)
12685 list_append_string(rettv->vval.v_list,
12686 ((char_u **)(ga.ga_data))[i], -1);
12687 ga_clear_strings(&ga);
12688 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012689 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012690 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012691}
12692
12693/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012694 * "glob2regpat()" function
12695 */
12696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012697f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012698{
12699 char_u *pat = get_tv_string_chk(&argvars[0]);
12700
12701 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010012702 rettv->vval.v_string = (pat == NULL)
12703 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012704}
12705
12706/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012707 * "has()" function
12708 */
12709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012710f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711{
12712 int i;
12713 char_u *name;
12714 int n = FALSE;
12715 static char *(has_list[]) =
12716 {
12717#ifdef AMIGA
12718 "amiga",
12719# ifdef FEAT_ARP
12720 "arp",
12721# endif
12722#endif
12723#ifdef __BEOS__
12724 "beos",
12725#endif
12726#ifdef MSDOS
12727# ifdef DJGPP
12728 "dos32",
12729# else
12730 "dos16",
12731# endif
12732#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012733#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012734 "mac",
12735#endif
12736#if defined(MACOS_X_UNIX)
12737 "macunix",
12738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012739#ifdef __QNX__
12740 "qnx",
12741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742#ifdef UNIX
12743 "unix",
12744#endif
12745#ifdef VMS
12746 "vms",
12747#endif
12748#ifdef WIN16
12749 "win16",
12750#endif
12751#ifdef WIN32
12752 "win32",
12753#endif
12754#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12755 "win32unix",
12756#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012757#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758 "win64",
12759#endif
12760#ifdef EBCDIC
12761 "ebcdic",
12762#endif
12763#ifndef CASE_INSENSITIVE_FILENAME
12764 "fname_case",
12765#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012766#ifdef HAVE_ACL
12767 "acl",
12768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769#ifdef FEAT_ARABIC
12770 "arabic",
12771#endif
12772#ifdef FEAT_AUTOCMD
12773 "autocmd",
12774#endif
12775#ifdef FEAT_BEVAL
12776 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012777# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12778 "balloon_multiline",
12779# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780#endif
12781#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12782 "builtin_terms",
12783# ifdef ALL_BUILTIN_TCAPS
12784 "all_builtin_terms",
12785# endif
12786#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012787#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12788 || defined(FEAT_GUI_W32) \
12789 || defined(FEAT_GUI_MOTIF))
12790 "browsefilter",
12791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792#ifdef FEAT_BYTEOFF
12793 "byte_offset",
12794#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010012795#ifdef FEAT_CHANNEL
12796 "channel",
12797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798#ifdef FEAT_CINDENT
12799 "cindent",
12800#endif
12801#ifdef FEAT_CLIENTSERVER
12802 "clientserver",
12803#endif
12804#ifdef FEAT_CLIPBOARD
12805 "clipboard",
12806#endif
12807#ifdef FEAT_CMDL_COMPL
12808 "cmdline_compl",
12809#endif
12810#ifdef FEAT_CMDHIST
12811 "cmdline_hist",
12812#endif
12813#ifdef FEAT_COMMENTS
12814 "comments",
12815#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012816#ifdef FEAT_CONCEAL
12817 "conceal",
12818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819#ifdef FEAT_CRYPT
12820 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010012821 "crypt-blowfish",
12822 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012823#endif
12824#ifdef FEAT_CSCOPE
12825 "cscope",
12826#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012827#ifdef FEAT_CURSORBIND
12828 "cursorbind",
12829#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012830#ifdef CURSOR_SHAPE
12831 "cursorshape",
12832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833#ifdef DEBUG
12834 "debug",
12835#endif
12836#ifdef FEAT_CON_DIALOG
12837 "dialog_con",
12838#endif
12839#ifdef FEAT_GUI_DIALOG
12840 "dialog_gui",
12841#endif
12842#ifdef FEAT_DIFF
12843 "diff",
12844#endif
12845#ifdef FEAT_DIGRAPHS
12846 "digraphs",
12847#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012848#ifdef FEAT_DIRECTX
12849 "directx",
12850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851#ifdef FEAT_DND
12852 "dnd",
12853#endif
12854#ifdef FEAT_EMACS_TAGS
12855 "emacs_tags",
12856#endif
12857 "eval", /* always present, of course! */
12858#ifdef FEAT_EX_EXTRA
12859 "ex_extra",
12860#endif
12861#ifdef FEAT_SEARCH_EXTRA
12862 "extra_search",
12863#endif
12864#ifdef FEAT_FKMAP
12865 "farsi",
12866#endif
12867#ifdef FEAT_SEARCHPATH
12868 "file_in_path",
12869#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012870#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012871 "filterpipe",
12872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873#ifdef FEAT_FIND_ID
12874 "find_in_path",
12875#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012876#ifdef FEAT_FLOAT
12877 "float",
12878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879#ifdef FEAT_FOLDING
12880 "folding",
12881#endif
12882#ifdef FEAT_FOOTER
12883 "footer",
12884#endif
12885#if !defined(USE_SYSTEM) && defined(UNIX)
12886 "fork",
12887#endif
12888#ifdef FEAT_GETTEXT
12889 "gettext",
12890#endif
12891#ifdef FEAT_GUI
12892 "gui",
12893#endif
12894#ifdef FEAT_GUI_ATHENA
12895# ifdef FEAT_GUI_NEXTAW
12896 "gui_neXtaw",
12897# else
12898 "gui_athena",
12899# endif
12900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012901#ifdef FEAT_GUI_GTK
12902 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012903 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012905#ifdef FEAT_GUI_GNOME
12906 "gui_gnome",
12907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908#ifdef FEAT_GUI_MAC
12909 "gui_mac",
12910#endif
12911#ifdef FEAT_GUI_MOTIF
12912 "gui_motif",
12913#endif
12914#ifdef FEAT_GUI_PHOTON
12915 "gui_photon",
12916#endif
12917#ifdef FEAT_GUI_W16
12918 "gui_win16",
12919#endif
12920#ifdef FEAT_GUI_W32
12921 "gui_win32",
12922#endif
12923#ifdef FEAT_HANGULIN
12924 "hangul_input",
12925#endif
12926#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12927 "iconv",
12928#endif
12929#ifdef FEAT_INS_EXPAND
12930 "insert_expand",
12931#endif
12932#ifdef FEAT_JUMPLIST
12933 "jumplist",
12934#endif
12935#ifdef FEAT_KEYMAP
12936 "keymap",
12937#endif
12938#ifdef FEAT_LANGMAP
12939 "langmap",
12940#endif
12941#ifdef FEAT_LIBCALL
12942 "libcall",
12943#endif
12944#ifdef FEAT_LINEBREAK
12945 "linebreak",
12946#endif
12947#ifdef FEAT_LISP
12948 "lispindent",
12949#endif
12950#ifdef FEAT_LISTCMDS
12951 "listcmds",
12952#endif
12953#ifdef FEAT_LOCALMAP
12954 "localmap",
12955#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012956#ifdef FEAT_LUA
12957# ifndef DYNAMIC_LUA
12958 "lua",
12959# endif
12960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961#ifdef FEAT_MENU
12962 "menu",
12963#endif
12964#ifdef FEAT_SESSION
12965 "mksession",
12966#endif
12967#ifdef FEAT_MODIFY_FNAME
12968 "modify_fname",
12969#endif
12970#ifdef FEAT_MOUSE
12971 "mouse",
12972#endif
12973#ifdef FEAT_MOUSESHAPE
12974 "mouseshape",
12975#endif
12976#if defined(UNIX) || defined(VMS)
12977# ifdef FEAT_MOUSE_DEC
12978 "mouse_dec",
12979# endif
12980# ifdef FEAT_MOUSE_GPM
12981 "mouse_gpm",
12982# endif
12983# ifdef FEAT_MOUSE_JSB
12984 "mouse_jsbterm",
12985# endif
12986# ifdef FEAT_MOUSE_NET
12987 "mouse_netterm",
12988# endif
12989# ifdef FEAT_MOUSE_PTERM
12990 "mouse_pterm",
12991# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012992# ifdef FEAT_MOUSE_SGR
12993 "mouse_sgr",
12994# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012995# ifdef FEAT_SYSMOUSE
12996 "mouse_sysmouse",
12997# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012998# ifdef FEAT_MOUSE_URXVT
12999 "mouse_urxvt",
13000# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013001# ifdef FEAT_MOUSE_XTERM
13002 "mouse_xterm",
13003# endif
13004#endif
13005#ifdef FEAT_MBYTE
13006 "multi_byte",
13007#endif
13008#ifdef FEAT_MBYTE_IME
13009 "multi_byte_ime",
13010#endif
13011#ifdef FEAT_MULTI_LANG
13012 "multi_lang",
13013#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013014#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013015#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013016 "mzscheme",
13017#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013019#ifdef FEAT_OLE
13020 "ole",
13021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022#ifdef FEAT_PATH_EXTRA
13023 "path_extra",
13024#endif
13025#ifdef FEAT_PERL
13026#ifndef DYNAMIC_PERL
13027 "perl",
13028#endif
13029#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013030#ifdef FEAT_PERSISTENT_UNDO
13031 "persistent_undo",
13032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033#ifdef FEAT_PYTHON
13034#ifndef DYNAMIC_PYTHON
13035 "python",
13036#endif
13037#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013038#ifdef FEAT_PYTHON3
13039#ifndef DYNAMIC_PYTHON3
13040 "python3",
13041#endif
13042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043#ifdef FEAT_POSTSCRIPT
13044 "postscript",
13045#endif
13046#ifdef FEAT_PRINTER
13047 "printer",
13048#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013049#ifdef FEAT_PROFILE
13050 "profile",
13051#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013052#ifdef FEAT_RELTIME
13053 "reltime",
13054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055#ifdef FEAT_QUICKFIX
13056 "quickfix",
13057#endif
13058#ifdef FEAT_RIGHTLEFT
13059 "rightleft",
13060#endif
13061#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13062 "ruby",
13063#endif
13064#ifdef FEAT_SCROLLBIND
13065 "scrollbind",
13066#endif
13067#ifdef FEAT_CMDL_INFO
13068 "showcmd",
13069 "cmdline_info",
13070#endif
13071#ifdef FEAT_SIGNS
13072 "signs",
13073#endif
13074#ifdef FEAT_SMARTINDENT
13075 "smartindent",
13076#endif
13077#ifdef FEAT_SNIFF
13078 "sniff",
13079#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013080#ifdef STARTUPTIME
13081 "startuptime",
13082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083#ifdef FEAT_STL_OPT
13084 "statusline",
13085#endif
13086#ifdef FEAT_SUN_WORKSHOP
13087 "sun_workshop",
13088#endif
13089#ifdef FEAT_NETBEANS_INTG
13090 "netbeans_intg",
13091#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013092#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013093 "spell",
13094#endif
13095#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013096 "syntax",
13097#endif
13098#if defined(USE_SYSTEM) || !defined(UNIX)
13099 "system",
13100#endif
13101#ifdef FEAT_TAG_BINS
13102 "tag_binary",
13103#endif
13104#ifdef FEAT_TAG_OLDSTATIC
13105 "tag_old_static",
13106#endif
13107#ifdef FEAT_TAG_ANYWHITE
13108 "tag_any_white",
13109#endif
13110#ifdef FEAT_TCL
13111# ifndef DYNAMIC_TCL
13112 "tcl",
13113# endif
13114#endif
13115#ifdef TERMINFO
13116 "terminfo",
13117#endif
13118#ifdef FEAT_TERMRESPONSE
13119 "termresponse",
13120#endif
13121#ifdef FEAT_TEXTOBJ
13122 "textobjects",
13123#endif
13124#ifdef HAVE_TGETENT
13125 "tgetent",
13126#endif
13127#ifdef FEAT_TITLE
13128 "title",
13129#endif
13130#ifdef FEAT_TOOLBAR
13131 "toolbar",
13132#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013133#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13134 "unnamedplus",
13135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136#ifdef FEAT_USR_CMDS
13137 "user-commands", /* was accidentally included in 5.4 */
13138 "user_commands",
13139#endif
13140#ifdef FEAT_VIMINFO
13141 "viminfo",
13142#endif
13143#ifdef FEAT_VERTSPLIT
13144 "vertsplit",
13145#endif
13146#ifdef FEAT_VIRTUALEDIT
13147 "virtualedit",
13148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150#ifdef FEAT_VISUALEXTRA
13151 "visualextra",
13152#endif
13153#ifdef FEAT_VREPLACE
13154 "vreplace",
13155#endif
13156#ifdef FEAT_WILDIGN
13157 "wildignore",
13158#endif
13159#ifdef FEAT_WILDMENU
13160 "wildmenu",
13161#endif
13162#ifdef FEAT_WINDOWS
13163 "windows",
13164#endif
13165#ifdef FEAT_WAK
13166 "winaltkeys",
13167#endif
13168#ifdef FEAT_WRITEBACKUP
13169 "writebackup",
13170#endif
13171#ifdef FEAT_XIM
13172 "xim",
13173#endif
13174#ifdef FEAT_XFONTSET
13175 "xfontset",
13176#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013177#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013178 "xpm",
13179 "xpm_w32", /* for backward compatibility */
13180#else
13181# if defined(HAVE_XPM)
13182 "xpm",
13183# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013185#ifdef USE_XSMP
13186 "xsmp",
13187#endif
13188#ifdef USE_XSMP_INTERACT
13189 "xsmp_interact",
13190#endif
13191#ifdef FEAT_XCLIPBOARD
13192 "xterm_clipboard",
13193#endif
13194#ifdef FEAT_XTERM_SAVE
13195 "xterm_save",
13196#endif
13197#if defined(UNIX) && defined(FEAT_X11)
13198 "X11",
13199#endif
13200 NULL
13201 };
13202
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013203 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204 for (i = 0; has_list[i] != NULL; ++i)
13205 if (STRICMP(name, has_list[i]) == 0)
13206 {
13207 n = TRUE;
13208 break;
13209 }
13210
13211 if (n == FALSE)
13212 {
13213 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013214 {
13215 if (name[5] == '-'
13216 && STRLEN(name) > 11
13217 && vim_isdigit(name[6])
13218 && vim_isdigit(name[8])
13219 && vim_isdigit(name[10]))
13220 {
13221 int major = atoi((char *)name + 6);
13222 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013223
13224 /* Expect "patch-9.9.01234". */
13225 n = (major < VIM_VERSION_MAJOR
13226 || (major == VIM_VERSION_MAJOR
13227 && (minor < VIM_VERSION_MINOR
13228 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013229 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013230 }
13231 else
13232 n = has_patch(atoi((char *)name + 5));
13233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013234 else if (STRICMP(name, "vim_starting") == 0)
13235 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013236#ifdef FEAT_MBYTE
13237 else if (STRICMP(name, "multi_byte_encoding") == 0)
13238 n = has_mbyte;
13239#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013240#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13241 else if (STRICMP(name, "balloon_multiline") == 0)
13242 n = multiline_balloon_available();
13243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244#ifdef DYNAMIC_TCL
13245 else if (STRICMP(name, "tcl") == 0)
13246 n = tcl_enabled(FALSE);
13247#endif
13248#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13249 else if (STRICMP(name, "iconv") == 0)
13250 n = iconv_enabled(FALSE);
13251#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013252#ifdef DYNAMIC_LUA
13253 else if (STRICMP(name, "lua") == 0)
13254 n = lua_enabled(FALSE);
13255#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013256#ifdef DYNAMIC_MZSCHEME
13257 else if (STRICMP(name, "mzscheme") == 0)
13258 n = mzscheme_enabled(FALSE);
13259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260#ifdef DYNAMIC_RUBY
13261 else if (STRICMP(name, "ruby") == 0)
13262 n = ruby_enabled(FALSE);
13263#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013264#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265#ifdef DYNAMIC_PYTHON
13266 else if (STRICMP(name, "python") == 0)
13267 n = python_enabled(FALSE);
13268#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013269#endif
13270#ifdef FEAT_PYTHON3
13271#ifdef DYNAMIC_PYTHON3
13272 else if (STRICMP(name, "python3") == 0)
13273 n = python3_enabled(FALSE);
13274#endif
13275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013276#ifdef DYNAMIC_PERL
13277 else if (STRICMP(name, "perl") == 0)
13278 n = perl_enabled(FALSE);
13279#endif
13280#ifdef FEAT_GUI
13281 else if (STRICMP(name, "gui_running") == 0)
13282 n = (gui.in_use || gui.starting);
13283# ifdef FEAT_GUI_W32
13284 else if (STRICMP(name, "gui_win32s") == 0)
13285 n = gui_is_win32s();
13286# endif
13287# ifdef FEAT_BROWSE
13288 else if (STRICMP(name, "browse") == 0)
13289 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13290# endif
13291#endif
13292#ifdef FEAT_SYN_HL
13293 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013294 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295#endif
13296#if defined(WIN3264)
13297 else if (STRICMP(name, "win95") == 0)
13298 n = mch_windows95();
13299#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013300#ifdef FEAT_NETBEANS_INTG
13301 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013302 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304 }
13305
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013306 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013307}
13308
13309/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013310 * "has_key()" function
13311 */
13312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013313f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013314{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013315 if (argvars[0].v_type != VAR_DICT)
13316 {
13317 EMSG(_(e_dictreq));
13318 return;
13319 }
13320 if (argvars[0].vval.v_dict == NULL)
13321 return;
13322
13323 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013324 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013325}
13326
13327/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013328 * "haslocaldir()" function
13329 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013331f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013332{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013333 win_T *wp = NULL;
13334
13335 wp = find_tabwin(&argvars[0], &argvars[1]);
13336 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013337}
13338
13339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340 * "hasmapto()" function
13341 */
13342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013343f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013344{
13345 char_u *name;
13346 char_u *mode;
13347 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013348 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013350 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013351 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352 mode = (char_u *)"nvo";
13353 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013354 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013355 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013356 if (argvars[2].v_type != VAR_UNKNOWN)
13357 abbr = get_tv_number(&argvars[2]);
13358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013359
Bram Moolenaar2c932302006-03-18 21:42:09 +000013360 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013361 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013363 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364}
13365
13366/*
13367 * "histadd()" function
13368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013370f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013371{
13372#ifdef FEAT_CMDHIST
13373 int histype;
13374 char_u *str;
13375 char_u buf[NUMBUFLEN];
13376#endif
13377
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013378 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379 if (check_restricted() || check_secure())
13380 return;
13381#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013382 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13383 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384 if (histype >= 0)
13385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013386 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387 if (*str != NUL)
13388 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013389 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013391 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013392 return;
13393 }
13394 }
13395#endif
13396}
13397
13398/*
13399 * "histdel()" function
13400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013402f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403{
13404#ifdef FEAT_CMDHIST
13405 int n;
13406 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013407 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013408
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013409 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13410 if (str == NULL)
13411 n = 0;
13412 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013414 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013415 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013417 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013418 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013419 else
13420 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013421 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013422 get_tv_string_buf(&argvars[1], buf));
13423 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013424#endif
13425}
13426
13427/*
13428 * "histget()" function
13429 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013431f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432{
13433#ifdef FEAT_CMDHIST
13434 int type;
13435 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013436 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013437
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013438 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13439 if (str == NULL)
13440 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013442 {
13443 type = get_histtype(str);
13444 if (argvars[1].v_type == VAR_UNKNOWN)
13445 idx = get_history_idx(type);
13446 else
13447 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13448 /* -1 on type error */
13449 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013451#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013452 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013453#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013454 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013455}
13456
13457/*
13458 * "histnr()" function
13459 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013461f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462{
13463 int i;
13464
13465#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013466 char_u *history = get_tv_string_chk(&argvars[0]);
13467
13468 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013469 if (i >= HIST_CMD && i < HIST_COUNT)
13470 i = get_history_idx(i);
13471 else
13472#endif
13473 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013474 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475}
13476
13477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478 * "highlightID(name)" function
13479 */
13480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013481f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013483 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484}
13485
13486/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013487 * "highlight_exists()" function
13488 */
13489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013490f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013491{
13492 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13493}
13494
13495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496 * "hostname()" function
13497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013499f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500{
13501 char_u hostname[256];
13502
13503 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013504 rettv->v_type = VAR_STRING;
13505 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506}
13507
13508/*
13509 * iconv() function
13510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013512f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513{
13514#ifdef FEAT_MBYTE
13515 char_u buf1[NUMBUFLEN];
13516 char_u buf2[NUMBUFLEN];
13517 char_u *from, *to, *str;
13518 vimconv_T vimconv;
13519#endif
13520
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013521 rettv->v_type = VAR_STRING;
13522 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523
13524#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013525 str = get_tv_string(&argvars[0]);
13526 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13527 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528 vimconv.vc_type = CONV_NONE;
13529 convert_setup(&vimconv, from, to);
13530
13531 /* If the encodings are equal, no conversion needed. */
13532 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013533 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013535 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536
13537 convert_setup(&vimconv, NULL, NULL);
13538 vim_free(from);
13539 vim_free(to);
13540#endif
13541}
13542
13543/*
13544 * "indent()" function
13545 */
13546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013547f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548{
13549 linenr_T lnum;
13550
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013551 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013552 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013553 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013555 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013556}
13557
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013558/*
13559 * "index()" function
13560 */
13561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013562f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013563{
Bram Moolenaar33570922005-01-25 22:26:29 +000013564 list_T *l;
13565 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013566 long idx = 0;
13567 int ic = FALSE;
13568
13569 rettv->vval.v_number = -1;
13570 if (argvars[0].v_type != VAR_LIST)
13571 {
13572 EMSG(_(e_listreq));
13573 return;
13574 }
13575 l = argvars[0].vval.v_list;
13576 if (l != NULL)
13577 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013578 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013579 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013580 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013581 int error = FALSE;
13582
Bram Moolenaar758711c2005-02-02 23:11:38 +000013583 /* Start at specified item. Use the cached index that list_find()
13584 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013585 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013586 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013587 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013588 ic = get_tv_number_chk(&argvars[3], &error);
13589 if (error)
13590 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013591 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013592
Bram Moolenaar758711c2005-02-02 23:11:38 +000013593 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013594 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013595 {
13596 rettv->vval.v_number = idx;
13597 break;
13598 }
13599 }
13600}
13601
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602static int inputsecret_flag = 0;
13603
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013604static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013605
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013607 * This function is used by f_input() and f_inputdialog() functions. The third
13608 * argument to f_input() specifies the type of completion to use at the
13609 * prompt. The third argument to f_inputdialog() specifies the value to return
13610 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013611 */
13612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013613get_user_input(
13614 typval_T *argvars,
13615 typval_T *rettv,
13616 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013618 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619 char_u *p = NULL;
13620 int c;
13621 char_u buf[NUMBUFLEN];
13622 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013623 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013624 int xp_type = EXPAND_NOTHING;
13625 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013627 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013628 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629
13630#ifdef NO_CONSOLE_INPUT
13631 /* While starting up, there is no place to enter text. */
13632 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634#endif
13635
13636 cmd_silent = FALSE; /* Want to see the prompt. */
13637 if (prompt != NULL)
13638 {
13639 /* Only the part of the message after the last NL is considered as
13640 * prompt for the command line */
13641 p = vim_strrchr(prompt, '\n');
13642 if (p == NULL)
13643 p = prompt;
13644 else
13645 {
13646 ++p;
13647 c = *p;
13648 *p = NUL;
13649 msg_start();
13650 msg_clr_eos();
13651 msg_puts_attr(prompt, echo_attr);
13652 msg_didout = FALSE;
13653 msg_starthere();
13654 *p = c;
13655 }
13656 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013658 if (argvars[1].v_type != VAR_UNKNOWN)
13659 {
13660 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13661 if (defstr != NULL)
13662 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013664 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013665 {
13666 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013667 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013668 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013669
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013670 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013671 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013672
Bram Moolenaar4463f292005-09-25 22:20:24 +000013673 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13674 if (xp_name == NULL)
13675 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013676
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013677 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013678
Bram Moolenaar4463f292005-09-25 22:20:24 +000013679 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13680 &xp_arg) == FAIL)
13681 return;
13682 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013683 }
13684
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013685 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013686 {
13687# ifdef FEAT_EX_EXTRA
13688 int save_ex_normal_busy = ex_normal_busy;
13689 ex_normal_busy = 0;
13690# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013691 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013692 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13693 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013694# ifdef FEAT_EX_EXTRA
13695 ex_normal_busy = save_ex_normal_busy;
13696# endif
13697 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013698 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013699 && argvars[1].v_type != VAR_UNKNOWN
13700 && argvars[2].v_type != VAR_UNKNOWN)
13701 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13702 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013703
13704 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013706 /* since the user typed this, no need to wait for return */
13707 need_wait_return = FALSE;
13708 msg_didout = FALSE;
13709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710 cmd_silent = cmd_silent_save;
13711}
13712
13713/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013714 * "input()" function
13715 * Also handles inputsecret() when inputsecret is set.
13716 */
13717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013718f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013719{
13720 get_user_input(argvars, rettv, FALSE);
13721}
13722
13723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724 * "inputdialog()" function
13725 */
13726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013727f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728{
13729#if defined(FEAT_GUI_TEXTDIALOG)
13730 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13731 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13732 {
13733 char_u *message;
13734 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013735 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013737 message = get_tv_string_chk(&argvars[0]);
13738 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013739 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013740 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741 else
13742 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013743 if (message != NULL && defstr != NULL
13744 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013745 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013746 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013747 else
13748 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013749 if (message != NULL && defstr != NULL
13750 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013751 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013752 rettv->vval.v_string = vim_strsave(
13753 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013755 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013757 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758 }
13759 else
13760#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013761 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013762}
13763
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013764/*
13765 * "inputlist()" function
13766 */
13767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013768f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013769{
13770 listitem_T *li;
13771 int selected;
13772 int mouse_used;
13773
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013774#ifdef NO_CONSOLE_INPUT
13775 /* While starting up, there is no place to enter text. */
13776 if (no_console_input())
13777 return;
13778#endif
13779 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13780 {
13781 EMSG2(_(e_listarg), "inputlist()");
13782 return;
13783 }
13784
13785 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013786 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013787 lines_left = Rows; /* avoid more prompt */
13788 msg_scroll = TRUE;
13789 msg_clr_eos();
13790
13791 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13792 {
13793 msg_puts(get_tv_string(&li->li_tv));
13794 msg_putchar('\n');
13795 }
13796
13797 /* Ask for choice. */
13798 selected = prompt_for_number(&mouse_used);
13799 if (mouse_used)
13800 selected -= lines_left;
13801
13802 rettv->vval.v_number = selected;
13803}
13804
13805
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13807
13808/*
13809 * "inputrestore()" function
13810 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013812f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813{
13814 if (ga_userinput.ga_len > 0)
13815 {
13816 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013817 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13818 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013819 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820 }
13821 else if (p_verbose > 1)
13822 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013823 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013824 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825 }
13826}
13827
13828/*
13829 * "inputsave()" function
13830 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013832f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013834 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835 if (ga_grow(&ga_userinput, 1) == OK)
13836 {
13837 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13838 + ga_userinput.ga_len);
13839 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013840 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841 }
13842 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013843 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844}
13845
13846/*
13847 * "inputsecret()" function
13848 */
13849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013850f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013851{
13852 ++cmdline_star;
13853 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013854 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855 --cmdline_star;
13856 --inputsecret_flag;
13857}
13858
13859/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013860 * "insert()" function
13861 */
13862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013863f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013864{
13865 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013866 listitem_T *item;
13867 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013868 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013869
13870 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013871 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013872 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013873 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013874 {
13875 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013876 before = get_tv_number_chk(&argvars[2], &error);
13877 if (error)
13878 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013879
Bram Moolenaar758711c2005-02-02 23:11:38 +000013880 if (before == l->lv_len)
13881 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013882 else
13883 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013884 item = list_find(l, before);
13885 if (item == NULL)
13886 {
13887 EMSGN(_(e_listidx), before);
13888 l = NULL;
13889 }
13890 }
13891 if (l != NULL)
13892 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013893 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013894 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013895 }
13896 }
13897}
13898
13899/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013900 * "invert(expr)" function
13901 */
13902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013903f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013904{
13905 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13906}
13907
13908/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909 * "isdirectory()" function
13910 */
13911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013912f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013914 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915}
13916
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013917/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013918 * "islocked()" function
13919 */
13920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013921f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013922{
13923 lval_T lv;
13924 char_u *end;
13925 dictitem_T *di;
13926
13927 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013928 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13929 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013930 if (end != NULL && lv.ll_name != NULL)
13931 {
13932 if (*end != NUL)
13933 EMSG(_(e_trailing));
13934 else
13935 {
13936 if (lv.ll_tv == NULL)
13937 {
13938 if (check_changedtick(lv.ll_name))
13939 rettv->vval.v_number = 1; /* always locked */
13940 else
13941 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013942 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013943 if (di != NULL)
13944 {
13945 /* Consider a variable locked when:
13946 * 1. the variable itself is locked
13947 * 2. the value of the variable is locked.
13948 * 3. the List or Dict value is locked.
13949 */
13950 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13951 || tv_islocked(&di->di_tv));
13952 }
13953 }
13954 }
13955 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013956 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013957 else if (lv.ll_newkey != NULL)
13958 EMSG2(_(e_dictkey), lv.ll_newkey);
13959 else if (lv.ll_list != NULL)
13960 /* List item. */
13961 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13962 else
13963 /* Dictionary item. */
13964 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13965 }
13966 }
13967
13968 clear_lval(&lv);
13969}
13970
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013971static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013972
13973/*
13974 * Turn a dict into a list:
13975 * "what" == 0: list of keys
13976 * "what" == 1: list of values
13977 * "what" == 2: list of items
13978 */
13979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013980dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013981{
Bram Moolenaar33570922005-01-25 22:26:29 +000013982 list_T *l2;
13983 dictitem_T *di;
13984 hashitem_T *hi;
13985 listitem_T *li;
13986 listitem_T *li2;
13987 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013988 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013989
Bram Moolenaar8c711452005-01-14 21:53:12 +000013990 if (argvars[0].v_type != VAR_DICT)
13991 {
13992 EMSG(_(e_dictreq));
13993 return;
13994 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013995 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013996 return;
13997
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013998 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013999 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014000
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014001 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014002 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014003 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014004 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014005 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014006 --todo;
14007 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014008
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014009 li = listitem_alloc();
14010 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014011 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014012 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014013
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014014 if (what == 0)
14015 {
14016 /* keys() */
14017 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014018 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014019 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14020 }
14021 else if (what == 1)
14022 {
14023 /* values() */
14024 copy_tv(&di->di_tv, &li->li_tv);
14025 }
14026 else
14027 {
14028 /* items() */
14029 l2 = list_alloc();
14030 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014031 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014032 li->li_tv.vval.v_list = l2;
14033 if (l2 == NULL)
14034 break;
14035 ++l2->lv_refcount;
14036
14037 li2 = listitem_alloc();
14038 if (li2 == NULL)
14039 break;
14040 list_append(l2, li2);
14041 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014042 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014043 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14044
14045 li2 = listitem_alloc();
14046 if (li2 == NULL)
14047 break;
14048 list_append(l2, li2);
14049 copy_tv(&di->di_tv, &li2->li_tv);
14050 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014051 }
14052 }
14053}
14054
14055/*
14056 * "items(dict)" function
14057 */
14058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014059f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014060{
14061 dict_list(argvars, rettv, 2);
14062}
14063
Bram Moolenaar071d4272004-06-13 20:20:40 +000014064/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014065 * "join()" function
14066 */
14067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014068f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014069{
14070 garray_T ga;
14071 char_u *sep;
14072
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014073 if (argvars[0].v_type != VAR_LIST)
14074 {
14075 EMSG(_(e_listreq));
14076 return;
14077 }
14078 if (argvars[0].vval.v_list == NULL)
14079 return;
14080 if (argvars[1].v_type == VAR_UNKNOWN)
14081 sep = (char_u *)" ";
14082 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014083 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014084
14085 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014086
14087 if (sep != NULL)
14088 {
14089 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014090 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014091 ga_append(&ga, NUL);
14092 rettv->vval.v_string = (char_u *)ga.ga_data;
14093 }
14094 else
14095 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014096}
14097
14098/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014099 * "jsondecode()" function
14100 */
14101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014102f_jsondecode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014103{
14104 js_read_T reader;
14105
14106 reader.js_buf = get_tv_string(&argvars[0]);
14107 reader.js_eof = TRUE;
14108 reader.js_used = 0;
14109 json_decode(&reader, rettv);
14110}
14111
14112/*
14113 * "jsonencode()" function
14114 */
14115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014116f_jsonencode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014117{
14118 rettv->v_type = VAR_STRING;
14119 rettv->vval.v_string = json_encode(&argvars[0]);
14120}
14121
14122/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014123 * "keys()" function
14124 */
14125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014126f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014127{
14128 dict_list(argvars, rettv, 0);
14129}
14130
14131/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132 * "last_buffer_nr()" function.
14133 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014135f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136{
14137 int n = 0;
14138 buf_T *buf;
14139
14140 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14141 if (n < buf->b_fnum)
14142 n = buf->b_fnum;
14143
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014144 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014145}
14146
14147/*
14148 * "len()" function
14149 */
14150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014151f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014152{
14153 switch (argvars[0].v_type)
14154 {
14155 case VAR_STRING:
14156 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014157 rettv->vval.v_number = (varnumber_T)STRLEN(
14158 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014159 break;
14160 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014161 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014162 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014163 case VAR_DICT:
14164 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14165 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014166 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014167 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014168 break;
14169 }
14170}
14171
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014172static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014173
14174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014175libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014176{
14177#ifdef FEAT_LIBCALL
14178 char_u *string_in;
14179 char_u **string_result;
14180 int nr_result;
14181#endif
14182
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014183 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014184 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014185 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014186
14187 if (check_restricted() || check_secure())
14188 return;
14189
14190#ifdef FEAT_LIBCALL
14191 /* The first two args must be strings, otherwise its meaningless */
14192 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14193 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014194 string_in = NULL;
14195 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014196 string_in = argvars[2].vval.v_string;
14197 if (type == VAR_NUMBER)
14198 string_result = NULL;
14199 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014200 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014201 if (mch_libcall(argvars[0].vval.v_string,
14202 argvars[1].vval.v_string,
14203 string_in,
14204 argvars[2].vval.v_number,
14205 string_result,
14206 &nr_result) == OK
14207 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014208 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014209 }
14210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211}
14212
14213/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014214 * "libcall()" function
14215 */
14216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014217f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014218{
14219 libcall_common(argvars, rettv, VAR_STRING);
14220}
14221
14222/*
14223 * "libcallnr()" function
14224 */
14225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014226f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014227{
14228 libcall_common(argvars, rettv, VAR_NUMBER);
14229}
14230
14231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232 * "line(string)" function
14233 */
14234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014235f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236{
14237 linenr_T lnum = 0;
14238 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014239 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014241 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242 if (fp != NULL)
14243 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014244 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014245}
14246
14247/*
14248 * "line2byte(lnum)" function
14249 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014251f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252{
14253#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014254 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255#else
14256 linenr_T lnum;
14257
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014258 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014259 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014260 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014262 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14263 if (rettv->vval.v_number >= 0)
14264 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014265#endif
14266}
14267
14268/*
14269 * "lispindent(lnum)" function
14270 */
14271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014272f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273{
14274#ifdef FEAT_LISP
14275 pos_T pos;
14276 linenr_T lnum;
14277
14278 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014279 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14281 {
14282 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014283 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014284 curwin->w_cursor = pos;
14285 }
14286 else
14287#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014288 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289}
14290
14291/*
14292 * "localtime()" function
14293 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014295f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014296{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014297 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298}
14299
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014300static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301
14302 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014303get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014304{
14305 char_u *keys;
14306 char_u *which;
14307 char_u buf[NUMBUFLEN];
14308 char_u *keys_buf = NULL;
14309 char_u *rhs;
14310 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014311 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014312 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014313 mapblock_T *mp;
14314 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315
14316 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014317 rettv->v_type = VAR_STRING;
14318 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014320 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014321 if (*keys == NUL)
14322 return;
14323
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014324 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014325 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014326 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014327 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014328 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014329 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014330 if (argvars[3].v_type != VAR_UNKNOWN)
14331 get_dict = get_tv_number(&argvars[3]);
14332 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014334 else
14335 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014336 if (which == NULL)
14337 return;
14338
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339 mode = get_map_mode(&which, 0);
14340
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014341 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014342 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014344
14345 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014347 /* Return a string. */
14348 if (rhs != NULL)
14349 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350
Bram Moolenaarbd743252010-10-20 21:23:33 +020014351 }
14352 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14353 {
14354 /* Return a dictionary. */
14355 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14356 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14357 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358
Bram Moolenaarbd743252010-10-20 21:23:33 +020014359 dict_add_nr_str(dict, "lhs", 0L, lhs);
14360 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14361 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14362 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14363 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14364 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14365 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014366 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014367 dict_add_nr_str(dict, "mode", 0L, mapmode);
14368
14369 vim_free(lhs);
14370 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371 }
14372}
14373
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014374#ifdef FEAT_FLOAT
14375/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014376 * "log()" function
14377 */
14378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014379f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014380{
14381 float_T f;
14382
14383 rettv->v_type = VAR_FLOAT;
14384 if (get_float_arg(argvars, &f) == OK)
14385 rettv->vval.v_float = log(f);
14386 else
14387 rettv->vval.v_float = 0.0;
14388}
14389
14390/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014391 * "log10()" function
14392 */
14393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014394f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014395{
14396 float_T f;
14397
14398 rettv->v_type = VAR_FLOAT;
14399 if (get_float_arg(argvars, &f) == OK)
14400 rettv->vval.v_float = log10(f);
14401 else
14402 rettv->vval.v_float = 0.0;
14403}
14404#endif
14405
Bram Moolenaar1dced572012-04-05 16:54:08 +020014406#ifdef FEAT_LUA
14407/*
14408 * "luaeval()" function
14409 */
14410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014411f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020014412{
14413 char_u *str;
14414 char_u buf[NUMBUFLEN];
14415
14416 str = get_tv_string_buf(&argvars[0], buf);
14417 do_luaeval(str, argvars + 1, rettv);
14418}
14419#endif
14420
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014422 * "map()" function
14423 */
14424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014425f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014426{
14427 filter_map(argvars, rettv, TRUE);
14428}
14429
14430/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014431 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432 */
14433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014434f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014436 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437}
14438
14439/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014440 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014441 */
14442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014443f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014445 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446}
14447
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014448static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014449
14450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014451find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014452{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014453 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014454 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014455 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456 char_u *pat;
14457 regmatch_T regmatch;
14458 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014459 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014460 char_u *save_cpo;
14461 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014462 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014463 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014464 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014465 list_T *l = NULL;
14466 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014467 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014468 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014469
14470 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14471 save_cpo = p_cpo;
14472 p_cpo = (char_u *)"";
14473
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014474 rettv->vval.v_number = -1;
14475 if (type == 3)
14476 {
14477 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014478 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014479 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014480 }
14481 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014483 rettv->v_type = VAR_STRING;
14484 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014487 if (argvars[0].v_type == VAR_LIST)
14488 {
14489 if ((l = argvars[0].vval.v_list) == NULL)
14490 goto theend;
14491 li = l->lv_first;
14492 }
14493 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014494 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014495 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014496 len = (long)STRLEN(str);
14497 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014498
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014499 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14500 if (pat == NULL)
14501 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014502
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014503 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014505 int error = FALSE;
14506
14507 start = get_tv_number_chk(&argvars[2], &error);
14508 if (error)
14509 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014510 if (l != NULL)
14511 {
14512 li = list_find(l, start);
14513 if (li == NULL)
14514 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014515 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014516 }
14517 else
14518 {
14519 if (start < 0)
14520 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014521 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014522 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014523 /* When "count" argument is there ignore matches before "start",
14524 * otherwise skip part of the string. Differs when pattern is "^"
14525 * or "\<". */
14526 if (argvars[3].v_type != VAR_UNKNOWN)
14527 startcol = start;
14528 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014529 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014530 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014531 len -= start;
14532 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014533 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014534
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014535 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014536 nth = get_tv_number_chk(&argvars[3], &error);
14537 if (error)
14538 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539 }
14540
14541 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14542 if (regmatch.regprog != NULL)
14543 {
14544 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014545
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014546 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014547 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014548 if (l != NULL)
14549 {
14550 if (li == NULL)
14551 {
14552 match = FALSE;
14553 break;
14554 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014555 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014556 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014557 if (str == NULL)
14558 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014559 }
14560
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014561 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014562
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014563 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014564 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014565 if (l == NULL && !match)
14566 break;
14567
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014568 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014569 if (l != NULL)
14570 {
14571 li = li->li_next;
14572 ++idx;
14573 }
14574 else
14575 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014576#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014577 startcol = (colnr_T)(regmatch.startp[0]
14578 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014579#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014580 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014581#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014582 if (startcol > (colnr_T)len
14583 || str + startcol <= regmatch.startp[0])
14584 {
14585 match = FALSE;
14586 break;
14587 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014588 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014589 }
14590
14591 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014592 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014593 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014594 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014595 int i;
14596
14597 /* return list with matched string and submatches */
14598 for (i = 0; i < NSUBEXP; ++i)
14599 {
14600 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014601 {
14602 if (list_append_string(rettv->vval.v_list,
14603 (char_u *)"", 0) == FAIL)
14604 break;
14605 }
14606 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014607 regmatch.startp[i],
14608 (int)(regmatch.endp[i] - regmatch.startp[i]))
14609 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014610 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014611 }
14612 }
14613 else if (type == 2)
14614 {
14615 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014616 if (l != NULL)
14617 copy_tv(&li->li_tv, rettv);
14618 else
14619 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014620 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014621 }
14622 else if (l != NULL)
14623 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624 else
14625 {
14626 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014627 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014628 (varnumber_T)(regmatch.startp[0] - str);
14629 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014630 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014631 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014632 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014633 }
14634 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014635 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636 }
14637
14638theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014639 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014640 p_cpo = save_cpo;
14641}
14642
14643/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014644 * "match()" function
14645 */
14646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014647f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014648{
14649 find_some_match(argvars, rettv, 1);
14650}
14651
14652/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014653 * "matchadd()" function
14654 */
14655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014656f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014657{
14658#ifdef FEAT_SEARCH_EXTRA
14659 char_u buf[NUMBUFLEN];
14660 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14661 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14662 int prio = 10; /* default priority */
14663 int id = -1;
14664 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014665 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014666
14667 rettv->vval.v_number = -1;
14668
14669 if (grp == NULL || pat == NULL)
14670 return;
14671 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014672 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014673 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014674 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014675 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014676 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014677 if (argvars[4].v_type != VAR_UNKNOWN)
14678 {
14679 if (argvars[4].v_type != VAR_DICT)
14680 {
14681 EMSG(_(e_dictreq));
14682 return;
14683 }
14684 if (dict_find(argvars[4].vval.v_dict,
14685 (char_u *)"conceal", -1) != NULL)
14686 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14687 (char_u *)"conceal", FALSE);
14688 }
14689 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014690 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014691 if (error == TRUE)
14692 return;
14693 if (id >= 1 && id <= 3)
14694 {
14695 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14696 return;
14697 }
14698
Bram Moolenaar6561d522015-07-21 15:48:27 +020014699 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14700 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014701#endif
14702}
14703
14704/*
14705 * "matchaddpos()" function
14706 */
14707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014708f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020014709{
14710#ifdef FEAT_SEARCH_EXTRA
14711 char_u buf[NUMBUFLEN];
14712 char_u *group;
14713 int prio = 10;
14714 int id = -1;
14715 int error = FALSE;
14716 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014717 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014718
14719 rettv->vval.v_number = -1;
14720
14721 group = get_tv_string_buf_chk(&argvars[0], buf);
14722 if (group == NULL)
14723 return;
14724
14725 if (argvars[1].v_type != VAR_LIST)
14726 {
14727 EMSG2(_(e_listarg), "matchaddpos()");
14728 return;
14729 }
14730 l = argvars[1].vval.v_list;
14731 if (l == NULL)
14732 return;
14733
14734 if (argvars[2].v_type != VAR_UNKNOWN)
14735 {
14736 prio = get_tv_number_chk(&argvars[2], &error);
14737 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014738 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014739 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014740 if (argvars[4].v_type != VAR_UNKNOWN)
14741 {
14742 if (argvars[4].v_type != VAR_DICT)
14743 {
14744 EMSG(_(e_dictreq));
14745 return;
14746 }
14747 if (dict_find(argvars[4].vval.v_dict,
14748 (char_u *)"conceal", -1) != NULL)
14749 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14750 (char_u *)"conceal", FALSE);
14751 }
14752 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014753 }
14754 if (error == TRUE)
14755 return;
14756
14757 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14758 if (id == 1 || id == 2)
14759 {
14760 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14761 return;
14762 }
14763
Bram Moolenaar6561d522015-07-21 15:48:27 +020014764 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14765 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014766#endif
14767}
14768
14769/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014770 * "matcharg()" function
14771 */
14772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014773f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014774{
14775 if (rettv_list_alloc(rettv) == OK)
14776 {
14777#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014778 int id = get_tv_number(&argvars[0]);
14779 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014780
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014781 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014782 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014783 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14784 {
14785 list_append_string(rettv->vval.v_list,
14786 syn_id2name(m->hlg_id), -1);
14787 list_append_string(rettv->vval.v_list, m->pattern, -1);
14788 }
14789 else
14790 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014791 list_append_string(rettv->vval.v_list, NULL, -1);
14792 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014793 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014794 }
14795#endif
14796 }
14797}
14798
14799/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014800 * "matchdelete()" function
14801 */
14802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014803f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014804{
14805#ifdef FEAT_SEARCH_EXTRA
14806 rettv->vval.v_number = match_delete(curwin,
14807 (int)get_tv_number(&argvars[0]), TRUE);
14808#endif
14809}
14810
14811/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014812 * "matchend()" function
14813 */
14814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014815f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014816{
14817 find_some_match(argvars, rettv, 0);
14818}
14819
14820/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014821 * "matchlist()" function
14822 */
14823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014824f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014825{
14826 find_some_match(argvars, rettv, 3);
14827}
14828
14829/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014830 * "matchstr()" function
14831 */
14832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014833f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014834{
14835 find_some_match(argvars, rettv, 2);
14836}
14837
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014838static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014839
14840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014841max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014842{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014843 long n = 0;
14844 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014845 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014846
14847 if (argvars[0].v_type == VAR_LIST)
14848 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014849 list_T *l;
14850 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014851
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014852 l = argvars[0].vval.v_list;
14853 if (l != NULL)
14854 {
14855 li = l->lv_first;
14856 if (li != NULL)
14857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014858 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014859 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014860 {
14861 li = li->li_next;
14862 if (li == NULL)
14863 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014864 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014865 if (domax ? i > n : i < n)
14866 n = i;
14867 }
14868 }
14869 }
14870 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014871 else if (argvars[0].v_type == VAR_DICT)
14872 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014873 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014874 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014875 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014876 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014877
14878 d = argvars[0].vval.v_dict;
14879 if (d != NULL)
14880 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014881 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014882 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014883 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014884 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014885 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014886 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014887 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014888 if (first)
14889 {
14890 n = i;
14891 first = FALSE;
14892 }
14893 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014894 n = i;
14895 }
14896 }
14897 }
14898 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014899 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014900 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014901 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014902}
14903
14904/*
14905 * "max()" function
14906 */
14907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014908f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014909{
14910 max_min(argvars, rettv, TRUE);
14911}
14912
14913/*
14914 * "min()" function
14915 */
14916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014917f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014918{
14919 max_min(argvars, rettv, FALSE);
14920}
14921
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014922static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014923
14924/*
14925 * Create the directory in which "dir" is located, and higher levels when
14926 * needed.
14927 */
14928 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010014929mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014930{
14931 char_u *p;
14932 char_u *updir;
14933 int r = FAIL;
14934
14935 /* Get end of directory name in "dir".
14936 * We're done when it's "/" or "c:/". */
14937 p = gettail_sep(dir);
14938 if (p <= get_past_head(dir))
14939 return OK;
14940
14941 /* If the directory exists we're done. Otherwise: create it.*/
14942 updir = vim_strnsave(dir, (int)(p - dir));
14943 if (updir == NULL)
14944 return FAIL;
14945 if (mch_isdir(updir))
14946 r = OK;
14947 else if (mkdir_recurse(updir, prot) == OK)
14948 r = vim_mkdir_emsg(updir, prot);
14949 vim_free(updir);
14950 return r;
14951}
14952
14953#ifdef vim_mkdir
14954/*
14955 * "mkdir()" function
14956 */
14957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014958f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014959{
14960 char_u *dir;
14961 char_u buf[NUMBUFLEN];
14962 int prot = 0755;
14963
14964 rettv->vval.v_number = FAIL;
14965 if (check_restricted() || check_secure())
14966 return;
14967
14968 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014969 if (*dir == NUL)
14970 rettv->vval.v_number = FAIL;
14971 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014972 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014973 if (*gettail(dir) == NUL)
14974 /* remove trailing slashes */
14975 *gettail_sep(dir) = NUL;
14976
14977 if (argvars[1].v_type != VAR_UNKNOWN)
14978 {
14979 if (argvars[2].v_type != VAR_UNKNOWN)
14980 prot = get_tv_number_chk(&argvars[2], NULL);
14981 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14982 mkdir_recurse(dir, prot);
14983 }
14984 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014985 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014986}
14987#endif
14988
Bram Moolenaar0d660222005-01-07 21:51:51 +000014989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014990 * "mode()" function
14991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014993f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014994{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014995 char_u buf[3];
14996
14997 buf[1] = NUL;
14998 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014999
Bram Moolenaar071d4272004-06-13 20:20:40 +000015000 if (VIsual_active)
15001 {
15002 if (VIsual_select)
15003 buf[0] = VIsual_mode + 's' - 'v';
15004 else
15005 buf[0] = VIsual_mode;
15006 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015007 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015008 || State == CONFIRM)
15009 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015010 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015011 if (State == ASKMORE)
15012 buf[1] = 'm';
15013 else if (State == CONFIRM)
15014 buf[1] = '?';
15015 }
15016 else if (State == EXTERNCMD)
15017 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015018 else if (State & INSERT)
15019 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015020#ifdef FEAT_VREPLACE
15021 if (State & VREPLACE_FLAG)
15022 {
15023 buf[0] = 'R';
15024 buf[1] = 'v';
15025 }
15026 else
15027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015028 if (State & REPLACE_FLAG)
15029 buf[0] = 'R';
15030 else
15031 buf[0] = 'i';
15032 }
15033 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015034 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015035 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015036 if (exmode_active)
15037 buf[1] = 'v';
15038 }
15039 else if (exmode_active)
15040 {
15041 buf[0] = 'c';
15042 buf[1] = 'e';
15043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015045 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015046 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015047 if (finish_op)
15048 buf[1] = 'o';
15049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015050
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015051 /* Clear out the minor mode when the argument is not a non-zero number or
15052 * non-empty string. */
15053 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015054 buf[1] = NUL;
15055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015056 rettv->vval.v_string = vim_strsave(buf);
15057 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058}
15059
Bram Moolenaar429fa852013-04-15 12:27:36 +020015060#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015061/*
15062 * "mzeval()" function
15063 */
15064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015065f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015066{
15067 char_u *str;
15068 char_u buf[NUMBUFLEN];
15069
15070 str = get_tv_string_buf(&argvars[0], buf);
15071 do_mzeval(str, rettv);
15072}
Bram Moolenaar75676462013-01-30 14:55:42 +010015073
15074 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015075mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015076{
15077 typval_T argvars[3];
15078
15079 argvars[0].v_type = VAR_STRING;
15080 argvars[0].vval.v_string = name;
15081 copy_tv(args, &argvars[1]);
15082 argvars[2].v_type = VAR_UNKNOWN;
15083 f_call(argvars, rettv);
15084 clear_tv(&argvars[1]);
15085}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015086#endif
15087
Bram Moolenaar071d4272004-06-13 20:20:40 +000015088/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015089 * "nextnonblank()" function
15090 */
15091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015092f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015093{
15094 linenr_T lnum;
15095
15096 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15097 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015098 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015099 {
15100 lnum = 0;
15101 break;
15102 }
15103 if (*skipwhite(ml_get(lnum)) != NUL)
15104 break;
15105 }
15106 rettv->vval.v_number = lnum;
15107}
15108
15109/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015110 * "nr2char()" function
15111 */
15112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015113f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015114{
15115 char_u buf[NUMBUFLEN];
15116
15117#ifdef FEAT_MBYTE
15118 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015119 {
15120 int utf8 = 0;
15121
15122 if (argvars[1].v_type != VAR_UNKNOWN)
15123 utf8 = get_tv_number_chk(&argvars[1], NULL);
15124 if (utf8)
15125 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15126 else
15127 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015129 else
15130#endif
15131 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015132 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015133 buf[1] = NUL;
15134 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015135 rettv->v_type = VAR_STRING;
15136 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015137}
15138
15139/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015140 * "or(expr, expr)" function
15141 */
15142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015143f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015144{
15145 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15146 | get_tv_number_chk(&argvars[1], NULL);
15147}
15148
15149/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015150 * "pathshorten()" function
15151 */
15152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015153f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015154{
15155 char_u *p;
15156
15157 rettv->v_type = VAR_STRING;
15158 p = get_tv_string_chk(&argvars[0]);
15159 if (p == NULL)
15160 rettv->vval.v_string = NULL;
15161 else
15162 {
15163 p = vim_strsave(p);
15164 rettv->vval.v_string = p;
15165 if (p != NULL)
15166 shorten_dir(p);
15167 }
15168}
15169
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015170#ifdef FEAT_PERL
15171/*
15172 * "perleval()" function
15173 */
15174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015175f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015176{
15177 char_u *str;
15178 char_u buf[NUMBUFLEN];
15179
15180 str = get_tv_string_buf(&argvars[0], buf);
15181 do_perleval(str, rettv);
15182}
15183#endif
15184
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015185#ifdef FEAT_FLOAT
15186/*
15187 * "pow()" function
15188 */
15189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015190f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015191{
15192 float_T fx, fy;
15193
15194 rettv->v_type = VAR_FLOAT;
15195 if (get_float_arg(argvars, &fx) == OK
15196 && get_float_arg(&argvars[1], &fy) == OK)
15197 rettv->vval.v_float = pow(fx, fy);
15198 else
15199 rettv->vval.v_float = 0.0;
15200}
15201#endif
15202
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015203/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015204 * "prevnonblank()" function
15205 */
15206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015207f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015208{
15209 linenr_T lnum;
15210
15211 lnum = get_tv_lnum(argvars);
15212 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15213 lnum = 0;
15214 else
15215 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15216 --lnum;
15217 rettv->vval.v_number = lnum;
15218}
15219
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015220/* This dummy va_list is here because:
15221 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15222 * - locally in the function results in a "used before set" warning
15223 * - using va_start() to initialize it gives "function with fixed args" error */
15224static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015225
Bram Moolenaar8c711452005-01-14 21:53:12 +000015226/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015227 * "printf()" function
15228 */
15229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015230f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015231{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015232 char_u buf[NUMBUFLEN];
15233 int len;
15234 char_u *s;
15235 int saved_did_emsg = did_emsg;
15236 char *fmt;
15237
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015238 rettv->v_type = VAR_STRING;
15239 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015240
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015241 /* Get the required length, allocate the buffer and do it for real. */
15242 did_emsg = FALSE;
15243 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15244 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15245 if (!did_emsg)
15246 {
15247 s = alloc(len + 1);
15248 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015249 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015250 rettv->vval.v_string = s;
15251 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015252 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015253 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015254 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015255}
15256
15257/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015258 * "pumvisible()" function
15259 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015261f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015262{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015263#ifdef FEAT_INS_EXPAND
15264 if (pum_visible())
15265 rettv->vval.v_number = 1;
15266#endif
15267}
15268
Bram Moolenaardb913952012-06-29 12:54:53 +020015269#ifdef FEAT_PYTHON3
15270/*
15271 * "py3eval()" function
15272 */
15273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015274f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015275{
15276 char_u *str;
15277 char_u buf[NUMBUFLEN];
15278
15279 str = get_tv_string_buf(&argvars[0], buf);
15280 do_py3eval(str, rettv);
15281}
15282#endif
15283
15284#ifdef FEAT_PYTHON
15285/*
15286 * "pyeval()" function
15287 */
15288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015289f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015290{
15291 char_u *str;
15292 char_u buf[NUMBUFLEN];
15293
15294 str = get_tv_string_buf(&argvars[0], buf);
15295 do_pyeval(str, rettv);
15296}
15297#endif
15298
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015299/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015300 * "range()" function
15301 */
15302 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015303f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015304{
15305 long start;
15306 long end;
15307 long stride = 1;
15308 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015309 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015310
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015311 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015312 if (argvars[1].v_type == VAR_UNKNOWN)
15313 {
15314 end = start - 1;
15315 start = 0;
15316 }
15317 else
15318 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015319 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015320 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015321 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015322 }
15323
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015324 if (error)
15325 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015326 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015327 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015328 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015329 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015330 else
15331 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015332 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015333 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015334 if (list_append_number(rettv->vval.v_list,
15335 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015336 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015337 }
15338}
15339
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015340/*
15341 * "readfile()" function
15342 */
15343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015344f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015345{
15346 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015347 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015348 char_u *fname;
15349 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015350 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15351 int io_size = sizeof(buf);
15352 int readlen; /* size of last fread() */
15353 char_u *prev = NULL; /* previously read bytes, if any */
15354 long prevlen = 0; /* length of data in prev */
15355 long prevsize = 0; /* size of prev buffer */
15356 long maxline = MAXLNUM;
15357 long cnt = 0;
15358 char_u *p; /* position in buf */
15359 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015360
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015361 if (argvars[1].v_type != VAR_UNKNOWN)
15362 {
15363 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15364 binary = TRUE;
15365 if (argvars[2].v_type != VAR_UNKNOWN)
15366 maxline = get_tv_number(&argvars[2]);
15367 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015368
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015369 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015370 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015371
15372 /* Always open the file in binary mode, library functions have a mind of
15373 * their own about CR-LF conversion. */
15374 fname = get_tv_string(&argvars[0]);
15375 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15376 {
15377 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15378 return;
15379 }
15380
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015381 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015382 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015383 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015384
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015385 /* This for loop processes what was read, but is also entered at end
15386 * of file so that either:
15387 * - an incomplete line gets written
15388 * - a "binary" file gets an empty line at the end if it ends in a
15389 * newline. */
15390 for (p = buf, start = buf;
15391 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15392 ++p)
15393 {
15394 if (*p == '\n' || readlen <= 0)
15395 {
15396 listitem_T *li;
15397 char_u *s = NULL;
15398 long_u len = p - start;
15399
15400 /* Finished a line. Remove CRs before NL. */
15401 if (readlen > 0 && !binary)
15402 {
15403 while (len > 0 && start[len - 1] == '\r')
15404 --len;
15405 /* removal may cross back to the "prev" string */
15406 if (len == 0)
15407 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15408 --prevlen;
15409 }
15410 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015411 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015412 else
15413 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015414 /* Change "prev" buffer to be the right size. This way
15415 * the bytes are only copied once, and very long lines are
15416 * allocated only once. */
15417 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015418 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015419 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015420 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015421 prev = NULL; /* the list will own the string */
15422 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015423 }
15424 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015425 if (s == NULL)
15426 {
15427 do_outofmem_msg((long_u) prevlen + len + 1);
15428 failed = TRUE;
15429 break;
15430 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015431
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015432 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015433 {
15434 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015435 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015436 break;
15437 }
15438 li->li_tv.v_type = VAR_STRING;
15439 li->li_tv.v_lock = 0;
15440 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015441 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015442
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015443 start = p + 1; /* step over newline */
15444 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015445 break;
15446 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015447 else if (*p == NUL)
15448 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015449#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015450 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15451 * when finding the BF and check the previous two bytes. */
15452 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015453 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015454 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15455 * + 1, these may be in the "prev" string. */
15456 char_u back1 = p >= buf + 1 ? p[-1]
15457 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15458 char_u back2 = p >= buf + 2 ? p[-2]
15459 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15460 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015461
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015462 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015463 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015464 char_u *dest = p - 2;
15465
15466 /* Usually a BOM is at the beginning of a file, and so at
15467 * the beginning of a line; then we can just step over it.
15468 */
15469 if (start == dest)
15470 start = p + 1;
15471 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015472 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015473 /* have to shuffle buf to close gap */
15474 int adjust_prevlen = 0;
15475
15476 if (dest < buf)
15477 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015478 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015479 dest = buf;
15480 }
15481 if (readlen > p - buf + 1)
15482 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15483 readlen -= 3 - adjust_prevlen;
15484 prevlen -= adjust_prevlen;
15485 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015486 }
15487 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015488 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015489#endif
15490 } /* for */
15491
15492 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15493 break;
15494 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015495 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015496 /* There's part of a line in buf, store it in "prev". */
15497 if (p - start + prevlen >= prevsize)
15498 {
15499 /* need bigger "prev" buffer */
15500 char_u *newprev;
15501
15502 /* A common use case is ordinary text files and "prev" gets a
15503 * fragment of a line, so the first allocation is made
15504 * small, to avoid repeatedly 'allocing' large and
15505 * 'reallocing' small. */
15506 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015507 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015508 else
15509 {
15510 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015511 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015512 prevsize = grow50pc > growmin ? grow50pc : growmin;
15513 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015514 newprev = prev == NULL ? alloc(prevsize)
15515 : vim_realloc(prev, prevsize);
15516 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015517 {
15518 do_outofmem_msg((long_u)prevsize);
15519 failed = TRUE;
15520 break;
15521 }
15522 prev = newprev;
15523 }
15524 /* Add the line part to end of "prev". */
15525 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015526 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015527 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015528 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015529
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015530 /*
15531 * For a negative line count use only the lines at the end of the file,
15532 * free the rest.
15533 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015534 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015535 while (cnt > -maxline)
15536 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015537 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015538 --cnt;
15539 }
15540
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015541 if (failed)
15542 {
15543 list_free(rettv->vval.v_list, TRUE);
15544 /* readfile doc says an empty list is returned on error */
15545 rettv->vval.v_list = list_alloc();
15546 }
15547
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015548 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015549 fclose(fd);
15550}
15551
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015552#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015553static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015554
15555/*
15556 * Convert a List to proftime_T.
15557 * Return FAIL when there is something wrong.
15558 */
15559 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015560list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015561{
15562 long n1, n2;
15563 int error = FALSE;
15564
15565 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15566 || arg->vval.v_list->lv_len != 2)
15567 return FAIL;
15568 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15569 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15570# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015571 tm->HighPart = n1;
15572 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015573# else
15574 tm->tv_sec = n1;
15575 tm->tv_usec = n2;
15576# endif
15577 return error ? FAIL : OK;
15578}
15579#endif /* FEAT_RELTIME */
15580
15581/*
15582 * "reltime()" function
15583 */
15584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015585f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015586{
15587#ifdef FEAT_RELTIME
15588 proftime_T res;
15589 proftime_T start;
15590
15591 if (argvars[0].v_type == VAR_UNKNOWN)
15592 {
15593 /* No arguments: get current time. */
15594 profile_start(&res);
15595 }
15596 else if (argvars[1].v_type == VAR_UNKNOWN)
15597 {
15598 if (list2proftime(&argvars[0], &res) == FAIL)
15599 return;
15600 profile_end(&res);
15601 }
15602 else
15603 {
15604 /* Two arguments: compute the difference. */
15605 if (list2proftime(&argvars[0], &start) == FAIL
15606 || list2proftime(&argvars[1], &res) == FAIL)
15607 return;
15608 profile_sub(&res, &start);
15609 }
15610
15611 if (rettv_list_alloc(rettv) == OK)
15612 {
15613 long n1, n2;
15614
15615# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015616 n1 = res.HighPart;
15617 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015618# else
15619 n1 = res.tv_sec;
15620 n2 = res.tv_usec;
15621# endif
15622 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15623 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15624 }
15625#endif
15626}
15627
15628/*
15629 * "reltimestr()" function
15630 */
15631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015632f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015633{
15634#ifdef FEAT_RELTIME
15635 proftime_T tm;
15636#endif
15637
15638 rettv->v_type = VAR_STRING;
15639 rettv->vval.v_string = NULL;
15640#ifdef FEAT_RELTIME
15641 if (list2proftime(&argvars[0], &tm) == OK)
15642 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15643#endif
15644}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015645
Bram Moolenaar0d660222005-01-07 21:51:51 +000015646#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015647static void make_connection(void);
15648static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015649
15650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015651make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015652{
15653 if (X_DISPLAY == NULL
15654# ifdef FEAT_GUI
15655 && !gui.in_use
15656# endif
15657 )
15658 {
15659 x_force_connect = TRUE;
15660 setup_term_clip();
15661 x_force_connect = FALSE;
15662 }
15663}
15664
15665 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015666check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015667{
15668 make_connection();
15669 if (X_DISPLAY == NULL)
15670 {
15671 EMSG(_("E240: No connection to Vim server"));
15672 return FAIL;
15673 }
15674 return OK;
15675}
15676#endif
15677
15678#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015679static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015680
15681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015682remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015683{
15684 char_u *server_name;
15685 char_u *keys;
15686 char_u *r = NULL;
15687 char_u buf[NUMBUFLEN];
15688# ifdef WIN32
15689 HWND w;
15690# else
15691 Window w;
15692# endif
15693
15694 if (check_restricted() || check_secure())
15695 return;
15696
15697# ifdef FEAT_X11
15698 if (check_connection() == FAIL)
15699 return;
15700# endif
15701
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015702 server_name = get_tv_string_chk(&argvars[0]);
15703 if (server_name == NULL)
15704 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015705 keys = get_tv_string_buf(&argvars[1], buf);
15706# ifdef WIN32
15707 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15708# else
15709 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15710 < 0)
15711# endif
15712 {
15713 if (r != NULL)
15714 EMSG(r); /* sending worked but evaluation failed */
15715 else
15716 EMSG2(_("E241: Unable to send to %s"), server_name);
15717 return;
15718 }
15719
15720 rettv->vval.v_string = r;
15721
15722 if (argvars[2].v_type != VAR_UNKNOWN)
15723 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015724 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015725 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015726 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015727
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015728 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015729 v.di_tv.v_type = VAR_STRING;
15730 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015731 idvar = get_tv_string_chk(&argvars[2]);
15732 if (idvar != NULL)
15733 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015734 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015735 }
15736}
15737#endif
15738
15739/*
15740 * "remote_expr()" function
15741 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015743f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015744{
15745 rettv->v_type = VAR_STRING;
15746 rettv->vval.v_string = NULL;
15747#ifdef FEAT_CLIENTSERVER
15748 remote_common(argvars, rettv, TRUE);
15749#endif
15750}
15751
15752/*
15753 * "remote_foreground()" function
15754 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015756f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015757{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015758#ifdef FEAT_CLIENTSERVER
15759# ifdef WIN32
15760 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015761 {
15762 char_u *server_name = get_tv_string_chk(&argvars[0]);
15763
15764 if (server_name != NULL)
15765 serverForeground(server_name);
15766 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015767# else
15768 /* Send a foreground() expression to the server. */
15769 argvars[1].v_type = VAR_STRING;
15770 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15771 argvars[2].v_type = VAR_UNKNOWN;
15772 remote_common(argvars, rettv, TRUE);
15773 vim_free(argvars[1].vval.v_string);
15774# endif
15775#endif
15776}
15777
Bram Moolenaar0d660222005-01-07 21:51:51 +000015778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015779f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015780{
15781#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015782 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015783 char_u *s = NULL;
15784# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015785 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015786# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015787 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015788
15789 if (check_restricted() || check_secure())
15790 {
15791 rettv->vval.v_number = -1;
15792 return;
15793 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015794 serverid = get_tv_string_chk(&argvars[0]);
15795 if (serverid == NULL)
15796 {
15797 rettv->vval.v_number = -1;
15798 return; /* type error; errmsg already given */
15799 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015800# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015801 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015802 if (n == 0)
15803 rettv->vval.v_number = -1;
15804 else
15805 {
15806 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15807 rettv->vval.v_number = (s != NULL);
15808 }
15809# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015810 if (check_connection() == FAIL)
15811 return;
15812
15813 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015814 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015815# endif
15816
15817 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15818 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015819 char_u *retvar;
15820
Bram Moolenaar33570922005-01-25 22:26:29 +000015821 v.di_tv.v_type = VAR_STRING;
15822 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015823 retvar = get_tv_string_chk(&argvars[1]);
15824 if (retvar != NULL)
15825 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015826 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015827 }
15828#else
15829 rettv->vval.v_number = -1;
15830#endif
15831}
15832
Bram Moolenaar0d660222005-01-07 21:51:51 +000015833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015834f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015835{
15836 char_u *r = NULL;
15837
15838#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015839 char_u *serverid = get_tv_string_chk(&argvars[0]);
15840
15841 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015842 {
15843# ifdef WIN32
15844 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015845 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015846
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015847 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015848 if (n != 0)
15849 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15850 if (r == NULL)
15851# else
15852 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015853 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015854# endif
15855 EMSG(_("E277: Unable to read a server reply"));
15856 }
15857#endif
15858 rettv->v_type = VAR_STRING;
15859 rettv->vval.v_string = r;
15860}
15861
15862/*
15863 * "remote_send()" function
15864 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015866f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015867{
15868 rettv->v_type = VAR_STRING;
15869 rettv->vval.v_string = NULL;
15870#ifdef FEAT_CLIENTSERVER
15871 remote_common(argvars, rettv, FALSE);
15872#endif
15873}
15874
15875/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015876 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015877 */
15878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015879f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015880{
Bram Moolenaar33570922005-01-25 22:26:29 +000015881 list_T *l;
15882 listitem_T *item, *item2;
15883 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015884 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015885 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015886 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015887 dict_T *d;
15888 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020015889 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015890
Bram Moolenaar8c711452005-01-14 21:53:12 +000015891 if (argvars[0].v_type == VAR_DICT)
15892 {
15893 if (argvars[2].v_type != VAR_UNKNOWN)
15894 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015895 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015896 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015897 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015898 key = get_tv_string_chk(&argvars[1]);
15899 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015900 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015901 di = dict_find(d, key, -1);
15902 if (di == NULL)
15903 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020015904 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
15905 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015906 {
15907 *rettv = di->di_tv;
15908 init_tv(&di->di_tv);
15909 dictitem_remove(d, di);
15910 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015911 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015912 }
15913 }
15914 else if (argvars[0].v_type != VAR_LIST)
15915 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015916 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015917 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015918 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015919 int error = FALSE;
15920
15921 idx = get_tv_number_chk(&argvars[1], &error);
15922 if (error)
15923 ; /* type error: do nothing, errmsg already given */
15924 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015925 EMSGN(_(e_listidx), idx);
15926 else
15927 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015928 if (argvars[2].v_type == VAR_UNKNOWN)
15929 {
15930 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015931 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015932 *rettv = item->li_tv;
15933 vim_free(item);
15934 }
15935 else
15936 {
15937 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015938 end = get_tv_number_chk(&argvars[2], &error);
15939 if (error)
15940 ; /* type error: do nothing */
15941 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015942 EMSGN(_(e_listidx), end);
15943 else
15944 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015945 int cnt = 0;
15946
15947 for (li = item; li != NULL; li = li->li_next)
15948 {
15949 ++cnt;
15950 if (li == item2)
15951 break;
15952 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015953 if (li == NULL) /* didn't find "item2" after "item" */
15954 EMSG(_(e_invrange));
15955 else
15956 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015957 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015958 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015959 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015960 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015961 l->lv_first = item;
15962 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015963 item->li_prev = NULL;
15964 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015965 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015966 }
15967 }
15968 }
15969 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015970 }
15971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015972}
15973
15974/*
15975 * "rename({from}, {to})" function
15976 */
15977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015978f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015979{
15980 char_u buf[NUMBUFLEN];
15981
15982 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015983 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015984 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015985 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15986 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015987}
15988
15989/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015990 * "repeat()" function
15991 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015993f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015994{
15995 char_u *p;
15996 int n;
15997 int slen;
15998 int len;
15999 char_u *r;
16000 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016001
16002 n = get_tv_number(&argvars[1]);
16003 if (argvars[0].v_type == VAR_LIST)
16004 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016005 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016006 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016007 if (list_extend(rettv->vval.v_list,
16008 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016009 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016010 }
16011 else
16012 {
16013 p = get_tv_string(&argvars[0]);
16014 rettv->v_type = VAR_STRING;
16015 rettv->vval.v_string = NULL;
16016
16017 slen = (int)STRLEN(p);
16018 len = slen * n;
16019 if (len <= 0)
16020 return;
16021
16022 r = alloc(len + 1);
16023 if (r != NULL)
16024 {
16025 for (i = 0; i < n; i++)
16026 mch_memmove(r + i * slen, p, (size_t)slen);
16027 r[len] = NUL;
16028 }
16029
16030 rettv->vval.v_string = r;
16031 }
16032}
16033
16034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016035 * "resolve()" function
16036 */
16037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016038f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016039{
16040 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016041#ifdef HAVE_READLINK
16042 char_u *buf = NULL;
16043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016044
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016045 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016046#ifdef FEAT_SHORTCUT
16047 {
16048 char_u *v = NULL;
16049
16050 v = mch_resolve_shortcut(p);
16051 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016052 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016053 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016054 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016055 }
16056#else
16057# ifdef HAVE_READLINK
16058 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016059 char_u *cpy;
16060 int len;
16061 char_u *remain = NULL;
16062 char_u *q;
16063 int is_relative_to_current = FALSE;
16064 int has_trailing_pathsep = FALSE;
16065 int limit = 100;
16066
16067 p = vim_strsave(p);
16068
16069 if (p[0] == '.' && (vim_ispathsep(p[1])
16070 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16071 is_relative_to_current = TRUE;
16072
16073 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016074 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016075 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016076 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016077 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016079
16080 q = getnextcomp(p);
16081 if (*q != NUL)
16082 {
16083 /* Separate the first path component in "p", and keep the
16084 * remainder (beginning with the path separator). */
16085 remain = vim_strsave(q - 1);
16086 q[-1] = NUL;
16087 }
16088
Bram Moolenaard9462e32011-04-11 21:35:11 +020016089 buf = alloc(MAXPATHL + 1);
16090 if (buf == NULL)
16091 goto fail;
16092
Bram Moolenaar071d4272004-06-13 20:20:40 +000016093 for (;;)
16094 {
16095 for (;;)
16096 {
16097 len = readlink((char *)p, (char *)buf, MAXPATHL);
16098 if (len <= 0)
16099 break;
16100 buf[len] = NUL;
16101
16102 if (limit-- == 0)
16103 {
16104 vim_free(p);
16105 vim_free(remain);
16106 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016107 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016108 goto fail;
16109 }
16110
16111 /* Ensure that the result will have a trailing path separator
16112 * if the argument has one. */
16113 if (remain == NULL && has_trailing_pathsep)
16114 add_pathsep(buf);
16115
16116 /* Separate the first path component in the link value and
16117 * concatenate the remainders. */
16118 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16119 if (*q != NUL)
16120 {
16121 if (remain == NULL)
16122 remain = vim_strsave(q - 1);
16123 else
16124 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016125 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016126 if (cpy != NULL)
16127 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128 vim_free(remain);
16129 remain = cpy;
16130 }
16131 }
16132 q[-1] = NUL;
16133 }
16134
16135 q = gettail(p);
16136 if (q > p && *q == NUL)
16137 {
16138 /* Ignore trailing path separator. */
16139 q[-1] = NUL;
16140 q = gettail(p);
16141 }
16142 if (q > p && !mch_isFullName(buf))
16143 {
16144 /* symlink is relative to directory of argument */
16145 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16146 if (cpy != NULL)
16147 {
16148 STRCPY(cpy, p);
16149 STRCPY(gettail(cpy), buf);
16150 vim_free(p);
16151 p = cpy;
16152 }
16153 }
16154 else
16155 {
16156 vim_free(p);
16157 p = vim_strsave(buf);
16158 }
16159 }
16160
16161 if (remain == NULL)
16162 break;
16163
16164 /* Append the first path component of "remain" to "p". */
16165 q = getnextcomp(remain + 1);
16166 len = q - remain - (*q != NUL);
16167 cpy = vim_strnsave(p, STRLEN(p) + len);
16168 if (cpy != NULL)
16169 {
16170 STRNCAT(cpy, remain, len);
16171 vim_free(p);
16172 p = cpy;
16173 }
16174 /* Shorten "remain". */
16175 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016176 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016177 else
16178 {
16179 vim_free(remain);
16180 remain = NULL;
16181 }
16182 }
16183
16184 /* If the result is a relative path name, make it explicitly relative to
16185 * the current directory if and only if the argument had this form. */
16186 if (!vim_ispathsep(*p))
16187 {
16188 if (is_relative_to_current
16189 && *p != NUL
16190 && !(p[0] == '.'
16191 && (p[1] == NUL
16192 || vim_ispathsep(p[1])
16193 || (p[1] == '.'
16194 && (p[2] == NUL
16195 || vim_ispathsep(p[2]))))))
16196 {
16197 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016198 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016199 if (cpy != NULL)
16200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016201 vim_free(p);
16202 p = cpy;
16203 }
16204 }
16205 else if (!is_relative_to_current)
16206 {
16207 /* Strip leading "./". */
16208 q = p;
16209 while (q[0] == '.' && vim_ispathsep(q[1]))
16210 q += 2;
16211 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016212 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016213 }
16214 }
16215
16216 /* Ensure that the result will have no trailing path separator
16217 * if the argument had none. But keep "/" or "//". */
16218 if (!has_trailing_pathsep)
16219 {
16220 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016221 if (after_pathsep(p, q))
16222 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016223 }
16224
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016225 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226 }
16227# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016228 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016229# endif
16230#endif
16231
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016232 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233
16234#ifdef HAVE_READLINK
16235fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016236 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016237#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016238 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016239}
16240
16241/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016242 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016243 */
16244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016245f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016246{
Bram Moolenaar33570922005-01-25 22:26:29 +000016247 list_T *l;
16248 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016249
Bram Moolenaar0d660222005-01-07 21:51:51 +000016250 if (argvars[0].v_type != VAR_LIST)
16251 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016252 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016253 && !tv_check_lock(l->lv_lock,
16254 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016255 {
16256 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016257 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016258 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016259 while (li != NULL)
16260 {
16261 ni = li->li_prev;
16262 list_append(l, li);
16263 li = ni;
16264 }
16265 rettv->vval.v_list = l;
16266 rettv->v_type = VAR_LIST;
16267 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016268 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270}
16271
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016272#define SP_NOMOVE 0x01 /* don't move cursor */
16273#define SP_REPEAT 0x02 /* repeat to find outer pair */
16274#define SP_RETCOUNT 0x04 /* return matchcount */
16275#define SP_SETPCMARK 0x08 /* set previous context mark */
16276#define SP_START 0x10 /* accept match at start position */
16277#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16278#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016279#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016280
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016281static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016282
16283/*
16284 * Get flags for a search function.
16285 * Possibly sets "p_ws".
16286 * Returns BACKWARD, FORWARD or zero (for an error).
16287 */
16288 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016289get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016290{
16291 int dir = FORWARD;
16292 char_u *flags;
16293 char_u nbuf[NUMBUFLEN];
16294 int mask;
16295
16296 if (varp->v_type != VAR_UNKNOWN)
16297 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016298 flags = get_tv_string_buf_chk(varp, nbuf);
16299 if (flags == NULL)
16300 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016301 while (*flags != NUL)
16302 {
16303 switch (*flags)
16304 {
16305 case 'b': dir = BACKWARD; break;
16306 case 'w': p_ws = TRUE; break;
16307 case 'W': p_ws = FALSE; break;
16308 default: mask = 0;
16309 if (flagsp != NULL)
16310 switch (*flags)
16311 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016312 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016313 case 'e': mask = SP_END; break;
16314 case 'm': mask = SP_RETCOUNT; break;
16315 case 'n': mask = SP_NOMOVE; break;
16316 case 'p': mask = SP_SUBPAT; break;
16317 case 'r': mask = SP_REPEAT; break;
16318 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016319 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016320 }
16321 if (mask == 0)
16322 {
16323 EMSG2(_(e_invarg2), flags);
16324 dir = 0;
16325 }
16326 else
16327 *flagsp |= mask;
16328 }
16329 if (dir == 0)
16330 break;
16331 ++flags;
16332 }
16333 }
16334 return dir;
16335}
16336
Bram Moolenaar071d4272004-06-13 20:20:40 +000016337/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016338 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016339 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016340 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016341search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016342{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016343 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016344 char_u *pat;
16345 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016346 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016347 int save_p_ws = p_ws;
16348 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016349 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016350 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016351 proftime_T tm;
16352#ifdef FEAT_RELTIME
16353 long time_limit = 0;
16354#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016355 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016356 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016357
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016358 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016359 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016360 if (dir == 0)
16361 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016362 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016363 if (flags & SP_START)
16364 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016365 if (flags & SP_END)
16366 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016367 if (flags & SP_COLUMN)
16368 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016369
Bram Moolenaar76929292008-01-06 19:07:36 +000016370 /* Optional arguments: line number to stop searching and timeout. */
16371 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016372 {
16373 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16374 if (lnum_stop < 0)
16375 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016376#ifdef FEAT_RELTIME
16377 if (argvars[3].v_type != VAR_UNKNOWN)
16378 {
16379 time_limit = get_tv_number_chk(&argvars[3], NULL);
16380 if (time_limit < 0)
16381 goto theend;
16382 }
16383#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016384 }
16385
Bram Moolenaar76929292008-01-06 19:07:36 +000016386#ifdef FEAT_RELTIME
16387 /* Set the time limit, if there is one. */
16388 profile_setlimit(time_limit, &tm);
16389#endif
16390
Bram Moolenaar231334e2005-07-25 20:46:57 +000016391 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016392 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016393 * Check to make sure only those flags are set.
16394 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16395 * flags cannot be set. Check for that condition also.
16396 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016397 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016398 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016399 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016400 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016401 goto theend;
16402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016403
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016404 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016405 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016406 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016407 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016408 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016409 if (flags & SP_SUBPAT)
16410 retval = subpatnum;
16411 else
16412 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016413 if (flags & SP_SETPCMARK)
16414 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016415 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016416 if (match_pos != NULL)
16417 {
16418 /* Store the match cursor position */
16419 match_pos->lnum = pos.lnum;
16420 match_pos->col = pos.col + 1;
16421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016422 /* "/$" will put the cursor after the end of the line, may need to
16423 * correct that here */
16424 check_cursor();
16425 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016426
16427 /* If 'n' flag is used: restore cursor position. */
16428 if (flags & SP_NOMOVE)
16429 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016430 else
16431 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016432theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016433 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016434
16435 return retval;
16436}
16437
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016438#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016439
16440/*
16441 * round() is not in C90, use ceil() or floor() instead.
16442 */
16443 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010016444vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016445{
16446 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16447}
16448
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016449/*
16450 * "round({float})" function
16451 */
16452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016453f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016454{
16455 float_T f;
16456
16457 rettv->v_type = VAR_FLOAT;
16458 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016459 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016460 else
16461 rettv->vval.v_float = 0.0;
16462}
16463#endif
16464
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016465/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016466 * "screenattr()" function
16467 */
16468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016469f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016470{
16471 int row;
16472 int col;
16473 int c;
16474
16475 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16476 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16477 if (row < 0 || row >= screen_Rows
16478 || col < 0 || col >= screen_Columns)
16479 c = -1;
16480 else
16481 c = ScreenAttrs[LineOffset[row] + col];
16482 rettv->vval.v_number = c;
16483}
16484
16485/*
16486 * "screenchar()" function
16487 */
16488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016489f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016490{
16491 int row;
16492 int col;
16493 int off;
16494 int c;
16495
16496 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16497 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16498 if (row < 0 || row >= screen_Rows
16499 || col < 0 || col >= screen_Columns)
16500 c = -1;
16501 else
16502 {
16503 off = LineOffset[row] + col;
16504#ifdef FEAT_MBYTE
16505 if (enc_utf8 && ScreenLinesUC[off] != 0)
16506 c = ScreenLinesUC[off];
16507 else
16508#endif
16509 c = ScreenLines[off];
16510 }
16511 rettv->vval.v_number = c;
16512}
16513
16514/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016515 * "screencol()" function
16516 *
16517 * First column is 1 to be consistent with virtcol().
16518 */
16519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016520f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016521{
16522 rettv->vval.v_number = screen_screencol() + 1;
16523}
16524
16525/*
16526 * "screenrow()" function
16527 */
16528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016529f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016530{
16531 rettv->vval.v_number = screen_screenrow() + 1;
16532}
16533
16534/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016535 * "search()" function
16536 */
16537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016538f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016539{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016540 int flags = 0;
16541
16542 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543}
16544
Bram Moolenaar071d4272004-06-13 20:20:40 +000016545/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016546 * "searchdecl()" function
16547 */
16548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016549f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016550{
16551 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016552 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016553 int error = FALSE;
16554 char_u *name;
16555
16556 rettv->vval.v_number = 1; /* default: FAIL */
16557
16558 name = get_tv_string_chk(&argvars[0]);
16559 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016560 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016561 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016562 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16563 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16564 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016565 if (!error && name != NULL)
16566 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016567 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016568}
16569
16570/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016571 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016572 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016573 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016574searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016575{
16576 char_u *spat, *mpat, *epat;
16577 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016578 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016579 int dir;
16580 int flags = 0;
16581 char_u nbuf1[NUMBUFLEN];
16582 char_u nbuf2[NUMBUFLEN];
16583 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016584 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016585 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016586 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016587
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016589 spat = get_tv_string_chk(&argvars[0]);
16590 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16591 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16592 if (spat == NULL || mpat == NULL || epat == NULL)
16593 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016594
Bram Moolenaar071d4272004-06-13 20:20:40 +000016595 /* Handle the optional fourth argument: flags */
16596 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016597 if (dir == 0)
16598 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016599
16600 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016601 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16602 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016603 if ((flags & (SP_END | SP_SUBPAT)) != 0
16604 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016605 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016606 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016607 goto theend;
16608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016609
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016610 /* Using 'r' implies 'W', otherwise it doesn't work. */
16611 if (flags & SP_REPEAT)
16612 p_ws = FALSE;
16613
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016614 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016615 if (argvars[3].v_type == VAR_UNKNOWN
16616 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016617 skip = (char_u *)"";
16618 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016619 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016620 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016621 if (argvars[5].v_type != VAR_UNKNOWN)
16622 {
16623 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16624 if (lnum_stop < 0)
16625 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016626#ifdef FEAT_RELTIME
16627 if (argvars[6].v_type != VAR_UNKNOWN)
16628 {
16629 time_limit = get_tv_number_chk(&argvars[6], NULL);
16630 if (time_limit < 0)
16631 goto theend;
16632 }
16633#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016634 }
16635 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016636 if (skip == NULL)
16637 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016638
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016639 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016640 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016641
16642theend:
16643 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016644
16645 return retval;
16646}
16647
16648/*
16649 * "searchpair()" function
16650 */
16651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016652f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016653{
16654 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16655}
16656
16657/*
16658 * "searchpairpos()" function
16659 */
16660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016661f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016662{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016663 pos_T match_pos;
16664 int lnum = 0;
16665 int col = 0;
16666
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016667 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016668 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016669
16670 if (searchpair_cmn(argvars, &match_pos) > 0)
16671 {
16672 lnum = match_pos.lnum;
16673 col = match_pos.col;
16674 }
16675
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016676 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16677 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016678}
16679
16680/*
16681 * Search for a start/middle/end thing.
16682 * Used by searchpair(), see its documentation for the details.
16683 * Returns 0 or -1 for no match,
16684 */
16685 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010016686do_searchpair(
16687 char_u *spat, /* start pattern */
16688 char_u *mpat, /* middle pattern */
16689 char_u *epat, /* end pattern */
16690 int dir, /* BACKWARD or FORWARD */
16691 char_u *skip, /* skip expression */
16692 int flags, /* SP_SETPCMARK and other SP_ values */
16693 pos_T *match_pos,
16694 linenr_T lnum_stop, /* stop at this line if not zero */
16695 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016696{
16697 char_u *save_cpo;
16698 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16699 long retval = 0;
16700 pos_T pos;
16701 pos_T firstpos;
16702 pos_T foundpos;
16703 pos_T save_cursor;
16704 pos_T save_pos;
16705 int n;
16706 int r;
16707 int nest = 1;
16708 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016709 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016710 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016711
16712 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16713 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016714 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016715
Bram Moolenaar76929292008-01-06 19:07:36 +000016716#ifdef FEAT_RELTIME
16717 /* Set the time limit, if there is one. */
16718 profile_setlimit(time_limit, &tm);
16719#endif
16720
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016721 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16722 * start/middle/end (pat3, for the top pair). */
16723 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16724 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16725 if (pat2 == NULL || pat3 == NULL)
16726 goto theend;
16727 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16728 if (*mpat == NUL)
16729 STRCPY(pat3, pat2);
16730 else
16731 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16732 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016733 if (flags & SP_START)
16734 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016735
Bram Moolenaar071d4272004-06-13 20:20:40 +000016736 save_cursor = curwin->w_cursor;
16737 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016738 clearpos(&firstpos);
16739 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016740 pat = pat3;
16741 for (;;)
16742 {
16743 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016744 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016745 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16746 /* didn't find it or found the first match again: FAIL */
16747 break;
16748
16749 if (firstpos.lnum == 0)
16750 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016751 if (equalpos(pos, foundpos))
16752 {
16753 /* Found the same position again. Can happen with a pattern that
16754 * has "\zs" at the end and searching backwards. Advance one
16755 * character and try again. */
16756 if (dir == BACKWARD)
16757 decl(&pos);
16758 else
16759 incl(&pos);
16760 }
16761 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016762
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016763 /* clear the start flag to avoid getting stuck here */
16764 options &= ~SEARCH_START;
16765
Bram Moolenaar071d4272004-06-13 20:20:40 +000016766 /* If the skip pattern matches, ignore this match. */
16767 if (*skip != NUL)
16768 {
16769 save_pos = curwin->w_cursor;
16770 curwin->w_cursor = pos;
16771 r = eval_to_bool(skip, &err, NULL, FALSE);
16772 curwin->w_cursor = save_pos;
16773 if (err)
16774 {
16775 /* Evaluating {skip} caused an error, break here. */
16776 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016777 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016778 break;
16779 }
16780 if (r)
16781 continue;
16782 }
16783
16784 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16785 {
16786 /* Found end when searching backwards or start when searching
16787 * forward: nested pair. */
16788 ++nest;
16789 pat = pat2; /* nested, don't search for middle */
16790 }
16791 else
16792 {
16793 /* Found end when searching forward or start when searching
16794 * backward: end of (nested) pair; or found middle in outer pair. */
16795 if (--nest == 1)
16796 pat = pat3; /* outer level, search for middle */
16797 }
16798
16799 if (nest == 0)
16800 {
16801 /* Found the match: return matchcount or line number. */
16802 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016803 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016804 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016805 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016806 if (flags & SP_SETPCMARK)
16807 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016808 curwin->w_cursor = pos;
16809 if (!(flags & SP_REPEAT))
16810 break;
16811 nest = 1; /* search for next unmatched */
16812 }
16813 }
16814
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016815 if (match_pos != NULL)
16816 {
16817 /* Store the match cursor position */
16818 match_pos->lnum = curwin->w_cursor.lnum;
16819 match_pos->col = curwin->w_cursor.col + 1;
16820 }
16821
Bram Moolenaar071d4272004-06-13 20:20:40 +000016822 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016823 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824 curwin->w_cursor = save_cursor;
16825
16826theend:
16827 vim_free(pat2);
16828 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016829 if (p_cpo == empty_option)
16830 p_cpo = save_cpo;
16831 else
16832 /* Darn, evaluating the {skip} expression changed the value. */
16833 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016834
16835 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016836}
16837
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016838/*
16839 * "searchpos()" function
16840 */
16841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016842f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016843{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016844 pos_T match_pos;
16845 int lnum = 0;
16846 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016847 int n;
16848 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016849
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016850 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016851 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016852
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016853 n = search_cmn(argvars, &match_pos, &flags);
16854 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016855 {
16856 lnum = match_pos.lnum;
16857 col = match_pos.col;
16858 }
16859
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016860 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16861 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016862 if (flags & SP_SUBPAT)
16863 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016864}
16865
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016866#ifdef FEAT_CHANNEL
16867/*
16868 * common for "sendexpr()" and "sendraw()"
16869 * Returns the channel index if the caller should read the response.
16870 * Otherwise returns -1.
16871 */
16872 static int
16873send_common(typval_T *argvars, char_u *text, char *fun)
16874{
16875 int ch_idx;
16876 char_u *callback = NULL;
16877
16878 ch_idx = get_channel_arg(&argvars[0]);
16879 if (ch_idx < 0)
16880 return -1;
16881
16882 if (argvars[2].v_type != VAR_UNKNOWN)
16883 {
16884 callback = get_callback(&argvars[2]);
16885 if (callback == NULL)
16886 return -1;
16887 }
16888 /* Set the callback or clear it. An empty callback means no callback and
16889 * not reading the response. */
16890 channel_set_req_callback(ch_idx,
16891 callback != NULL && *callback == NUL ? NULL : callback);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016892
16893 if (channel_send(ch_idx, text, fun) == OK && callback == NULL)
16894 return ch_idx;
16895 return -1;
16896}
16897
16898/*
16899 * "sendexpr()" function
16900 */
16901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016902f_sendexpr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016903{
16904 char_u *text;
16905 char_u *resp;
16906 typval_T nrtv;
16907 typval_T listtv;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010016908 typval_T typetv;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016909 int ch_idx;
16910
16911 /* return an empty string by default */
16912 rettv->v_type = VAR_STRING;
16913 rettv->vval.v_string = NULL;
16914
16915 nrtv.v_type = VAR_NUMBER;
16916 nrtv.vval.v_number = channel_get_id();
16917 if (rettv_list_alloc(&listtv) == FAIL)
16918 return;
16919 if (list_append_tv(listtv.vval.v_list, &nrtv) == FAIL
16920 || list_append_tv(listtv.vval.v_list, &argvars[1]) == FAIL)
16921 {
16922 list_unref(listtv.vval.v_list);
16923 return;
16924 }
16925
16926 text = json_encode(&listtv);
16927 list_unref(listtv.vval.v_list);
16928
16929 ch_idx = send_common(argvars, text, "sendexpr");
16930 if (ch_idx >= 0)
16931 {
16932 /* TODO: read until the whole JSON message is received */
16933 /* TODO: only use the message with the right message ID */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010016934 /* TODO: check sequence number */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016935 resp = channel_read_block(ch_idx);
16936 if (resp != NULL)
16937 {
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010016938 channel_decode_json(resp, &typetv, rettv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016939 vim_free(resp);
16940 }
16941 }
16942}
16943
16944/*
16945 * "sendraw()" function
16946 */
16947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016948f_sendraw(typval_T *argvars, typval_T *rettv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016949{
16950 char_u buf[NUMBUFLEN];
16951 char_u *text;
16952 int ch_idx;
16953
16954 /* return an empty string by default */
16955 rettv->v_type = VAR_STRING;
16956 rettv->vval.v_string = NULL;
16957
16958 text = get_tv_string_buf(&argvars[1], buf);
16959 ch_idx = send_common(argvars, text, "sendraw");
16960 if (ch_idx >= 0)
16961 rettv->vval.v_string = channel_read_block(ch_idx);
16962}
16963#endif
16964
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016965
Bram Moolenaar0d660222005-01-07 21:51:51 +000016966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016967f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016968{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016969#ifdef FEAT_CLIENTSERVER
16970 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016971 char_u *server = get_tv_string_chk(&argvars[0]);
16972 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016973
Bram Moolenaar0d660222005-01-07 21:51:51 +000016974 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016975 if (server == NULL || reply == NULL)
16976 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016977 if (check_restricted() || check_secure())
16978 return;
16979# ifdef FEAT_X11
16980 if (check_connection() == FAIL)
16981 return;
16982# endif
16983
16984 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016985 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016986 EMSG(_("E258: Unable to send to client"));
16987 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016989 rettv->vval.v_number = 0;
16990#else
16991 rettv->vval.v_number = -1;
16992#endif
16993}
16994
Bram Moolenaar0d660222005-01-07 21:51:51 +000016995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016996f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016997{
16998 char_u *r = NULL;
16999
17000#ifdef FEAT_CLIENTSERVER
17001# ifdef WIN32
17002 r = serverGetVimNames();
17003# else
17004 make_connection();
17005 if (X_DISPLAY != NULL)
17006 r = serverGetVimNames(X_DISPLAY);
17007# endif
17008#endif
17009 rettv->v_type = VAR_STRING;
17010 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017011}
17012
17013/*
17014 * "setbufvar()" function
17015 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017017f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018{
17019 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017022 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017023 char_u nbuf[NUMBUFLEN];
17024
17025 if (check_restricted() || check_secure())
17026 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017027 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17028 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017029 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030 varp = &argvars[2];
17031
17032 if (buf != NULL && varname != NULL && varp != NULL)
17033 {
17034 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017035 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017036
17037 if (*varname == '&')
17038 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017039 long numval;
17040 char_u *strval;
17041 int error = FALSE;
17042
Bram Moolenaar071d4272004-06-13 20:20:40 +000017043 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017044 numval = get_tv_number_chk(varp, &error);
17045 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017046 if (!error && strval != NULL)
17047 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048 }
17049 else
17050 {
17051 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17052 if (bufvarname != NULL)
17053 {
17054 STRCPY(bufvarname, "b:");
17055 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017056 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017057 vim_free(bufvarname);
17058 }
17059 }
17060
17061 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017062 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017064}
17065
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017067f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017068{
17069 dict_T *d;
17070 dictitem_T *di;
17071 char_u *csearch;
17072
17073 if (argvars[0].v_type != VAR_DICT)
17074 {
17075 EMSG(_(e_dictreq));
17076 return;
17077 }
17078
17079 if ((d = argvars[0].vval.v_dict) != NULL)
17080 {
17081 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17082 if (csearch != NULL)
17083 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017084#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017085 if (enc_utf8)
17086 {
17087 int pcc[MAX_MCO];
17088 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017089
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017090 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17091 }
17092 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017093#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017094 set_last_csearch(PTR2CHAR(csearch),
17095 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017096 }
17097
17098 di = dict_find(d, (char_u *)"forward", -1);
17099 if (di != NULL)
17100 set_csearch_direction(get_tv_number(&di->di_tv)
17101 ? FORWARD : BACKWARD);
17102
17103 di = dict_find(d, (char_u *)"until", -1);
17104 if (di != NULL)
17105 set_csearch_until(!!get_tv_number(&di->di_tv));
17106 }
17107}
17108
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109/*
17110 * "setcmdpos()" function
17111 */
17112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017113f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017114{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017115 int pos = (int)get_tv_number(&argvars[0]) - 1;
17116
17117 if (pos >= 0)
17118 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119}
17120
17121/*
17122 * "setline()" function
17123 */
17124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017125f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126{
17127 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017128 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017129 list_T *l = NULL;
17130 listitem_T *li = NULL;
17131 long added = 0;
17132 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017133
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017134 lnum = get_tv_lnum(&argvars[0]);
17135 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017136 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017137 l = argvars[1].vval.v_list;
17138 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017139 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017140 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017141 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017142
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017143 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017144 for (;;)
17145 {
17146 if (l != NULL)
17147 {
17148 /* list argument, get next string */
17149 if (li == NULL)
17150 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017151 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017152 li = li->li_next;
17153 }
17154
17155 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017156 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017157 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017158
17159 /* When coming here from Insert mode, sync undo, so that this can be
17160 * undone separately from what was previously inserted. */
17161 if (u_sync_once == 2)
17162 {
17163 u_sync_once = 1; /* notify that u_sync() was called */
17164 u_sync(TRUE);
17165 }
17166
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017167 if (lnum <= curbuf->b_ml.ml_line_count)
17168 {
17169 /* existing line, replace it */
17170 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17171 {
17172 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017173 if (lnum == curwin->w_cursor.lnum)
17174 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017175 rettv->vval.v_number = 0; /* OK */
17176 }
17177 }
17178 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17179 {
17180 /* lnum is one past the last line, append the line */
17181 ++added;
17182 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17183 rettv->vval.v_number = 0; /* OK */
17184 }
17185
17186 if (l == NULL) /* only one string argument */
17187 break;
17188 ++lnum;
17189 }
17190
17191 if (added > 0)
17192 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193}
17194
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017195static 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 +000017196
Bram Moolenaar071d4272004-06-13 20:20:40 +000017197/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017198 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017199 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017201set_qf_ll_list(
17202 win_T *wp UNUSED,
17203 typval_T *list_arg UNUSED,
17204 typval_T *action_arg UNUSED,
17205 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017206{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017207#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017208 char_u *act;
17209 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017210#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017211
Bram Moolenaar2641f772005-03-25 21:58:17 +000017212 rettv->vval.v_number = -1;
17213
17214#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017215 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017216 EMSG(_(e_listreq));
17217 else
17218 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017219 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017220
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017221 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017222 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017223 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017224 if (act == NULL)
17225 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017226 if (*act == 'a' || *act == 'r')
17227 action = *act;
17228 }
17229
Bram Moolenaar81484f42012-12-05 15:16:47 +010017230 if (l != NULL && set_errorlist(wp, l, action,
17231 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017232 rettv->vval.v_number = 0;
17233 }
17234#endif
17235}
17236
17237/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017238 * "setloclist()" function
17239 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017241f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017242{
17243 win_T *win;
17244
17245 rettv->vval.v_number = -1;
17246
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017247 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017248 if (win != NULL)
17249 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17250}
17251
17252/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017253 * "setmatches()" function
17254 */
17255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017256f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017257{
17258#ifdef FEAT_SEARCH_EXTRA
17259 list_T *l;
17260 listitem_T *li;
17261 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017262 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017263
17264 rettv->vval.v_number = -1;
17265 if (argvars[0].v_type != VAR_LIST)
17266 {
17267 EMSG(_(e_listreq));
17268 return;
17269 }
17270 if ((l = argvars[0].vval.v_list) != NULL)
17271 {
17272
17273 /* To some extent make sure that we are dealing with a list from
17274 * "getmatches()". */
17275 li = l->lv_first;
17276 while (li != NULL)
17277 {
17278 if (li->li_tv.v_type != VAR_DICT
17279 || (d = li->li_tv.vval.v_dict) == NULL)
17280 {
17281 EMSG(_(e_invarg));
17282 return;
17283 }
17284 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017285 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17286 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017287 && dict_find(d, (char_u *)"priority", -1) != NULL
17288 && dict_find(d, (char_u *)"id", -1) != NULL))
17289 {
17290 EMSG(_(e_invarg));
17291 return;
17292 }
17293 li = li->li_next;
17294 }
17295
17296 clear_matches(curwin);
17297 li = l->lv_first;
17298 while (li != NULL)
17299 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017300 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017301 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017302 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017303 char_u *group;
17304 int priority;
17305 int id;
17306 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017307
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017308 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017309 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17310 {
17311 if (s == NULL)
17312 {
17313 s = list_alloc();
17314 if (s == NULL)
17315 return;
17316 }
17317
17318 /* match from matchaddpos() */
17319 for (i = 1; i < 9; i++)
17320 {
17321 sprintf((char *)buf, (char *)"pos%d", i);
17322 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17323 {
17324 if (di->di_tv.v_type != VAR_LIST)
17325 return;
17326
17327 list_append_tv(s, &di->di_tv);
17328 s->lv_refcount++;
17329 }
17330 else
17331 break;
17332 }
17333 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017334
17335 group = get_dict_string(d, (char_u *)"group", FALSE);
17336 priority = (int)get_dict_number(d, (char_u *)"priority");
17337 id = (int)get_dict_number(d, (char_u *)"id");
17338 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17339 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17340 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017341 if (i == 0)
17342 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017343 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017344 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017345 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017346 }
17347 else
17348 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017349 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017350 list_unref(s);
17351 s = NULL;
17352 }
17353
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017354 li = li->li_next;
17355 }
17356 rettv->vval.v_number = 0;
17357 }
17358#endif
17359}
17360
17361/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017362 * "setpos()" function
17363 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017365f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017366{
17367 pos_T pos;
17368 int fnum;
17369 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017370 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017371
Bram Moolenaar08250432008-02-13 11:42:46 +000017372 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017373 name = get_tv_string_chk(argvars);
17374 if (name != NULL)
17375 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017376 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017377 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017378 if (--pos.col < 0)
17379 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017380 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017381 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017382 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017383 if (fnum == curbuf->b_fnum)
17384 {
17385 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017386 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017387 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017388 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017389 curwin->w_set_curswant = FALSE;
17390 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017391 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017392 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017393 }
17394 else
17395 EMSG(_(e_invarg));
17396 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017397 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17398 {
17399 /* set mark */
17400 if (setmark_pos(name[1], &pos, fnum) == OK)
17401 rettv->vval.v_number = 0;
17402 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017403 else
17404 EMSG(_(e_invarg));
17405 }
17406 }
17407}
17408
17409/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017410 * "setqflist()" function
17411 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017413f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017414{
17415 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17416}
17417
17418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419 * "setreg()" function
17420 */
17421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017422f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017423{
17424 int regname;
17425 char_u *strregname;
17426 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017427 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017428 int append;
17429 char_u yank_type;
17430 long block_len;
17431
17432 block_len = -1;
17433 yank_type = MAUTO;
17434 append = FALSE;
17435
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017436 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017437 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017438
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017439 if (strregname == NULL)
17440 return; /* type error; errmsg already given */
17441 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017442 if (regname == 0 || regname == '@')
17443 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017444
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017445 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017446 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017447 stropt = get_tv_string_chk(&argvars[2]);
17448 if (stropt == NULL)
17449 return; /* type error */
17450 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017451 switch (*stropt)
17452 {
17453 case 'a': case 'A': /* append */
17454 append = TRUE;
17455 break;
17456 case 'v': case 'c': /* character-wise selection */
17457 yank_type = MCHAR;
17458 break;
17459 case 'V': case 'l': /* line-wise selection */
17460 yank_type = MLINE;
17461 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017462 case 'b': case Ctrl_V: /* block-wise selection */
17463 yank_type = MBLOCK;
17464 if (VIM_ISDIGIT(stropt[1]))
17465 {
17466 ++stropt;
17467 block_len = getdigits(&stropt) - 1;
17468 --stropt;
17469 }
17470 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471 }
17472 }
17473
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017474 if (argvars[1].v_type == VAR_LIST)
17475 {
17476 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017477 char_u **allocval;
17478 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017479 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017480 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017481 int len = argvars[1].vval.v_list->lv_len;
17482 listitem_T *li;
17483
Bram Moolenaar7d647822014-04-05 21:28:56 +020017484 /* First half: use for pointers to result lines; second half: use for
17485 * pointers to allocated copies. */
17486 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017487 if (lstval == NULL)
17488 return;
17489 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017490 allocval = lstval + len + 2;
17491 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017492
17493 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17494 li = li->li_next)
17495 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017496 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017497 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017498 goto free_lstval;
17499 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017500 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017501 /* Need to make a copy, next get_tv_string_buf_chk() will
17502 * overwrite the string. */
17503 strval = vim_strsave(buf);
17504 if (strval == NULL)
17505 goto free_lstval;
17506 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017507 }
17508 *curval++ = strval;
17509 }
17510 *curval++ = NULL;
17511
17512 write_reg_contents_lst(regname, lstval, -1,
17513 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017514free_lstval:
17515 while (curallocval > allocval)
17516 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017517 vim_free(lstval);
17518 }
17519 else
17520 {
17521 strval = get_tv_string_chk(&argvars[1]);
17522 if (strval == NULL)
17523 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017524 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017526 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017527 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528}
17529
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017530/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017531 * "settabvar()" function
17532 */
17533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017534f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017535{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017536#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017537 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017538 tabpage_T *tp;
17539#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017540 char_u *varname, *tabvarname;
17541 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017542
17543 rettv->vval.v_number = 0;
17544
17545 if (check_restricted() || check_secure())
17546 return;
17547
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017548#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017549 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017550#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017551 varname = get_tv_string_chk(&argvars[1]);
17552 varp = &argvars[2];
17553
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017554 if (varname != NULL && varp != NULL
17555#ifdef FEAT_WINDOWS
17556 && tp != NULL
17557#endif
17558 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017559 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017560#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017561 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017562 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017563#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017564
17565 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17566 if (tabvarname != NULL)
17567 {
17568 STRCPY(tabvarname, "t:");
17569 STRCPY(tabvarname + 2, varname);
17570 set_var(tabvarname, varp, TRUE);
17571 vim_free(tabvarname);
17572 }
17573
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017574#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017575 /* Restore current tabpage */
17576 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017577 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017578#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017579 }
17580}
17581
17582/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017583 * "settabwinvar()" function
17584 */
17585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017586f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017587{
17588 setwinvar(argvars, rettv, 1);
17589}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017590
17591/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017592 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017593 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017595f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017596{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017597 setwinvar(argvars, rettv, 0);
17598}
17599
17600/*
17601 * "setwinvar()" and "settabwinvar()" functions
17602 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017603
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017605setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017606{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017607 win_T *win;
17608#ifdef FEAT_WINDOWS
17609 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017610 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017611 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612#endif
17613 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017614 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017616 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617
17618 if (check_restricted() || check_secure())
17619 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017620
17621#ifdef FEAT_WINDOWS
17622 if (off == 1)
17623 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17624 else
17625 tp = curtab;
17626#endif
17627 win = find_win_by_nr(&argvars[off], tp);
17628 varname = get_tv_string_chk(&argvars[off + 1]);
17629 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017630
17631 if (win != NULL && varname != NULL && varp != NULL)
17632 {
17633#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017634 need_switch_win = !(tp == curtab && win == curwin);
17635 if (!need_switch_win
17636 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017639 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017641 long numval;
17642 char_u *strval;
17643 int error = FALSE;
17644
17645 ++varname;
17646 numval = get_tv_number_chk(varp, &error);
17647 strval = get_tv_string_buf_chk(varp, nbuf);
17648 if (!error && strval != NULL)
17649 set_option_value(varname, numval, strval, OPT_LOCAL);
17650 }
17651 else
17652 {
17653 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17654 if (winvarname != NULL)
17655 {
17656 STRCPY(winvarname, "w:");
17657 STRCPY(winvarname + 2, varname);
17658 set_var(winvarname, varp, TRUE);
17659 vim_free(winvarname);
17660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661 }
17662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017664 if (need_switch_win)
17665 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666#endif
17667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668}
17669
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017670#ifdef FEAT_CRYPT
17671/*
17672 * "sha256({string})" function
17673 */
17674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017675f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017676{
17677 char_u *p;
17678
17679 p = get_tv_string(&argvars[0]);
17680 rettv->vval.v_string = vim_strsave(
17681 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17682 rettv->v_type = VAR_STRING;
17683}
17684#endif /* FEAT_CRYPT */
17685
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017687 * "shellescape({string})" function
17688 */
17689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017690f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017691{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017692 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017693 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017694 rettv->v_type = VAR_STRING;
17695}
17696
17697/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017698 * shiftwidth() function
17699 */
17700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017701f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017702{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017703 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017704}
17705
17706/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017707 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708 */
17709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017710f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017712 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017713
Bram Moolenaar0d660222005-01-07 21:51:51 +000017714 p = get_tv_string(&argvars[0]);
17715 rettv->vval.v_string = vim_strsave(p);
17716 simplify_filename(rettv->vval.v_string); /* simplify in place */
17717 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718}
17719
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017720#ifdef FEAT_FLOAT
17721/*
17722 * "sin()" function
17723 */
17724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017725f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017726{
17727 float_T f;
17728
17729 rettv->v_type = VAR_FLOAT;
17730 if (get_float_arg(argvars, &f) == OK)
17731 rettv->vval.v_float = sin(f);
17732 else
17733 rettv->vval.v_float = 0.0;
17734}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017735
17736/*
17737 * "sinh()" function
17738 */
17739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017740f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017741{
17742 float_T f;
17743
17744 rettv->v_type = VAR_FLOAT;
17745 if (get_float_arg(argvars, &f) == OK)
17746 rettv->vval.v_float = sinh(f);
17747 else
17748 rettv->vval.v_float = 0.0;
17749}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017750#endif
17751
Bram Moolenaar0d660222005-01-07 21:51:51 +000017752static int
17753#ifdef __BORLANDC__
17754 _RTLENTRYF
17755#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017756 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017757static int
17758#ifdef __BORLANDC__
17759 _RTLENTRYF
17760#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017761 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017762
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017763/* struct used in the array that's given to qsort() */
17764typedef struct
17765{
17766 listitem_T *item;
17767 int idx;
17768} sortItem_T;
17769
Bram Moolenaar0d660222005-01-07 21:51:51 +000017770static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017771static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017772static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017773#ifdef FEAT_FLOAT
17774static int item_compare_float;
17775#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017776static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017777static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017778static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017779static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017780static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017781#define ITEM_COMPARE_FAIL 999
17782
Bram Moolenaar071d4272004-06-13 20:20:40 +000017783/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017784 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017785 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017786 static int
17787#ifdef __BORLANDC__
17788_RTLENTRYF
17789#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010017790item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017792 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017793 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017794 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017795 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017796 int res;
17797 char_u numbuf1[NUMBUFLEN];
17798 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017799
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017800 si1 = (sortItem_T *)s1;
17801 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017802 tv1 = &si1->item->li_tv;
17803 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017804
17805 if (item_compare_numbers)
17806 {
17807 long v1 = get_tv_number(tv1);
17808 long v2 = get_tv_number(tv2);
17809
17810 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17811 }
17812
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017813#ifdef FEAT_FLOAT
17814 if (item_compare_float)
17815 {
17816 float_T v1 = get_tv_float(tv1);
17817 float_T v2 = get_tv_float(tv2);
17818
17819 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17820 }
17821#endif
17822
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017823 /* tv2string() puts quotes around a string and allocates memory. Don't do
17824 * that for string variables. Use a single quote when comparing with a
17825 * non-string to do what the docs promise. */
17826 if (tv1->v_type == VAR_STRING)
17827 {
17828 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17829 p1 = (char_u *)"'";
17830 else
17831 p1 = tv1->vval.v_string;
17832 }
17833 else
17834 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17835 if (tv2->v_type == VAR_STRING)
17836 {
17837 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17838 p2 = (char_u *)"'";
17839 else
17840 p2 = tv2->vval.v_string;
17841 }
17842 else
17843 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017844 if (p1 == NULL)
17845 p1 = (char_u *)"";
17846 if (p2 == NULL)
17847 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017848 if (!item_compare_numeric)
17849 {
17850 if (item_compare_ic)
17851 res = STRICMP(p1, p2);
17852 else
17853 res = STRCMP(p1, p2);
17854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017855 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017856 {
17857 double n1, n2;
17858 n1 = strtod((char *)p1, (char **)&p1);
17859 n2 = strtod((char *)p2, (char **)&p2);
17860 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17861 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017862
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017863 /* When the result would be zero, compare the item indexes. Makes the
17864 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017865 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017866 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017867
Bram Moolenaar0d660222005-01-07 21:51:51 +000017868 vim_free(tofree1);
17869 vim_free(tofree2);
17870 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017871}
17872
17873 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017874#ifdef __BORLANDC__
17875_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017876#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010017877item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017878{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017879 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017880 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017881 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017882 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017883 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017884
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017885 /* shortcut after failure in previous call; compare all items equal */
17886 if (item_compare_func_err)
17887 return 0;
17888
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017889 si1 = (sortItem_T *)s1;
17890 si2 = (sortItem_T *)s2;
17891
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017892 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017893 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017894 copy_tv(&si1->item->li_tv, &argv[0]);
17895 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017896
17897 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017898 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017899 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17900 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017901 clear_tv(&argv[0]);
17902 clear_tv(&argv[1]);
17903
17904 if (res == FAIL)
17905 res = ITEM_COMPARE_FAIL;
17906 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017907 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17908 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017909 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017910 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017911
17912 /* When the result would be zero, compare the pointers themselves. Makes
17913 * the sort stable. */
17914 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017915 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017916
Bram Moolenaar0d660222005-01-07 21:51:51 +000017917 return res;
17918}
17919
17920/*
17921 * "sort({list})" function
17922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017924do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017925{
Bram Moolenaar33570922005-01-25 22:26:29 +000017926 list_T *l;
17927 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017928 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017929 long len;
17930 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017931
Bram Moolenaar0d660222005-01-07 21:51:51 +000017932 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017933 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017934 else
17935 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017936 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017937 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020017938 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
17939 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017940 return;
17941 rettv->vval.v_list = l;
17942 rettv->v_type = VAR_LIST;
17943 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017944
Bram Moolenaar0d660222005-01-07 21:51:51 +000017945 len = list_len(l);
17946 if (len <= 1)
17947 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017948
Bram Moolenaar0d660222005-01-07 21:51:51 +000017949 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017950 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017951 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017952#ifdef FEAT_FLOAT
17953 item_compare_float = FALSE;
17954#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017955 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017956 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017957 if (argvars[1].v_type != VAR_UNKNOWN)
17958 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017959 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017960 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017961 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017962 else
17963 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017964 int error = FALSE;
17965
17966 i = get_tv_number_chk(&argvars[1], &error);
17967 if (error)
17968 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017969 if (i == 1)
17970 item_compare_ic = TRUE;
17971 else
17972 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017973 if (item_compare_func != NULL)
17974 {
17975 if (STRCMP(item_compare_func, "n") == 0)
17976 {
17977 item_compare_func = NULL;
17978 item_compare_numeric = TRUE;
17979 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017980 else if (STRCMP(item_compare_func, "N") == 0)
17981 {
17982 item_compare_func = NULL;
17983 item_compare_numbers = TRUE;
17984 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017985#ifdef FEAT_FLOAT
17986 else if (STRCMP(item_compare_func, "f") == 0)
17987 {
17988 item_compare_func = NULL;
17989 item_compare_float = TRUE;
17990 }
17991#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020017992 else if (STRCMP(item_compare_func, "i") == 0)
17993 {
17994 item_compare_func = NULL;
17995 item_compare_ic = TRUE;
17996 }
17997 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017998 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017999
18000 if (argvars[2].v_type != VAR_UNKNOWN)
18001 {
18002 /* optional third argument: {dict} */
18003 if (argvars[2].v_type != VAR_DICT)
18004 {
18005 EMSG(_(e_dictreq));
18006 return;
18007 }
18008 item_compare_selfdict = argvars[2].vval.v_dict;
18009 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011
Bram Moolenaar0d660222005-01-07 21:51:51 +000018012 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018013 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018014 if (ptrs == NULL)
18015 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018016
Bram Moolenaar327aa022014-03-25 18:24:23 +010018017 i = 0;
18018 if (sort)
18019 {
18020 /* sort(): ptrs will be the list to sort */
18021 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018022 {
18023 ptrs[i].item = li;
18024 ptrs[i].idx = i;
18025 ++i;
18026 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018027
18028 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018029 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018030 /* test the compare function */
18031 if (item_compare_func != NULL
18032 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018033 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018034 EMSG(_("E702: Sort compare function failed"));
18035 else
18036 {
18037 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018038 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018039 item_compare_func == NULL ? item_compare : item_compare2);
18040
18041 if (!item_compare_func_err)
18042 {
18043 /* Clear the List and append the items in sorted order. */
18044 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18045 l->lv_len = 0;
18046 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018047 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018048 }
18049 }
18050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018052 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018053 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018054
18055 /* f_uniq(): ptrs will be a stack of items to remove */
18056 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018057 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018058 item_compare_func_ptr = item_compare_func
18059 ? item_compare2 : item_compare;
18060
18061 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18062 li = li->li_next)
18063 {
18064 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18065 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018066 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018067 if (item_compare_func_err)
18068 {
18069 EMSG(_("E882: Uniq compare function failed"));
18070 break;
18071 }
18072 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018073
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018074 if (!item_compare_func_err)
18075 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018076 while (--i >= 0)
18077 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018078 li = ptrs[i].item->li_next;
18079 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018080 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018081 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018082 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018083 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018084 list_fix_watch(l, li);
18085 listitem_free(li);
18086 l->lv_len--;
18087 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018088 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018089 }
18090
18091 vim_free(ptrs);
18092 }
18093}
18094
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018095/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018096 * "sort({list})" function
18097 */
18098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018099f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018100{
18101 do_sort_uniq(argvars, rettv, TRUE);
18102}
18103
18104/*
18105 * "uniq({list})" function
18106 */
18107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018108f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018109{
18110 do_sort_uniq(argvars, rettv, FALSE);
18111}
18112
18113/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018114 * "soundfold({word})" function
18115 */
18116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018117f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018118{
18119 char_u *s;
18120
18121 rettv->v_type = VAR_STRING;
18122 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018123#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018124 rettv->vval.v_string = eval_soundfold(s);
18125#else
18126 rettv->vval.v_string = vim_strsave(s);
18127#endif
18128}
18129
18130/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018131 * "spellbadword()" function
18132 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018134f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018135{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018136 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018137 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018138 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018139
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018140 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018141 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018142
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018143#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018144 if (argvars[0].v_type == VAR_UNKNOWN)
18145 {
18146 /* Find the start and length of the badly spelled word. */
18147 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18148 if (len != 0)
18149 word = ml_get_cursor();
18150 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018151 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018152 {
18153 char_u *str = get_tv_string_chk(&argvars[0]);
18154 int capcol = -1;
18155
18156 if (str != NULL)
18157 {
18158 /* Check the argument for spelling. */
18159 while (*str != NUL)
18160 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018161 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018162 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018163 {
18164 word = str;
18165 break;
18166 }
18167 str += len;
18168 }
18169 }
18170 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018171#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018172
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018173 list_append_string(rettv->vval.v_list, word, len);
18174 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018175 attr == HLF_SPB ? "bad" :
18176 attr == HLF_SPR ? "rare" :
18177 attr == HLF_SPL ? "local" :
18178 attr == HLF_SPC ? "caps" :
18179 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018180}
18181
18182/*
18183 * "spellsuggest()" function
18184 */
18185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018186f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018187{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018188#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018189 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018190 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018191 int maxcount;
18192 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018193 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018194 listitem_T *li;
18195 int need_capital = FALSE;
18196#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018197
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018198 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018199 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018200
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018201#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018202 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018203 {
18204 str = get_tv_string(&argvars[0]);
18205 if (argvars[1].v_type != VAR_UNKNOWN)
18206 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018207 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018208 if (maxcount <= 0)
18209 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018210 if (argvars[2].v_type != VAR_UNKNOWN)
18211 {
18212 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18213 if (typeerr)
18214 return;
18215 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018216 }
18217 else
18218 maxcount = 25;
18219
Bram Moolenaar4770d092006-01-12 23:22:24 +000018220 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018221
18222 for (i = 0; i < ga.ga_len; ++i)
18223 {
18224 str = ((char_u **)ga.ga_data)[i];
18225
18226 li = listitem_alloc();
18227 if (li == NULL)
18228 vim_free(str);
18229 else
18230 {
18231 li->li_tv.v_type = VAR_STRING;
18232 li->li_tv.v_lock = 0;
18233 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018234 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018235 }
18236 }
18237 ga_clear(&ga);
18238 }
18239#endif
18240}
18241
Bram Moolenaar0d660222005-01-07 21:51:51 +000018242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018243f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018244{
18245 char_u *str;
18246 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018247 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018248 regmatch_T regmatch;
18249 char_u patbuf[NUMBUFLEN];
18250 char_u *save_cpo;
18251 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018252 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018253 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018254 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018255
18256 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18257 save_cpo = p_cpo;
18258 p_cpo = (char_u *)"";
18259
18260 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018261 if (argvars[1].v_type != VAR_UNKNOWN)
18262 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018263 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18264 if (pat == NULL)
18265 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018266 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018267 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018268 }
18269 if (pat == NULL || *pat == NUL)
18270 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018271
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018272 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018273 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018274 if (typeerr)
18275 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018276
Bram Moolenaar0d660222005-01-07 21:51:51 +000018277 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18278 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018279 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018280 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018281 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018282 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018283 if (*str == NUL)
18284 match = FALSE; /* empty item at the end */
18285 else
18286 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018287 if (match)
18288 end = regmatch.startp[0];
18289 else
18290 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018291 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18292 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018293 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018294 if (list_append_string(rettv->vval.v_list, str,
18295 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018296 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018297 }
18298 if (!match)
18299 break;
18300 /* Advance to just after the match. */
18301 if (regmatch.endp[0] > str)
18302 col = 0;
18303 else
18304 {
18305 /* Don't get stuck at the same match. */
18306#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018307 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018308#else
18309 col = 1;
18310#endif
18311 }
18312 str = regmatch.endp[0];
18313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018314
Bram Moolenaar473de612013-06-08 18:19:48 +020018315 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018317
Bram Moolenaar0d660222005-01-07 21:51:51 +000018318 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018319}
18320
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018321#ifdef FEAT_FLOAT
18322/*
18323 * "sqrt()" function
18324 */
18325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018326f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018327{
18328 float_T f;
18329
18330 rettv->v_type = VAR_FLOAT;
18331 if (get_float_arg(argvars, &f) == OK)
18332 rettv->vval.v_float = sqrt(f);
18333 else
18334 rettv->vval.v_float = 0.0;
18335}
18336
18337/*
18338 * "str2float()" function
18339 */
18340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018341f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018342{
18343 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18344
18345 if (*p == '+')
18346 p = skipwhite(p + 1);
18347 (void)string2float(p, &rettv->vval.v_float);
18348 rettv->v_type = VAR_FLOAT;
18349}
18350#endif
18351
Bram Moolenaar2c932302006-03-18 21:42:09 +000018352/*
18353 * "str2nr()" function
18354 */
18355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018356f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018357{
18358 int base = 10;
18359 char_u *p;
18360 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018361 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018362
18363 if (argvars[1].v_type != VAR_UNKNOWN)
18364 {
18365 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018366 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018367 {
18368 EMSG(_(e_invarg));
18369 return;
18370 }
18371 }
18372
18373 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018374 if (*p == '+')
18375 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018376 switch (base)
18377 {
18378 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18379 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18380 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18381 default: what = 0;
18382 }
18383 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018384 rettv->vval.v_number = n;
18385}
18386
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387#ifdef HAVE_STRFTIME
18388/*
18389 * "strftime({format}[, {time}])" function
18390 */
18391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018392f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018393{
18394 char_u result_buf[256];
18395 struct tm *curtime;
18396 time_t seconds;
18397 char_u *p;
18398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018399 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018400
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018401 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018402 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018403 seconds = time(NULL);
18404 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018405 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018406 curtime = localtime(&seconds);
18407 /* MSVC returns NULL for an invalid value of seconds. */
18408 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018409 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018410 else
18411 {
18412# ifdef FEAT_MBYTE
18413 vimconv_T conv;
18414 char_u *enc;
18415
18416 conv.vc_type = CONV_NONE;
18417 enc = enc_locale();
18418 convert_setup(&conv, p_enc, enc);
18419 if (conv.vc_type != CONV_NONE)
18420 p = string_convert(&conv, p, NULL);
18421# endif
18422 if (p != NULL)
18423 (void)strftime((char *)result_buf, sizeof(result_buf),
18424 (char *)p, curtime);
18425 else
18426 result_buf[0] = NUL;
18427
18428# ifdef FEAT_MBYTE
18429 if (conv.vc_type != CONV_NONE)
18430 vim_free(p);
18431 convert_setup(&conv, enc, p_enc);
18432 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018433 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018434 else
18435# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018436 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018437
18438# ifdef FEAT_MBYTE
18439 /* Release conversion descriptors */
18440 convert_setup(&conv, NULL, NULL);
18441 vim_free(enc);
18442# endif
18443 }
18444}
18445#endif
18446
18447/*
18448 * "stridx()" function
18449 */
18450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018451f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018452{
18453 char_u buf[NUMBUFLEN];
18454 char_u *needle;
18455 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018456 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018457 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018458 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018459
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018460 needle = get_tv_string_chk(&argvars[1]);
18461 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018462 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018463 if (needle == NULL || haystack == NULL)
18464 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018465
Bram Moolenaar33570922005-01-25 22:26:29 +000018466 if (argvars[2].v_type != VAR_UNKNOWN)
18467 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018468 int error = FALSE;
18469
18470 start_idx = get_tv_number_chk(&argvars[2], &error);
18471 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018472 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018473 if (start_idx >= 0)
18474 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018475 }
18476
18477 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18478 if (pos != NULL)
18479 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480}
18481
18482/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018483 * "string()" function
18484 */
18485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018486f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018487{
18488 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018489 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018490
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018491 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018492 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018493 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018494 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018495 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018496}
18497
18498/*
18499 * "strlen()" function
18500 */
18501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018502f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018503{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018504 rettv->vval.v_number = (varnumber_T)(STRLEN(
18505 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018506}
18507
18508/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018509 * "strchars()" function
18510 */
18511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018512f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018513{
18514 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018515 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018516#ifdef FEAT_MBYTE
18517 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018518 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018519#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018520
18521 if (argvars[1].v_type != VAR_UNKNOWN)
18522 skipcc = get_tv_number_chk(&argvars[1], NULL);
18523 if (skipcc < 0 || skipcc > 1)
18524 EMSG(_(e_invarg));
18525 else
18526 {
18527#ifdef FEAT_MBYTE
18528 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18529 while (*s != NUL)
18530 {
18531 func_mb_ptr2char_adv(&s);
18532 ++len;
18533 }
18534 rettv->vval.v_number = len;
18535#else
18536 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18537#endif
18538 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018539}
18540
18541/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018542 * "strdisplaywidth()" function
18543 */
18544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018545f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020018546{
18547 char_u *s = get_tv_string(&argvars[0]);
18548 int col = 0;
18549
18550 if (argvars[1].v_type != VAR_UNKNOWN)
18551 col = get_tv_number(&argvars[1]);
18552
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018553 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018554}
18555
18556/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018557 * "strwidth()" function
18558 */
18559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018560f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018561{
18562 char_u *s = get_tv_string(&argvars[0]);
18563
18564 rettv->vval.v_number = (varnumber_T)(
18565#ifdef FEAT_MBYTE
18566 mb_string2cells(s, -1)
18567#else
18568 STRLEN(s)
18569#endif
18570 );
18571}
18572
18573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018574 * "strpart()" function
18575 */
18576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018577f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018578{
18579 char_u *p;
18580 int n;
18581 int len;
18582 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018583 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018585 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018586 slen = (int)STRLEN(p);
18587
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018588 n = get_tv_number_chk(&argvars[1], &error);
18589 if (error)
18590 len = 0;
18591 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018592 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018593 else
18594 len = slen - n; /* default len: all bytes that are available. */
18595
18596 /*
18597 * Only return the overlap between the specified part and the actual
18598 * string.
18599 */
18600 if (n < 0)
18601 {
18602 len += n;
18603 n = 0;
18604 }
18605 else if (n > slen)
18606 n = slen;
18607 if (len < 0)
18608 len = 0;
18609 else if (n + len > slen)
18610 len = slen - n;
18611
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018612 rettv->v_type = VAR_STRING;
18613 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018614}
18615
18616/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018617 * "strridx()" function
18618 */
18619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018620f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018621{
18622 char_u buf[NUMBUFLEN];
18623 char_u *needle;
18624 char_u *haystack;
18625 char_u *rest;
18626 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018627 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018628
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018629 needle = get_tv_string_chk(&argvars[1]);
18630 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018631
18632 rettv->vval.v_number = -1;
18633 if (needle == NULL || haystack == NULL)
18634 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018635
18636 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018637 if (argvars[2].v_type != VAR_UNKNOWN)
18638 {
18639 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018640 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018641 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018642 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018643 }
18644 else
18645 end_idx = haystack_len;
18646
Bram Moolenaar0d660222005-01-07 21:51:51 +000018647 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018648 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018649 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018650 lastmatch = haystack + end_idx;
18651 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018652 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018653 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018654 for (rest = haystack; *rest != '\0'; ++rest)
18655 {
18656 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018657 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018658 break;
18659 lastmatch = rest;
18660 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018661 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018662
18663 if (lastmatch == NULL)
18664 rettv->vval.v_number = -1;
18665 else
18666 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18667}
18668
18669/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670 * "strtrans()" function
18671 */
18672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018673f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018674{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018675 rettv->v_type = VAR_STRING;
18676 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018677}
18678
18679/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018680 * "submatch()" function
18681 */
18682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018683f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018684{
Bram Moolenaar41571762014-04-02 19:00:58 +020018685 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018686 int no;
18687 int retList = 0;
18688
18689 no = (int)get_tv_number_chk(&argvars[0], &error);
18690 if (error)
18691 return;
18692 error = FALSE;
18693 if (argvars[1].v_type != VAR_UNKNOWN)
18694 retList = get_tv_number_chk(&argvars[1], &error);
18695 if (error)
18696 return;
18697
18698 if (retList == 0)
18699 {
18700 rettv->v_type = VAR_STRING;
18701 rettv->vval.v_string = reg_submatch(no);
18702 }
18703 else
18704 {
18705 rettv->v_type = VAR_LIST;
18706 rettv->vval.v_list = reg_submatch_list(no);
18707 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018708}
18709
18710/*
18711 * "substitute()" function
18712 */
18713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018714f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018715{
18716 char_u patbuf[NUMBUFLEN];
18717 char_u subbuf[NUMBUFLEN];
18718 char_u flagsbuf[NUMBUFLEN];
18719
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018720 char_u *str = get_tv_string_chk(&argvars[0]);
18721 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18722 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18723 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18724
Bram Moolenaar0d660222005-01-07 21:51:51 +000018725 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018726 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18727 rettv->vval.v_string = NULL;
18728 else
18729 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018730}
18731
18732/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018733 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018736f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737{
18738 int id = 0;
18739#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018740 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741 long col;
18742 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018743 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018745 lnum = get_tv_lnum(argvars); /* -1 on type error */
18746 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18747 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018748
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018749 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018750 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018751 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018752#endif
18753
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018754 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018755}
18756
18757/*
18758 * "synIDattr(id, what [, mode])" function
18759 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018761f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762{
18763 char_u *p = NULL;
18764#ifdef FEAT_SYN_HL
18765 int id;
18766 char_u *what;
18767 char_u *mode;
18768 char_u modebuf[NUMBUFLEN];
18769 int modec;
18770
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018771 id = get_tv_number(&argvars[0]);
18772 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018773 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018774 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018775 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018776 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018777 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018778 modec = 0; /* replace invalid with current */
18779 }
18780 else
18781 {
18782#ifdef FEAT_GUI
18783 if (gui.in_use)
18784 modec = 'g';
18785 else
18786#endif
18787 if (t_colors > 1)
18788 modec = 'c';
18789 else
18790 modec = 't';
18791 }
18792
18793
18794 switch (TOLOWER_ASC(what[0]))
18795 {
18796 case 'b':
18797 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18798 p = highlight_color(id, what, modec);
18799 else /* bold */
18800 p = highlight_has_attr(id, HL_BOLD, modec);
18801 break;
18802
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018803 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018804 p = highlight_color(id, what, modec);
18805 break;
18806
18807 case 'i':
18808 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18809 p = highlight_has_attr(id, HL_INVERSE, modec);
18810 else /* italic */
18811 p = highlight_has_attr(id, HL_ITALIC, modec);
18812 break;
18813
18814 case 'n': /* name */
18815 p = get_highlight_name(NULL, id - 1);
18816 break;
18817
18818 case 'r': /* reverse */
18819 p = highlight_has_attr(id, HL_INVERSE, modec);
18820 break;
18821
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018822 case 's':
18823 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18824 p = highlight_color(id, what, modec);
18825 else /* standout */
18826 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018827 break;
18828
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018829 case 'u':
18830 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18831 /* underline */
18832 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18833 else
18834 /* undercurl */
18835 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836 break;
18837 }
18838
18839 if (p != NULL)
18840 p = vim_strsave(p);
18841#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018842 rettv->v_type = VAR_STRING;
18843 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018844}
18845
18846/*
18847 * "synIDtrans(id)" function
18848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018850f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018851{
18852 int id;
18853
18854#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018855 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856
18857 if (id > 0)
18858 id = syn_get_final_id(id);
18859 else
18860#endif
18861 id = 0;
18862
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018863 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018864}
18865
18866/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018867 * "synconcealed(lnum, col)" function
18868 */
18869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018870f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018871{
18872#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18873 long lnum;
18874 long col;
18875 int syntax_flags = 0;
18876 int cchar;
18877 int matchid = 0;
18878 char_u str[NUMBUFLEN];
18879#endif
18880
18881 rettv->v_type = VAR_LIST;
18882 rettv->vval.v_list = NULL;
18883
18884#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18885 lnum = get_tv_lnum(argvars); /* -1 on type error */
18886 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18887
18888 vim_memset(str, NUL, sizeof(str));
18889
18890 if (rettv_list_alloc(rettv) != FAIL)
18891 {
18892 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18893 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18894 && curwin->w_p_cole > 0)
18895 {
18896 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18897 syntax_flags = get_syntax_info(&matchid);
18898
18899 /* get the conceal character */
18900 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18901 {
18902 cchar = syn_get_sub_char();
18903 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18904 cchar = lcs_conceal;
18905 if (cchar != NUL)
18906 {
18907# ifdef FEAT_MBYTE
18908 if (has_mbyte)
18909 (*mb_char2bytes)(cchar, str);
18910 else
18911# endif
18912 str[0] = cchar;
18913 }
18914 }
18915 }
18916
18917 list_append_number(rettv->vval.v_list,
18918 (syntax_flags & HL_CONCEAL) != 0);
18919 /* -1 to auto-determine strlen */
18920 list_append_string(rettv->vval.v_list, str, -1);
18921 list_append_number(rettv->vval.v_list, matchid);
18922 }
18923#endif
18924}
18925
18926/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018927 * "synstack(lnum, col)" function
18928 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018930f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018931{
18932#ifdef FEAT_SYN_HL
18933 long lnum;
18934 long col;
18935 int i;
18936 int id;
18937#endif
18938
18939 rettv->v_type = VAR_LIST;
18940 rettv->vval.v_list = NULL;
18941
18942#ifdef FEAT_SYN_HL
18943 lnum = get_tv_lnum(argvars); /* -1 on type error */
18944 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18945
18946 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018947 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018948 && rettv_list_alloc(rettv) != FAIL)
18949 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018950 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018951 for (i = 0; ; ++i)
18952 {
18953 id = syn_get_stack_item(i);
18954 if (id < 0)
18955 break;
18956 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18957 break;
18958 }
18959 }
18960#endif
18961}
18962
Bram Moolenaar071d4272004-06-13 20:20:40 +000018963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018964get_cmd_output_as_rettv(
18965 typval_T *argvars,
18966 typval_T *rettv,
18967 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018968{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018969 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018971 char_u *infile = NULL;
18972 char_u buf[NUMBUFLEN];
18973 int err = FALSE;
18974 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018975 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020018976 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018978 rettv->v_type = VAR_STRING;
18979 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018980 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018981 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018982
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018983 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018984 {
18985 /*
18986 * Write the string to a temp file, to be used for input of the shell
18987 * command.
18988 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020018989 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018990 {
18991 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018992 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018993 }
18994
18995 fd = mch_fopen((char *)infile, WRITEBIN);
18996 if (fd == NULL)
18997 {
18998 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018999 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019000 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019001 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019002 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019003 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19004 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019005 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019006 else
19007 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019008 size_t len;
19009
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019010 p = get_tv_string_buf_chk(&argvars[1], buf);
19011 if (p == NULL)
19012 {
19013 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019014 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019015 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019016 len = STRLEN(p);
19017 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019018 err = TRUE;
19019 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019020 if (fclose(fd) != 0)
19021 err = TRUE;
19022 if (err)
19023 {
19024 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019025 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019026 }
19027 }
19028
Bram Moolenaar52a72462014-08-29 15:53:52 +020019029 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19030 * echoes typeahead, that messes up the display. */
19031 if (!msg_silent)
19032 flags += SHELL_COOKED;
19033
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019034 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019035 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019036 int len;
19037 listitem_T *li;
19038 char_u *s = NULL;
19039 char_u *start;
19040 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019041 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019042
Bram Moolenaar52a72462014-08-29 15:53:52 +020019043 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019044 if (res == NULL)
19045 goto errret;
19046
19047 list = list_alloc();
19048 if (list == NULL)
19049 goto errret;
19050
19051 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019052 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019053 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019054 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019055 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019056 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019057
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019058 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019059 if (s == NULL)
19060 goto errret;
19061
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019062 for (p = s; start < end; ++p, ++start)
19063 *p = *start == NUL ? NL : *start;
19064 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019065
19066 li = listitem_alloc();
19067 if (li == NULL)
19068 {
19069 vim_free(s);
19070 goto errret;
19071 }
19072 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019073 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019074 li->li_tv.vval.v_string = s;
19075 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019076 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019077
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019078 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019079 rettv->v_type = VAR_LIST;
19080 rettv->vval.v_list = list;
19081 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019083 else
19084 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019085 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019086#ifdef USE_CR
19087 /* translate <CR> into <NL> */
19088 if (res != NULL)
19089 {
19090 char_u *s;
19091
19092 for (s = res; *s; ++s)
19093 {
19094 if (*s == CAR)
19095 *s = NL;
19096 }
19097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098#else
19099# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019100 /* translate <CR><NL> into <NL> */
19101 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019102 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019103 char_u *s, *d;
19104
19105 d = res;
19106 for (s = res; *s; ++s)
19107 {
19108 if (s[0] == CAR && s[1] == NL)
19109 ++s;
19110 *d++ = *s;
19111 }
19112 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019114# endif
19115#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019116 rettv->vval.v_string = res;
19117 res = NULL;
19118 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019119
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019120errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019121 if (infile != NULL)
19122 {
19123 mch_remove(infile);
19124 vim_free(infile);
19125 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019126 if (res != NULL)
19127 vim_free(res);
19128 if (list != NULL)
19129 list_free(list, TRUE);
19130}
19131
19132/*
19133 * "system()" function
19134 */
19135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019136f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019137{
19138 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19139}
19140
19141/*
19142 * "systemlist()" function
19143 */
19144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019145f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019146{
19147 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019148}
19149
19150/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019151 * "tabpagebuflist()" function
19152 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019154f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019155{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019156#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019157 tabpage_T *tp;
19158 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019159
19160 if (argvars[0].v_type == VAR_UNKNOWN)
19161 wp = firstwin;
19162 else
19163 {
19164 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19165 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019166 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019167 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019168 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019169 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019170 for (; wp != NULL; wp = wp->w_next)
19171 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019172 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019173 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019174 }
19175#endif
19176}
19177
19178
19179/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019180 * "tabpagenr()" function
19181 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019183f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019184{
19185 int nr = 1;
19186#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019187 char_u *arg;
19188
19189 if (argvars[0].v_type != VAR_UNKNOWN)
19190 {
19191 arg = get_tv_string_chk(&argvars[0]);
19192 nr = 0;
19193 if (arg != NULL)
19194 {
19195 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019196 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019197 else
19198 EMSG2(_(e_invexpr2), arg);
19199 }
19200 }
19201 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019202 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019203#endif
19204 rettv->vval.v_number = nr;
19205}
19206
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019207
19208#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019209static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019210
19211/*
19212 * Common code for tabpagewinnr() and winnr().
19213 */
19214 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019215get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019216{
19217 win_T *twin;
19218 int nr = 1;
19219 win_T *wp;
19220 char_u *arg;
19221
19222 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19223 if (argvar->v_type != VAR_UNKNOWN)
19224 {
19225 arg = get_tv_string_chk(argvar);
19226 if (arg == NULL)
19227 nr = 0; /* type error; errmsg already given */
19228 else if (STRCMP(arg, "$") == 0)
19229 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19230 else if (STRCMP(arg, "#") == 0)
19231 {
19232 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19233 if (twin == NULL)
19234 nr = 0;
19235 }
19236 else
19237 {
19238 EMSG2(_(e_invexpr2), arg);
19239 nr = 0;
19240 }
19241 }
19242
19243 if (nr > 0)
19244 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19245 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019246 {
19247 if (wp == NULL)
19248 {
19249 /* didn't find it in this tabpage */
19250 nr = 0;
19251 break;
19252 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019253 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019254 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019255 return nr;
19256}
19257#endif
19258
19259/*
19260 * "tabpagewinnr()" function
19261 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019263f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019264{
19265 int nr = 1;
19266#ifdef FEAT_WINDOWS
19267 tabpage_T *tp;
19268
19269 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19270 if (tp == NULL)
19271 nr = 0;
19272 else
19273 nr = get_winnr(tp, &argvars[1]);
19274#endif
19275 rettv->vval.v_number = nr;
19276}
19277
19278
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019279/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019280 * "tagfiles()" function
19281 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019283f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019284{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019285 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019286 tagname_T tn;
19287 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019288
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019289 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019290 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019291 fname = alloc(MAXPATHL);
19292 if (fname == NULL)
19293 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019294
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019295 for (first = TRUE; ; first = FALSE)
19296 if (get_tagfname(&tn, first, fname) == FAIL
19297 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019298 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019299 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019300 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019301}
19302
19303/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019304 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019305 */
19306 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019307f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019308{
19309 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019310
19311 tag_pattern = get_tv_string(&argvars[0]);
19312
19313 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019314 if (*tag_pattern == NUL)
19315 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019316
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019317 if (rettv_list_alloc(rettv) == OK)
19318 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019319}
19320
19321/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019322 * "tempname()" function
19323 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019325f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019326{
19327 static int x = 'A';
19328
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019329 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019330 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019331
19332 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19333 * names. Skip 'I' and 'O', they are used for shell redirection. */
19334 do
19335 {
19336 if (x == 'Z')
19337 x = '0';
19338 else if (x == '9')
19339 x = 'A';
19340 else
19341 {
19342#ifdef EBCDIC
19343 if (x == 'I')
19344 x = 'J';
19345 else if (x == 'R')
19346 x = 'S';
19347 else
19348#endif
19349 ++x;
19350 }
19351 } while (x == 'I' || x == 'O');
19352}
19353
19354/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019355 * "test(list)" function: Just checking the walls...
19356 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019358f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019359{
19360 /* Used for unit testing. Change the code below to your liking. */
19361#if 0
19362 listitem_T *li;
19363 list_T *l;
19364 char_u *bad, *good;
19365
19366 if (argvars[0].v_type != VAR_LIST)
19367 return;
19368 l = argvars[0].vval.v_list;
19369 if (l == NULL)
19370 return;
19371 li = l->lv_first;
19372 if (li == NULL)
19373 return;
19374 bad = get_tv_string(&li->li_tv);
19375 li = li->li_next;
19376 if (li == NULL)
19377 return;
19378 good = get_tv_string(&li->li_tv);
19379 rettv->vval.v_number = test_edit_score(bad, good);
19380#endif
19381}
19382
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019383#ifdef FEAT_FLOAT
19384/*
19385 * "tan()" function
19386 */
19387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019388f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019389{
19390 float_T f;
19391
19392 rettv->v_type = VAR_FLOAT;
19393 if (get_float_arg(argvars, &f) == OK)
19394 rettv->vval.v_float = tan(f);
19395 else
19396 rettv->vval.v_float = 0.0;
19397}
19398
19399/*
19400 * "tanh()" function
19401 */
19402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019403f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019404{
19405 float_T f;
19406
19407 rettv->v_type = VAR_FLOAT;
19408 if (get_float_arg(argvars, &f) == OK)
19409 rettv->vval.v_float = tanh(f);
19410 else
19411 rettv->vval.v_float = 0.0;
19412}
19413#endif
19414
Bram Moolenaard52d9742005-08-21 22:20:28 +000019415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019416 * "tolower(string)" function
19417 */
19418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019419f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420{
19421 char_u *p;
19422
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019423 p = vim_strsave(get_tv_string(&argvars[0]));
19424 rettv->v_type = VAR_STRING;
19425 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019426
19427 if (p != NULL)
19428 while (*p != NUL)
19429 {
19430#ifdef FEAT_MBYTE
19431 int l;
19432
19433 if (enc_utf8)
19434 {
19435 int c, lc;
19436
19437 c = utf_ptr2char(p);
19438 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019439 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019440 /* TODO: reallocate string when byte count changes. */
19441 if (utf_char2len(lc) == l)
19442 utf_char2bytes(lc, p);
19443 p += l;
19444 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019445 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019446 p += l; /* skip multi-byte character */
19447 else
19448#endif
19449 {
19450 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19451 ++p;
19452 }
19453 }
19454}
19455
19456/*
19457 * "toupper(string)" function
19458 */
19459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019460f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019462 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019463 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019464}
19465
19466/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019467 * "tr(string, fromstr, tostr)" function
19468 */
19469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019470f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019471{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019472 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019473 char_u *fromstr;
19474 char_u *tostr;
19475 char_u *p;
19476#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019477 int inlen;
19478 int fromlen;
19479 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019480 int idx;
19481 char_u *cpstr;
19482 int cplen;
19483 int first = TRUE;
19484#endif
19485 char_u buf[NUMBUFLEN];
19486 char_u buf2[NUMBUFLEN];
19487 garray_T ga;
19488
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019489 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019490 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19491 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019492
19493 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019494 rettv->v_type = VAR_STRING;
19495 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019496 if (fromstr == NULL || tostr == NULL)
19497 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019498 ga_init2(&ga, (int)sizeof(char), 80);
19499
19500#ifdef FEAT_MBYTE
19501 if (!has_mbyte)
19502#endif
19503 /* not multi-byte: fromstr and tostr must be the same length */
19504 if (STRLEN(fromstr) != STRLEN(tostr))
19505 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019506#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019507error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019508#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019509 EMSG2(_(e_invarg2), fromstr);
19510 ga_clear(&ga);
19511 return;
19512 }
19513
19514 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019515 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019516 {
19517#ifdef FEAT_MBYTE
19518 if (has_mbyte)
19519 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019520 inlen = (*mb_ptr2len)(in_str);
19521 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019522 cplen = inlen;
19523 idx = 0;
19524 for (p = fromstr; *p != NUL; p += fromlen)
19525 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019526 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019527 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019528 {
19529 for (p = tostr; *p != NUL; p += tolen)
19530 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019531 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019532 if (idx-- == 0)
19533 {
19534 cplen = tolen;
19535 cpstr = p;
19536 break;
19537 }
19538 }
19539 if (*p == NUL) /* tostr is shorter than fromstr */
19540 goto error;
19541 break;
19542 }
19543 ++idx;
19544 }
19545
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019546 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019547 {
19548 /* Check that fromstr and tostr have the same number of
19549 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019550 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019551 first = FALSE;
19552 for (p = tostr; *p != NUL; p += tolen)
19553 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019554 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019555 --idx;
19556 }
19557 if (idx != 0)
19558 goto error;
19559 }
19560
Bram Moolenaarcde88542015-08-11 19:14:00 +020019561 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019562 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019563 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019564
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019565 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019566 }
19567 else
19568#endif
19569 {
19570 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019571 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019572 if (p != NULL)
19573 ga_append(&ga, tostr[p - fromstr]);
19574 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019575 ga_append(&ga, *in_str);
19576 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019577 }
19578 }
19579
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019580 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019581 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019582 ga_append(&ga, NUL);
19583
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019584 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019585}
19586
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019587#ifdef FEAT_FLOAT
19588/*
19589 * "trunc({float})" function
19590 */
19591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019592f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019593{
19594 float_T f;
19595
19596 rettv->v_type = VAR_FLOAT;
19597 if (get_float_arg(argvars, &f) == OK)
19598 /* trunc() is not in C90, use floor() or ceil() instead. */
19599 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19600 else
19601 rettv->vval.v_float = 0.0;
19602}
19603#endif
19604
Bram Moolenaar8299df92004-07-10 09:47:34 +000019605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019606 * "type(expr)" function
19607 */
19608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019609f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019610{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019611 int n;
19612
19613 switch (argvars[0].v_type)
19614 {
19615 case VAR_NUMBER: n = 0; break;
19616 case VAR_STRING: n = 1; break;
19617 case VAR_FUNC: n = 2; break;
19618 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019619 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019620#ifdef FEAT_FLOAT
19621 case VAR_FLOAT: n = 5; break;
19622#endif
Bram Moolenaarf95534c2016-01-23 21:59:52 +010019623 case VAR_SPECIAL:
19624 if (argvars[0].vval.v_number == VVAL_FALSE
19625 || argvars[0].vval.v_number == VVAL_TRUE)
19626 n = 6;
19627 else
19628 n = 7;
19629 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019630 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19631 }
19632 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633}
19634
19635/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019636 * "undofile(name)" function
19637 */
19638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019639f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019640{
19641 rettv->v_type = VAR_STRING;
19642#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019643 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019644 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019645
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019646 if (*fname == NUL)
19647 {
19648 /* If there is no file name there will be no undo file. */
19649 rettv->vval.v_string = NULL;
19650 }
19651 else
19652 {
19653 char_u *ffname = FullName_save(fname, FALSE);
19654
19655 if (ffname != NULL)
19656 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19657 vim_free(ffname);
19658 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019659 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019660#else
19661 rettv->vval.v_string = NULL;
19662#endif
19663}
19664
19665/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019666 * "undotree()" function
19667 */
19668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019669f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020019670{
19671 if (rettv_dict_alloc(rettv) == OK)
19672 {
19673 dict_T *dict = rettv->vval.v_dict;
19674 list_T *list;
19675
Bram Moolenaar730cde92010-06-27 05:18:54 +020019676 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019677 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019678 dict_add_nr_str(dict, "save_last",
19679 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019680 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19681 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019682 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019683
19684 list = list_alloc();
19685 if (list != NULL)
19686 {
19687 u_eval_tree(curbuf->b_u_oldhead, list);
19688 dict_add_list(dict, "entries", list);
19689 }
19690 }
19691}
19692
19693/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019694 * "values(dict)" function
19695 */
19696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019697f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000019698{
19699 dict_list(argvars, rettv, 1);
19700}
19701
19702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019703 * "virtcol(string)" function
19704 */
19705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019706f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019707{
19708 colnr_T vcol = 0;
19709 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019710 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019711
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019712 fp = var2fpos(&argvars[0], FALSE, &fnum);
19713 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19714 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019715 {
19716 getvvcol(curwin, fp, NULL, NULL, &vcol);
19717 ++vcol;
19718 }
19719
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019720 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019721}
19722
19723/*
19724 * "visualmode()" function
19725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019727f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019728{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019729 char_u str[2];
19730
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019731 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732 str[0] = curbuf->b_visual_mode_eval;
19733 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019734 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019735
19736 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019737 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019738 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019739}
19740
19741/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019742 * "wildmenumode()" function
19743 */
19744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019745f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019746{
19747#ifdef FEAT_WILDMENU
19748 if (wild_menu_showing)
19749 rettv->vval.v_number = 1;
19750#endif
19751}
19752
19753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019754 * "winbufnr(nr)" function
19755 */
19756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019757f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758{
19759 win_T *wp;
19760
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019761 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019762 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019763 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019764 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019765 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766}
19767
19768/*
19769 * "wincol()" function
19770 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019772f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773{
19774 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019775 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019776}
19777
19778/*
19779 * "winheight(nr)" function
19780 */
19781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019782f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783{
19784 win_T *wp;
19785
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019786 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019787 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019788 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019789 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019790 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791}
19792
19793/*
19794 * "winline()" function
19795 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019797f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019798{
19799 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019800 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801}
19802
19803/*
19804 * "winnr()" function
19805 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019807f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019808{
19809 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019810
Bram Moolenaar071d4272004-06-13 20:20:40 +000019811#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019812 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019813#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019814 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815}
19816
19817/*
19818 * "winrestcmd()" function
19819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019821f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822{
19823#ifdef FEAT_WINDOWS
19824 win_T *wp;
19825 int winnr = 1;
19826 garray_T ga;
19827 char_u buf[50];
19828
19829 ga_init2(&ga, (int)sizeof(char), 70);
19830 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19831 {
19832 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19833 ga_concat(&ga, buf);
19834# ifdef FEAT_VERTSPLIT
19835 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19836 ga_concat(&ga, buf);
19837# endif
19838 ++winnr;
19839 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019840 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019841
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019842 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019843#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019844 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019845#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019846 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019847}
19848
19849/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019850 * "winrestview()" function
19851 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019853f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019854{
19855 dict_T *dict;
19856
19857 if (argvars[0].v_type != VAR_DICT
19858 || (dict = argvars[0].vval.v_dict) == NULL)
19859 EMSG(_(e_invarg));
19860 else
19861 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019862 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19863 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19864 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19865 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019866#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019867 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19868 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019869#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019870 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19871 {
19872 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19873 curwin->w_set_curswant = FALSE;
19874 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019875
Bram Moolenaar82c25852014-05-28 16:47:16 +020019876 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19877 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019878#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019879 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19880 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019881#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019882 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19883 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19884 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19885 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019886
19887 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019888 win_new_height(curwin, curwin->w_height);
19889# ifdef FEAT_VERTSPLIT
19890 win_new_width(curwin, W_WIDTH(curwin));
19891# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019892 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019893
Bram Moolenaarb851a962014-10-31 15:45:52 +010019894 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019895 curwin->w_topline = 1;
19896 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19897 curwin->w_topline = curbuf->b_ml.ml_line_count;
19898#ifdef FEAT_DIFF
19899 check_topfill(curwin, TRUE);
19900#endif
19901 }
19902}
19903
19904/*
19905 * "winsaveview()" function
19906 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019908f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019909{
19910 dict_T *dict;
19911
Bram Moolenaara800b422010-06-27 01:15:55 +020019912 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019913 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019914 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019915
19916 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19917 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19918#ifdef FEAT_VIRTUALEDIT
19919 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19920#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019921 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019922 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19923
19924 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19925#ifdef FEAT_DIFF
19926 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19927#endif
19928 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19929 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19930}
19931
19932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019933 * "winwidth(nr)" function
19934 */
19935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019936f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937{
19938 win_T *wp;
19939
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019940 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019941 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019942 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019943 else
19944#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019945 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019946#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019947 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948#endif
19949}
19950
Bram Moolenaar071d4272004-06-13 20:20:40 +000019951/*
Bram Moolenaared767a22016-01-03 22:49:16 +010019952 * "wordcount()" function
19953 */
19954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019955f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010019956{
19957 if (rettv_dict_alloc(rettv) == FAIL)
19958 return;
19959 cursor_pos_info(rettv->vval.v_dict);
19960}
19961
19962/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019963 * Write list of strings to file
19964 */
19965 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019966write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019967{
19968 listitem_T *li;
19969 int c;
19970 int ret = OK;
19971 char_u *s;
19972
19973 for (li = list->lv_first; li != NULL; li = li->li_next)
19974 {
19975 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19976 {
19977 if (*s == '\n')
19978 c = putc(NUL, fd);
19979 else
19980 c = putc(*s, fd);
19981 if (c == EOF)
19982 {
19983 ret = FAIL;
19984 break;
19985 }
19986 }
19987 if (!binary || li->li_next != NULL)
19988 if (putc('\n', fd) == EOF)
19989 {
19990 ret = FAIL;
19991 break;
19992 }
19993 if (ret == FAIL)
19994 {
19995 EMSG(_(e_write));
19996 break;
19997 }
19998 }
19999 return ret;
20000}
20001
20002/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020003 * "writefile()" function
20004 */
20005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020006f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020007{
20008 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020009 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020010 char_u *fname;
20011 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020012 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020013
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020014 if (check_restricted() || check_secure())
20015 return;
20016
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020017 if (argvars[0].v_type != VAR_LIST)
20018 {
20019 EMSG2(_(e_listarg), "writefile()");
20020 return;
20021 }
20022 if (argvars[0].vval.v_list == NULL)
20023 return;
20024
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020025 if (argvars[2].v_type != VAR_UNKNOWN)
20026 {
20027 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20028 binary = TRUE;
20029 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20030 append = TRUE;
20031 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020032
20033 /* Always open the file in binary mode, library functions have a mind of
20034 * their own about CR-LF conversion. */
20035 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020036 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20037 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020038 {
20039 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20040 ret = -1;
20041 }
20042 else
20043 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020044 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20045 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020046 fclose(fd);
20047 }
20048
20049 rettv->vval.v_number = ret;
20050}
20051
20052/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020053 * "xor(expr, expr)" function
20054 */
20055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020056f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020057{
20058 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20059 ^ get_tv_number_chk(&argvars[1], NULL);
20060}
20061
20062
20063/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020065 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066 */
20067 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020068var2fpos(
20069 typval_T *varp,
20070 int dollar_lnum, /* TRUE when $ is last line */
20071 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020073 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020074 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020075 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076
Bram Moolenaara5525202006-03-02 22:52:09 +000020077 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020078 if (varp->v_type == VAR_LIST)
20079 {
20080 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020081 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020082 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020083 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020084
20085 l = varp->vval.v_list;
20086 if (l == NULL)
20087 return NULL;
20088
20089 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020090 pos.lnum = list_find_nr(l, 0L, &error);
20091 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020092 return NULL; /* invalid line number */
20093
20094 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020095 pos.col = list_find_nr(l, 1L, &error);
20096 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020097 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020098 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020099
20100 /* We accept "$" for the column number: last column. */
20101 li = list_find(l, 1L);
20102 if (li != NULL && li->li_tv.v_type == VAR_STRING
20103 && li->li_tv.vval.v_string != NULL
20104 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20105 pos.col = len + 1;
20106
Bram Moolenaara5525202006-03-02 22:52:09 +000020107 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020108 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020109 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020110 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020111
Bram Moolenaara5525202006-03-02 22:52:09 +000020112#ifdef FEAT_VIRTUALEDIT
20113 /* Get the virtual offset. Defaults to zero. */
20114 pos.coladd = list_find_nr(l, 2L, &error);
20115 if (error)
20116 pos.coladd = 0;
20117#endif
20118
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020119 return &pos;
20120 }
20121
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020122 name = get_tv_string_chk(varp);
20123 if (name == NULL)
20124 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020125 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020126 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020127 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20128 {
20129 if (VIsual_active)
20130 return &VIsual;
20131 return &curwin->w_cursor;
20132 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020133 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020135 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020136 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20137 return NULL;
20138 return pp;
20139 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020140
20141#ifdef FEAT_VIRTUALEDIT
20142 pos.coladd = 0;
20143#endif
20144
Bram Moolenaar477933c2007-07-17 14:32:23 +000020145 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020146 {
20147 pos.col = 0;
20148 if (name[1] == '0') /* "w0": first visible line */
20149 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020150 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020151 pos.lnum = curwin->w_topline;
20152 return &pos;
20153 }
20154 else if (name[1] == '$') /* "w$": last visible line */
20155 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020156 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020157 pos.lnum = curwin->w_botline - 1;
20158 return &pos;
20159 }
20160 }
20161 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020162 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020163 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020164 {
20165 pos.lnum = curbuf->b_ml.ml_line_count;
20166 pos.col = 0;
20167 }
20168 else
20169 {
20170 pos.lnum = curwin->w_cursor.lnum;
20171 pos.col = (colnr_T)STRLEN(ml_get_curline());
20172 }
20173 return &pos;
20174 }
20175 return NULL;
20176}
20177
20178/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020179 * Convert list in "arg" into a position and optional file number.
20180 * When "fnump" is NULL there is no file number, only 3 items.
20181 * Note that the column is passed on as-is, the caller may want to decrement
20182 * it to use 1 for the first column.
20183 * Return FAIL when conversion is not possible, doesn't check the position for
20184 * validity.
20185 */
20186 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020187list2fpos(
20188 typval_T *arg,
20189 pos_T *posp,
20190 int *fnump,
20191 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020192{
20193 list_T *l = arg->vval.v_list;
20194 long i = 0;
20195 long n;
20196
Bram Moolenaar493c1782014-05-28 14:34:46 +020020197 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20198 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020199 if (arg->v_type != VAR_LIST
20200 || l == NULL
20201 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020202 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020203 return FAIL;
20204
20205 if (fnump != NULL)
20206 {
20207 n = list_find_nr(l, i++, NULL); /* fnum */
20208 if (n < 0)
20209 return FAIL;
20210 if (n == 0)
20211 n = curbuf->b_fnum; /* current buffer */
20212 *fnump = n;
20213 }
20214
20215 n = list_find_nr(l, i++, NULL); /* lnum */
20216 if (n < 0)
20217 return FAIL;
20218 posp->lnum = n;
20219
20220 n = list_find_nr(l, i++, NULL); /* col */
20221 if (n < 0)
20222 return FAIL;
20223 posp->col = n;
20224
20225#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020226 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020227 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020228 posp->coladd = 0;
20229 else
20230 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020231#endif
20232
Bram Moolenaar493c1782014-05-28 14:34:46 +020020233 if (curswantp != NULL)
20234 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20235
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020236 return OK;
20237}
20238
20239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020240 * Get the length of an environment variable name.
20241 * Advance "arg" to the first character after the name.
20242 * Return 0 for error.
20243 */
20244 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020245get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020246{
20247 char_u *p;
20248 int len;
20249
20250 for (p = *arg; vim_isIDc(*p); ++p)
20251 ;
20252 if (p == *arg) /* no name found */
20253 return 0;
20254
20255 len = (int)(p - *arg);
20256 *arg = p;
20257 return len;
20258}
20259
20260/*
20261 * Get the length of the name of a function or internal variable.
20262 * "arg" is advanced to the first non-white character after the name.
20263 * Return 0 if something is wrong.
20264 */
20265 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020266get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020267{
20268 char_u *p;
20269 int len;
20270
20271 /* Find the end of the name. */
20272 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020273 {
20274 if (*p == ':')
20275 {
20276 /* "s:" is start of "s:var", but "n:" is not and can be used in
20277 * slice "[n:]". Also "xx:" is not a namespace. */
20278 len = (int)(p - *arg);
20279 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20280 || len > 1)
20281 break;
20282 }
20283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284 if (p == *arg) /* no name found */
20285 return 0;
20286
20287 len = (int)(p - *arg);
20288 *arg = skipwhite(p);
20289
20290 return len;
20291}
20292
20293/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020294 * Get the length of the name of a variable or function.
20295 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020297 * Return -1 if curly braces expansion failed.
20298 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020299 * If the name contains 'magic' {}'s, expand them and return the
20300 * expanded name in an allocated string via 'alias' - caller must free.
20301 */
20302 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020303get_name_len(
20304 char_u **arg,
20305 char_u **alias,
20306 int evaluate,
20307 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020308{
20309 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310 char_u *p;
20311 char_u *expr_start;
20312 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020313
20314 *alias = NULL; /* default to no alias */
20315
20316 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20317 && (*arg)[2] == (int)KE_SNR)
20318 {
20319 /* hard coded <SNR>, already translated */
20320 *arg += 3;
20321 return get_id_len(arg) + 3;
20322 }
20323 len = eval_fname_script(*arg);
20324 if (len > 0)
20325 {
20326 /* literal "<SID>", "s:" or "<SNR>" */
20327 *arg += len;
20328 }
20329
Bram Moolenaar071d4272004-06-13 20:20:40 +000020330 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020331 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020332 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020333 p = find_name_end(*arg, &expr_start, &expr_end,
20334 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020335 if (expr_start != NULL)
20336 {
20337 char_u *temp_string;
20338
20339 if (!evaluate)
20340 {
20341 len += (int)(p - *arg);
20342 *arg = skipwhite(p);
20343 return len;
20344 }
20345
20346 /*
20347 * Include any <SID> etc in the expanded string:
20348 * Thus the -len here.
20349 */
20350 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20351 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020352 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020353 *alias = temp_string;
20354 *arg = skipwhite(p);
20355 return (int)STRLEN(temp_string);
20356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020357
20358 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020359 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360 EMSG2(_(e_invexpr2), *arg);
20361
20362 return len;
20363}
20364
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020365/*
20366 * Find the end of a variable or function name, taking care of magic braces.
20367 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20368 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020369 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020370 * Return a pointer to just after the name. Equal to "arg" if there is no
20371 * valid name.
20372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020373 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020374find_name_end(
20375 char_u *arg,
20376 char_u **expr_start,
20377 char_u **expr_end,
20378 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020379{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020380 int mb_nest = 0;
20381 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020382 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020383 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020385 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020386 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020387 *expr_start = NULL;
20388 *expr_end = NULL;
20389 }
20390
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020391 /* Quick check for valid starting character. */
20392 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20393 return arg;
20394
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020395 for (p = arg; *p != NUL
20396 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020397 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020398 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020399 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020400 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020401 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020402 if (*p == '\'')
20403 {
20404 /* skip over 'string' to avoid counting [ and ] inside it. */
20405 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20406 ;
20407 if (*p == NUL)
20408 break;
20409 }
20410 else if (*p == '"')
20411 {
20412 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20413 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20414 if (*p == '\\' && p[1] != NUL)
20415 ++p;
20416 if (*p == NUL)
20417 break;
20418 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020419 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20420 {
20421 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020422 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020423 len = (int)(p - arg);
20424 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020425 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020426 break;
20427 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020429 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020430 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020431 if (*p == '[')
20432 ++br_nest;
20433 else if (*p == ']')
20434 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020436
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020437 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020438 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020439 if (*p == '{')
20440 {
20441 mb_nest++;
20442 if (expr_start != NULL && *expr_start == NULL)
20443 *expr_start = p;
20444 }
20445 else if (*p == '}')
20446 {
20447 mb_nest--;
20448 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20449 *expr_end = p;
20450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020452 }
20453
20454 return p;
20455}
20456
20457/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020458 * Expands out the 'magic' {}'s in a variable/function name.
20459 * Note that this can call itself recursively, to deal with
20460 * constructs like foo{bar}{baz}{bam}
20461 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20462 * "in_start" ^
20463 * "expr_start" ^
20464 * "expr_end" ^
20465 * "in_end" ^
20466 *
20467 * Returns a new allocated string, which the caller must free.
20468 * Returns NULL for failure.
20469 */
20470 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020471make_expanded_name(
20472 char_u *in_start,
20473 char_u *expr_start,
20474 char_u *expr_end,
20475 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020476{
20477 char_u c1;
20478 char_u *retval = NULL;
20479 char_u *temp_result;
20480 char_u *nextcmd = NULL;
20481
20482 if (expr_end == NULL || in_end == NULL)
20483 return NULL;
20484 *expr_start = NUL;
20485 *expr_end = NUL;
20486 c1 = *in_end;
20487 *in_end = NUL;
20488
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020489 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020490 if (temp_result != NULL && nextcmd == NULL)
20491 {
20492 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20493 + (in_end - expr_end) + 1));
20494 if (retval != NULL)
20495 {
20496 STRCPY(retval, in_start);
20497 STRCAT(retval, temp_result);
20498 STRCAT(retval, expr_end + 1);
20499 }
20500 }
20501 vim_free(temp_result);
20502
20503 *in_end = c1; /* put char back for error messages */
20504 *expr_start = '{';
20505 *expr_end = '}';
20506
20507 if (retval != NULL)
20508 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020509 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020510 if (expr_start != NULL)
20511 {
20512 /* Further expansion! */
20513 temp_result = make_expanded_name(retval, expr_start,
20514 expr_end, temp_result);
20515 vim_free(retval);
20516 retval = temp_result;
20517 }
20518 }
20519
20520 return retval;
20521}
20522
20523/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020524 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020525 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020526 */
20527 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020528eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020529{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020530 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20531}
20532
20533/*
20534 * Return TRUE if character "c" can be used as the first character in a
20535 * variable or function name (excluding '{' and '}').
20536 */
20537 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020538eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020539{
20540 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020541}
20542
20543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020544 * Set number v: variable to "val".
20545 */
20546 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020547set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020548{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020549 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020550}
20551
20552/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020553 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020554 */
20555 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010020556get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020557{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020558 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020559}
20560
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020561/*
20562 * Get string v: variable value. Uses a static buffer, can only be used once.
20563 */
20564 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020565get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020566{
20567 return get_tv_string(&vimvars[idx].vv_tv);
20568}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020569
Bram Moolenaar071d4272004-06-13 20:20:40 +000020570/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020571 * Get List v: variable value. Caller must take care of reference count when
20572 * needed.
20573 */
20574 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020575get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000020576{
20577 return vimvars[idx].vv_list;
20578}
20579
20580/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020581 * Set v:char to character "c".
20582 */
20583 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020584set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020585{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020586 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020587
20588#ifdef FEAT_MBYTE
20589 if (has_mbyte)
20590 buf[(*mb_char2bytes)(c, buf)] = NUL;
20591 else
20592#endif
20593 {
20594 buf[0] = c;
20595 buf[1] = NUL;
20596 }
20597 set_vim_var_string(VV_CHAR, buf, -1);
20598}
20599
20600/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020601 * Set v:count to "count" and v:count1 to "count1".
20602 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020603 */
20604 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020605set_vcount(
20606 long count,
20607 long count1,
20608 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020609{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020610 if (set_prevcount)
20611 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020612 vimvars[VV_COUNT].vv_nr = count;
20613 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020614}
20615
20616/*
20617 * Set string v: variable to a copy of "val".
20618 */
20619 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020620set_vim_var_string(
20621 int idx,
20622 char_u *val,
20623 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020624{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020625 /* Need to do this (at least) once, since we can't initialize a union.
20626 * Will always be invoked when "v:progname" is set. */
20627 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20628
Bram Moolenaare9a41262005-01-15 22:18:47 +000020629 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020630 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020631 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020632 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020633 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020634 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020635 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020636}
20637
20638/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020639 * Set List v: variable to "val".
20640 */
20641 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020642set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000020643{
20644 list_unref(vimvars[idx].vv_list);
20645 vimvars[idx].vv_list = val;
20646 if (val != NULL)
20647 ++val->lv_refcount;
20648}
20649
20650/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020651 * Set Dictionary v: variable to "val".
20652 */
20653 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020654set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020020655{
20656 int todo;
20657 hashitem_T *hi;
20658
20659 dict_unref(vimvars[idx].vv_dict);
20660 vimvars[idx].vv_dict = val;
20661 if (val != NULL)
20662 {
20663 ++val->dv_refcount;
20664
20665 /* Set readonly */
20666 todo = (int)val->dv_hashtab.ht_used;
20667 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20668 {
20669 if (HASHITEM_EMPTY(hi))
20670 continue;
20671 --todo;
20672 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20673 }
20674 }
20675}
20676
20677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020678 * Set v:register if needed.
20679 */
20680 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020681set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020682{
20683 char_u regname;
20684
20685 if (c == 0 || c == ' ')
20686 regname = '"';
20687 else
20688 regname = c;
20689 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020690 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020691 set_vim_var_string(VV_REG, &regname, 1);
20692}
20693
20694/*
20695 * Get or set v:exception. If "oldval" == NULL, return the current value.
20696 * Otherwise, restore the value to "oldval" and return NULL.
20697 * Must always be called in pairs to save and restore v:exception! Does not
20698 * take care of memory allocations.
20699 */
20700 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020701v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702{
20703 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020704 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020705
Bram Moolenaare9a41262005-01-15 22:18:47 +000020706 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020707 return NULL;
20708}
20709
20710/*
20711 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20712 * Otherwise, restore the value to "oldval" and return NULL.
20713 * Must always be called in pairs to save and restore v:throwpoint! Does not
20714 * take care of memory allocations.
20715 */
20716 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020717v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020718{
20719 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020720 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020721
Bram Moolenaare9a41262005-01-15 22:18:47 +000020722 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723 return NULL;
20724}
20725
20726#if defined(FEAT_AUTOCMD) || defined(PROTO)
20727/*
20728 * Set v:cmdarg.
20729 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20730 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20731 * Must always be called in pairs!
20732 */
20733 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020734set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020735{
20736 char_u *oldval;
20737 char_u *newval;
20738 unsigned len;
20739
Bram Moolenaare9a41262005-01-15 22:18:47 +000020740 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020741 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020742 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020743 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020744 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020745 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020746 }
20747
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020748 if (eap->force_bin == FORCE_BIN)
20749 len = 6;
20750 else if (eap->force_bin == FORCE_NOBIN)
20751 len = 8;
20752 else
20753 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020754
20755 if (eap->read_edit)
20756 len += 7;
20757
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020758 if (eap->force_ff != 0)
20759 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20760# ifdef FEAT_MBYTE
20761 if (eap->force_enc != 0)
20762 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020763 if (eap->bad_char != 0)
20764 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020765# endif
20766
20767 newval = alloc(len + 1);
20768 if (newval == NULL)
20769 return NULL;
20770
20771 if (eap->force_bin == FORCE_BIN)
20772 sprintf((char *)newval, " ++bin");
20773 else if (eap->force_bin == FORCE_NOBIN)
20774 sprintf((char *)newval, " ++nobin");
20775 else
20776 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020777
20778 if (eap->read_edit)
20779 STRCAT(newval, " ++edit");
20780
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020781 if (eap->force_ff != 0)
20782 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20783 eap->cmd + eap->force_ff);
20784# ifdef FEAT_MBYTE
20785 if (eap->force_enc != 0)
20786 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20787 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020788 if (eap->bad_char == BAD_KEEP)
20789 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20790 else if (eap->bad_char == BAD_DROP)
20791 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20792 else if (eap->bad_char != 0)
20793 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020794# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020795 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020796 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020797}
20798#endif
20799
20800/*
20801 * Get the value of internal variable "name".
20802 * Return OK or FAIL.
20803 */
20804 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020805get_var_tv(
20806 char_u *name,
20807 int len, /* length of "name" */
20808 typval_T *rettv, /* NULL when only checking existence */
20809 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
20810 int verbose, /* may give error message */
20811 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020812{
20813 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020814 typval_T *tv = NULL;
20815 typval_T atv;
20816 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020817 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020818
20819 /* truncate the name, so that we can use strcmp() */
20820 cc = name[len];
20821 name[len] = NUL;
20822
20823 /*
20824 * Check for "b:changedtick".
20825 */
20826 if (STRCMP(name, "b:changedtick") == 0)
20827 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020828 atv.v_type = VAR_NUMBER;
20829 atv.vval.v_number = curbuf->b_changedtick;
20830 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020831 }
20832
20833 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020834 * Check for user-defined variables.
20835 */
20836 else
20837 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020838 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020839 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020840 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020841 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020842 if (dip != NULL)
20843 *dip = v;
20844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020845 }
20846
Bram Moolenaare9a41262005-01-15 22:18:47 +000020847 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020848 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020849 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020850 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020851 ret = FAIL;
20852 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020853 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020854 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020855
20856 name[len] = cc;
20857
20858 return ret;
20859}
20860
20861/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020862 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20863 * Also handle function call with Funcref variable: func(expr)
20864 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20865 */
20866 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020867handle_subscript(
20868 char_u **arg,
20869 typval_T *rettv,
20870 int evaluate, /* do more than finding the end */
20871 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020872{
20873 int ret = OK;
20874 dict_T *selfdict = NULL;
20875 char_u *s;
20876 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020877 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020878
20879 while (ret == OK
20880 && (**arg == '['
20881 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020882 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020883 && !vim_iswhite(*(*arg - 1)))
20884 {
20885 if (**arg == '(')
20886 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020887 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020888 if (evaluate)
20889 {
20890 functv = *rettv;
20891 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020892
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020893 /* Invoke the function. Recursive! */
20894 s = functv.vval.v_string;
20895 }
20896 else
20897 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020898 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020899 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20900 &len, evaluate, selfdict);
20901
20902 /* Clear the funcref afterwards, so that deleting it while
20903 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020904 if (evaluate)
20905 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020906
20907 /* Stop the expression evaluation when immediately aborting on
20908 * error, or when an interrupt occurred or an exception was thrown
20909 * but not caught. */
20910 if (aborting())
20911 {
20912 if (ret == OK)
20913 clear_tv(rettv);
20914 ret = FAIL;
20915 }
20916 dict_unref(selfdict);
20917 selfdict = NULL;
20918 }
20919 else /* **arg == '[' || **arg == '.' */
20920 {
20921 dict_unref(selfdict);
20922 if (rettv->v_type == VAR_DICT)
20923 {
20924 selfdict = rettv->vval.v_dict;
20925 if (selfdict != NULL)
20926 ++selfdict->dv_refcount;
20927 }
20928 else
20929 selfdict = NULL;
20930 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20931 {
20932 clear_tv(rettv);
20933 ret = FAIL;
20934 }
20935 }
20936 }
20937 dict_unref(selfdict);
20938 return ret;
20939}
20940
20941/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020942 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020943 * value).
20944 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020945 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020946alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020947{
Bram Moolenaar33570922005-01-25 22:26:29 +000020948 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020949}
20950
20951/*
20952 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020953 * The string "s" must have been allocated, it is consumed.
20954 * Return NULL for out of memory, the variable otherwise.
20955 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020956 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020957alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958{
Bram Moolenaar33570922005-01-25 22:26:29 +000020959 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020960
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020961 rettv = alloc_tv();
20962 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020963 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020964 rettv->v_type = VAR_STRING;
20965 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020966 }
20967 else
20968 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020969 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020970}
20971
20972/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020973 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020974 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020975 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020976free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020977{
20978 if (varp != NULL)
20979 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020980 switch (varp->v_type)
20981 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020982 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020983 func_unref(varp->vval.v_string);
20984 /*FALLTHROUGH*/
20985 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020986 vim_free(varp->vval.v_string);
20987 break;
20988 case VAR_LIST:
20989 list_unref(varp->vval.v_list);
20990 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020991 case VAR_DICT:
20992 dict_unref(varp->vval.v_dict);
20993 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020994 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020995#ifdef FEAT_FLOAT
20996 case VAR_FLOAT:
20997#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020998 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010020999 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021000 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021001 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021002 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021003 break;
21004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005 vim_free(varp);
21006 }
21007}
21008
21009/*
21010 * Free the memory for a variable value and set the value to NULL or 0.
21011 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021012 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021013clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021014{
21015 if (varp != NULL)
21016 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021017 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021018 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021019 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021020 func_unref(varp->vval.v_string);
21021 /*FALLTHROUGH*/
21022 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021023 vim_free(varp->vval.v_string);
21024 varp->vval.v_string = NULL;
21025 break;
21026 case VAR_LIST:
21027 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021028 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021029 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021030 case VAR_DICT:
21031 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021032 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021033 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021034 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021035 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021036 varp->vval.v_number = 0;
21037 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021038#ifdef FEAT_FLOAT
21039 case VAR_FLOAT:
21040 varp->vval.v_float = 0.0;
21041 break;
21042#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021043 case VAR_UNKNOWN:
21044 break;
21045 default:
21046 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021047 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021048 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021049 }
21050}
21051
21052/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021053 * Set the value of a variable to NULL without freeing items.
21054 */
21055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021056init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021057{
21058 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021059 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021060}
21061
21062/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021063 * Get the number value of a variable.
21064 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021065 * For incompatible types, return 0.
21066 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21067 * caller of incompatible types: it sets *denote to TRUE if "denote"
21068 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021069 */
21070 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021071get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021072{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021073 int error = FALSE;
21074
21075 return get_tv_number_chk(varp, &error); /* return 0L on error */
21076}
21077
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021078 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021079get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021080{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021081 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021082
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021083 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021084 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021085 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021086 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021087#ifdef FEAT_FLOAT
21088 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021089 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021090 break;
21091#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021092 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021093 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021094 break;
21095 case VAR_STRING:
21096 if (varp->vval.v_string != NULL)
21097 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021098 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021099 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021100 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021101 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021102 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021103 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021104 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021105 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021106 case VAR_SPECIAL:
21107 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21108 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021109 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021110 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021111 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021112 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021113 if (denote == NULL) /* useful for values that must be unsigned */
21114 n = -1;
21115 else
21116 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021117 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021118}
21119
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021120#ifdef FEAT_FLOAT
21121 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021122get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021123{
21124 switch (varp->v_type)
21125 {
21126 case VAR_NUMBER:
21127 return (float_T)(varp->vval.v_number);
21128#ifdef FEAT_FLOAT
21129 case VAR_FLOAT:
21130 return varp->vval.v_float;
21131 break;
21132#endif
21133 case VAR_FUNC:
21134 EMSG(_("E891: Using a Funcref as a Float"));
21135 break;
21136 case VAR_STRING:
21137 EMSG(_("E892: Using a String as a Float"));
21138 break;
21139 case VAR_LIST:
21140 EMSG(_("E893: Using a List as a Float"));
21141 break;
21142 case VAR_DICT:
21143 EMSG(_("E894: Using a Dictionary as a Float"));
21144 break;
21145 default:
21146 EMSG2(_(e_intern2), "get_tv_float()");
21147 break;
21148 }
21149 return 0;
21150}
21151#endif
21152
Bram Moolenaar071d4272004-06-13 20:20:40 +000021153/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021154 * Get the lnum from the first argument.
21155 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021156 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021157 */
21158 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021159get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021160{
Bram Moolenaar33570922005-01-25 22:26:29 +000021161 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162 linenr_T lnum;
21163
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021164 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165 if (lnum == 0) /* no valid number, try using line() */
21166 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021167 rettv.v_type = VAR_NUMBER;
21168 f_line(argvars, &rettv);
21169 lnum = rettv.vval.v_number;
21170 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021171 }
21172 return lnum;
21173}
21174
21175/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021176 * Get the lnum from the first argument.
21177 * Also accepts "$", then "buf" is used.
21178 * Returns 0 on error.
21179 */
21180 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021181get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021182{
21183 if (argvars[0].v_type == VAR_STRING
21184 && argvars[0].vval.v_string != NULL
21185 && argvars[0].vval.v_string[0] == '$'
21186 && buf != NULL)
21187 return buf->b_ml.ml_line_count;
21188 return get_tv_number_chk(&argvars[0], NULL);
21189}
21190
21191/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192 * Get the string value of a variable.
21193 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021194 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21195 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196 * If the String variable has never been set, return an empty string.
21197 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021198 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21199 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021200 */
21201 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021202get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021203{
21204 static char_u mybuf[NUMBUFLEN];
21205
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021206 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021207}
21208
21209 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021210get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021211{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021212 char_u *res = get_tv_string_buf_chk(varp, buf);
21213
21214 return res != NULL ? res : (char_u *)"";
21215}
21216
Bram Moolenaar7d647822014-04-05 21:28:56 +020021217/*
21218 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21219 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021220 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021221get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021222{
21223 static char_u mybuf[NUMBUFLEN];
21224
21225 return get_tv_string_buf_chk(varp, mybuf);
21226}
21227
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021228 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021229get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021230{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021231 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021232 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021233 case VAR_NUMBER:
21234 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21235 return buf;
21236 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021237 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021238 break;
21239 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021240 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021241 break;
21242 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021243 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021244 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021245#ifdef FEAT_FLOAT
21246 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021247 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021248 break;
21249#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021250 case VAR_STRING:
21251 if (varp->vval.v_string != NULL)
21252 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021253 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021254 case VAR_SPECIAL:
21255 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21256 return buf;
21257
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021258 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021259 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021260 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021261 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021262 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263}
21264
21265/*
21266 * Find variable "name" in the list of variables.
21267 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021268 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021269 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021270 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021272 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021273find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021274{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021275 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021276 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021277
Bram Moolenaara7043832005-01-21 11:56:39 +000021278 ht = find_var_ht(name, &varname);
21279 if (htp != NULL)
21280 *htp = ht;
21281 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021282 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021283 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021284}
21285
21286/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021287 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021288 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021289 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021290 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021291find_var_in_ht(
21292 hashtab_T *ht,
21293 int htname,
21294 char_u *varname,
21295 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021296{
Bram Moolenaar33570922005-01-25 22:26:29 +000021297 hashitem_T *hi;
21298
21299 if (*varname == NUL)
21300 {
21301 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021302 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021303 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021304 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021305 case 'g': return &globvars_var;
21306 case 'v': return &vimvars_var;
21307 case 'b': return &curbuf->b_bufvar;
21308 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021309#ifdef FEAT_WINDOWS
21310 case 't': return &curtab->tp_winvar;
21311#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021312 case 'l': return current_funccal == NULL
21313 ? NULL : &current_funccal->l_vars_var;
21314 case 'a': return current_funccal == NULL
21315 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021316 }
21317 return NULL;
21318 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021319
21320 hi = hash_find(ht, varname);
21321 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021322 {
21323 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021324 * worked find the variable again. Don't auto-load a script if it was
21325 * loaded already, otherwise it would be loaded every time when
21326 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021327 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021328 {
21329 /* Note: script_autoload() may make "hi" invalid. It must either
21330 * be obtained again or not used. */
21331 if (!script_autoload(varname, FALSE) || aborting())
21332 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021333 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021334 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021335 if (HASHITEM_EMPTY(hi))
21336 return NULL;
21337 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021338 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021339}
21340
21341/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021342 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021343 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021344 * Set "varname" to the start of name without ':'.
21345 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021346 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021347find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021348{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021349 hashitem_T *hi;
21350
Bram Moolenaar73627d02015-08-11 15:46:09 +020021351 if (name[0] == NUL)
21352 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021353 if (name[1] != ':')
21354 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021355 /* The name must not start with a colon or #. */
21356 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021357 return NULL;
21358 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021359
21360 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021361 hi = hash_find(&compat_hashtab, name);
21362 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021363 return &compat_hashtab;
21364
Bram Moolenaar071d4272004-06-13 20:20:40 +000021365 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021366 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021367 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368 }
21369 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021370 if (*name == 'g') /* global variable */
21371 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021372 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21373 */
21374 if (vim_strchr(name + 2, ':') != NULL
21375 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021376 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021377 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021378 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021379 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021380 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021381#ifdef FEAT_WINDOWS
21382 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021383 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021384#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021385 if (*name == 'v') /* v: variable */
21386 return &vimvarht;
21387 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021388 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021389 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021390 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021391 if (*name == 's' /* script variable */
21392 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21393 return &SCRIPT_VARS(current_SID);
21394 return NULL;
21395}
21396
21397/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021398 * Get function call environment based on bactrace debug level
21399 */
21400 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021401get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021402{
21403 int i;
21404 funccall_T *funccal;
21405 funccall_T *temp_funccal;
21406
21407 funccal = current_funccal;
21408 if (debug_backtrace_level > 0)
21409 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021410 for (i = 0; i < debug_backtrace_level; i++)
21411 {
21412 temp_funccal = funccal->caller;
21413 if (temp_funccal)
21414 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021415 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021416 /* backtrace level overflow. reset to max */
21417 debug_backtrace_level = i;
21418 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021419 }
21420 return funccal;
21421}
21422
21423/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021424 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021425 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021426 * Returns NULL when it doesn't exist.
21427 */
21428 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021429get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021430{
Bram Moolenaar33570922005-01-25 22:26:29 +000021431 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021432
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021433 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021434 if (v == NULL)
21435 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021436 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021437}
21438
21439/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021440 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021441 * sourcing this script and when executing functions defined in the script.
21442 */
21443 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021444new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021445{
Bram Moolenaara7043832005-01-21 11:56:39 +000021446 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021447 hashtab_T *ht;
21448 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021449
Bram Moolenaar071d4272004-06-13 20:20:40 +000021450 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21451 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021452 /* Re-allocating ga_data means that an ht_array pointing to
21453 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021454 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021455 for (i = 1; i <= ga_scripts.ga_len; ++i)
21456 {
21457 ht = &SCRIPT_VARS(i);
21458 if (ht->ht_mask == HT_INIT_SIZE - 1)
21459 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021460 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021461 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021462 }
21463
Bram Moolenaar071d4272004-06-13 20:20:40 +000021464 while (ga_scripts.ga_len < id)
21465 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021466 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021467 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021468 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021469 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021470 }
21471 }
21472}
21473
21474/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021475 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21476 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477 */
21478 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021479init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021480{
Bram Moolenaar33570922005-01-25 22:26:29 +000021481 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021482 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021483 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021484 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021485 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021486 dict_var->di_tv.vval.v_dict = dict;
21487 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021488 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021489 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21490 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021491}
21492
21493/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021494 * Unreference a dictionary initialized by init_var_dict().
21495 */
21496 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021497unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020021498{
21499 /* Now the dict needs to be freed if no one else is using it, go back to
21500 * normal reference counting. */
21501 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21502 dict_unref(dict);
21503}
21504
21505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021506 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021507 * Frees all allocated variables and the value they contain.
21508 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509 */
21510 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021511vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021512{
21513 vars_clear_ext(ht, TRUE);
21514}
21515
21516/*
21517 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21518 */
21519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021520vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021521{
Bram Moolenaara7043832005-01-21 11:56:39 +000021522 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021523 hashitem_T *hi;
21524 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021525
Bram Moolenaar33570922005-01-25 22:26:29 +000021526 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021527 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021528 for (hi = ht->ht_array; todo > 0; ++hi)
21529 {
21530 if (!HASHITEM_EMPTY(hi))
21531 {
21532 --todo;
21533
Bram Moolenaar33570922005-01-25 22:26:29 +000021534 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021535 * ht_array might change then. hash_clear() takes care of it
21536 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021537 v = HI2DI(hi);
21538 if (free_val)
21539 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021540 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021541 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021542 }
21543 }
21544 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021545 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021546}
21547
Bram Moolenaara7043832005-01-21 11:56:39 +000021548/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021549 * Delete a variable from hashtab "ht" at item "hi".
21550 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021551 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021553delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021554{
Bram Moolenaar33570922005-01-25 22:26:29 +000021555 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021556
21557 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021558 clear_tv(&di->di_tv);
21559 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021560}
21561
21562/*
21563 * List the value of one internal variable.
21564 */
21565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021566list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021567{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021568 char_u *tofree;
21569 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021570 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021571
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021572 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000021573 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021574 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021575 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021576}
21577
Bram Moolenaar071d4272004-06-13 20:20:40 +000021578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021579list_one_var_a(
21580 char_u *prefix,
21581 char_u *name,
21582 int type,
21583 char_u *string,
21584 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021585{
Bram Moolenaar31859182007-08-14 20:41:13 +000021586 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21587 msg_start();
21588 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021589 if (name != NULL) /* "a:" vars don't have a name stored */
21590 msg_puts(name);
21591 msg_putchar(' ');
21592 msg_advance(22);
21593 if (type == VAR_NUMBER)
21594 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021595 else if (type == VAR_FUNC)
21596 msg_putchar('*');
21597 else if (type == VAR_LIST)
21598 {
21599 msg_putchar('[');
21600 if (*string == '[')
21601 ++string;
21602 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021603 else if (type == VAR_DICT)
21604 {
21605 msg_putchar('{');
21606 if (*string == '{')
21607 ++string;
21608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609 else
21610 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021611
Bram Moolenaar071d4272004-06-13 20:20:40 +000021612 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021613
21614 if (type == VAR_FUNC)
21615 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021616 if (*first)
21617 {
21618 msg_clr_eos();
21619 *first = FALSE;
21620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021621}
21622
21623/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021624 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021625 * If the variable already exists, the value is updated.
21626 * Otherwise the variable is created.
21627 */
21628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021629set_var(
21630 char_u *name,
21631 typval_T *tv,
21632 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021633{
Bram Moolenaar33570922005-01-25 22:26:29 +000021634 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021635 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021636 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021637
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021638 ht = find_var_ht(name, &varname);
21639 if (ht == NULL || *varname == NUL)
21640 {
21641 EMSG2(_(e_illvar), name);
21642 return;
21643 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021644 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021645
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021646 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21647 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021648
Bram Moolenaar33570922005-01-25 22:26:29 +000021649 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021650 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021651 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021652 if (var_check_ro(v->di_flags, name, FALSE)
21653 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021654 return;
21655 if (v->di_tv.v_type != tv->v_type
21656 && !((v->di_tv.v_type == VAR_STRING
21657 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021658 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021659 || tv->v_type == VAR_NUMBER))
21660#ifdef FEAT_FLOAT
21661 && !((v->di_tv.v_type == VAR_NUMBER
21662 || v->di_tv.v_type == VAR_FLOAT)
21663 && (tv->v_type == VAR_NUMBER
21664 || tv->v_type == VAR_FLOAT))
21665#endif
21666 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021667 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021668 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021669 return;
21670 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021671
21672 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021673 * Handle setting internal v: variables separately where needed to
21674 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021675 */
21676 if (ht == &vimvarht)
21677 {
21678 if (v->di_tv.v_type == VAR_STRING)
21679 {
21680 vim_free(v->di_tv.vval.v_string);
21681 if (copy || tv->v_type != VAR_STRING)
21682 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21683 else
21684 {
21685 /* Take over the string to avoid an extra alloc/free. */
21686 v->di_tv.vval.v_string = tv->vval.v_string;
21687 tv->vval.v_string = NULL;
21688 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021689 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021690 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021691 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021692 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021693 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021694 if (STRCMP(varname, "searchforward") == 0)
21695 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021696#ifdef FEAT_SEARCH_EXTRA
21697 else if (STRCMP(varname, "hlsearch") == 0)
21698 {
21699 no_hlsearch = !v->di_tv.vval.v_number;
21700 redraw_all_later(SOME_VALID);
21701 }
21702#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021703 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021704 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021705 else if (v->di_tv.v_type != tv->v_type)
21706 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021707 }
21708
21709 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021710 }
21711 else /* add a new variable */
21712 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021713 /* Can't add "v:" variable. */
21714 if (ht == &vimvarht)
21715 {
21716 EMSG2(_(e_illvar), name);
21717 return;
21718 }
21719
Bram Moolenaar92124a32005-06-17 22:03:40 +000021720 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021721 if (!valid_varname(varname))
21722 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021723
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021724 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21725 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021726 if (v == NULL)
21727 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021728 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021729 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021731 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021732 return;
21733 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021734 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021735 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021736
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021737 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021738 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021739 else
21740 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021741 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021742 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021743 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021745}
21746
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021747/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021748 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021749 * Also give an error message.
21750 */
21751 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021752var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000021753{
21754 if (flags & DI_FLAGS_RO)
21755 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021756 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021757 return TRUE;
21758 }
21759 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21760 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021761 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021762 return TRUE;
21763 }
21764 return FALSE;
21765}
21766
21767/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021768 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21769 * Also give an error message.
21770 */
21771 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021772var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021773{
21774 if (flags & DI_FLAGS_FIX)
21775 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021776 EMSG2(_("E795: Cannot delete variable %s"),
21777 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021778 return TRUE;
21779 }
21780 return FALSE;
21781}
21782
21783/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021784 * Check if a funcref is assigned to a valid variable name.
21785 * Return TRUE and give an error if not.
21786 */
21787 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021788var_check_func_name(
21789 char_u *name, /* points to start of variable name */
21790 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021791{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021792 /* Allow for w: b: s: and t:. */
21793 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021794 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21795 ? name[2] : name[0]))
21796 {
21797 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21798 name);
21799 return TRUE;
21800 }
21801 /* Don't allow hiding a function. When "v" is not NULL we might be
21802 * assigning another function to the same var, the type is checked
21803 * below. */
21804 if (new_var && function_exists(name))
21805 {
21806 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21807 name);
21808 return TRUE;
21809 }
21810 return FALSE;
21811}
21812
21813/*
21814 * Check if a variable name is valid.
21815 * Return FALSE and give an error if not.
21816 */
21817 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021818valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021819{
21820 char_u *p;
21821
21822 for (p = varname; *p != NUL; ++p)
21823 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21824 && *p != AUTOLOAD_CHAR)
21825 {
21826 EMSG2(_(e_illvar), varname);
21827 return FALSE;
21828 }
21829 return TRUE;
21830}
21831
21832/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021833 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020021834 * Also give an error message, using "name" or _("name") when use_gettext is
21835 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021836 */
21837 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021838tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021839{
21840 if (lock & VAR_LOCKED)
21841 {
21842 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021843 name == NULL ? (char_u *)_("Unknown")
21844 : use_gettext ? (char_u *)_(name)
21845 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021846 return TRUE;
21847 }
21848 if (lock & VAR_FIXED)
21849 {
21850 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021851 name == NULL ? (char_u *)_("Unknown")
21852 : use_gettext ? (char_u *)_(name)
21853 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021854 return TRUE;
21855 }
21856 return FALSE;
21857}
21858
21859/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021860 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021861 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021862 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021863 * It is OK for "from" and "to" to point to the same item. This is used to
21864 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021865 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021866 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021867copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021868{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021869 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021870 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021871 switch (from->v_type)
21872 {
21873 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021874 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021875 to->vval.v_number = from->vval.v_number;
21876 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021877#ifdef FEAT_FLOAT
21878 case VAR_FLOAT:
21879 to->vval.v_float = from->vval.v_float;
21880 break;
21881#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021882 case VAR_STRING:
21883 case VAR_FUNC:
21884 if (from->vval.v_string == NULL)
21885 to->vval.v_string = NULL;
21886 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021887 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021888 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021889 if (from->v_type == VAR_FUNC)
21890 func_ref(to->vval.v_string);
21891 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021892 break;
21893 case VAR_LIST:
21894 if (from->vval.v_list == NULL)
21895 to->vval.v_list = NULL;
21896 else
21897 {
21898 to->vval.v_list = from->vval.v_list;
21899 ++to->vval.v_list->lv_refcount;
21900 }
21901 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021902 case VAR_DICT:
21903 if (from->vval.v_dict == NULL)
21904 to->vval.v_dict = NULL;
21905 else
21906 {
21907 to->vval.v_dict = from->vval.v_dict;
21908 ++to->vval.v_dict->dv_refcount;
21909 }
21910 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021911 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021912 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021913 break;
21914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021915}
21916
21917/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021918 * Make a copy of an item.
21919 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021920 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21921 * reference to an already copied list/dict can be used.
21922 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021923 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021924 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021925item_copy(
21926 typval_T *from,
21927 typval_T *to,
21928 int deep,
21929 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021930{
21931 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021932 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021933
Bram Moolenaar33570922005-01-25 22:26:29 +000021934 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021935 {
21936 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021937 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021938 }
21939 ++recurse;
21940
21941 switch (from->v_type)
21942 {
21943 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021944#ifdef FEAT_FLOAT
21945 case VAR_FLOAT:
21946#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021947 case VAR_STRING:
21948 case VAR_FUNC:
21949 copy_tv(from, to);
21950 break;
21951 case VAR_LIST:
21952 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021953 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021954 if (from->vval.v_list == NULL)
21955 to->vval.v_list = NULL;
21956 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21957 {
21958 /* use the copy made earlier */
21959 to->vval.v_list = from->vval.v_list->lv_copylist;
21960 ++to->vval.v_list->lv_refcount;
21961 }
21962 else
21963 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21964 if (to->vval.v_list == NULL)
21965 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021966 break;
21967 case VAR_DICT:
21968 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021969 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021970 if (from->vval.v_dict == NULL)
21971 to->vval.v_dict = NULL;
21972 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21973 {
21974 /* use the copy made earlier */
21975 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21976 ++to->vval.v_dict->dv_refcount;
21977 }
21978 else
21979 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21980 if (to->vval.v_dict == NULL)
21981 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021982 break;
21983 default:
21984 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021985 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021986 }
21987 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021988 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021989}
21990
21991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021992 * ":echo expr1 ..." print each argument separated with a space, add a
21993 * newline at the end.
21994 * ":echon expr1 ..." print each argument plain.
21995 */
21996 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021997ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021998{
21999 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022000 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022001 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002 char_u *p;
22003 int needclr = TRUE;
22004 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022005 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022006
22007 if (eap->skip)
22008 ++emsg_skip;
22009 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22010 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022011 /* If eval1() causes an error message the text from the command may
22012 * still need to be cleared. E.g., "echo 22,44". */
22013 need_clr_eos = needclr;
22014
Bram Moolenaar071d4272004-06-13 20:20:40 +000022015 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022016 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022017 {
22018 /*
22019 * Report the invalid expression unless the expression evaluation
22020 * has been cancelled due to an aborting error, an interrupt, or an
22021 * exception.
22022 */
22023 if (!aborting())
22024 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022025 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022026 break;
22027 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022028 need_clr_eos = FALSE;
22029
Bram Moolenaar071d4272004-06-13 20:20:40 +000022030 if (!eap->skip)
22031 {
22032 if (atstart)
22033 {
22034 atstart = FALSE;
22035 /* Call msg_start() after eval1(), evaluating the expression
22036 * may cause a message to appear. */
22037 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022038 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022039 /* Mark the saved text as finishing the line, so that what
22040 * follows is displayed on a new line when scrolling back
22041 * at the more prompt. */
22042 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022045 }
22046 else if (eap->cmdidx == CMD_echo)
22047 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022048 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022049 if (p != NULL)
22050 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022051 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022052 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022053 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022054 if (*p != TAB && needclr)
22055 {
22056 /* remove any text still there from the command */
22057 msg_clr_eos();
22058 needclr = FALSE;
22059 }
22060 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022061 }
22062 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022063 {
22064#ifdef FEAT_MBYTE
22065 if (has_mbyte)
22066 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022067 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022068
22069 (void)msg_outtrans_len_attr(p, i, echo_attr);
22070 p += i - 1;
22071 }
22072 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022073#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022074 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022076 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022077 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022078 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022079 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022080 arg = skipwhite(arg);
22081 }
22082 eap->nextcmd = check_nextcmd(arg);
22083
22084 if (eap->skip)
22085 --emsg_skip;
22086 else
22087 {
22088 /* remove text that may still be there from the command */
22089 if (needclr)
22090 msg_clr_eos();
22091 if (eap->cmdidx == CMD_echo)
22092 msg_end();
22093 }
22094}
22095
22096/*
22097 * ":echohl {name}".
22098 */
22099 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022100ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022101{
22102 int id;
22103
22104 id = syn_name2id(eap->arg);
22105 if (id == 0)
22106 echo_attr = 0;
22107 else
22108 echo_attr = syn_id2attr(id);
22109}
22110
22111/*
22112 * ":execute expr1 ..." execute the result of an expression.
22113 * ":echomsg expr1 ..." Print a message
22114 * ":echoerr expr1 ..." Print an error
22115 * Each gets spaces around each argument and a newline at the end for
22116 * echo commands
22117 */
22118 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022119ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120{
22121 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022122 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022123 int ret = OK;
22124 char_u *p;
22125 garray_T ga;
22126 int len;
22127 int save_did_emsg;
22128
22129 ga_init2(&ga, 1, 80);
22130
22131 if (eap->skip)
22132 ++emsg_skip;
22133 while (*arg != NUL && *arg != '|' && *arg != '\n')
22134 {
22135 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022136 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022137 {
22138 /*
22139 * Report the invalid expression unless the expression evaluation
22140 * has been cancelled due to an aborting error, an interrupt, or an
22141 * exception.
22142 */
22143 if (!aborting())
22144 EMSG2(_(e_invexpr2), p);
22145 ret = FAIL;
22146 break;
22147 }
22148
22149 if (!eap->skip)
22150 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022151 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022152 len = (int)STRLEN(p);
22153 if (ga_grow(&ga, len + 2) == FAIL)
22154 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022155 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022156 ret = FAIL;
22157 break;
22158 }
22159 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022160 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022161 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022162 ga.ga_len += len;
22163 }
22164
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022165 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022166 arg = skipwhite(arg);
22167 }
22168
22169 if (ret != FAIL && ga.ga_data != NULL)
22170 {
22171 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022172 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022173 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022174 out_flush();
22175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022176 else if (eap->cmdidx == CMD_echoerr)
22177 {
22178 /* We don't want to abort following commands, restore did_emsg. */
22179 save_did_emsg = did_emsg;
22180 EMSG((char_u *)ga.ga_data);
22181 if (!force_abort)
22182 did_emsg = save_did_emsg;
22183 }
22184 else if (eap->cmdidx == CMD_execute)
22185 do_cmdline((char_u *)ga.ga_data,
22186 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22187 }
22188
22189 ga_clear(&ga);
22190
22191 if (eap->skip)
22192 --emsg_skip;
22193
22194 eap->nextcmd = check_nextcmd(arg);
22195}
22196
22197/*
22198 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22199 * "arg" points to the "&" or '+' when called, to "option" when returning.
22200 * Returns NULL when no option name found. Otherwise pointer to the char
22201 * after the option name.
22202 */
22203 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022204find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022205{
22206 char_u *p = *arg;
22207
22208 ++p;
22209 if (*p == 'g' && p[1] == ':')
22210 {
22211 *opt_flags = OPT_GLOBAL;
22212 p += 2;
22213 }
22214 else if (*p == 'l' && p[1] == ':')
22215 {
22216 *opt_flags = OPT_LOCAL;
22217 p += 2;
22218 }
22219 else
22220 *opt_flags = 0;
22221
22222 if (!ASCII_ISALPHA(*p))
22223 return NULL;
22224 *arg = p;
22225
22226 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22227 p += 4; /* termcap option */
22228 else
22229 while (ASCII_ISALPHA(*p))
22230 ++p;
22231 return p;
22232}
22233
22234/*
22235 * ":function"
22236 */
22237 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022238ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022239{
22240 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022241 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022242 int j;
22243 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022244 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022245 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022246 char_u *name = NULL;
22247 char_u *p;
22248 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022249 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022250 garray_T newargs;
22251 garray_T newlines;
22252 int varargs = FALSE;
22253 int mustend = FALSE;
22254 int flags = 0;
22255 ufunc_T *fp;
22256 int indent;
22257 int nesting;
22258 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022259 dictitem_T *v;
22260 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022261 static int func_nr = 0; /* number for nameless function */
22262 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022263 hashtab_T *ht;
22264 int todo;
22265 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022266 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022267
22268 /*
22269 * ":function" without argument: list functions.
22270 */
22271 if (ends_excmd(*eap->arg))
22272 {
22273 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022274 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022275 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022276 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022277 {
22278 if (!HASHITEM_EMPTY(hi))
22279 {
22280 --todo;
22281 fp = HI2UF(hi);
22282 if (!isdigit(*fp->uf_name))
22283 list_func_head(fp, FALSE);
22284 }
22285 }
22286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022287 eap->nextcmd = check_nextcmd(eap->arg);
22288 return;
22289 }
22290
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022291 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022292 * ":function /pat": list functions matching pattern.
22293 */
22294 if (*eap->arg == '/')
22295 {
22296 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22297 if (!eap->skip)
22298 {
22299 regmatch_T regmatch;
22300
22301 c = *p;
22302 *p = NUL;
22303 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22304 *p = c;
22305 if (regmatch.regprog != NULL)
22306 {
22307 regmatch.rm_ic = p_ic;
22308
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022309 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022310 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22311 {
22312 if (!HASHITEM_EMPTY(hi))
22313 {
22314 --todo;
22315 fp = HI2UF(hi);
22316 if (!isdigit(*fp->uf_name)
22317 && vim_regexec(&regmatch, fp->uf_name, 0))
22318 list_func_head(fp, FALSE);
22319 }
22320 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022321 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022322 }
22323 }
22324 if (*p == '/')
22325 ++p;
22326 eap->nextcmd = check_nextcmd(p);
22327 return;
22328 }
22329
22330 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022331 * Get the function name. There are these situations:
22332 * func normal function name
22333 * "name" == func, "fudi.fd_dict" == NULL
22334 * dict.func new dictionary entry
22335 * "name" == NULL, "fudi.fd_dict" set,
22336 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22337 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022338 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022339 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22340 * dict.func existing dict entry that's not a Funcref
22341 * "name" == NULL, "fudi.fd_dict" set,
22342 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022343 * s:func script-local function name
22344 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022346 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022347 name = trans_function_name(&p, eap->skip, 0, &fudi);
22348 paren = (vim_strchr(p, '(') != NULL);
22349 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022350 {
22351 /*
22352 * Return on an invalid expression in braces, unless the expression
22353 * evaluation has been cancelled due to an aborting error, an
22354 * interrupt, or an exception.
22355 */
22356 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022357 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022358 if (!eap->skip && fudi.fd_newkey != NULL)
22359 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022360 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022361 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022363 else
22364 eap->skip = TRUE;
22365 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022366
Bram Moolenaar071d4272004-06-13 20:20:40 +000022367 /* An error in a function call during evaluation of an expression in magic
22368 * braces should not cause the function not to be defined. */
22369 saved_did_emsg = did_emsg;
22370 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022371
22372 /*
22373 * ":function func" with only function name: list function.
22374 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022375 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022376 {
22377 if (!ends_excmd(*skipwhite(p)))
22378 {
22379 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022380 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022381 }
22382 eap->nextcmd = check_nextcmd(p);
22383 if (eap->nextcmd != NULL)
22384 *p = NUL;
22385 if (!eap->skip && !got_int)
22386 {
22387 fp = find_func(name);
22388 if (fp != NULL)
22389 {
22390 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022391 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022392 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022393 if (FUNCLINE(fp, j) == NULL)
22394 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022395 msg_putchar('\n');
22396 msg_outnum((long)(j + 1));
22397 if (j < 9)
22398 msg_putchar(' ');
22399 if (j < 99)
22400 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022401 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022402 out_flush(); /* show a line at a time */
22403 ui_breakcheck();
22404 }
22405 if (!got_int)
22406 {
22407 msg_putchar('\n');
22408 msg_puts((char_u *)" endfunction");
22409 }
22410 }
22411 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022412 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022413 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022414 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022415 }
22416
22417 /*
22418 * ":function name(arg1, arg2)" Define function.
22419 */
22420 p = skipwhite(p);
22421 if (*p != '(')
22422 {
22423 if (!eap->skip)
22424 {
22425 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022426 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022427 }
22428 /* attempt to continue by skipping some text */
22429 if (vim_strchr(p, '(') != NULL)
22430 p = vim_strchr(p, '(');
22431 }
22432 p = skipwhite(p + 1);
22433
22434 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22435 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22436
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022437 if (!eap->skip)
22438 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022439 /* Check the name of the function. Unless it's a dictionary function
22440 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022441 if (name != NULL)
22442 arg = name;
22443 else
22444 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022445 if (arg != NULL && (fudi.fd_di == NULL
22446 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022447 {
22448 if (*arg == K_SPECIAL)
22449 j = 3;
22450 else
22451 j = 0;
22452 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22453 : eval_isnamec(arg[j])))
22454 ++j;
22455 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022456 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022457 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022458 /* Disallow using the g: dict. */
22459 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22460 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022461 }
22462
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463 /*
22464 * Isolate the arguments: "arg1, arg2, ...)"
22465 */
22466 while (*p != ')')
22467 {
22468 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22469 {
22470 varargs = TRUE;
22471 p += 3;
22472 mustend = TRUE;
22473 }
22474 else
22475 {
22476 arg = p;
22477 while (ASCII_ISALNUM(*p) || *p == '_')
22478 ++p;
22479 if (arg == p || isdigit(*arg)
22480 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22481 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22482 {
22483 if (!eap->skip)
22484 EMSG2(_("E125: Illegal argument: %s"), arg);
22485 break;
22486 }
22487 if (ga_grow(&newargs, 1) == FAIL)
22488 goto erret;
22489 c = *p;
22490 *p = NUL;
22491 arg = vim_strsave(arg);
22492 if (arg == NULL)
22493 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022494
22495 /* Check for duplicate argument name. */
22496 for (i = 0; i < newargs.ga_len; ++i)
22497 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22498 {
22499 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022500 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022501 goto erret;
22502 }
22503
Bram Moolenaar071d4272004-06-13 20:20:40 +000022504 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22505 *p = c;
22506 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022507 if (*p == ',')
22508 ++p;
22509 else
22510 mustend = TRUE;
22511 }
22512 p = skipwhite(p);
22513 if (mustend && *p != ')')
22514 {
22515 if (!eap->skip)
22516 EMSG2(_(e_invarg2), eap->arg);
22517 break;
22518 }
22519 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022520 if (*p != ')')
22521 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022522 ++p; /* skip the ')' */
22523
Bram Moolenaare9a41262005-01-15 22:18:47 +000022524 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022525 for (;;)
22526 {
22527 p = skipwhite(p);
22528 if (STRNCMP(p, "range", 5) == 0)
22529 {
22530 flags |= FC_RANGE;
22531 p += 5;
22532 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022533 else if (STRNCMP(p, "dict", 4) == 0)
22534 {
22535 flags |= FC_DICT;
22536 p += 4;
22537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022538 else if (STRNCMP(p, "abort", 5) == 0)
22539 {
22540 flags |= FC_ABORT;
22541 p += 5;
22542 }
22543 else
22544 break;
22545 }
22546
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022547 /* When there is a line break use what follows for the function body.
22548 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22549 if (*p == '\n')
22550 line_arg = p + 1;
22551 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022552 EMSG(_(e_trailing));
22553
22554 /*
22555 * Read the body of the function, until ":endfunction" is found.
22556 */
22557 if (KeyTyped)
22558 {
22559 /* Check if the function already exists, don't let the user type the
22560 * whole function before telling him it doesn't work! For a script we
22561 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022562 if (!eap->skip && !eap->forceit)
22563 {
22564 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22565 EMSG(_(e_funcdict));
22566 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022567 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022569
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022570 if (!eap->skip && did_emsg)
22571 goto erret;
22572
Bram Moolenaar071d4272004-06-13 20:20:40 +000022573 msg_putchar('\n'); /* don't overwrite the function name */
22574 cmdline_row = msg_row;
22575 }
22576
22577 indent = 2;
22578 nesting = 0;
22579 for (;;)
22580 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022581 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022582 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022583 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022584 saved_wait_return = FALSE;
22585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022586 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022587 sourcing_lnum_off = sourcing_lnum;
22588
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022589 if (line_arg != NULL)
22590 {
22591 /* Use eap->arg, split up in parts by line breaks. */
22592 theline = line_arg;
22593 p = vim_strchr(theline, '\n');
22594 if (p == NULL)
22595 line_arg += STRLEN(line_arg);
22596 else
22597 {
22598 *p = NUL;
22599 line_arg = p + 1;
22600 }
22601 }
22602 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022603 theline = getcmdline(':', 0L, indent);
22604 else
22605 theline = eap->getline(':', eap->cookie, indent);
22606 if (KeyTyped)
22607 lines_left = Rows - 1;
22608 if (theline == NULL)
22609 {
22610 EMSG(_("E126: Missing :endfunction"));
22611 goto erret;
22612 }
22613
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022614 /* Detect line continuation: sourcing_lnum increased more than one. */
22615 if (sourcing_lnum > sourcing_lnum_off + 1)
22616 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22617 else
22618 sourcing_lnum_off = 0;
22619
Bram Moolenaar071d4272004-06-13 20:20:40 +000022620 if (skip_until != NULL)
22621 {
22622 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22623 * don't check for ":endfunc". */
22624 if (STRCMP(theline, skip_until) == 0)
22625 {
22626 vim_free(skip_until);
22627 skip_until = NULL;
22628 }
22629 }
22630 else
22631 {
22632 /* skip ':' and blanks*/
22633 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22634 ;
22635
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022636 /* Check for "endfunction". */
22637 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022638 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022639 if (line_arg == NULL)
22640 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022641 break;
22642 }
22643
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022644 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645 * at "end". */
22646 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22647 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022648 else if (STRNCMP(p, "if", 2) == 0
22649 || STRNCMP(p, "wh", 2) == 0
22650 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022651 || STRNCMP(p, "try", 3) == 0)
22652 indent += 2;
22653
22654 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022655 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022656 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022657 if (*p == '!')
22658 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022659 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022660 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22661 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022662 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022663 ++nesting;
22664 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665 }
22666 }
22667
22668 /* Check for ":append" or ":insert". */
22669 p = skip_range(p, NULL);
22670 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22671 || (p[0] == 'i'
22672 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22673 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22674 skip_until = vim_strsave((char_u *)".");
22675
22676 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22677 arg = skipwhite(skiptowhite(p));
22678 if (arg[0] == '<' && arg[1] =='<'
22679 && ((p[0] == 'p' && p[1] == 'y'
22680 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22681 || (p[0] == 'p' && p[1] == 'e'
22682 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22683 || (p[0] == 't' && p[1] == 'c'
22684 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022685 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22686 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022687 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22688 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022689 || (p[0] == 'm' && p[1] == 'z'
22690 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022691 ))
22692 {
22693 /* ":python <<" continues until a dot, like ":append" */
22694 p = skipwhite(arg + 2);
22695 if (*p == NUL)
22696 skip_until = vim_strsave((char_u *)".");
22697 else
22698 skip_until = vim_strsave(p);
22699 }
22700 }
22701
22702 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022703 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022704 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022705 if (line_arg == NULL)
22706 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022707 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022708 }
22709
22710 /* Copy the line to newly allocated memory. get_one_sourceline()
22711 * allocates 250 bytes per line, this saves 80% on average. The cost
22712 * is an extra alloc/free. */
22713 p = vim_strsave(theline);
22714 if (p != NULL)
22715 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022716 if (line_arg == NULL)
22717 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022718 theline = p;
22719 }
22720
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022721 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22722
22723 /* Add NULL lines for continuation lines, so that the line count is
22724 * equal to the index in the growarray. */
22725 while (sourcing_lnum_off-- > 0)
22726 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022727
22728 /* Check for end of eap->arg. */
22729 if (line_arg != NULL && *line_arg == NUL)
22730 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022731 }
22732
22733 /* Don't define the function when skipping commands or when an error was
22734 * detected. */
22735 if (eap->skip || did_emsg)
22736 goto erret;
22737
22738 /*
22739 * If there are no errors, add the function
22740 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022741 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022742 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022743 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022744 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022745 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022746 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022747 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022748 goto erret;
22749 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022750
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022751 fp = find_func(name);
22752 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022753 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022754 if (!eap->forceit)
22755 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022756 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022757 goto erret;
22758 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022759 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022760 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022761 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022762 name);
22763 goto erret;
22764 }
22765 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022766 ga_clear_strings(&(fp->uf_args));
22767 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022768 vim_free(name);
22769 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022771 }
22772 else
22773 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022774 char numbuf[20];
22775
22776 fp = NULL;
22777 if (fudi.fd_newkey == NULL && !eap->forceit)
22778 {
22779 EMSG(_(e_funcdict));
22780 goto erret;
22781 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022782 if (fudi.fd_di == NULL)
22783 {
22784 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022785 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022786 goto erret;
22787 }
22788 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022789 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022790 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022791
22792 /* Give the function a sequential number. Can only be used with a
22793 * Funcref! */
22794 vim_free(name);
22795 sprintf(numbuf, "%d", ++func_nr);
22796 name = vim_strsave((char_u *)numbuf);
22797 if (name == NULL)
22798 goto erret;
22799 }
22800
22801 if (fp == NULL)
22802 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022803 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022804 {
22805 int slen, plen;
22806 char_u *scriptname;
22807
22808 /* Check that the autoload name matches the script name. */
22809 j = FAIL;
22810 if (sourcing_name != NULL)
22811 {
22812 scriptname = autoload_name(name);
22813 if (scriptname != NULL)
22814 {
22815 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022816 plen = (int)STRLEN(p);
22817 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022818 if (slen > plen && fnamecmp(p,
22819 sourcing_name + slen - plen) == 0)
22820 j = OK;
22821 vim_free(scriptname);
22822 }
22823 }
22824 if (j == FAIL)
22825 {
22826 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22827 goto erret;
22828 }
22829 }
22830
22831 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022832 if (fp == NULL)
22833 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022834
22835 if (fudi.fd_dict != NULL)
22836 {
22837 if (fudi.fd_di == NULL)
22838 {
22839 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022840 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022841 if (fudi.fd_di == NULL)
22842 {
22843 vim_free(fp);
22844 goto erret;
22845 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022846 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22847 {
22848 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022849 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022850 goto erret;
22851 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022852 }
22853 else
22854 /* overwrite existing dict entry */
22855 clear_tv(&fudi.fd_di->di_tv);
22856 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022857 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022858 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022859 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022860
22861 /* behave like "dict" was used */
22862 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022863 }
22864
Bram Moolenaar071d4272004-06-13 20:20:40 +000022865 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022866 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010022867 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
22868 {
22869 vim_free(fp);
22870 goto erret;
22871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022873 fp->uf_args = newargs;
22874 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022875#ifdef FEAT_PROFILE
22876 fp->uf_tml_count = NULL;
22877 fp->uf_tml_total = NULL;
22878 fp->uf_tml_self = NULL;
22879 fp->uf_profiling = FALSE;
22880 if (prof_def_func())
22881 func_do_profile(fp);
22882#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022883 fp->uf_varargs = varargs;
22884 fp->uf_flags = flags;
22885 fp->uf_calls = 0;
22886 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022887 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022888
22889erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022890 ga_clear_strings(&newargs);
22891 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022892ret_free:
22893 vim_free(skip_until);
22894 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022895 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022896 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022897 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022898}
22899
22900/*
22901 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022902 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022904 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010022905 * TFN_INT: internal function name OK
22906 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022907 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908 * Advances "pp" to just after the function name (if no error).
22909 */
22910 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022911trans_function_name(
22912 char_u **pp,
22913 int skip, /* only find the end, don't evaluate */
22914 int flags,
22915 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022916{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022917 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022918 char_u *start;
22919 char_u *end;
22920 int lead;
22921 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022922 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022923 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022924
22925 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022926 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022927 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022928
22929 /* Check for hard coded <SNR>: already translated function ID (from a user
22930 * command). */
22931 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22932 && (*pp)[2] == (int)KE_SNR)
22933 {
22934 *pp += 3;
22935 len = get_id_len(pp) + 3;
22936 return vim_strnsave(start, len);
22937 }
22938
22939 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22940 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022941 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022942 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022943 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022944
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022945 /* Note that TFN_ flags use the same values as GLV_ flags. */
22946 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022947 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022948 if (end == start)
22949 {
22950 if (!skip)
22951 EMSG(_("E129: Function name required"));
22952 goto theend;
22953 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022954 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022955 {
22956 /*
22957 * Report an invalid expression in braces, unless the expression
22958 * evaluation has been cancelled due to an aborting error, an
22959 * interrupt, or an exception.
22960 */
22961 if (!aborting())
22962 {
22963 if (end != NULL)
22964 EMSG2(_(e_invarg2), start);
22965 }
22966 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022967 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022968 goto theend;
22969 }
22970
22971 if (lv.ll_tv != NULL)
22972 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022973 if (fdp != NULL)
22974 {
22975 fdp->fd_dict = lv.ll_dict;
22976 fdp->fd_newkey = lv.ll_newkey;
22977 lv.ll_newkey = NULL;
22978 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022979 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022980 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22981 {
22982 name = vim_strsave(lv.ll_tv->vval.v_string);
22983 *pp = end;
22984 }
22985 else
22986 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022987 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22988 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022989 EMSG(_(e_funcref));
22990 else
22991 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022992 name = NULL;
22993 }
22994 goto theend;
22995 }
22996
22997 if (lv.ll_name == NULL)
22998 {
22999 /* Error found, but continue after the function name. */
23000 *pp = end;
23001 goto theend;
23002 }
23003
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023004 /* Check if the name is a Funcref. If so, use the value. */
23005 if (lv.ll_exp_name != NULL)
23006 {
23007 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023008 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023009 if (name == lv.ll_exp_name)
23010 name = NULL;
23011 }
23012 else
23013 {
23014 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023015 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023016 if (name == *pp)
23017 name = NULL;
23018 }
23019 if (name != NULL)
23020 {
23021 name = vim_strsave(name);
23022 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023023 if (STRNCMP(name, "<SNR>", 5) == 0)
23024 {
23025 /* Change "<SNR>" to the byte sequence. */
23026 name[0] = K_SPECIAL;
23027 name[1] = KS_EXTRA;
23028 name[2] = (int)KE_SNR;
23029 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23030 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023031 goto theend;
23032 }
23033
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023034 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023035 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023036 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023037 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23038 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23039 {
23040 /* When there was "s:" already or the name expanded to get a
23041 * leading "s:" then remove it. */
23042 lv.ll_name += 2;
23043 len -= 2;
23044 lead = 2;
23045 }
23046 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023047 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023048 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023049 /* skip over "s:" and "g:" */
23050 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023051 lv.ll_name += 2;
23052 len = (int)(end - lv.ll_name);
23053 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023054
23055 /*
23056 * Copy the function name to allocated memory.
23057 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23058 * Accept <SNR>123_name() outside a script.
23059 */
23060 if (skip)
23061 lead = 0; /* do nothing */
23062 else if (lead > 0)
23063 {
23064 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023065 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23066 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023067 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023068 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023069 if (current_SID <= 0)
23070 {
23071 EMSG(_(e_usingsid));
23072 goto theend;
23073 }
23074 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23075 lead += (int)STRLEN(sid_buf);
23076 }
23077 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023078 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023079 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023080 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023081 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023082 goto theend;
23083 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023084 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023085 {
23086 char_u *cp = vim_strchr(lv.ll_name, ':');
23087
23088 if (cp != NULL && cp < end)
23089 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023090 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023091 goto theend;
23092 }
23093 }
23094
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023095 name = alloc((unsigned)(len + lead + 1));
23096 if (name != NULL)
23097 {
23098 if (lead > 0)
23099 {
23100 name[0] = K_SPECIAL;
23101 name[1] = KS_EXTRA;
23102 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023103 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023104 STRCPY(name + 3, sid_buf);
23105 }
23106 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023107 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023108 }
23109 *pp = end;
23110
23111theend:
23112 clear_lval(&lv);
23113 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023114}
23115
23116/*
23117 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23118 * Return 2 if "p" starts with "s:".
23119 * Return 0 otherwise.
23120 */
23121 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023122eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023123{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023124 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23125 * the standard library function. */
23126 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23127 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023128 return 5;
23129 if (p[0] == 's' && p[1] == ':')
23130 return 2;
23131 return 0;
23132}
23133
23134/*
23135 * Return TRUE if "p" starts with "<SID>" or "s:".
23136 * Only works if eval_fname_script() returned non-zero for "p"!
23137 */
23138 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023139eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023140{
23141 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23142}
23143
23144/*
23145 * List the head of the function: "name(arg1, arg2)".
23146 */
23147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023148list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023149{
23150 int j;
23151
23152 msg_start();
23153 if (indent)
23154 MSG_PUTS(" ");
23155 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023156 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023157 {
23158 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023159 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023160 }
23161 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023162 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023163 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023164 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023165 {
23166 if (j)
23167 MSG_PUTS(", ");
23168 msg_puts(FUNCARG(fp, j));
23169 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023170 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171 {
23172 if (j)
23173 MSG_PUTS(", ");
23174 MSG_PUTS("...");
23175 }
23176 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023177 if (fp->uf_flags & FC_ABORT)
23178 MSG_PUTS(" abort");
23179 if (fp->uf_flags & FC_RANGE)
23180 MSG_PUTS(" range");
23181 if (fp->uf_flags & FC_DICT)
23182 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023183 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023184 if (p_verbose > 0)
23185 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023186}
23187
23188/*
23189 * Find a function by name, return pointer to it in ufuncs.
23190 * Return NULL for unknown function.
23191 */
23192 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023193find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023194{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023195 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023196
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023197 hi = hash_find(&func_hashtab, name);
23198 if (!HASHITEM_EMPTY(hi))
23199 return HI2UF(hi);
23200 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023201}
23202
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023203#if defined(EXITFREE) || defined(PROTO)
23204 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023205free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023206{
23207 hashitem_T *hi;
23208
23209 /* Need to start all over every time, because func_free() may change the
23210 * hash table. */
23211 while (func_hashtab.ht_used > 0)
23212 for (hi = func_hashtab.ht_array; ; ++hi)
23213 if (!HASHITEM_EMPTY(hi))
23214 {
23215 func_free(HI2UF(hi));
23216 break;
23217 }
23218}
23219#endif
23220
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023221 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023222translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023223{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023224 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023225 return find_internal_func(name) >= 0;
23226 return find_func(name) != NULL;
23227}
23228
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023229/*
23230 * Return TRUE if a function "name" exists.
23231 */
23232 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023233function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023234{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023235 char_u *nm = name;
23236 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023237 int n = FALSE;
23238
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023239 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23240 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023241 nm = skipwhite(nm);
23242
23243 /* Only accept "funcname", "funcname ", "funcname (..." and
23244 * "funcname(...", not "funcname!...". */
23245 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023246 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023247 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023248 return n;
23249}
23250
Bram Moolenaara1544c02013-05-30 12:35:52 +020023251 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023252get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023253{
23254 char_u *nm = name;
23255 char_u *p;
23256
23257 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23258
23259 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023260 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023261 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023262
Bram Moolenaara1544c02013-05-30 12:35:52 +020023263 vim_free(p);
23264 return NULL;
23265}
23266
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023267/*
23268 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023269 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23270 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023271 */
23272 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023273builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023274{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023275 char_u *p;
23276
23277 if (!ASCII_ISLOWER(name[0]))
23278 return FALSE;
23279 p = vim_strchr(name, AUTOLOAD_CHAR);
23280 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023281}
23282
Bram Moolenaar05159a02005-02-26 23:04:13 +000023283#if defined(FEAT_PROFILE) || defined(PROTO)
23284/*
23285 * Start profiling function "fp".
23286 */
23287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023288func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023289{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023290 int len = fp->uf_lines.ga_len;
23291
23292 if (len == 0)
23293 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023294 fp->uf_tm_count = 0;
23295 profile_zero(&fp->uf_tm_self);
23296 profile_zero(&fp->uf_tm_total);
23297 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023298 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023299 if (fp->uf_tml_total == NULL)
23300 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023301 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023302 if (fp->uf_tml_self == NULL)
23303 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023304 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023305 fp->uf_tml_idx = -1;
23306 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23307 || fp->uf_tml_self == NULL)
23308 return; /* out of memory */
23309
23310 fp->uf_profiling = TRUE;
23311}
23312
23313/*
23314 * Dump the profiling results for all functions in file "fd".
23315 */
23316 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023317func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023318{
23319 hashitem_T *hi;
23320 int todo;
23321 ufunc_T *fp;
23322 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023323 ufunc_T **sorttab;
23324 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023325
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023326 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023327 if (todo == 0)
23328 return; /* nothing to dump */
23329
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023330 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023331
Bram Moolenaar05159a02005-02-26 23:04:13 +000023332 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23333 {
23334 if (!HASHITEM_EMPTY(hi))
23335 {
23336 --todo;
23337 fp = HI2UF(hi);
23338 if (fp->uf_profiling)
23339 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023340 if (sorttab != NULL)
23341 sorttab[st_len++] = fp;
23342
Bram Moolenaar05159a02005-02-26 23:04:13 +000023343 if (fp->uf_name[0] == K_SPECIAL)
23344 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23345 else
23346 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23347 if (fp->uf_tm_count == 1)
23348 fprintf(fd, "Called 1 time\n");
23349 else
23350 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23351 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23352 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23353 fprintf(fd, "\n");
23354 fprintf(fd, "count total (s) self (s)\n");
23355
23356 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23357 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023358 if (FUNCLINE(fp, i) == NULL)
23359 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023360 prof_func_line(fd, fp->uf_tml_count[i],
23361 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023362 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23363 }
23364 fprintf(fd, "\n");
23365 }
23366 }
23367 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023368
23369 if (sorttab != NULL && st_len > 0)
23370 {
23371 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23372 prof_total_cmp);
23373 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23374 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23375 prof_self_cmp);
23376 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23377 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023378
23379 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023380}
Bram Moolenaar73830342005-02-28 22:48:19 +000023381
23382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023383prof_sort_list(
23384 FILE *fd,
23385 ufunc_T **sorttab,
23386 int st_len,
23387 char *title,
23388 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023389{
23390 int i;
23391 ufunc_T *fp;
23392
23393 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23394 fprintf(fd, "count total (s) self (s) function\n");
23395 for (i = 0; i < 20 && i < st_len; ++i)
23396 {
23397 fp = sorttab[i];
23398 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23399 prefer_self);
23400 if (fp->uf_name[0] == K_SPECIAL)
23401 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23402 else
23403 fprintf(fd, " %s()\n", fp->uf_name);
23404 }
23405 fprintf(fd, "\n");
23406}
23407
23408/*
23409 * Print the count and times for one function or function line.
23410 */
23411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023412prof_func_line(
23413 FILE *fd,
23414 int count,
23415 proftime_T *total,
23416 proftime_T *self,
23417 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023418{
23419 if (count > 0)
23420 {
23421 fprintf(fd, "%5d ", count);
23422 if (prefer_self && profile_equal(total, self))
23423 fprintf(fd, " ");
23424 else
23425 fprintf(fd, "%s ", profile_msg(total));
23426 if (!prefer_self && profile_equal(total, self))
23427 fprintf(fd, " ");
23428 else
23429 fprintf(fd, "%s ", profile_msg(self));
23430 }
23431 else
23432 fprintf(fd, " ");
23433}
23434
23435/*
23436 * Compare function for total time sorting.
23437 */
23438 static int
23439#ifdef __BORLANDC__
23440_RTLENTRYF
23441#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023442prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023443{
23444 ufunc_T *p1, *p2;
23445
23446 p1 = *(ufunc_T **)s1;
23447 p2 = *(ufunc_T **)s2;
23448 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23449}
23450
23451/*
23452 * Compare function for self time sorting.
23453 */
23454 static int
23455#ifdef __BORLANDC__
23456_RTLENTRYF
23457#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023458prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023459{
23460 ufunc_T *p1, *p2;
23461
23462 p1 = *(ufunc_T **)s1;
23463 p2 = *(ufunc_T **)s2;
23464 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23465}
23466
Bram Moolenaar05159a02005-02-26 23:04:13 +000023467#endif
23468
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023469/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023470 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023471 * Return TRUE if a package was loaded.
23472 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023473 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023474script_autoload(
23475 char_u *name,
23476 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023477{
23478 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023479 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023480 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023481 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023482
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023483 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023484 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023485 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023486 return FALSE;
23487
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023488 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023489
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023490 /* Find the name in the list of previously loaded package names. Skip
23491 * "autoload/", it's always the same. */
23492 for (i = 0; i < ga_loaded.ga_len; ++i)
23493 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23494 break;
23495 if (!reload && i < ga_loaded.ga_len)
23496 ret = FALSE; /* was loaded already */
23497 else
23498 {
23499 /* Remember the name if it wasn't loaded already. */
23500 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23501 {
23502 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23503 tofree = NULL;
23504 }
23505
23506 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023507 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023508 ret = TRUE;
23509 }
23510
23511 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023512 return ret;
23513}
23514
23515/*
23516 * Return the autoload script name for a function or variable name.
23517 * Returns NULL when out of memory.
23518 */
23519 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023520autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023521{
23522 char_u *p;
23523 char_u *scriptname;
23524
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023525 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023526 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23527 if (scriptname == NULL)
23528 return FALSE;
23529 STRCPY(scriptname, "autoload/");
23530 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023531 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023532 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023533 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023534 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023535 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023536}
23537
Bram Moolenaar071d4272004-06-13 20:20:40 +000023538#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23539
23540/*
23541 * Function given to ExpandGeneric() to obtain the list of user defined
23542 * function names.
23543 */
23544 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023545get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023546{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023547 static long_u done;
23548 static hashitem_T *hi;
23549 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023550
23551 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023552 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023553 done = 0;
23554 hi = func_hashtab.ht_array;
23555 }
23556 if (done < func_hashtab.ht_used)
23557 {
23558 if (done++ > 0)
23559 ++hi;
23560 while (HASHITEM_EMPTY(hi))
23561 ++hi;
23562 fp = HI2UF(hi);
23563
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023564 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023565 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023566
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023567 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23568 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023569
23570 cat_func_name(IObuff, fp);
23571 if (xp->xp_context != EXPAND_USER_FUNC)
23572 {
23573 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023574 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023575 STRCAT(IObuff, ")");
23576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023577 return IObuff;
23578 }
23579 return NULL;
23580}
23581
23582#endif /* FEAT_CMDL_COMPL */
23583
23584/*
23585 * Copy the function name of "fp" to buffer "buf".
23586 * "buf" must be able to hold the function name plus three bytes.
23587 * Takes care of script-local function names.
23588 */
23589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023590cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023591{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023592 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023593 {
23594 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023595 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023596 }
23597 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023598 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023599}
23600
23601/*
23602 * ":delfunction {name}"
23603 */
23604 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023605ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023606{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023607 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023608 char_u *p;
23609 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023610 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023611
23612 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023613 name = trans_function_name(&p, eap->skip, 0, &fudi);
23614 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023615 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023616 {
23617 if (fudi.fd_dict != NULL && !eap->skip)
23618 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023619 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023621 if (!ends_excmd(*skipwhite(p)))
23622 {
23623 vim_free(name);
23624 EMSG(_(e_trailing));
23625 return;
23626 }
23627 eap->nextcmd = check_nextcmd(p);
23628 if (eap->nextcmd != NULL)
23629 *p = NUL;
23630
23631 if (!eap->skip)
23632 fp = find_func(name);
23633 vim_free(name);
23634
23635 if (!eap->skip)
23636 {
23637 if (fp == NULL)
23638 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023639 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023640 return;
23641 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023642 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023643 {
23644 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23645 return;
23646 }
23647
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023648 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023649 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023650 /* Delete the dict item that refers to the function, it will
23651 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023652 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023653 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023654 else
23655 func_free(fp);
23656 }
23657}
23658
23659/*
23660 * Free a function and remove it from the list of functions.
23661 */
23662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023663func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023664{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023665 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023666
23667 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023668 ga_clear_strings(&(fp->uf_args));
23669 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023670#ifdef FEAT_PROFILE
23671 vim_free(fp->uf_tml_count);
23672 vim_free(fp->uf_tml_total);
23673 vim_free(fp->uf_tml_self);
23674#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023675
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023676 /* remove the function from the function hashtable */
23677 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23678 if (HASHITEM_EMPTY(hi))
23679 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023680 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023681 hash_remove(&func_hashtab, hi);
23682
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023683 vim_free(fp);
23684}
23685
23686/*
23687 * Unreference a Function: decrement the reference count and free it when it
23688 * becomes zero. Only for numbered functions.
23689 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023690 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023691func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023692{
23693 ufunc_T *fp;
23694
23695 if (name != NULL && isdigit(*name))
23696 {
23697 fp = find_func(name);
23698 if (fp == NULL)
23699 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023700 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023701 {
23702 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023703 * when "uf_calls" becomes zero. */
23704 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023705 func_free(fp);
23706 }
23707 }
23708}
23709
23710/*
23711 * Count a reference to a Function.
23712 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023713 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023714func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023715{
23716 ufunc_T *fp;
23717
23718 if (name != NULL && isdigit(*name))
23719 {
23720 fp = find_func(name);
23721 if (fp == NULL)
23722 EMSG2(_(e_intern2), "func_ref()");
23723 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023724 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023725 }
23726}
23727
23728/*
23729 * Call a user function.
23730 */
23731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023732call_user_func(
23733 ufunc_T *fp, /* pointer to function */
23734 int argcount, /* nr of args */
23735 typval_T *argvars, /* arguments */
23736 typval_T *rettv, /* return value */
23737 linenr_T firstline, /* first line of range */
23738 linenr_T lastline, /* last line of range */
23739 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023740{
Bram Moolenaar33570922005-01-25 22:26:29 +000023741 char_u *save_sourcing_name;
23742 linenr_T save_sourcing_lnum;
23743 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023744 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023745 int save_did_emsg;
23746 static int depth = 0;
23747 dictitem_T *v;
23748 int fixvar_idx = 0; /* index in fixvar[] */
23749 int i;
23750 int ai;
23751 char_u numbuf[NUMBUFLEN];
23752 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023753 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023754#ifdef FEAT_PROFILE
23755 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023756 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023758
23759 /* If depth of calling is getting too high, don't execute the function */
23760 if (depth >= p_mfd)
23761 {
23762 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023763 rettv->v_type = VAR_NUMBER;
23764 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023765 return;
23766 }
23767 ++depth;
23768
23769 line_breakcheck(); /* check for CTRL-C hit */
23770
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023771 fc = (funccall_T *)alloc(sizeof(funccall_T));
23772 fc->caller = current_funccal;
23773 current_funccal = fc;
23774 fc->func = fp;
23775 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023776 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023777 fc->linenr = 0;
23778 fc->returned = FALSE;
23779 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023780 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023781 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23782 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023783
Bram Moolenaar33570922005-01-25 22:26:29 +000023784 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023785 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023786 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23787 * each argument variable and saves a lot of time.
23788 */
23789 /*
23790 * Init l: variables.
23791 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023792 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023793 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023794 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023795 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23796 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023797 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023798 name = v->di_key;
23799 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023800 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023801 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023802 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023803 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023804 v->di_tv.vval.v_dict = selfdict;
23805 ++selfdict->dv_refcount;
23806 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023807
Bram Moolenaar33570922005-01-25 22:26:29 +000023808 /*
23809 * Init a: variables.
23810 * Set a:0 to "argcount".
23811 * Set a:000 to a list with room for the "..." arguments.
23812 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023813 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023814 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023815 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023816 /* Use "name" to avoid a warning from some compiler that checks the
23817 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023818 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023819 name = v->di_key;
23820 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023821 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023822 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023823 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023824 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023825 v->di_tv.vval.v_list = &fc->l_varlist;
23826 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23827 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23828 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023829
23830 /*
23831 * Set a:firstline to "firstline" and a:lastline to "lastline".
23832 * Set a:name to named arguments.
23833 * Set a:N to the "..." arguments.
23834 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023835 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023836 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023837 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023838 (varnumber_T)lastline);
23839 for (i = 0; i < argcount; ++i)
23840 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023841 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023842 if (ai < 0)
23843 /* named argument a:name */
23844 name = FUNCARG(fp, i);
23845 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023846 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023847 /* "..." argument a:1, a:2, etc. */
23848 sprintf((char *)numbuf, "%d", ai + 1);
23849 name = numbuf;
23850 }
23851 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23852 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023853 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023854 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23855 }
23856 else
23857 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023858 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23859 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023860 if (v == NULL)
23861 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023862 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000023863 }
23864 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023865 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023866
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023867 /* Note: the values are copied directly to avoid alloc/free.
23868 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023869 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023870 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023871
23872 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23873 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023874 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23875 fc->l_listitems[ai].li_tv = argvars[i];
23876 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023877 }
23878 }
23879
Bram Moolenaar071d4272004-06-13 20:20:40 +000023880 /* Don't redraw while executing the function. */
23881 ++RedrawingDisabled;
23882 save_sourcing_name = sourcing_name;
23883 save_sourcing_lnum = sourcing_lnum;
23884 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023885 /* need space for function name + ("function " + 3) or "[number]" */
23886 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
23887 + STRLEN(fp->uf_name) + 20;
23888 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023889 if (sourcing_name != NULL)
23890 {
23891 if (save_sourcing_name != NULL
23892 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023893 sprintf((char *)sourcing_name, "%s[%d]..",
23894 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023895 else
23896 STRCPY(sourcing_name, "function ");
23897 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23898
23899 if (p_verbose >= 12)
23900 {
23901 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023902 verbose_enter_scroll();
23903
Bram Moolenaar555b2802005-05-19 21:08:39 +000023904 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023905 if (p_verbose >= 14)
23906 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023907 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023908 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023909 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023910 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023911
23912 msg_puts((char_u *)"(");
23913 for (i = 0; i < argcount; ++i)
23914 {
23915 if (i > 0)
23916 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023917 if (argvars[i].v_type == VAR_NUMBER)
23918 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023919 else
23920 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023921 /* Do not want errors such as E724 here. */
23922 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023923 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023924 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023925 if (s != NULL)
23926 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023927 if (vim_strsize(s) > MSG_BUF_CLEN)
23928 {
23929 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23930 s = buf;
23931 }
23932 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023933 vim_free(tofree);
23934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023935 }
23936 }
23937 msg_puts((char_u *)")");
23938 }
23939 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023940
23941 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023942 --no_wait_return;
23943 }
23944 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023945#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023946 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023947 {
23948 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23949 func_do_profile(fp);
23950 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023951 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023952 {
23953 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023954 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023955 profile_zero(&fp->uf_tm_children);
23956 }
23957 script_prof_save(&wait_start);
23958 }
23959#endif
23960
Bram Moolenaar071d4272004-06-13 20:20:40 +000023961 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023962 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023963 save_did_emsg = did_emsg;
23964 did_emsg = FALSE;
23965
23966 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023967 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023968 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23969
23970 --RedrawingDisabled;
23971
23972 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023973 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023974 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023975 clear_tv(rettv);
23976 rettv->v_type = VAR_NUMBER;
23977 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023978 }
23979
Bram Moolenaar05159a02005-02-26 23:04:13 +000023980#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023981 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023982 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023983 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023984 profile_end(&call_start);
23985 profile_sub_wait(&wait_start, &call_start);
23986 profile_add(&fp->uf_tm_total, &call_start);
23987 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023988 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023989 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023990 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23991 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023992 }
23993 }
23994#endif
23995
Bram Moolenaar071d4272004-06-13 20:20:40 +000023996 /* when being verbose, mention the return value */
23997 if (p_verbose >= 12)
23998 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023999 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024000 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024001
Bram Moolenaar071d4272004-06-13 20:20:40 +000024002 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024003 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024004 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024005 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024006 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024007 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024009 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024010 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024011 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024012 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024013
Bram Moolenaar555b2802005-05-19 21:08:39 +000024014 /* The value may be very long. Skip the middle part, so that we
24015 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024016 * truncate it at the end. Don't want errors such as E724 here. */
24017 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024018 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024019 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024020 if (s != NULL)
24021 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024022 if (vim_strsize(s) > MSG_BUF_CLEN)
24023 {
24024 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24025 s = buf;
24026 }
24027 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024028 vim_free(tofree);
24029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024030 }
24031 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024032
24033 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024034 --no_wait_return;
24035 }
24036
24037 vim_free(sourcing_name);
24038 sourcing_name = save_sourcing_name;
24039 sourcing_lnum = save_sourcing_lnum;
24040 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024041#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024042 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024043 script_prof_restore(&wait_start);
24044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024045
24046 if (p_verbose >= 12 && sourcing_name != NULL)
24047 {
24048 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024049 verbose_enter_scroll();
24050
Bram Moolenaar555b2802005-05-19 21:08:39 +000024051 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024052 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024053
24054 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024055 --no_wait_return;
24056 }
24057
24058 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024059 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024060 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024061
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024062 /* If the a:000 list and the l: and a: dicts are not referenced we can
24063 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024064 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24065 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24066 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24067 {
24068 free_funccal(fc, FALSE);
24069 }
24070 else
24071 {
24072 hashitem_T *hi;
24073 listitem_T *li;
24074 int todo;
24075
24076 /* "fc" is still in use. This can happen when returning "a:000" or
24077 * assigning "l:" to a global variable.
24078 * Link "fc" in the list for garbage collection later. */
24079 fc->caller = previous_funccal;
24080 previous_funccal = fc;
24081
24082 /* Make a copy of the a: variables, since we didn't do that above. */
24083 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24084 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24085 {
24086 if (!HASHITEM_EMPTY(hi))
24087 {
24088 --todo;
24089 v = HI2DI(hi);
24090 copy_tv(&v->di_tv, &v->di_tv);
24091 }
24092 }
24093
24094 /* Make a copy of the a:000 items, since we didn't do that above. */
24095 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24096 copy_tv(&li->li_tv, &li->li_tv);
24097 }
24098}
24099
24100/*
24101 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024102 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024103 */
24104 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024105can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024106{
24107 return (fc->l_varlist.lv_copyID != copyID
24108 && fc->l_vars.dv_copyID != copyID
24109 && fc->l_avars.dv_copyID != copyID);
24110}
24111
24112/*
24113 * Free "fc" and what it contains.
24114 */
24115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024116free_funccal(
24117 funccall_T *fc,
24118 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024119{
24120 listitem_T *li;
24121
24122 /* The a: variables typevals may not have been allocated, only free the
24123 * allocated variables. */
24124 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24125
24126 /* free all l: variables */
24127 vars_clear(&fc->l_vars.dv_hashtab);
24128
24129 /* Free the a:000 variables if they were allocated. */
24130 if (free_val)
24131 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24132 clear_tv(&li->li_tv);
24133
24134 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024135}
24136
24137/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024138 * Add a number variable "name" to dict "dp" with value "nr".
24139 */
24140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024141add_nr_var(
24142 dict_T *dp,
24143 dictitem_T *v,
24144 char *name,
24145 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024146{
24147 STRCPY(v->di_key, name);
24148 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24149 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24150 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024151 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024152 v->di_tv.vval.v_number = nr;
24153}
24154
24155/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024156 * ":return [expr]"
24157 */
24158 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024159ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024160{
24161 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024162 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024163 int returning = FALSE;
24164
24165 if (current_funccal == NULL)
24166 {
24167 EMSG(_("E133: :return not inside a function"));
24168 return;
24169 }
24170
24171 if (eap->skip)
24172 ++emsg_skip;
24173
24174 eap->nextcmd = NULL;
24175 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024176 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024177 {
24178 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024179 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024180 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024181 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024182 }
24183 /* It's safer to return also on error. */
24184 else if (!eap->skip)
24185 {
24186 /*
24187 * Return unless the expression evaluation has been cancelled due to an
24188 * aborting error, an interrupt, or an exception.
24189 */
24190 if (!aborting())
24191 returning = do_return(eap, FALSE, TRUE, NULL);
24192 }
24193
24194 /* When skipping or the return gets pending, advance to the next command
24195 * in this line (!returning). Otherwise, ignore the rest of the line.
24196 * Following lines will be ignored by get_func_line(). */
24197 if (returning)
24198 eap->nextcmd = NULL;
24199 else if (eap->nextcmd == NULL) /* no argument */
24200 eap->nextcmd = check_nextcmd(arg);
24201
24202 if (eap->skip)
24203 --emsg_skip;
24204}
24205
24206/*
24207 * Return from a function. Possibly makes the return pending. Also called
24208 * for a pending return at the ":endtry" or after returning from an extra
24209 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024210 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024211 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024212 * FALSE when the return gets pending.
24213 */
24214 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024215do_return(
24216 exarg_T *eap,
24217 int reanimate,
24218 int is_cmd,
24219 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024220{
24221 int idx;
24222 struct condstack *cstack = eap->cstack;
24223
24224 if (reanimate)
24225 /* Undo the return. */
24226 current_funccal->returned = FALSE;
24227
24228 /*
24229 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24230 * not in its finally clause (which then is to be executed next) is found.
24231 * In this case, make the ":return" pending for execution at the ":endtry".
24232 * Otherwise, return normally.
24233 */
24234 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24235 if (idx >= 0)
24236 {
24237 cstack->cs_pending[idx] = CSTP_RETURN;
24238
24239 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024240 /* A pending return again gets pending. "rettv" points to an
24241 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024242 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024243 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024244 else
24245 {
24246 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024247 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024248 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024249 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024250
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024251 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024252 {
24253 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024254 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024255 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024256 else
24257 EMSG(_(e_outofmem));
24258 }
24259 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024260 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024261
24262 if (reanimate)
24263 {
24264 /* The pending return value could be overwritten by a ":return"
24265 * without argument in a finally clause; reset the default
24266 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024267 current_funccal->rettv->v_type = VAR_NUMBER;
24268 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024269 }
24270 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024271 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024272 }
24273 else
24274 {
24275 current_funccal->returned = TRUE;
24276
24277 /* If the return is carried out now, store the return value. For
24278 * a return immediately after reanimation, the value is already
24279 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024280 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024281 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024282 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024283 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024284 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024285 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024286 }
24287 }
24288
24289 return idx < 0;
24290}
24291
24292/*
24293 * Free the variable with a pending return value.
24294 */
24295 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024296discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024297{
Bram Moolenaar33570922005-01-25 22:26:29 +000024298 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024299}
24300
24301/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024302 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024303 * is an allocated string. Used by report_pending() for verbose messages.
24304 */
24305 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024306get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024307{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024308 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024309 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024310 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024311
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024312 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024313 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024314 if (s == NULL)
24315 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024316
24317 STRCPY(IObuff, ":return ");
24318 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24319 if (STRLEN(s) + 8 >= IOSIZE)
24320 STRCPY(IObuff + IOSIZE - 4, "...");
24321 vim_free(tofree);
24322 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024323}
24324
24325/*
24326 * Get next function line.
24327 * Called by do_cmdline() to get the next line.
24328 * Returns allocated string, or NULL for end of function.
24329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024330 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024331get_func_line(
24332 int c UNUSED,
24333 void *cookie,
24334 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024335{
Bram Moolenaar33570922005-01-25 22:26:29 +000024336 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024337 ufunc_T *fp = fcp->func;
24338 char_u *retval;
24339 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024340
24341 /* If breakpoints have been added/deleted need to check for it. */
24342 if (fcp->dbg_tick != debug_tick)
24343 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024344 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024345 sourcing_lnum);
24346 fcp->dbg_tick = debug_tick;
24347 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024348#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024349 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024350 func_line_end(cookie);
24351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024352
Bram Moolenaar05159a02005-02-26 23:04:13 +000024353 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024354 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24355 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024356 retval = NULL;
24357 else
24358 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024359 /* Skip NULL lines (continuation lines). */
24360 while (fcp->linenr < gap->ga_len
24361 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24362 ++fcp->linenr;
24363 if (fcp->linenr >= gap->ga_len)
24364 retval = NULL;
24365 else
24366 {
24367 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24368 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024369#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024370 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024371 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024372#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024374 }
24375
24376 /* Did we encounter a breakpoint? */
24377 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24378 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024379 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024380 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024381 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024382 sourcing_lnum);
24383 fcp->dbg_tick = debug_tick;
24384 }
24385
24386 return retval;
24387}
24388
Bram Moolenaar05159a02005-02-26 23:04:13 +000024389#if defined(FEAT_PROFILE) || defined(PROTO)
24390/*
24391 * Called when starting to read a function line.
24392 * "sourcing_lnum" must be correct!
24393 * When skipping lines it may not actually be executed, but we won't find out
24394 * until later and we need to store the time now.
24395 */
24396 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024397func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024398{
24399 funccall_T *fcp = (funccall_T *)cookie;
24400 ufunc_T *fp = fcp->func;
24401
24402 if (fp->uf_profiling && sourcing_lnum >= 1
24403 && sourcing_lnum <= fp->uf_lines.ga_len)
24404 {
24405 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024406 /* Skip continuation lines. */
24407 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24408 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024409 fp->uf_tml_execed = FALSE;
24410 profile_start(&fp->uf_tml_start);
24411 profile_zero(&fp->uf_tml_children);
24412 profile_get_wait(&fp->uf_tml_wait);
24413 }
24414}
24415
24416/*
24417 * Called when actually executing a function line.
24418 */
24419 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024420func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024421{
24422 funccall_T *fcp = (funccall_T *)cookie;
24423 ufunc_T *fp = fcp->func;
24424
24425 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24426 fp->uf_tml_execed = TRUE;
24427}
24428
24429/*
24430 * Called when done with a function line.
24431 */
24432 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024433func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024434{
24435 funccall_T *fcp = (funccall_T *)cookie;
24436 ufunc_T *fp = fcp->func;
24437
24438 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24439 {
24440 if (fp->uf_tml_execed)
24441 {
24442 ++fp->uf_tml_count[fp->uf_tml_idx];
24443 profile_end(&fp->uf_tml_start);
24444 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024445 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024446 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24447 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024448 }
24449 fp->uf_tml_idx = -1;
24450 }
24451}
24452#endif
24453
Bram Moolenaar071d4272004-06-13 20:20:40 +000024454/*
24455 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024456 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024457 */
24458 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024459func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024460{
Bram Moolenaar33570922005-01-25 22:26:29 +000024461 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024462
24463 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24464 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024465 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024466 || fcp->returned);
24467}
24468
24469/*
24470 * return TRUE if cookie indicates a function which "abort"s on errors.
24471 */
24472 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024473func_has_abort(
24474 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024475{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024476 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024477}
24478
24479#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24480typedef enum
24481{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024482 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24483 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24484 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024485} var_flavour_T;
24486
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024487static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024488
24489 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010024490var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024491{
24492 char_u *p = varname;
24493
24494 if (ASCII_ISUPPER(*p))
24495 {
24496 while (*(++p))
24497 if (ASCII_ISLOWER(*p))
24498 return VAR_FLAVOUR_SESSION;
24499 return VAR_FLAVOUR_VIMINFO;
24500 }
24501 else
24502 return VAR_FLAVOUR_DEFAULT;
24503}
24504#endif
24505
24506#if defined(FEAT_VIMINFO) || defined(PROTO)
24507/*
24508 * Restore global vars that start with a capital from the viminfo file
24509 */
24510 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024511read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024512{
24513 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024514 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024515 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024516 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024517
24518 if (!writing && (find_viminfo_parameter('!') != NULL))
24519 {
24520 tab = vim_strchr(virp->vir_line + 1, '\t');
24521 if (tab != NULL)
24522 {
24523 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024524 switch (*tab)
24525 {
24526 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024527#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024528 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024529#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024530 case 'D': type = VAR_DICT; break;
24531 case 'L': type = VAR_LIST; break;
24532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024533
24534 tab = vim_strchr(tab, '\t');
24535 if (tab != NULL)
24536 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024537 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024538 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024539 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024540 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024541#ifdef FEAT_FLOAT
24542 else if (type == VAR_FLOAT)
24543 (void)string2float(tab + 1, &tv.vval.v_float);
24544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024545 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024546 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024547 if (type == VAR_DICT || type == VAR_LIST)
24548 {
24549 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24550
24551 if (etv == NULL)
24552 /* Failed to parse back the dict or list, use it as a
24553 * string. */
24554 tv.v_type = VAR_STRING;
24555 else
24556 {
24557 vim_free(tv.vval.v_string);
24558 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024559 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024560 }
24561 }
24562
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024563 /* when in a function use global variables */
24564 save_funccal = current_funccal;
24565 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024566 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024567 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024568
24569 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024570 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024571 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24572 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024573 }
24574 }
24575 }
24576
24577 return viminfo_readline(virp);
24578}
24579
24580/*
24581 * Write global vars that start with a capital to the viminfo file
24582 */
24583 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024584write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024585{
Bram Moolenaar33570922005-01-25 22:26:29 +000024586 hashitem_T *hi;
24587 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024588 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024589 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024590 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024591 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024592 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024593
24594 if (find_viminfo_parameter('!') == NULL)
24595 return;
24596
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024597 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024598
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024599 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024600 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024601 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024602 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024603 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024604 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024605 this_var = HI2DI(hi);
24606 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024607 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024608 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024609 {
24610 case VAR_STRING: s = "STR"; break;
24611 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024612#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024613 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024614#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024615 case VAR_DICT: s = "DIC"; break;
24616 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024617 default: continue;
24618 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024619 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024620 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024621 if (p != NULL)
24622 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024623 vim_free(tofree);
24624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024625 }
24626 }
24627}
24628#endif
24629
24630#if defined(FEAT_SESSION) || defined(PROTO)
24631 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024632store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024633{
Bram Moolenaar33570922005-01-25 22:26:29 +000024634 hashitem_T *hi;
24635 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024636 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024637 char_u *p, *t;
24638
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024639 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024640 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024641 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024642 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024643 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024644 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024645 this_var = HI2DI(hi);
24646 if ((this_var->di_tv.v_type == VAR_NUMBER
24647 || this_var->di_tv.v_type == VAR_STRING)
24648 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024649 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024650 /* Escape special characters with a backslash. Turn a LF and
24651 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024652 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024653 (char_u *)"\\\"\n\r");
24654 if (p == NULL) /* out of memory */
24655 break;
24656 for (t = p; *t != NUL; ++t)
24657 if (*t == '\n')
24658 *t = 'n';
24659 else if (*t == '\r')
24660 *t = 'r';
24661 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024662 this_var->di_key,
24663 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24664 : ' ',
24665 p,
24666 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24667 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024668 || put_eol(fd) == FAIL)
24669 {
24670 vim_free(p);
24671 return FAIL;
24672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024673 vim_free(p);
24674 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024675#ifdef FEAT_FLOAT
24676 else if (this_var->di_tv.v_type == VAR_FLOAT
24677 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24678 {
24679 float_T f = this_var->di_tv.vval.v_float;
24680 int sign = ' ';
24681
24682 if (f < 0)
24683 {
24684 f = -f;
24685 sign = '-';
24686 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024687 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024688 this_var->di_key, sign, f) < 0)
24689 || put_eol(fd) == FAIL)
24690 return FAIL;
24691 }
24692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024693 }
24694 }
24695 return OK;
24696}
24697#endif
24698
Bram Moolenaar661b1822005-07-28 22:36:45 +000024699/*
24700 * Display script name where an item was last set.
24701 * Should only be invoked when 'verbose' is non-zero.
24702 */
24703 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024704last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000024705{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024706 char_u *p;
24707
Bram Moolenaar661b1822005-07-28 22:36:45 +000024708 if (scriptID != 0)
24709 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024710 p = home_replace_save(NULL, get_scriptname(scriptID));
24711 if (p != NULL)
24712 {
24713 verbose_enter();
24714 MSG_PUTS(_("\n\tLast set from "));
24715 MSG_PUTS(p);
24716 vim_free(p);
24717 verbose_leave();
24718 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024719 }
24720}
24721
Bram Moolenaard812df62008-11-09 12:46:09 +000024722/*
24723 * List v:oldfiles in a nice way.
24724 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024725 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024726ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000024727{
24728 list_T *l = vimvars[VV_OLDFILES].vv_list;
24729 listitem_T *li;
24730 int nr = 0;
24731
24732 if (l == NULL)
24733 msg((char_u *)_("No old files"));
24734 else
24735 {
24736 msg_start();
24737 msg_scroll = TRUE;
24738 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24739 {
24740 msg_outnum((long)++nr);
24741 MSG_PUTS(": ");
24742 msg_outtrans(get_tv_string(&li->li_tv));
24743 msg_putchar('\n');
24744 out_flush(); /* output one line at a time */
24745 ui_breakcheck();
24746 }
24747 /* Assume "got_int" was set to truncate the listing. */
24748 got_int = FALSE;
24749
24750#ifdef FEAT_BROWSE_CMD
24751 if (cmdmod.browse)
24752 {
24753 quit_more = FALSE;
24754 nr = prompt_for_number(FALSE);
24755 msg_starthere();
24756 if (nr > 0)
24757 {
24758 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24759 (long)nr);
24760
24761 if (p != NULL)
24762 {
24763 p = expand_env_save(p);
24764 eap->arg = p;
24765 eap->cmdidx = CMD_edit;
24766 cmdmod.browse = FALSE;
24767 do_exedit(eap, NULL);
24768 vim_free(p);
24769 }
24770 }
24771 }
24772#endif
24773 }
24774}
24775
Bram Moolenaar53744302015-07-17 17:38:22 +020024776/* reset v:option_new, v:option_old and v:option_type */
24777 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024778reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020024779{
24780 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
24781 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
24782 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
24783}
24784
24785
Bram Moolenaar071d4272004-06-13 20:20:40 +000024786#endif /* FEAT_EVAL */
24787
Bram Moolenaar071d4272004-06-13 20:20:40 +000024788
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024789#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024790
24791#ifdef WIN3264
24792/*
24793 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24794 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024795static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
24796static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
24797static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024798
24799/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024800 * Get the short path (8.3) for the filename in "fnamep".
24801 * Only works for a valid file name.
24802 * When the path gets longer "fnamep" is changed and the allocated buffer
24803 * is put in "bufp".
24804 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24805 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024806 */
24807 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024808get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024809{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024810 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024811 char_u *newbuf;
24812
24813 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024814 l = GetShortPathName(*fnamep, *fnamep, len);
24815 if (l > len - 1)
24816 {
24817 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024818 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024819 newbuf = vim_strnsave(*fnamep, l);
24820 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024821 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024822
24823 vim_free(*bufp);
24824 *fnamep = *bufp = newbuf;
24825
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024826 /* Really should always succeed, as the buffer is big enough. */
24827 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024828 }
24829
24830 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024831 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024832}
24833
24834/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024835 * Get the short path (8.3) for the filename in "fname". The converted
24836 * path is returned in "bufp".
24837 *
24838 * Some of the directories specified in "fname" may not exist. This function
24839 * will shorten the existing directories at the beginning of the path and then
24840 * append the remaining non-existing path.
24841 *
24842 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024843 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024844 * bufp - Pointer to an allocated buffer for the filename.
24845 * fnamelen - Length of the filename pointed to by fname
24846 *
24847 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024848 */
24849 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024850shortpath_for_invalid_fname(
24851 char_u **fname,
24852 char_u **bufp,
24853 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024854{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024855 char_u *short_fname, *save_fname, *pbuf_unused;
24856 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024857 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024858 int old_len, len;
24859 int new_len, sfx_len;
24860 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024861
24862 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024863 old_len = *fnamelen;
24864 save_fname = vim_strnsave(*fname, old_len);
24865 pbuf_unused = NULL;
24866 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024867
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024868 endp = save_fname + old_len - 1; /* Find the end of the copy */
24869 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024870
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024871 /*
24872 * Try shortening the supplied path till it succeeds by removing one
24873 * directory at a time from the tail of the path.
24874 */
24875 len = 0;
24876 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024877 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024878 /* go back one path-separator */
24879 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24880 --endp;
24881 if (endp <= save_fname)
24882 break; /* processed the complete path */
24883
24884 /*
24885 * Replace the path separator with a NUL and try to shorten the
24886 * resulting path.
24887 */
24888 ch = *endp;
24889 *endp = 0;
24890 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024891 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024892 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24893 {
24894 retval = FAIL;
24895 goto theend;
24896 }
24897 *endp = ch; /* preserve the string */
24898
24899 if (len > 0)
24900 break; /* successfully shortened the path */
24901
24902 /* failed to shorten the path. Skip the path separator */
24903 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024904 }
24905
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024906 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024907 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024908 /*
24909 * Succeeded in shortening the path. Now concatenate the shortened
24910 * path with the remaining path at the tail.
24911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024912
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024913 /* Compute the length of the new path. */
24914 sfx_len = (int)(save_endp - endp) + 1;
24915 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024916
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024917 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024918 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024919 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024920 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024921 /* There is not enough space in the currently allocated string,
24922 * copy it to a buffer big enough. */
24923 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024924 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024925 {
24926 retval = FAIL;
24927 goto theend;
24928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024929 }
24930 else
24931 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024932 /* Transfer short_fname to the main buffer (it's big enough),
24933 * unless get_short_pathname() did its work in-place. */
24934 *fname = *bufp = save_fname;
24935 if (short_fname != save_fname)
24936 vim_strncpy(save_fname, short_fname, len);
24937 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024938 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024939
24940 /* concat the not-shortened part of the path */
24941 vim_strncpy(*fname + len, endp, sfx_len);
24942 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024943 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024944
24945theend:
24946 vim_free(pbuf_unused);
24947 vim_free(save_fname);
24948
24949 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024950}
24951
24952/*
24953 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024954 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024955 */
24956 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024957shortpath_for_partial(
24958 char_u **fnamep,
24959 char_u **bufp,
24960 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024961{
24962 int sepcount, len, tflen;
24963 char_u *p;
24964 char_u *pbuf, *tfname;
24965 int hasTilde;
24966
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024967 /* Count up the path separators from the RHS.. so we know which part
24968 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024969 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024970 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024971 if (vim_ispathsep(*p))
24972 ++sepcount;
24973
24974 /* Need full path first (use expand_env() to remove a "~/") */
24975 hasTilde = (**fnamep == '~');
24976 if (hasTilde)
24977 pbuf = tfname = expand_env_save(*fnamep);
24978 else
24979 pbuf = tfname = FullName_save(*fnamep, FALSE);
24980
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024981 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024982
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024983 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24984 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024985
24986 if (len == 0)
24987 {
24988 /* Don't have a valid filename, so shorten the rest of the
24989 * path if we can. This CAN give us invalid 8.3 filenames, but
24990 * there's not a lot of point in guessing what it might be.
24991 */
24992 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024993 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24994 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024995 }
24996
24997 /* Count the paths backward to find the beginning of the desired string. */
24998 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024999 {
25000#ifdef FEAT_MBYTE
25001 if (has_mbyte)
25002 p -= mb_head_off(tfname, p);
25003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025004 if (vim_ispathsep(*p))
25005 {
25006 if (sepcount == 0 || (hasTilde && sepcount == 1))
25007 break;
25008 else
25009 sepcount --;
25010 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025012 if (hasTilde)
25013 {
25014 --p;
25015 if (p >= tfname)
25016 *p = '~';
25017 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025018 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025019 }
25020 else
25021 ++p;
25022
25023 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25024 vim_free(*bufp);
25025 *fnamelen = (int)STRLEN(p);
25026 *bufp = pbuf;
25027 *fnamep = p;
25028
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025029 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025030}
25031#endif /* WIN3264 */
25032
25033/*
25034 * Adjust a filename, according to a string of modifiers.
25035 * *fnamep must be NUL terminated when called. When returning, the length is
25036 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025037 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025038 * When there is an error, *fnamep is set to NULL.
25039 */
25040 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025041modify_fname(
25042 char_u *src, /* string with modifiers */
25043 int *usedlen, /* characters after src that are used */
25044 char_u **fnamep, /* file name so far */
25045 char_u **bufp, /* buffer for allocated file name or NULL */
25046 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025047{
25048 int valid = 0;
25049 char_u *tail;
25050 char_u *s, *p, *pbuf;
25051 char_u dirname[MAXPATHL];
25052 int c;
25053 int has_fullname = 0;
25054#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025055 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025056 int has_shortname = 0;
25057#endif
25058
25059repeat:
25060 /* ":p" - full path/file_name */
25061 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25062 {
25063 has_fullname = 1;
25064
25065 valid |= VALID_PATH;
25066 *usedlen += 2;
25067
25068 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25069 if ((*fnamep)[0] == '~'
25070#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25071 && ((*fnamep)[1] == '/'
25072# ifdef BACKSLASH_IN_FILENAME
25073 || (*fnamep)[1] == '\\'
25074# endif
25075 || (*fnamep)[1] == NUL)
25076
25077#endif
25078 )
25079 {
25080 *fnamep = expand_env_save(*fnamep);
25081 vim_free(*bufp); /* free any allocated file name */
25082 *bufp = *fnamep;
25083 if (*fnamep == NULL)
25084 return -1;
25085 }
25086
25087 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025088 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025089 {
25090 if (vim_ispathsep(*p)
25091 && p[1] == '.'
25092 && (p[2] == NUL
25093 || vim_ispathsep(p[2])
25094 || (p[2] == '.'
25095 && (p[3] == NUL || vim_ispathsep(p[3])))))
25096 break;
25097 }
25098
25099 /* FullName_save() is slow, don't use it when not needed. */
25100 if (*p != NUL || !vim_isAbsName(*fnamep))
25101 {
25102 *fnamep = FullName_save(*fnamep, *p != NUL);
25103 vim_free(*bufp); /* free any allocated file name */
25104 *bufp = *fnamep;
25105 if (*fnamep == NULL)
25106 return -1;
25107 }
25108
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025109#ifdef WIN3264
25110# if _WIN32_WINNT >= 0x0500
25111 if (vim_strchr(*fnamep, '~') != NULL)
25112 {
25113 /* Expand 8.3 filename to full path. Needed to make sure the same
25114 * file does not have two different names.
25115 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25116 p = alloc(_MAX_PATH + 1);
25117 if (p != NULL)
25118 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025119 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025120 {
25121 vim_free(*bufp);
25122 *bufp = *fnamep = p;
25123 }
25124 else
25125 vim_free(p);
25126 }
25127 }
25128# endif
25129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025130 /* Append a path separator to a directory. */
25131 if (mch_isdir(*fnamep))
25132 {
25133 /* Make room for one or two extra characters. */
25134 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25135 vim_free(*bufp); /* free any allocated file name */
25136 *bufp = *fnamep;
25137 if (*fnamep == NULL)
25138 return -1;
25139 add_pathsep(*fnamep);
25140 }
25141 }
25142
25143 /* ":." - path relative to the current directory */
25144 /* ":~" - path relative to the home directory */
25145 /* ":8" - shortname path - postponed till after */
25146 while (src[*usedlen] == ':'
25147 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25148 {
25149 *usedlen += 2;
25150 if (c == '8')
25151 {
25152#ifdef WIN3264
25153 has_shortname = 1; /* Postpone this. */
25154#endif
25155 continue;
25156 }
25157 pbuf = NULL;
25158 /* Need full path first (use expand_env() to remove a "~/") */
25159 if (!has_fullname)
25160 {
25161 if (c == '.' && **fnamep == '~')
25162 p = pbuf = expand_env_save(*fnamep);
25163 else
25164 p = pbuf = FullName_save(*fnamep, FALSE);
25165 }
25166 else
25167 p = *fnamep;
25168
25169 has_fullname = 0;
25170
25171 if (p != NULL)
25172 {
25173 if (c == '.')
25174 {
25175 mch_dirname(dirname, MAXPATHL);
25176 s = shorten_fname(p, dirname);
25177 if (s != NULL)
25178 {
25179 *fnamep = s;
25180 if (pbuf != NULL)
25181 {
25182 vim_free(*bufp); /* free any allocated file name */
25183 *bufp = pbuf;
25184 pbuf = NULL;
25185 }
25186 }
25187 }
25188 else
25189 {
25190 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25191 /* Only replace it when it starts with '~' */
25192 if (*dirname == '~')
25193 {
25194 s = vim_strsave(dirname);
25195 if (s != NULL)
25196 {
25197 *fnamep = s;
25198 vim_free(*bufp);
25199 *bufp = s;
25200 }
25201 }
25202 }
25203 vim_free(pbuf);
25204 }
25205 }
25206
25207 tail = gettail(*fnamep);
25208 *fnamelen = (int)STRLEN(*fnamep);
25209
25210 /* ":h" - head, remove "/file_name", can be repeated */
25211 /* Don't remove the first "/" or "c:\" */
25212 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25213 {
25214 valid |= VALID_HEAD;
25215 *usedlen += 2;
25216 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025217 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025218 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025219 *fnamelen = (int)(tail - *fnamep);
25220#ifdef VMS
25221 if (*fnamelen > 0)
25222 *fnamelen += 1; /* the path separator is part of the path */
25223#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025224 if (*fnamelen == 0)
25225 {
25226 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25227 p = vim_strsave((char_u *)".");
25228 if (p == NULL)
25229 return -1;
25230 vim_free(*bufp);
25231 *bufp = *fnamep = tail = p;
25232 *fnamelen = 1;
25233 }
25234 else
25235 {
25236 while (tail > s && !after_pathsep(s, tail))
25237 mb_ptr_back(*fnamep, tail);
25238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025239 }
25240
25241 /* ":8" - shortname */
25242 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25243 {
25244 *usedlen += 2;
25245#ifdef WIN3264
25246 has_shortname = 1;
25247#endif
25248 }
25249
25250#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025251 /*
25252 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025253 */
25254 if (has_shortname)
25255 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025256 /* Copy the string if it is shortened by :h and when it wasn't copied
25257 * yet, because we are going to change it in place. Avoids changing
25258 * the buffer name for "%:8". */
25259 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025260 {
25261 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025262 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025263 return -1;
25264 vim_free(*bufp);
25265 *bufp = *fnamep = p;
25266 }
25267
25268 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025269 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025270 if (!has_fullname && !vim_isAbsName(*fnamep))
25271 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025272 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025273 return -1;
25274 }
25275 else
25276 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025277 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025278
Bram Moolenaardc935552011-08-17 15:23:23 +020025279 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025280 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025281 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025282 return -1;
25283
25284 if (l == 0)
25285 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025286 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025287 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025288 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025289 return -1;
25290 }
25291 *fnamelen = l;
25292 }
25293 }
25294#endif /* WIN3264 */
25295
25296 /* ":t" - tail, just the basename */
25297 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25298 {
25299 *usedlen += 2;
25300 *fnamelen -= (int)(tail - *fnamep);
25301 *fnamep = tail;
25302 }
25303
25304 /* ":e" - extension, can be repeated */
25305 /* ":r" - root, without extension, can be repeated */
25306 while (src[*usedlen] == ':'
25307 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25308 {
25309 /* find a '.' in the tail:
25310 * - for second :e: before the current fname
25311 * - otherwise: The last '.'
25312 */
25313 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25314 s = *fnamep - 2;
25315 else
25316 s = *fnamep + *fnamelen - 1;
25317 for ( ; s > tail; --s)
25318 if (s[0] == '.')
25319 break;
25320 if (src[*usedlen + 1] == 'e') /* :e */
25321 {
25322 if (s > tail)
25323 {
25324 *fnamelen += (int)(*fnamep - (s + 1));
25325 *fnamep = s + 1;
25326#ifdef VMS
25327 /* cut version from the extension */
25328 s = *fnamep + *fnamelen - 1;
25329 for ( ; s > *fnamep; --s)
25330 if (s[0] == ';')
25331 break;
25332 if (s > *fnamep)
25333 *fnamelen = s - *fnamep;
25334#endif
25335 }
25336 else if (*fnamep <= tail)
25337 *fnamelen = 0;
25338 }
25339 else /* :r */
25340 {
25341 if (s > tail) /* remove one extension */
25342 *fnamelen = (int)(s - *fnamep);
25343 }
25344 *usedlen += 2;
25345 }
25346
25347 /* ":s?pat?foo?" - substitute */
25348 /* ":gs?pat?foo?" - global substitute */
25349 if (src[*usedlen] == ':'
25350 && (src[*usedlen + 1] == 's'
25351 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25352 {
25353 char_u *str;
25354 char_u *pat;
25355 char_u *sub;
25356 int sep;
25357 char_u *flags;
25358 int didit = FALSE;
25359
25360 flags = (char_u *)"";
25361 s = src + *usedlen + 2;
25362 if (src[*usedlen + 1] == 'g')
25363 {
25364 flags = (char_u *)"g";
25365 ++s;
25366 }
25367
25368 sep = *s++;
25369 if (sep)
25370 {
25371 /* find end of pattern */
25372 p = vim_strchr(s, sep);
25373 if (p != NULL)
25374 {
25375 pat = vim_strnsave(s, (int)(p - s));
25376 if (pat != NULL)
25377 {
25378 s = p + 1;
25379 /* find end of substitution */
25380 p = vim_strchr(s, sep);
25381 if (p != NULL)
25382 {
25383 sub = vim_strnsave(s, (int)(p - s));
25384 str = vim_strnsave(*fnamep, *fnamelen);
25385 if (sub != NULL && str != NULL)
25386 {
25387 *usedlen = (int)(p + 1 - src);
25388 s = do_string_sub(str, pat, sub, flags);
25389 if (s != NULL)
25390 {
25391 *fnamep = s;
25392 *fnamelen = (int)STRLEN(s);
25393 vim_free(*bufp);
25394 *bufp = s;
25395 didit = TRUE;
25396 }
25397 }
25398 vim_free(sub);
25399 vim_free(str);
25400 }
25401 vim_free(pat);
25402 }
25403 }
25404 /* after using ":s", repeat all the modifiers */
25405 if (didit)
25406 goto repeat;
25407 }
25408 }
25409
Bram Moolenaar26df0922014-02-23 23:39:13 +010025410 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25411 {
25412 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25413 if (p == NULL)
25414 return -1;
25415 vim_free(*bufp);
25416 *bufp = *fnamep = p;
25417 *fnamelen = (int)STRLEN(p);
25418 *usedlen += 2;
25419 }
25420
Bram Moolenaar071d4272004-06-13 20:20:40 +000025421 return valid;
25422}
25423
25424/*
25425 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25426 * "flags" can be "g" to do a global substitute.
25427 * Returns an allocated string, NULL for error.
25428 */
25429 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025430do_string_sub(
25431 char_u *str,
25432 char_u *pat,
25433 char_u *sub,
25434 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025435{
25436 int sublen;
25437 regmatch_T regmatch;
25438 int i;
25439 int do_all;
25440 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025441 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025442 garray_T ga;
25443 char_u *ret;
25444 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025445 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025446
25447 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25448 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025449 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025450
25451 ga_init2(&ga, 1, 200);
25452
25453 do_all = (flags[0] == 'g');
25454
25455 regmatch.rm_ic = p_ic;
25456 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25457 if (regmatch.regprog != NULL)
25458 {
25459 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025460 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025461 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25462 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025463 /* Skip empty match except for first match. */
25464 if (regmatch.startp[0] == regmatch.endp[0])
25465 {
25466 if (zero_width == regmatch.startp[0])
25467 {
25468 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025469 i = MB_PTR2LEN(tail);
25470 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25471 (size_t)i);
25472 ga.ga_len += i;
25473 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025474 continue;
25475 }
25476 zero_width = regmatch.startp[0];
25477 }
25478
Bram Moolenaar071d4272004-06-13 20:20:40 +000025479 /*
25480 * Get some space for a temporary buffer to do the substitution
25481 * into. It will contain:
25482 * - The text up to where the match is.
25483 * - The substituted text.
25484 * - The text after the match.
25485 */
25486 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025487 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025488 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25489 {
25490 ga_clear(&ga);
25491 break;
25492 }
25493
25494 /* copy the text up to where the match is */
25495 i = (int)(regmatch.startp[0] - tail);
25496 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25497 /* add the substituted text */
25498 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25499 + ga.ga_len + i, TRUE, TRUE, FALSE);
25500 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025501 tail = regmatch.endp[0];
25502 if (*tail == NUL)
25503 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025504 if (!do_all)
25505 break;
25506 }
25507
25508 if (ga.ga_data != NULL)
25509 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25510
Bram Moolenaar473de612013-06-08 18:19:48 +020025511 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025512 }
25513
25514 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25515 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025516 if (p_cpo == empty_option)
25517 p_cpo = save_cpo;
25518 else
25519 /* Darn, evaluating {sub} expression changed the value. */
25520 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025521
25522 return ret;
25523}
25524
25525#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */