blob: b544fc349526cd0cb4c2a535c52c8dd34a778214 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000102static char *e_listreq = N_("E714: List required");
103static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000104static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000105static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
106static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
107static char *e_funcdict = N_("E717: Dictionary entry already exists");
108static char *e_funcref = N_("E718: Funcref required");
109static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
110static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000111static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000112static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200113#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200114static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000116
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100117#define NAMESPACE_CHAR (char_u *)"abglstvw"
118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
Bram Moolenaar8502c702014-06-17 12:51:16 +0200137/* Abort conversion to string after a recursion error. */
138static int did_echo_string_emsg = FALSE;
139
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100159#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
160
161/* Values for get_lval() flags argument: */
162#define GLV_QUIET TFN_QUIET /* no error messages */
163#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000215/* list heads for garbage collection */
216static dict_T *first_dict = NULL; /* list of all dicts */
217static list_T *first_list = NULL; /* list of all lists */
218
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000219/* From user function to hashitem and back. */
220static ufunc_T dumuf;
221#define UF2HIKEY(fp) ((fp)->uf_name)
222#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
223#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
224
225#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
226#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
Bram Moolenaar33570922005-01-25 22:26:29 +0000228#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
229#define VAR_SHORT_LEN 20 /* short variable name length */
230#define FIXVAR_CNT 12 /* number of fixed variables */
231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000233typedef struct funccall_S funccall_T;
234
235struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
237 ufunc_T *func; /* function being called */
238 int linenr; /* next line to be executed */
239 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000240 struct /* fixed variables for arguments */
241 {
242 dictitem_T var; /* variable (without room for name) */
243 char_u room[VAR_SHORT_LEN]; /* room for the name */
244 } fixvar[FIXVAR_CNT];
245 dict_T l_vars; /* l: local function variables */
246 dictitem_T l_vars_var; /* variable for l: scope */
247 dict_T l_avars; /* a: argument variables */
248 dictitem_T l_avars_var; /* variable for a: scope */
249 list_T l_varlist; /* list for a:000 */
250 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
251 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 linenr_T breakpoint; /* next line with breakpoint or zero */
253 int dbg_tick; /* debug_tick when breakpoint was set */
254 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000255#ifdef FEAT_PROFILE
256 proftime_T prof_child; /* time spent in a child */
257#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000258 funccall_T *caller; /* calling function or NULL */
259};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
261/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262 * Info used by a ":for" loop.
263 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000264typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000265{
266 int fi_semicolon; /* TRUE if ending in '; var]' */
267 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000268 listwatch_T fi_lw; /* keep an eye on the item used. */
269 list_T *fi_list; /* list being used */
270} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000272/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000273 * Struct used by trans_function_name()
274 */
275typedef struct
276{
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000278 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dictitem_T *fd_di; /* Dictionary item used */
280} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000281
Bram Moolenaara7043832005-01-21 11:56:39 +0000282
283/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000284 * Array to hold the value of v: variables.
285 * The value is in a dictitem, so that it can also be used in the v: scope.
286 * The reason to use this table anyway is for very quick access to the
287 * variables with the VV_ defines.
288 */
289#include "version.h"
290
291/* values for vv_flags: */
292#define VV_COMPAT 1 /* compatible, also used without "v:" */
293#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000294#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000295
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000296#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
298static struct vimvar
299{
300 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301 dictitem_T vv_di; /* value and name for key */
302 char vv_filler[16]; /* space for LONGEST name below!!! */
303 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
304} vimvars[VV_LEN] =
305{
306 /*
307 * The order here must match the VV_ defines in vim.h!
308 * Initializing a union does not work, leave tv.vval empty to get zero's.
309 */
310 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
311 {VV_NAME("count1", VAR_NUMBER), VV_RO},
312 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
313 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
314 {VV_NAME("warningmsg", VAR_STRING), 0},
315 {VV_NAME("statusmsg", VAR_STRING), 0},
316 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
318 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
319 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("termresponse", VAR_STRING), VV_RO},
321 {VV_NAME("fname", VAR_STRING), VV_RO},
322 {VV_NAME("lang", VAR_STRING), VV_RO},
323 {VV_NAME("lc_time", VAR_STRING), VV_RO},
324 {VV_NAME("ctype", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
327 {VV_NAME("fname_in", VAR_STRING), VV_RO},
328 {VV_NAME("fname_out", VAR_STRING), VV_RO},
329 {VV_NAME("fname_new", VAR_STRING), VV_RO},
330 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
331 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
332 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
335 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
336 {VV_NAME("progname", VAR_STRING), VV_RO},
337 {VV_NAME("servername", VAR_STRING), VV_RO},
338 {VV_NAME("dying", VAR_NUMBER), VV_RO},
339 {VV_NAME("exception", VAR_STRING), VV_RO},
340 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
341 {VV_NAME("register", VAR_STRING), VV_RO},
342 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
343 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000344 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
345 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000346 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000347 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
348 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000349 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000354 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000355 {VV_NAME("swapname", VAR_STRING), VV_RO},
356 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000357 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200358 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000359 {VV_NAME("mouse_win", VAR_NUMBER), 0},
360 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
361 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000362 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000363 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100364 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000365 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200366 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200367 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200368 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200369 {VV_NAME("option_new", VAR_STRING), VV_RO},
370 {VV_NAME("option_old", VAR_STRING), VV_RO},
371 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100372 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100373 {VV_NAME("false", VAR_SPECIAL), VV_RO},
374 {VV_NAME("true", VAR_SPECIAL), VV_RO},
375 {VV_NAME("null", VAR_SPECIAL), VV_RO},
376 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000377};
378
379/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000380#define vv_type vv_di.di_tv.v_type
381#define vv_nr vv_di.di_tv.vval.v_number
382#define vv_float vv_di.di_tv.vval.v_float
383#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000384#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200385#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000386#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000387
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200388static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000389#define vimvarht vimvardict.dv_hashtab
390
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100391static void prepare_vimvar(int idx, typval_T *save_tv);
392static void restore_vimvar(int idx, typval_T *save_tv);
393static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
394static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
395static char_u *skip_var_one(char_u *arg);
396static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
397static void list_glob_vars(int *first);
398static void list_buf_vars(int *first);
399static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000400#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100401static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000402#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100403static void list_vim_vars(int *first);
404static void list_script_vars(int *first);
405static void list_func_vars(int *first);
406static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
407static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
408static int check_changedtick(char_u *arg);
409static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
410static void clear_lval(lval_T *lp);
411static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
412static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
413static void list_fix_watch(list_T *l, listitem_T *item);
414static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
415static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
416static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
417static void item_lock(typval_T *tv, int deep, int lock);
418static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000419
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100420static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
421static int eval1(char_u **arg, typval_T *rettv, int evaluate);
422static int eval2(char_u **arg, typval_T *rettv, int evaluate);
423static int eval3(char_u **arg, typval_T *rettv, int evaluate);
424static int eval4(char_u **arg, typval_T *rettv, int evaluate);
425static int eval5(char_u **arg, typval_T *rettv, int evaluate);
426static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
427static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000428
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100429static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
430static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
431static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
432static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
433static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
434static long list_len(list_T *l);
435static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
436static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
437static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
438static long list_find_nr(list_T *l, long idx, int *errorp);
439static long list_idx_of_item(list_T *l, listitem_T *item);
440static int list_append_number(list_T *l, varnumber_T n);
441static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
442static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
443static list_T *list_copy(list_T *orig, int deep, int copyID);
444static char_u *list2string(typval_T *tv, int copyID);
445static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
446static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
447static int free_unref_items(int copyID);
448static dictitem_T *dictitem_copy(dictitem_T *org);
449static void dictitem_remove(dict_T *dict, dictitem_T *item);
450static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
451static long dict_len(dict_T *d);
452static char_u *dict2string(typval_T *tv, int copyID);
453static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100454#ifdef FEAT_JOB
455static void job_free(job_T *job);
456#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100457static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
458static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
459static char_u *string_quote(char_u *str, int function);
460static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
461static int find_internal_func(char_u *name);
462static char_u *deref_func_name(char_u *name, int *lenp, int no_autoload);
463static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100464static void emsg_funcname(char *ermsg, char_u *name);
465static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000466
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100468static void f_abs(typval_T *argvars, typval_T *rettv);
469static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100471static void f_add(typval_T *argvars, typval_T *rettv);
472static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
473static void f_and(typval_T *argvars, typval_T *rettv);
474static void f_append(typval_T *argvars, typval_T *rettv);
475static void f_argc(typval_T *argvars, typval_T *rettv);
476static void f_argidx(typval_T *argvars, typval_T *rettv);
477static void f_arglistid(typval_T *argvars, typval_T *rettv);
478static void f_argv(typval_T *argvars, typval_T *rettv);
479static void f_assert_equal(typval_T *argvars, typval_T *rettv);
480static void f_assert_exception(typval_T *argvars, typval_T *rettv);
481static void f_assert_fails(typval_T *argvars, typval_T *rettv);
482static void f_assert_false(typval_T *argvars, typval_T *rettv);
483static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100485static void f_asin(typval_T *argvars, typval_T *rettv);
486static void f_atan(typval_T *argvars, typval_T *rettv);
487static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000488#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100489static void f_browse(typval_T *argvars, typval_T *rettv);
490static void f_browsedir(typval_T *argvars, typval_T *rettv);
491static void f_bufexists(typval_T *argvars, typval_T *rettv);
492static void f_buflisted(typval_T *argvars, typval_T *rettv);
493static void f_bufloaded(typval_T *argvars, typval_T *rettv);
494static void f_bufname(typval_T *argvars, typval_T *rettv);
495static void f_bufnr(typval_T *argvars, typval_T *rettv);
496static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
497static void f_byte2line(typval_T *argvars, typval_T *rettv);
498static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
499static void f_byteidx(typval_T *argvars, typval_T *rettv);
500static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
501static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000502#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100503static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000504#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100505#ifdef FEAT_CHANNEL
506static void f_ch_open(typval_T *argvars, typval_T *rettv);
507static void f_ch_close(typval_T *argvars, typval_T *rettv);
508static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
509static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
510#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100511static void f_changenr(typval_T *argvars, typval_T *rettv);
512static void f_char2nr(typval_T *argvars, typval_T *rettv);
513static void f_cindent(typval_T *argvars, typval_T *rettv);
514static void f_clearmatches(typval_T *argvars, typval_T *rettv);
515static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000516#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100517static void f_complete(typval_T *argvars, typval_T *rettv);
518static void f_complete_add(typval_T *argvars, typval_T *rettv);
519static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000520#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100521static void f_confirm(typval_T *argvars, typval_T *rettv);
522static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000523#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100524static void f_cos(typval_T *argvars, typval_T *rettv);
525static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000526#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100527static void f_count(typval_T *argvars, typval_T *rettv);
528static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
529static void f_cursor(typval_T *argsvars, typval_T *rettv);
530static void f_deepcopy(typval_T *argvars, typval_T *rettv);
531static void f_delete(typval_T *argvars, typval_T *rettv);
532static void f_did_filetype(typval_T *argvars, typval_T *rettv);
533static void f_diff_filler(typval_T *argvars, typval_T *rettv);
534static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
535static void f_empty(typval_T *argvars, typval_T *rettv);
536static void f_escape(typval_T *argvars, typval_T *rettv);
537static void f_eval(typval_T *argvars, typval_T *rettv);
538static void f_eventhandler(typval_T *argvars, typval_T *rettv);
539static void f_executable(typval_T *argvars, typval_T *rettv);
540static void f_exepath(typval_T *argvars, typval_T *rettv);
541static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200542#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100543static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200544#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100545static void f_expand(typval_T *argvars, typval_T *rettv);
546static void f_extend(typval_T *argvars, typval_T *rettv);
547static void f_feedkeys(typval_T *argvars, typval_T *rettv);
548static void f_filereadable(typval_T *argvars, typval_T *rettv);
549static void f_filewritable(typval_T *argvars, typval_T *rettv);
550static void f_filter(typval_T *argvars, typval_T *rettv);
551static void f_finddir(typval_T *argvars, typval_T *rettv);
552static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000553#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100554static void f_float2nr(typval_T *argvars, typval_T *rettv);
555static void f_floor(typval_T *argvars, typval_T *rettv);
556static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000557#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100558static void f_fnameescape(typval_T *argvars, typval_T *rettv);
559static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
560static void f_foldclosed(typval_T *argvars, typval_T *rettv);
561static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
562static void f_foldlevel(typval_T *argvars, typval_T *rettv);
563static void f_foldtext(typval_T *argvars, typval_T *rettv);
564static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
565static void f_foreground(typval_T *argvars, typval_T *rettv);
566static void f_function(typval_T *argvars, typval_T *rettv);
567static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
568static void f_get(typval_T *argvars, typval_T *rettv);
569static void f_getbufline(typval_T *argvars, typval_T *rettv);
570static void f_getbufvar(typval_T *argvars, typval_T *rettv);
571static void f_getchar(typval_T *argvars, typval_T *rettv);
572static void f_getcharmod(typval_T *argvars, typval_T *rettv);
573static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
574static void f_getcmdline(typval_T *argvars, typval_T *rettv);
575static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
576static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
577static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
578static void f_getcwd(typval_T *argvars, typval_T *rettv);
579static void f_getfontname(typval_T *argvars, typval_T *rettv);
580static void f_getfperm(typval_T *argvars, typval_T *rettv);
581static void f_getfsize(typval_T *argvars, typval_T *rettv);
582static void f_getftime(typval_T *argvars, typval_T *rettv);
583static void f_getftype(typval_T *argvars, typval_T *rettv);
584static void f_getline(typval_T *argvars, typval_T *rettv);
585static void f_getmatches(typval_T *argvars, typval_T *rettv);
586static void f_getpid(typval_T *argvars, typval_T *rettv);
587static void f_getcurpos(typval_T *argvars, typval_T *rettv);
588static void f_getpos(typval_T *argvars, typval_T *rettv);
589static void f_getqflist(typval_T *argvars, typval_T *rettv);
590static void f_getreg(typval_T *argvars, typval_T *rettv);
591static void f_getregtype(typval_T *argvars, typval_T *rettv);
592static void f_gettabvar(typval_T *argvars, typval_T *rettv);
593static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
594static void f_getwinposx(typval_T *argvars, typval_T *rettv);
595static void f_getwinposy(typval_T *argvars, typval_T *rettv);
596static void f_getwinvar(typval_T *argvars, typval_T *rettv);
597static void f_glob(typval_T *argvars, typval_T *rettv);
598static void f_globpath(typval_T *argvars, typval_T *rettv);
599static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
600static void f_has(typval_T *argvars, typval_T *rettv);
601static void f_has_key(typval_T *argvars, typval_T *rettv);
602static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
603static void f_hasmapto(typval_T *argvars, typval_T *rettv);
604static void f_histadd(typval_T *argvars, typval_T *rettv);
605static void f_histdel(typval_T *argvars, typval_T *rettv);
606static void f_histget(typval_T *argvars, typval_T *rettv);
607static void f_histnr(typval_T *argvars, typval_T *rettv);
608static void f_hlID(typval_T *argvars, typval_T *rettv);
609static void f_hlexists(typval_T *argvars, typval_T *rettv);
610static void f_hostname(typval_T *argvars, typval_T *rettv);
611static void f_iconv(typval_T *argvars, typval_T *rettv);
612static void f_indent(typval_T *argvars, typval_T *rettv);
613static void f_index(typval_T *argvars, typval_T *rettv);
614static void f_input(typval_T *argvars, typval_T *rettv);
615static void f_inputdialog(typval_T *argvars, typval_T *rettv);
616static void f_inputlist(typval_T *argvars, typval_T *rettv);
617static void f_inputrestore(typval_T *argvars, typval_T *rettv);
618static void f_inputsave(typval_T *argvars, typval_T *rettv);
619static void f_inputsecret(typval_T *argvars, typval_T *rettv);
620static void f_insert(typval_T *argvars, typval_T *rettv);
621static void f_invert(typval_T *argvars, typval_T *rettv);
622static void f_isdirectory(typval_T *argvars, typval_T *rettv);
623static void f_islocked(typval_T *argvars, typval_T *rettv);
624static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100625#ifdef FEAT_JOB
626static void f_job_start(typval_T *argvars, typval_T *rettv);
627static void f_job_stop(typval_T *argvars, typval_T *rettv);
628static void f_job_status(typval_T *argvars, typval_T *rettv);
629#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100630static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100631static void f_jsdecode(typval_T *argvars, typval_T *rettv);
632static void f_jsencode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100633static void f_jsondecode(typval_T *argvars, typval_T *rettv);
634static void f_jsonencode(typval_T *argvars, typval_T *rettv);
635static void f_keys(typval_T *argvars, typval_T *rettv);
636static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
637static void f_len(typval_T *argvars, typval_T *rettv);
638static void f_libcall(typval_T *argvars, typval_T *rettv);
639static void f_libcallnr(typval_T *argvars, typval_T *rettv);
640static void f_line(typval_T *argvars, typval_T *rettv);
641static void f_line2byte(typval_T *argvars, typval_T *rettv);
642static void f_lispindent(typval_T *argvars, typval_T *rettv);
643static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000644#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100645static void f_log(typval_T *argvars, typval_T *rettv);
646static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000647#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200648#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100649static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200650#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100651static void f_map(typval_T *argvars, typval_T *rettv);
652static void f_maparg(typval_T *argvars, typval_T *rettv);
653static void f_mapcheck(typval_T *argvars, typval_T *rettv);
654static void f_match(typval_T *argvars, typval_T *rettv);
655static void f_matchadd(typval_T *argvars, typval_T *rettv);
656static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
657static void f_matcharg(typval_T *argvars, typval_T *rettv);
658static void f_matchdelete(typval_T *argvars, typval_T *rettv);
659static void f_matchend(typval_T *argvars, typval_T *rettv);
660static void f_matchlist(typval_T *argvars, typval_T *rettv);
661static void f_matchstr(typval_T *argvars, typval_T *rettv);
662static void f_max(typval_T *argvars, typval_T *rettv);
663static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000664#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100665static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000666#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100667static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100668#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100669static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100670#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100671static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
672static void f_nr2char(typval_T *argvars, typval_T *rettv);
673static void f_or(typval_T *argvars, typval_T *rettv);
674static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100675#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100676static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100677#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000678#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100679static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000680#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100681static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
682static void f_printf(typval_T *argvars, typval_T *rettv);
683static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200684#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100685static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200686#endif
687#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100688static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200689#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_range(typval_T *argvars, typval_T *rettv);
691static void f_readfile(typval_T *argvars, typval_T *rettv);
692static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100693#ifdef FEAT_FLOAT
694static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
695#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100696static void f_reltimestr(typval_T *argvars, typval_T *rettv);
697static void f_remote_expr(typval_T *argvars, typval_T *rettv);
698static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
699static void f_remote_peek(typval_T *argvars, typval_T *rettv);
700static void f_remote_read(typval_T *argvars, typval_T *rettv);
701static void f_remote_send(typval_T *argvars, typval_T *rettv);
702static void f_remove(typval_T *argvars, typval_T *rettv);
703static void f_rename(typval_T *argvars, typval_T *rettv);
704static void f_repeat(typval_T *argvars, typval_T *rettv);
705static void f_resolve(typval_T *argvars, typval_T *rettv);
706static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000707#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100708static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000709#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100710static void f_screenattr(typval_T *argvars, typval_T *rettv);
711static void f_screenchar(typval_T *argvars, typval_T *rettv);
712static void f_screencol(typval_T *argvars, typval_T *rettv);
713static void f_screenrow(typval_T *argvars, typval_T *rettv);
714static void f_search(typval_T *argvars, typval_T *rettv);
715static void f_searchdecl(typval_T *argvars, typval_T *rettv);
716static void f_searchpair(typval_T *argvars, typval_T *rettv);
717static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
718static void f_searchpos(typval_T *argvars, typval_T *rettv);
719static void f_server2client(typval_T *argvars, typval_T *rettv);
720static void f_serverlist(typval_T *argvars, typval_T *rettv);
721static void f_setbufvar(typval_T *argvars, typval_T *rettv);
722static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
723static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
724static void f_setline(typval_T *argvars, typval_T *rettv);
725static void f_setloclist(typval_T *argvars, typval_T *rettv);
726static void f_setmatches(typval_T *argvars, typval_T *rettv);
727static void f_setpos(typval_T *argvars, typval_T *rettv);
728static void f_setqflist(typval_T *argvars, typval_T *rettv);
729static void f_setreg(typval_T *argvars, typval_T *rettv);
730static void f_settabvar(typval_T *argvars, typval_T *rettv);
731static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
732static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100733#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100734static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100735#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100736static void f_shellescape(typval_T *argvars, typval_T *rettv);
737static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
738static void f_simplify(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_sin(typval_T *argvars, typval_T *rettv);
741static void f_sinh(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_sort(typval_T *argvars, typval_T *rettv);
744static void f_soundfold(typval_T *argvars, typval_T *rettv);
745static void f_spellbadword(typval_T *argvars, typval_T *rettv);
746static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
747static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000748#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100749static void f_sqrt(typval_T *argvars, typval_T *rettv);
750static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000751#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100752static void f_str2nr(typval_T *argvars, typval_T *rettv);
753static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000754#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100755static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000756#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100757static void f_stridx(typval_T *argvars, typval_T *rettv);
758static void f_string(typval_T *argvars, typval_T *rettv);
759static void f_strlen(typval_T *argvars, typval_T *rettv);
760static void f_strpart(typval_T *argvars, typval_T *rettv);
761static void f_strridx(typval_T *argvars, typval_T *rettv);
762static void f_strtrans(typval_T *argvars, typval_T *rettv);
763static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
764static void f_strwidth(typval_T *argvars, typval_T *rettv);
765static void f_submatch(typval_T *argvars, typval_T *rettv);
766static void f_substitute(typval_T *argvars, typval_T *rettv);
767static void f_synID(typval_T *argvars, typval_T *rettv);
768static void f_synIDattr(typval_T *argvars, typval_T *rettv);
769static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
770static void f_synstack(typval_T *argvars, typval_T *rettv);
771static void f_synconcealed(typval_T *argvars, typval_T *rettv);
772static void f_system(typval_T *argvars, typval_T *rettv);
773static void f_systemlist(typval_T *argvars, typval_T *rettv);
774static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
775static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
776static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
777static void f_taglist(typval_T *argvars, typval_T *rettv);
778static void f_tagfiles(typval_T *argvars, typval_T *rettv);
779static void f_tempname(typval_T *argvars, typval_T *rettv);
780static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200781#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100782static void f_tan(typval_T *argvars, typval_T *rettv);
783static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200784#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100785static void f_tolower(typval_T *argvars, typval_T *rettv);
786static void f_toupper(typval_T *argvars, typval_T *rettv);
787static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000788#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100789static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000790#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100791static void f_type(typval_T *argvars, typval_T *rettv);
792static void f_undofile(typval_T *argvars, typval_T *rettv);
793static void f_undotree(typval_T *argvars, typval_T *rettv);
794static void f_uniq(typval_T *argvars, typval_T *rettv);
795static void f_values(typval_T *argvars, typval_T *rettv);
796static void f_virtcol(typval_T *argvars, typval_T *rettv);
797static void f_visualmode(typval_T *argvars, typval_T *rettv);
798static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
799static void f_winbufnr(typval_T *argvars, typval_T *rettv);
800static void f_wincol(typval_T *argvars, typval_T *rettv);
801static void f_winheight(typval_T *argvars, typval_T *rettv);
802static void f_winline(typval_T *argvars, typval_T *rettv);
803static void f_winnr(typval_T *argvars, typval_T *rettv);
804static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
805static void f_winrestview(typval_T *argvars, typval_T *rettv);
806static void f_winsaveview(typval_T *argvars, typval_T *rettv);
807static void f_winwidth(typval_T *argvars, typval_T *rettv);
808static void f_writefile(typval_T *argvars, typval_T *rettv);
809static void f_wordcount(typval_T *argvars, typval_T *rettv);
810static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000811
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100812static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
813static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
814static int get_env_len(char_u **arg);
815static int get_id_len(char_u **arg);
816static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
817static 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 +0000818#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
819#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
820 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100821static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
822static int eval_isnamec(int c);
823static int eval_isnamec1(int c);
824static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
825static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100826static typval_T *alloc_string_tv(char_u *string);
827static void init_tv(typval_T *varp);
828static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100829#ifdef FEAT_FLOAT
830static float_T get_tv_float(typval_T *varp);
831#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100832static linenr_T get_tv_lnum(typval_T *argvars);
833static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
834static char_u *get_tv_string(typval_T *varp);
835static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
836static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
837static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
838static hashtab_T *find_var_ht(char_u *name, char_u **varname);
839static funccall_T *get_funccal(void);
840static void vars_clear_ext(hashtab_T *ht, int free_val);
841static void delete_var(hashtab_T *ht, hashitem_T *hi);
842static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
843static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
844static void set_var(char_u *name, typval_T *varp, int copy);
845static int var_check_ro(int flags, char_u *name, int use_gettext);
846static int var_check_fixed(int flags, char_u *name, int use_gettext);
847static int var_check_func_name(char_u *name, int new_var);
848static int valid_varname(char_u *varname);
849static int tv_check_lock(int lock, char_u *name, int use_gettext);
850static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
851static char_u *find_option_end(char_u **arg, int *opt_flags);
852static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
853static int eval_fname_script(char_u *p);
854static int eval_fname_sid(char_u *p);
855static void list_func_head(ufunc_T *fp, int indent);
856static ufunc_T *find_func(char_u *name);
857static int function_exists(char_u *name);
858static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000859#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100860static void func_do_profile(ufunc_T *fp);
861static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
862static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000863static int
864# ifdef __BORLANDC__
865 _RTLENTRYF
866# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100867 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000868static int
869# ifdef __BORLANDC__
870 _RTLENTRYF
871# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100872 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000873#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100874static int script_autoload(char_u *name, int reload);
875static char_u *autoload_name(char_u *name);
876static void cat_func_name(char_u *buf, ufunc_T *fp);
877static void func_free(ufunc_T *fp);
878static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
879static int can_free_funccal(funccall_T *fc, int copyID) ;
880static void free_funccal(funccall_T *fc, int free_val);
881static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
882static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
883static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
884static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
885static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
886static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
887static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
888static int write_list(FILE *fd, list_T *list, int binary);
889static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000890
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200891
892#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100893static int compare_func_name(const void *s1, const void *s2);
894static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200895#endif
896
Bram Moolenaar33570922005-01-25 22:26:29 +0000897/*
898 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000899 */
900 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100901eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000902{
Bram Moolenaar33570922005-01-25 22:26:29 +0000903 int i;
904 struct vimvar *p;
905
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200906 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
907 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200908 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000909 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000910 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000911
912 for (i = 0; i < VV_LEN; ++i)
913 {
914 p = &vimvars[i];
915 STRCPY(p->vv_di.di_key, p->vv_name);
916 if (p->vv_flags & VV_RO)
917 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
918 else if (p->vv_flags & VV_RO_SBX)
919 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
920 else
921 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000922
923 /* add to v: scope dict, unless the value is not always available */
924 if (p->vv_type != VAR_UNKNOWN)
925 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000926 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000927 /* add to compat scope dict */
928 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000929 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100930 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
931
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000932 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100933 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200934 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100935 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100936
937 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
938 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
939 set_vim_var_nr(VV_NONE, VVAL_NONE);
940 set_vim_var_nr(VV_NULL, VVAL_NULL);
941
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200942 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200943
944#ifdef EBCDIC
945 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100946 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200947 */
948 sortFunctions();
949#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000950}
951
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000952#if defined(EXITFREE) || defined(PROTO)
953 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100954eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000955{
956 int i;
957 struct vimvar *p;
958
959 for (i = 0; i < VV_LEN; ++i)
960 {
961 p = &vimvars[i];
962 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000963 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000964 vim_free(p->vv_str);
965 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000966 }
967 else if (p->vv_di.di_tv.v_type == VAR_LIST)
968 {
969 list_unref(p->vv_list);
970 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000971 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000972 }
973 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000974 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000975 hash_clear(&compat_hashtab);
976
Bram Moolenaard9fba312005-06-26 22:34:35 +0000977 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100978# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200979 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100980# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000981
982 /* global variables */
983 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000984
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000985 /* autoloaded script names */
986 ga_clear_strings(&ga_loaded);
987
Bram Moolenaarcca74132013-09-25 21:00:28 +0200988 /* Script-local variables. First clear all the variables and in a second
989 * loop free the scriptvar_T, because a variable in one script might hold
990 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200991 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200992 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200993 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200994 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200995 ga_clear(&ga_scripts);
996
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000997 /* unreferenced lists and dicts */
998 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000999
1000 /* functions */
1001 free_all_functions();
1002 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001003}
1004#endif
1005
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 * Return the name of the executed function.
1008 */
1009 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001010func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001012 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013}
1014
1015/*
1016 * Return the address holding the next breakpoint line for a funccall cookie.
1017 */
1018 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001019func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020{
Bram Moolenaar33570922005-01-25 22:26:29 +00001021 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022}
1023
1024/*
1025 * Return the address holding the debug tick for a funccall cookie.
1026 */
1027 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001028func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029{
Bram Moolenaar33570922005-01-25 22:26:29 +00001030 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031}
1032
1033/*
1034 * Return the nesting level for a funccall cookie.
1035 */
1036 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001037func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038{
Bram Moolenaar33570922005-01-25 22:26:29 +00001039 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040}
1041
1042/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001043funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001045/* pointer to list of previously used funccal, still around because some
1046 * item in it is still being used. */
1047funccall_T *previous_funccal = NULL;
1048
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049/*
1050 * Return TRUE when a function was ended by a ":return" command.
1051 */
1052 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001053current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054{
1055 return current_funccal->returned;
1056}
1057
1058
1059/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 * Set an internal variable to a string value. Creates the variable if it does
1061 * not already exist.
1062 */
1063 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001064set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001066 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001067 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068
1069 val = vim_strsave(value);
1070 if (val != NULL)
1071 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001072 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001073 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001075 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001076 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 }
1078 }
1079}
1080
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001081static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001082static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083static char_u *redir_endp = NULL;
1084static char_u *redir_varname = NULL;
1085
1086/*
1087 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001088 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001089 * Returns OK if successfully completed the setup. FAIL otherwise.
1090 */
1091 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001092var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093{
1094 int save_emsg;
1095 int err;
1096 typval_T tv;
1097
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001098 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001099 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001100 {
1101 EMSG(_(e_invarg));
1102 return FAIL;
1103 }
1104
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001105 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106 redir_varname = vim_strsave(name);
1107 if (redir_varname == NULL)
1108 return FAIL;
1109
1110 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1111 if (redir_lval == NULL)
1112 {
1113 var_redir_stop();
1114 return FAIL;
1115 }
1116
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001117 /* The output is stored in growarray "redir_ga" until redirection ends. */
1118 ga_init2(&redir_ga, (int)sizeof(char), 500);
1119
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001121 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001122 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1124 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001125 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 if (redir_endp != NULL && *redir_endp != NUL)
1127 /* Trailing characters are present after the variable name */
1128 EMSG(_(e_trailing));
1129 else
1130 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001131 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132 var_redir_stop();
1133 return FAIL;
1134 }
1135
1136 /* check if we can write to the variable: set it to or append an empty
1137 * string */
1138 save_emsg = did_emsg;
1139 did_emsg = FALSE;
1140 tv.v_type = VAR_STRING;
1141 tv.vval.v_string = (char_u *)"";
1142 if (append)
1143 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1144 else
1145 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001146 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001147 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001148 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001149 if (err)
1150 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001151 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001152 var_redir_stop();
1153 return FAIL;
1154 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001155
1156 return OK;
1157}
1158
1159/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001160 * Append "value[value_len]" to the variable set by var_redir_start().
1161 * The actual appending is postponed until redirection ends, because the value
1162 * appended may in fact be the string we write to, changing it may cause freed
1163 * memory to be used:
1164 * :redir => foo
1165 * :let foo
1166 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167 */
1168 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001169var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001170{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001171 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001172
1173 if (redir_lval == NULL)
1174 return;
1175
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001176 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001177 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001178 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001179 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001180
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001181 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001182 {
1183 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001184 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001185 }
1186 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001187 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001188}
1189
1190/*
1191 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001192 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001193 */
1194 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001195var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001196{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001197 typval_T tv;
1198
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001199 if (redir_lval != NULL)
1200 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001201 /* If there was no error: assign the text to the variable. */
1202 if (redir_endp != NULL)
1203 {
1204 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1205 tv.v_type = VAR_STRING;
1206 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001207 /* Call get_lval() again, if it's inside a Dict or List it may
1208 * have changed. */
1209 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001210 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001211 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1212 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1213 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001214 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001215
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001216 /* free the collected output */
1217 vim_free(redir_ga.ga_data);
1218 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001219
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001220 vim_free(redir_lval);
1221 redir_lval = NULL;
1222 }
1223 vim_free(redir_varname);
1224 redir_varname = NULL;
1225}
1226
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227# if defined(FEAT_MBYTE) || defined(PROTO)
1228 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001229eval_charconvert(
1230 char_u *enc_from,
1231 char_u *enc_to,
1232 char_u *fname_from,
1233 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234{
1235 int err = FALSE;
1236
1237 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1238 set_vim_var_string(VV_CC_TO, enc_to, -1);
1239 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1240 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1241 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1242 err = TRUE;
1243 set_vim_var_string(VV_CC_FROM, NULL, -1);
1244 set_vim_var_string(VV_CC_TO, NULL, -1);
1245 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1246 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1247
1248 if (err)
1249 return FAIL;
1250 return OK;
1251}
1252# endif
1253
1254# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1255 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001256eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257{
1258 int err = FALSE;
1259
1260 set_vim_var_string(VV_FNAME_IN, fname, -1);
1261 set_vim_var_string(VV_CMDARG, args, -1);
1262 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1263 err = TRUE;
1264 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1265 set_vim_var_string(VV_CMDARG, NULL, -1);
1266
1267 if (err)
1268 {
1269 mch_remove(fname);
1270 return FAIL;
1271 }
1272 return OK;
1273}
1274# endif
1275
1276# if defined(FEAT_DIFF) || defined(PROTO)
1277 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001278eval_diff(
1279 char_u *origfile,
1280 char_u *newfile,
1281 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282{
1283 int err = FALSE;
1284
1285 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1286 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1287 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1288 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1289 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1290 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1291 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1292}
1293
1294 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001295eval_patch(
1296 char_u *origfile,
1297 char_u *difffile,
1298 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299{
1300 int err;
1301
1302 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1303 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1304 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1305 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1306 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1307 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1308 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1309}
1310# endif
1311
1312/*
1313 * Top level evaluation function, returning a boolean.
1314 * Sets "error" to TRUE if there was an error.
1315 * Return TRUE or FALSE.
1316 */
1317 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001318eval_to_bool(
1319 char_u *arg,
1320 int *error,
1321 char_u **nextcmd,
1322 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323{
Bram Moolenaar33570922005-01-25 22:26:29 +00001324 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 int retval = FALSE;
1326
1327 if (skip)
1328 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001329 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 else
1332 {
1333 *error = FALSE;
1334 if (!skip)
1335 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001336 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001337 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 }
1339 }
1340 if (skip)
1341 --emsg_skip;
1342
1343 return retval;
1344}
1345
1346/*
1347 * Top level evaluation function, returning a string. If "skip" is TRUE,
1348 * only parsing to "nextcmd" is done, without reporting errors. Return
1349 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1350 */
1351 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001352eval_to_string_skip(
1353 char_u *arg,
1354 char_u **nextcmd,
1355 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356{
Bram Moolenaar33570922005-01-25 22:26:29 +00001357 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 char_u *retval;
1359
1360 if (skip)
1361 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001362 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 retval = NULL;
1364 else
1365 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001366 retval = vim_strsave(get_tv_string(&tv));
1367 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 }
1369 if (skip)
1370 --emsg_skip;
1371
1372 return retval;
1373}
1374
1375/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001376 * Skip over an expression at "*pp".
1377 * Return FAIL for an error, OK otherwise.
1378 */
1379 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001380skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001381{
Bram Moolenaar33570922005-01-25 22:26:29 +00001382 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001383
1384 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001385 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001386}
1387
1388/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001390 * When "convert" is TRUE convert a List into a sequence of lines and convert
1391 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 * Return pointer to allocated memory, or NULL for failure.
1393 */
1394 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001395eval_to_string(
1396 char_u *arg,
1397 char_u **nextcmd,
1398 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399{
Bram Moolenaar33570922005-01-25 22:26:29 +00001400 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001402 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001403#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001404 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001407 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 retval = NULL;
1409 else
1410 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001411 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001412 {
1413 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001414 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001415 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001416 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001417 if (tv.vval.v_list->lv_len > 0)
1418 ga_append(&ga, NL);
1419 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001420 ga_append(&ga, NUL);
1421 retval = (char_u *)ga.ga_data;
1422 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001423#ifdef FEAT_FLOAT
1424 else if (convert && tv.v_type == VAR_FLOAT)
1425 {
1426 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1427 retval = vim_strsave(numbuf);
1428 }
1429#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001430 else
1431 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001432 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 }
1434
1435 return retval;
1436}
1437
1438/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001439 * Call eval_to_string() without using current local variables and using
1440 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 */
1442 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001443eval_to_string_safe(
1444 char_u *arg,
1445 char_u **nextcmd,
1446 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447{
1448 char_u *retval;
1449 void *save_funccalp;
1450
1451 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001452 if (use_sandbox)
1453 ++sandbox;
1454 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001455 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001456 if (use_sandbox)
1457 --sandbox;
1458 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 restore_funccal(save_funccalp);
1460 return retval;
1461}
1462
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463/*
1464 * Top level evaluation function, returning a number.
1465 * Evaluates "expr" silently.
1466 * Returns -1 for an error.
1467 */
1468 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001469eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
Bram Moolenaar33570922005-01-25 22:26:29 +00001471 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001473 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474
1475 ++emsg_off;
1476
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001477 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 retval = -1;
1479 else
1480 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001481 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001482 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 }
1484 --emsg_off;
1485
1486 return retval;
1487}
1488
Bram Moolenaara40058a2005-07-11 22:42:07 +00001489/*
1490 * Prepare v: variable "idx" to be used.
1491 * Save the current typeval in "save_tv".
1492 * When not used yet add the variable to the v: hashtable.
1493 */
1494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001495prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001496{
1497 *save_tv = vimvars[idx].vv_tv;
1498 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1499 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1500}
1501
1502/*
1503 * Restore v: variable "idx" to typeval "save_tv".
1504 * When no longer defined, remove the variable from the v: hashtable.
1505 */
1506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001507restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001508{
1509 hashitem_T *hi;
1510
Bram Moolenaara40058a2005-07-11 22:42:07 +00001511 vimvars[idx].vv_tv = *save_tv;
1512 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1513 {
1514 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1515 if (HASHITEM_EMPTY(hi))
1516 EMSG2(_(e_intern2), "restore_vimvar()");
1517 else
1518 hash_remove(&vimvarht, hi);
1519 }
1520}
1521
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001522#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001523/*
1524 * Evaluate an expression to a list with suggestions.
1525 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001526 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001527 */
1528 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001529eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001530{
1531 typval_T save_val;
1532 typval_T rettv;
1533 list_T *list = NULL;
1534 char_u *p = skipwhite(expr);
1535
1536 /* Set "v:val" to the bad word. */
1537 prepare_vimvar(VV_VAL, &save_val);
1538 vimvars[VV_VAL].vv_type = VAR_STRING;
1539 vimvars[VV_VAL].vv_str = badword;
1540 if (p_verbose == 0)
1541 ++emsg_off;
1542
1543 if (eval1(&p, &rettv, TRUE) == OK)
1544 {
1545 if (rettv.v_type != VAR_LIST)
1546 clear_tv(&rettv);
1547 else
1548 list = rettv.vval.v_list;
1549 }
1550
1551 if (p_verbose == 0)
1552 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001553 restore_vimvar(VV_VAL, &save_val);
1554
1555 return list;
1556}
1557
1558/*
1559 * "list" is supposed to contain two items: a word and a number. Return the
1560 * word in "pp" and the number as the return value.
1561 * Return -1 if anything isn't right.
1562 * Used to get the good word and score from the eval_spell_expr() result.
1563 */
1564 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001565get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001566{
1567 listitem_T *li;
1568
1569 li = list->lv_first;
1570 if (li == NULL)
1571 return -1;
1572 *pp = get_tv_string(&li->li_tv);
1573
1574 li = li->li_next;
1575 if (li == NULL)
1576 return -1;
1577 return get_tv_number(&li->li_tv);
1578}
1579#endif
1580
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001581/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001582 * Top level evaluation function.
1583 * Returns an allocated typval_T with the result.
1584 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001585 */
1586 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001587eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001588{
1589 typval_T *tv;
1590
1591 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001592 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001593 {
1594 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001595 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001596 }
1597
1598 return tv;
1599}
1600
1601
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001603 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001604 * Uses argv[argc] for the function arguments. Only Number and String
1605 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001606 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001608 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001609call_vim_function(
1610 char_u *func,
1611 int argc,
1612 char_u **argv,
1613 int safe, /* use the sandbox */
1614 int str_arg_only, /* all arguments are strings */
1615 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616{
Bram Moolenaar33570922005-01-25 22:26:29 +00001617 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 long n;
1619 int len;
1620 int i;
1621 int doesrange;
1622 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001623 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001625 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001627 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628
1629 for (i = 0; i < argc; i++)
1630 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001631 /* Pass a NULL or empty argument as an empty string */
1632 if (argv[i] == NULL || *argv[i] == NUL)
1633 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001634 argvars[i].v_type = VAR_STRING;
1635 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001636 continue;
1637 }
1638
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001639 if (str_arg_only)
1640 len = 0;
1641 else
1642 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001643 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 if (len != 0 && len == (int)STRLEN(argv[i]))
1645 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001646 argvars[i].v_type = VAR_NUMBER;
1647 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 }
1649 else
1650 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001651 argvars[i].v_type = VAR_STRING;
1652 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 }
1654 }
1655
1656 if (safe)
1657 {
1658 save_funccalp = save_funccal();
1659 ++sandbox;
1660 }
1661
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001662 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1663 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001665 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 if (safe)
1667 {
1668 --sandbox;
1669 restore_funccal(save_funccalp);
1670 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001671 vim_free(argvars);
1672
1673 if (ret == FAIL)
1674 clear_tv(rettv);
1675
1676 return ret;
1677}
1678
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001679/*
1680 * Call vimL function "func" and return the result as a number.
1681 * Returns -1 when calling the function fails.
1682 * Uses argv[argc] for the function arguments.
1683 */
1684 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001685call_func_retnr(
1686 char_u *func,
1687 int argc,
1688 char_u **argv,
1689 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001690{
1691 typval_T rettv;
1692 long retval;
1693
1694 /* All arguments are passed as strings, no conversion to number. */
1695 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1696 return -1;
1697
1698 retval = get_tv_number_chk(&rettv, NULL);
1699 clear_tv(&rettv);
1700 return retval;
1701}
1702
1703#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1704 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1705
Bram Moolenaar4f688582007-07-24 12:34:30 +00001706# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001707/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001708 * Call vimL function "func" and return the result as a string.
1709 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001710 * Uses argv[argc] for the function arguments.
1711 */
1712 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001713call_func_retstr(
1714 char_u *func,
1715 int argc,
1716 char_u **argv,
1717 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001718{
1719 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001720 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001721
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001722 /* All arguments are passed as strings, no conversion to number. */
1723 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001724 return NULL;
1725
1726 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001727 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 return retval;
1729}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001730# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001731
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001732/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001733 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001734 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001735 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001736 */
1737 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001738call_func_retlist(
1739 char_u *func,
1740 int argc,
1741 char_u **argv,
1742 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001743{
1744 typval_T rettv;
1745
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001746 /* All arguments are passed as strings, no conversion to number. */
1747 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001748 return NULL;
1749
1750 if (rettv.v_type != VAR_LIST)
1751 {
1752 clear_tv(&rettv);
1753 return NULL;
1754 }
1755
1756 return rettv.vval.v_list;
1757}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758#endif
1759
1760/*
1761 * Save the current function call pointer, and set it to NULL.
1762 * Used when executing autocommands and for ":source".
1763 */
1764 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001765save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001767 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 current_funccal = NULL;
1770 return (void *)fc;
1771}
1772
1773 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001774restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001776 funccall_T *fc = (funccall_T *)vfc;
1777
1778 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779}
1780
Bram Moolenaar05159a02005-02-26 23:04:13 +00001781#if defined(FEAT_PROFILE) || defined(PROTO)
1782/*
1783 * Prepare profiling for entering a child or something else that is not
1784 * counted for the script/function itself.
1785 * Should always be called in pair with prof_child_exit().
1786 */
1787 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001788prof_child_enter(
1789 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001790{
1791 funccall_T *fc = current_funccal;
1792
1793 if (fc != NULL && fc->func->uf_profiling)
1794 profile_start(&fc->prof_child);
1795 script_prof_save(tm);
1796}
1797
1798/*
1799 * Take care of time spent in a child.
1800 * Should always be called after prof_child_enter().
1801 */
1802 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001803prof_child_exit(
1804 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001805{
1806 funccall_T *fc = current_funccal;
1807
1808 if (fc != NULL && fc->func->uf_profiling)
1809 {
1810 profile_end(&fc->prof_child);
1811 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1812 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1813 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1814 }
1815 script_prof_restore(tm);
1816}
1817#endif
1818
1819
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820#ifdef FEAT_FOLDING
1821/*
1822 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1823 * it in "*cp". Doesn't give error messages.
1824 */
1825 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001826eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827{
Bram Moolenaar33570922005-01-25 22:26:29 +00001828 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 int retval;
1830 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001831 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1832 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833
1834 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001835 if (use_sandbox)
1836 ++sandbox;
1837 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 retval = 0;
1841 else
1842 {
1843 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 if (tv.v_type == VAR_NUMBER)
1845 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001846 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 retval = 0;
1848 else
1849 {
1850 /* If the result is a string, check if there is a non-digit before
1851 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001852 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 if (!VIM_ISDIGIT(*s) && *s != '-')
1854 *cp = *s++;
1855 retval = atol((char *)s);
1856 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 }
1859 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001860 if (use_sandbox)
1861 --sandbox;
1862 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863
1864 return retval;
1865}
1866#endif
1867
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 * ":let" list all variable values
1870 * ":let var1 var2" list variable values
1871 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001872 * ":let var += expr" assignment command.
1873 * ":let var -= expr" assignment command.
1874 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001875 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 */
1877 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001878ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879{
1880 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001882 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884 int var_count = 0;
1885 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001886 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001887 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001888 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889
Bram Moolenaardb552d602006-03-23 22:59:57 +00001890 argend = skip_var_list(arg, &var_count, &semicolon);
1891 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001893 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1894 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001895 expr = skipwhite(argend);
1896 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1897 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001899 /*
1900 * ":let" without "=": list variables
1901 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001902 if (*arg == '[')
1903 EMSG(_(e_invarg));
1904 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001905 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001906 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001908 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001909 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001910 list_glob_vars(&first);
1911 list_buf_vars(&first);
1912 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001913#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001914 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001915#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001916 list_script_vars(&first);
1917 list_func_vars(&first);
1918 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 eap->nextcmd = check_nextcmd(arg);
1921 }
1922 else
1923 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001924 op[0] = '=';
1925 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001926 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001927 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001928 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1929 op[0] = *expr; /* +=, -= or .= */
1930 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001931 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001932 else
1933 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001934
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 if (eap->skip)
1936 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001937 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 if (eap->skip)
1939 {
1940 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001941 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 --emsg_skip;
1943 }
1944 else if (i != FAIL)
1945 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001947 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001948 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 }
1950 }
1951}
1952
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001953/*
1954 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1955 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001956 * When "nextchars" is not NULL it points to a string with characters that
1957 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1958 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 * Returns OK or FAIL;
1960 */
1961 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001962ex_let_vars(
1963 char_u *arg_start,
1964 typval_T *tv,
1965 int copy, /* copy values from "tv", don't move */
1966 int semicolon, /* from skip_var_list() */
1967 int var_count, /* from skip_var_list() */
1968 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001969{
1970 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001971 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001973 listitem_T *item;
1974 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001975
1976 if (*arg != '[')
1977 {
1978 /*
1979 * ":let var = expr" or ":for var in list"
1980 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001981 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001982 return FAIL;
1983 return OK;
1984 }
1985
1986 /*
1987 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1988 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001989 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990 {
1991 EMSG(_(e_listreq));
1992 return FAIL;
1993 }
1994
1995 i = list_len(l);
1996 if (semicolon == 0 && var_count < i)
1997 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001998 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 return FAIL;
2000 }
2001 if (var_count - semicolon > i)
2002 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002003 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002004 return FAIL;
2005 }
2006
2007 item = l->lv_first;
2008 while (*arg != ']')
2009 {
2010 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002011 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012 item = item->li_next;
2013 if (arg == NULL)
2014 return FAIL;
2015
2016 arg = skipwhite(arg);
2017 if (*arg == ';')
2018 {
2019 /* Put the rest of the list (may be empty) in the var after ';'.
2020 * Create a new list for this. */
2021 l = list_alloc();
2022 if (l == NULL)
2023 return FAIL;
2024 while (item != NULL)
2025 {
2026 list_append_tv(l, &item->li_tv);
2027 item = item->li_next;
2028 }
2029
2030 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002031 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002032 ltv.vval.v_list = l;
2033 l->lv_refcount = 1;
2034
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002035 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2036 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002037 clear_tv(&ltv);
2038 if (arg == NULL)
2039 return FAIL;
2040 break;
2041 }
2042 else if (*arg != ',' && *arg != ']')
2043 {
2044 EMSG2(_(e_intern2), "ex_let_vars()");
2045 return FAIL;
2046 }
2047 }
2048
2049 return OK;
2050}
2051
2052/*
2053 * Skip over assignable variable "var" or list of variables "[var, var]".
2054 * Used for ":let varvar = expr" and ":for varvar in expr".
2055 * For "[var, var]" increment "*var_count" for each variable.
2056 * for "[var, var; var]" set "semicolon".
2057 * Return NULL for an error.
2058 */
2059 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002060skip_var_list(
2061 char_u *arg,
2062 int *var_count,
2063 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002064{
2065 char_u *p, *s;
2066
2067 if (*arg == '[')
2068 {
2069 /* "[var, var]": find the matching ']'. */
2070 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002071 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002072 {
2073 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2074 s = skip_var_one(p);
2075 if (s == p)
2076 {
2077 EMSG2(_(e_invarg2), p);
2078 return NULL;
2079 }
2080 ++*var_count;
2081
2082 p = skipwhite(s);
2083 if (*p == ']')
2084 break;
2085 else if (*p == ';')
2086 {
2087 if (*semicolon == 1)
2088 {
2089 EMSG(_("Double ; in list of variables"));
2090 return NULL;
2091 }
2092 *semicolon = 1;
2093 }
2094 else if (*p != ',')
2095 {
2096 EMSG2(_(e_invarg2), p);
2097 return NULL;
2098 }
2099 }
2100 return p + 1;
2101 }
2102 else
2103 return skip_var_one(arg);
2104}
2105
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002106/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002107 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002108 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002109 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002110 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002111skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002112{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002113 if (*arg == '@' && arg[1] != NUL)
2114 return arg + 2;
2115 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2116 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002117}
2118
Bram Moolenaara7043832005-01-21 11:56:39 +00002119/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002120 * List variables for hashtab "ht" with prefix "prefix".
2121 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002122 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002124list_hashtable_vars(
2125 hashtab_T *ht,
2126 char_u *prefix,
2127 int empty,
2128 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002129{
Bram Moolenaar33570922005-01-25 22:26:29 +00002130 hashitem_T *hi;
2131 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002132 int todo;
2133
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002134 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002135 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2136 {
2137 if (!HASHITEM_EMPTY(hi))
2138 {
2139 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002140 di = HI2DI(hi);
2141 if (empty || di->di_tv.v_type != VAR_STRING
2142 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002143 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002144 }
2145 }
2146}
2147
2148/*
2149 * List global variables.
2150 */
2151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002152list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002153{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002154 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002155}
2156
2157/*
2158 * List buffer variables.
2159 */
2160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002161list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002162{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002163 char_u numbuf[NUMBUFLEN];
2164
Bram Moolenaar429fa852013-04-15 12:27:36 +02002165 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002166 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002167
2168 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002169 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2170 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002171}
2172
2173/*
2174 * List window variables.
2175 */
2176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002177list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002178{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002179 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002181}
2182
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002183#ifdef FEAT_WINDOWS
2184/*
2185 * List tab page variables.
2186 */
2187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002188list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002189{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002190 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002192}
2193#endif
2194
Bram Moolenaara7043832005-01-21 11:56:39 +00002195/*
2196 * List Vim variables.
2197 */
2198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002199list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002200{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002201 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202}
2203
2204/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002205 * List script-local variables, if there is a script.
2206 */
2207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002208list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002209{
2210 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002211 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2212 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002213}
2214
2215/*
2216 * List function variables, if there is a function.
2217 */
2218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002219list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002220{
2221 if (current_funccal != NULL)
2222 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002223 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002224}
2225
2226/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 * List variables in "arg".
2228 */
2229 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002230list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231{
2232 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002233 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002235 char_u *name_start;
2236 char_u *arg_subsc;
2237 char_u *tofree;
2238 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239
2240 while (!ends_excmd(*arg) && !got_int)
2241 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002244 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002245 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2246 {
2247 emsg_severe = TRUE;
2248 EMSG(_(e_trailing));
2249 break;
2250 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002252 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 /* get_name_len() takes care of expanding curly braces */
2255 name_start = name = arg;
2256 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2257 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 /* This is mainly to keep test 49 working: when expanding
2260 * curly braces fails overrule the exception error message. */
2261 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 emsg_severe = TRUE;
2264 EMSG2(_(e_invarg2), arg);
2265 break;
2266 }
2267 error = TRUE;
2268 }
2269 else
2270 {
2271 if (tofree != NULL)
2272 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002273 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002274 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002275 else
2276 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002277 /* handle d.key, l[idx], f(expr) */
2278 arg_subsc = arg;
2279 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002280 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002282 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002284 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002286 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002287 case 'g': list_glob_vars(first); break;
2288 case 'b': list_buf_vars(first); break;
2289 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002290#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002291 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002292#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002293 case 'v': list_vim_vars(first); break;
2294 case 's': list_script_vars(first); break;
2295 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296 default:
2297 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002298 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002299 }
2300 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002301 {
2302 char_u numbuf[NUMBUFLEN];
2303 char_u *tf;
2304 int c;
2305 char_u *s;
2306
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002307 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002308 c = *arg;
2309 *arg = NUL;
2310 list_one_var_a((char_u *)"",
2311 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002312 tv.v_type,
2313 s == NULL ? (char_u *)"" : s,
2314 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002315 *arg = c;
2316 vim_free(tf);
2317 }
2318 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002319 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002320 }
2321 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002322
2323 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002325
2326 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 }
2328
2329 return arg;
2330}
2331
2332/*
2333 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2334 * Returns a pointer to the char just after the var name.
2335 * Returns NULL if there is an error.
2336 */
2337 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002338ex_let_one(
2339 char_u *arg, /* points to variable name */
2340 typval_T *tv, /* value to assign to variable */
2341 int copy, /* copy value from "tv" */
2342 char_u *endchars, /* valid chars after variable name or NULL */
2343 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344{
2345 int c1;
2346 char_u *name;
2347 char_u *p;
2348 char_u *arg_end = NULL;
2349 int len;
2350 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002351 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002352
2353 /*
2354 * ":let $VAR = expr": Set environment variable.
2355 */
2356 if (*arg == '$')
2357 {
2358 /* Find the end of the name. */
2359 ++arg;
2360 name = arg;
2361 len = get_env_len(&arg);
2362 if (len == 0)
2363 EMSG2(_(e_invarg2), name - 1);
2364 else
2365 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002366 if (op != NULL && (*op == '+' || *op == '-'))
2367 EMSG2(_(e_letwrong), op);
2368 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002369 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002370 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002371 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002372 {
2373 c1 = name[len];
2374 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002375 p = get_tv_string_chk(tv);
2376 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 {
2378 int mustfree = FALSE;
2379 char_u *s = vim_getenv(name, &mustfree);
2380
2381 if (s != NULL)
2382 {
2383 p = tofree = concat_str(s, p);
2384 if (mustfree)
2385 vim_free(s);
2386 }
2387 }
2388 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002389 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002391 if (STRICMP(name, "HOME") == 0)
2392 init_homedir();
2393 else if (didset_vim && STRICMP(name, "VIM") == 0)
2394 didset_vim = FALSE;
2395 else if (didset_vimruntime
2396 && STRICMP(name, "VIMRUNTIME") == 0)
2397 didset_vimruntime = FALSE;
2398 arg_end = arg;
2399 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002400 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002401 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002402 }
2403 }
2404 }
2405
2406 /*
2407 * ":let &option = expr": Set option value.
2408 * ":let &l:option = expr": Set local option value.
2409 * ":let &g:option = expr": Set global option value.
2410 */
2411 else if (*arg == '&')
2412 {
2413 /* Find the end of the name. */
2414 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002415 if (p == NULL || (endchars != NULL
2416 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002417 EMSG(_(e_letunexp));
2418 else
2419 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002420 long n;
2421 int opt_type;
2422 long numval;
2423 char_u *stringval = NULL;
2424 char_u *s;
2425
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002426 c1 = *p;
2427 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002428
2429 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002430 s = get_tv_string_chk(tv); /* != NULL if number or string */
2431 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002432 {
2433 opt_type = get_option_value(arg, &numval,
2434 &stringval, opt_flags);
2435 if ((opt_type == 1 && *op == '.')
2436 || (opt_type == 0 && *op != '.'))
2437 EMSG2(_(e_letwrong), op);
2438 else
2439 {
2440 if (opt_type == 1) /* number */
2441 {
2442 if (*op == '+')
2443 n = numval + n;
2444 else
2445 n = numval - n;
2446 }
2447 else if (opt_type == 0 && stringval != NULL) /* string */
2448 {
2449 s = concat_str(stringval, s);
2450 vim_free(stringval);
2451 stringval = s;
2452 }
2453 }
2454 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002455 if (s != NULL)
2456 {
2457 set_option_value(arg, n, s, opt_flags);
2458 arg_end = p;
2459 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002460 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002461 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 }
2463 }
2464
2465 /*
2466 * ":let @r = expr": Set register contents.
2467 */
2468 else if (*arg == '@')
2469 {
2470 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002471 if (op != NULL && (*op == '+' || *op == '-'))
2472 EMSG2(_(e_letwrong), op);
2473 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002474 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 EMSG(_(e_letunexp));
2476 else
2477 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002478 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002479 char_u *s;
2480
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002481 p = get_tv_string_chk(tv);
2482 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002483 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002484 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002485 if (s != NULL)
2486 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002487 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488 vim_free(s);
2489 }
2490 }
2491 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002492 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002493 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002494 arg_end = arg + 1;
2495 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002496 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 }
2498 }
2499
2500 /*
2501 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002503 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002504 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002506 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002508 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2512 EMSG(_(e_letunexp));
2513 else
2514 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002515 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 arg_end = p;
2517 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002520 }
2521
2522 else
2523 EMSG2(_(e_invarg2), arg);
2524
2525 return arg_end;
2526}
2527
2528/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002529 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2530 */
2531 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002532check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533{
2534 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2535 {
2536 EMSG2(_(e_readonlyvar), arg);
2537 return TRUE;
2538 }
2539 return FALSE;
2540}
2541
2542/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 * Get an lval: variable, Dict item or List item that can be assigned a value
2544 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2545 * "name.key", "name.key[expr]" etc.
2546 * Indexing only works if "name" is an existing List or Dictionary.
2547 * "name" points to the start of the name.
2548 * If "rettv" is not NULL it points to the value to be assigned.
2549 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2550 * wrong; must end in space or cmd separator.
2551 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002552 * flags:
2553 * GLV_QUIET: do not give error messages
2554 * GLV_NO_AUTOLOAD: do not use script autoloading
2555 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002557 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002559 */
2560 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002561get_lval(
2562 char_u *name,
2563 typval_T *rettv,
2564 lval_T *lp,
2565 int unlet,
2566 int skip,
2567 int flags, /* GLV_ values */
2568 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 char_u *expr_start, *expr_end;
2572 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002573 dictitem_T *v;
2574 typval_T var1;
2575 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002577 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002578 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002579 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002580 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002581 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002582
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002584 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585
2586 if (skip)
2587 {
2588 /* When skipping just find the end of the name. */
2589 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002590 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 }
2592
2593 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002594 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 if (expr_start != NULL)
2596 {
2597 /* Don't expand the name when we already know there is an error. */
2598 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2599 && *p != '[' && *p != '.')
2600 {
2601 EMSG(_(e_trailing));
2602 return NULL;
2603 }
2604
2605 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2606 if (lp->ll_exp_name == NULL)
2607 {
2608 /* Report an invalid expression in braces, unless the
2609 * expression evaluation has been cancelled due to an
2610 * aborting error, an interrupt, or an exception. */
2611 if (!aborting() && !quiet)
2612 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002613 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 EMSG2(_(e_invarg2), name);
2615 return NULL;
2616 }
2617 }
2618 lp->ll_name = lp->ll_exp_name;
2619 }
2620 else
2621 lp->ll_name = name;
2622
2623 /* Without [idx] or .key we are done. */
2624 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2625 return p;
2626
2627 cc = *p;
2628 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002629 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 if (v == NULL && !quiet)
2631 EMSG2(_(e_undefvar), lp->ll_name);
2632 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 if (v == NULL)
2634 return NULL;
2635
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 /*
2637 * Loop until no more [idx] or .key is following.
2638 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002639 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002641 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2643 && !(lp->ll_tv->v_type == VAR_DICT
2644 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002645 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (!quiet)
2647 EMSG(_("E689: Can only index a List or Dictionary"));
2648 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002649 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002651 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (!quiet)
2653 EMSG(_("E708: [:] must come last"));
2654 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002655 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002656
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 len = -1;
2658 if (*p == '.')
2659 {
2660 key = p + 1;
2661 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2662 ;
2663 if (len == 0)
2664 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 if (!quiet)
2666 EMSG(_(e_emptykey));
2667 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 }
2669 p = key + len;
2670 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002671 else
2672 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002674 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 if (*p == ':')
2676 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002677 else
2678 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 empty1 = FALSE;
2680 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002682 if (get_tv_string_chk(&var1) == NULL)
2683 {
2684 /* not a number or string */
2685 clear_tv(&var1);
2686 return NULL;
2687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 }
2689
2690 /* Optionally get the second index [ :expr]. */
2691 if (*p == ':')
2692 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002696 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002697 if (!empty1)
2698 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002700 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (rettv != NULL && (rettv->v_type != VAR_LIST
2702 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 if (!quiet)
2705 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 if (!empty1)
2707 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 }
2710 p = skipwhite(p + 1);
2711 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 else
2714 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2717 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 if (!empty1)
2719 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002722 if (get_tv_string_chk(&var2) == NULL)
2723 {
2724 /* not a number or string */
2725 if (!empty1)
2726 clear_tv(&var1);
2727 clear_tv(&var2);
2728 return NULL;
2729 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002732 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002735
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002737 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 if (!quiet)
2739 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 if (!empty1)
2741 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 }
2746
2747 /* Skip to past ']'. */
2748 ++p;
2749 }
2750
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 {
2753 if (len == -1)
2754 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002756 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 if (*key == NUL)
2758 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 if (!quiet)
2760 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 }
2764 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002765 lp->ll_list = NULL;
2766 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002767 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002768
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002769 /* When assigning to a scope dictionary check that a function and
2770 * variable name is valid (only variable name unless it is l: or
2771 * g: dictionary). Disallow overwriting a builtin function. */
2772 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002773 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002774 int prevval;
2775 int wrong;
2776
2777 if (len != -1)
2778 {
2779 prevval = key[len];
2780 key[len] = NUL;
2781 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002782 else
2783 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002784 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2785 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002786 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002787 || !valid_varname(key);
2788 if (len != -1)
2789 key[len] = prevval;
2790 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002791 return NULL;
2792 }
2793
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002796 /* Can't add "v:" variable. */
2797 if (lp->ll_dict == &vimvardict)
2798 {
2799 EMSG2(_(e_illvar), name);
2800 return NULL;
2801 }
2802
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002803 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002807 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 if (len == -1)
2809 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 }
2812 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 if (len == -1)
2817 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002819 p = NULL;
2820 break;
2821 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002822 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002823 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002824 return NULL;
2825
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 if (len == -1)
2827 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002829 }
2830 else
2831 {
2832 /*
2833 * Get the number and item for the only or first index of the List.
2834 */
2835 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 else
2838 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002839 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 clear_tv(&var1);
2841 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002842 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 lp->ll_list = lp->ll_tv->vval.v_list;
2844 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2845 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002847 if (lp->ll_n1 < 0)
2848 {
2849 lp->ll_n1 = 0;
2850 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2851 }
2852 }
2853 if (lp->ll_li == NULL)
2854 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002857 if (!quiet)
2858 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 }
2861
2862 /*
2863 * May need to find the item or absolute index for the second
2864 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 * When no index given: "lp->ll_empty2" is TRUE.
2866 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002870 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002875 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002876 {
2877 if (!quiet)
2878 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002880 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002882 }
2883
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2885 if (lp->ll_n1 < 0)
2886 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2887 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002888 {
2889 if (!quiet)
2890 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002892 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002893 }
2894
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002896 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002897 }
2898
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 return p;
2900}
2901
2902/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002903 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 */
2905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002906clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907{
2908 vim_free(lp->ll_exp_name);
2909 vim_free(lp->ll_newkey);
2910}
2911
2912/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002913 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002915 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 */
2917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002918set_var_lval(
2919 lval_T *lp,
2920 char_u *endp,
2921 typval_T *rettv,
2922 int copy,
2923 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924{
2925 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002926 listitem_T *ri;
2927 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928
2929 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002930 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002932 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 cc = *endp;
2934 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002935 if (op != NULL && *op != '=')
2936 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002937 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002938
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002939 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002940 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002941 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002942 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002943 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002944 if ((di == NULL
2945 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2946 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2947 FALSE)))
2948 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002949 set_var(lp->ll_name, &tv, FALSE);
2950 clear_tv(&tv);
2951 }
2952 }
2953 else
2954 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002956 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002957 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002958 else if (tv_check_lock(lp->ll_newkey == NULL
2959 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002960 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002961 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 else if (lp->ll_range)
2963 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002964 listitem_T *ll_li = lp->ll_li;
2965 int ll_n1 = lp->ll_n1;
2966
2967 /*
2968 * Check whether any of the list items is locked
2969 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002970 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002971 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002972 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002973 return;
2974 ri = ri->li_next;
2975 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2976 break;
2977 ll_li = ll_li->li_next;
2978 ++ll_n1;
2979 }
2980
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002981 /*
2982 * Assign the List values to the list items.
2983 */
2984 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002985 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002986 if (op != NULL && *op != '=')
2987 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2988 else
2989 {
2990 clear_tv(&lp->ll_li->li_tv);
2991 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2992 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002993 ri = ri->li_next;
2994 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2995 break;
2996 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002997 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002998 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002999 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003000 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003001 ri = NULL;
3002 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003003 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003004 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003005 lp->ll_li = lp->ll_li->li_next;
3006 ++lp->ll_n1;
3007 }
3008 if (ri != NULL)
3009 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003010 else if (lp->ll_empty2
3011 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 : lp->ll_n1 != lp->ll_n2)
3013 EMSG(_("E711: List value has not enough items"));
3014 }
3015 else
3016 {
3017 /*
3018 * Assign to a List or Dictionary item.
3019 */
3020 if (lp->ll_newkey != NULL)
3021 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003022 if (op != NULL && *op != '=')
3023 {
3024 EMSG2(_(e_letwrong), op);
3025 return;
3026 }
3027
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003028 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003029 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003030 if (di == NULL)
3031 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003032 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3033 {
3034 vim_free(di);
3035 return;
3036 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003037 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003038 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003039 else if (op != NULL && *op != '=')
3040 {
3041 tv_op(lp->ll_tv, rettv, op);
3042 return;
3043 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003044 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003045 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003046
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003047 /*
3048 * Assign the value to the variable or list item.
3049 */
3050 if (copy)
3051 copy_tv(rettv, lp->ll_tv);
3052 else
3053 {
3054 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003055 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003056 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003057 }
3058 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003059}
3060
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003061/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003062 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3063 * Returns OK or FAIL.
3064 */
3065 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003066tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003067{
3068 long n;
3069 char_u numbuf[NUMBUFLEN];
3070 char_u *s;
3071
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003072 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3073 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3074 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003075 {
3076 switch (tv1->v_type)
3077 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003078 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003079 case VAR_DICT:
3080 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003081 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003082 case VAR_JOB:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003083 break;
3084
3085 case VAR_LIST:
3086 if (*op != '+' || tv2->v_type != VAR_LIST)
3087 break;
3088 /* List += List */
3089 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3090 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3091 return OK;
3092
3093 case VAR_NUMBER:
3094 case VAR_STRING:
3095 if (tv2->v_type == VAR_LIST)
3096 break;
3097 if (*op == '+' || *op == '-')
3098 {
3099 /* nr += nr or nr -= nr*/
3100 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003101#ifdef FEAT_FLOAT
3102 if (tv2->v_type == VAR_FLOAT)
3103 {
3104 float_T f = n;
3105
3106 if (*op == '+')
3107 f += tv2->vval.v_float;
3108 else
3109 f -= tv2->vval.v_float;
3110 clear_tv(tv1);
3111 tv1->v_type = VAR_FLOAT;
3112 tv1->vval.v_float = f;
3113 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003114 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003115#endif
3116 {
3117 if (*op == '+')
3118 n += get_tv_number(tv2);
3119 else
3120 n -= get_tv_number(tv2);
3121 clear_tv(tv1);
3122 tv1->v_type = VAR_NUMBER;
3123 tv1->vval.v_number = n;
3124 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003125 }
3126 else
3127 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003128 if (tv2->v_type == VAR_FLOAT)
3129 break;
3130
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003131 /* str .= str */
3132 s = get_tv_string(tv1);
3133 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3134 clear_tv(tv1);
3135 tv1->v_type = VAR_STRING;
3136 tv1->vval.v_string = s;
3137 }
3138 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003139
3140#ifdef FEAT_FLOAT
3141 case VAR_FLOAT:
3142 {
3143 float_T f;
3144
3145 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3146 && tv2->v_type != VAR_NUMBER
3147 && tv2->v_type != VAR_STRING))
3148 break;
3149 if (tv2->v_type == VAR_FLOAT)
3150 f = tv2->vval.v_float;
3151 else
3152 f = get_tv_number(tv2);
3153 if (*op == '+')
3154 tv1->vval.v_float += f;
3155 else
3156 tv1->vval.v_float -= f;
3157 }
3158 return OK;
3159#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003160 }
3161 }
3162
3163 EMSG2(_(e_letwrong), op);
3164 return FAIL;
3165}
3166
3167/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003168 * Add a watcher to a list.
3169 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003170 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003171list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003172{
3173 lw->lw_next = l->lv_watch;
3174 l->lv_watch = lw;
3175}
3176
3177/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003178 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003179 * No warning when it isn't found...
3180 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003181 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003182list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003183{
Bram Moolenaar33570922005-01-25 22:26:29 +00003184 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185
3186 lwp = &l->lv_watch;
3187 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3188 {
3189 if (lw == lwrem)
3190 {
3191 *lwp = lw->lw_next;
3192 break;
3193 }
3194 lwp = &lw->lw_next;
3195 }
3196}
3197
3198/*
3199 * Just before removing an item from a list: advance watchers to the next
3200 * item.
3201 */
3202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003203list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003204{
Bram Moolenaar33570922005-01-25 22:26:29 +00003205 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003206
3207 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3208 if (lw->lw_item == item)
3209 lw->lw_item = item->li_next;
3210}
3211
3212/*
3213 * Evaluate the expression used in a ":for var in expr" command.
3214 * "arg" points to "var".
3215 * Set "*errp" to TRUE for an error, FALSE otherwise;
3216 * Return a pointer that holds the info. Null when there is an error.
3217 */
3218 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003219eval_for_line(
3220 char_u *arg,
3221 int *errp,
3222 char_u **nextcmdp,
3223 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003224{
Bram Moolenaar33570922005-01-25 22:26:29 +00003225 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003226 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003227 typval_T tv;
3228 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229
3230 *errp = TRUE; /* default: there is an error */
3231
Bram Moolenaar33570922005-01-25 22:26:29 +00003232 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233 if (fi == NULL)
3234 return NULL;
3235
3236 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3237 if (expr == NULL)
3238 return fi;
3239
3240 expr = skipwhite(expr);
3241 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3242 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003243 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003244 return fi;
3245 }
3246
3247 if (skip)
3248 ++emsg_skip;
3249 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3250 {
3251 *errp = FALSE;
3252 if (!skip)
3253 {
3254 l = tv.vval.v_list;
3255 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003256 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003257 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003258 clear_tv(&tv);
3259 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003260 else
3261 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003262 /* No need to increment the refcount, it's already set for the
3263 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003264 fi->fi_list = l;
3265 list_add_watch(l, &fi->fi_lw);
3266 fi->fi_lw.lw_item = l->lv_first;
3267 }
3268 }
3269 }
3270 if (skip)
3271 --emsg_skip;
3272
3273 return fi;
3274}
3275
3276/*
3277 * Use the first item in a ":for" list. Advance to the next.
3278 * Assign the values to the variable (list). "arg" points to the first one.
3279 * Return TRUE when a valid item was found, FALSE when at end of list or
3280 * something wrong.
3281 */
3282 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003283next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003284{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003285 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003286 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003287 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003288
3289 item = fi->fi_lw.lw_item;
3290 if (item == NULL)
3291 result = FALSE;
3292 else
3293 {
3294 fi->fi_lw.lw_item = item->li_next;
3295 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3296 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3297 }
3298 return result;
3299}
3300
3301/*
3302 * Free the structure used to store info used by ":for".
3303 */
3304 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003305free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003306{
Bram Moolenaar33570922005-01-25 22:26:29 +00003307 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003308
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003309 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003310 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003311 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003312 list_unref(fi->fi_list);
3313 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003314 vim_free(fi);
3315}
3316
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3318
3319 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003320set_context_for_expression(
3321 expand_T *xp,
3322 char_u *arg,
3323 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324{
3325 int got_eq = FALSE;
3326 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003327 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003329 if (cmdidx == CMD_let)
3330 {
3331 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003332 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003333 {
3334 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003335 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003336 {
3337 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003338 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003339 if (vim_iswhite(*p))
3340 break;
3341 }
3342 return;
3343 }
3344 }
3345 else
3346 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3347 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 while ((xp->xp_pattern = vim_strpbrk(arg,
3349 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3350 {
3351 c = *xp->xp_pattern;
3352 if (c == '&')
3353 {
3354 c = xp->xp_pattern[1];
3355 if (c == '&')
3356 {
3357 ++xp->xp_pattern;
3358 xp->xp_context = cmdidx != CMD_let || got_eq
3359 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3360 }
3361 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003362 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003364 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3365 xp->xp_pattern += 2;
3366
3367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 }
3369 else if (c == '$')
3370 {
3371 /* environment variable */
3372 xp->xp_context = EXPAND_ENV_VARS;
3373 }
3374 else if (c == '=')
3375 {
3376 got_eq = TRUE;
3377 xp->xp_context = EXPAND_EXPRESSION;
3378 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003379 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 && xp->xp_context == EXPAND_FUNCTIONS
3381 && vim_strchr(xp->xp_pattern, '(') == NULL)
3382 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003383 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 break;
3385 }
3386 else if (cmdidx != CMD_let || got_eq)
3387 {
3388 if (c == '"') /* string */
3389 {
3390 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3391 if (c == '\\' && xp->xp_pattern[1] != NUL)
3392 ++xp->xp_pattern;
3393 xp->xp_context = EXPAND_NOTHING;
3394 }
3395 else if (c == '\'') /* literal string */
3396 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003397 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3399 /* skip */ ;
3400 xp->xp_context = EXPAND_NOTHING;
3401 }
3402 else if (c == '|')
3403 {
3404 if (xp->xp_pattern[1] == '|')
3405 {
3406 ++xp->xp_pattern;
3407 xp->xp_context = EXPAND_EXPRESSION;
3408 }
3409 else
3410 xp->xp_context = EXPAND_COMMANDS;
3411 }
3412 else
3413 xp->xp_context = EXPAND_EXPRESSION;
3414 }
3415 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003416 /* Doesn't look like something valid, expand as an expression
3417 * anyway. */
3418 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 arg = xp->xp_pattern;
3420 if (*arg != NUL)
3421 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3422 /* skip */ ;
3423 }
3424 xp->xp_pattern = arg;
3425}
3426
3427#endif /* FEAT_CMDL_COMPL */
3428
3429/*
3430 * ":1,25call func(arg1, arg2)" function call.
3431 */
3432 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003433ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434{
3435 char_u *arg = eap->arg;
3436 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003438 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003440 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 linenr_T lnum;
3442 int doesrange;
3443 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003444 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003446 if (eap->skip)
3447 {
3448 /* trans_function_name() doesn't work well when skipping, use eval0()
3449 * instead to skip to any following command, e.g. for:
3450 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003451 ++emsg_skip;
3452 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3453 clear_tv(&rettv);
3454 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003455 return;
3456 }
3457
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003458 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003459 if (fudi.fd_newkey != NULL)
3460 {
3461 /* Still need to give an error message for missing key. */
3462 EMSG2(_(e_dictkey), fudi.fd_newkey);
3463 vim_free(fudi.fd_newkey);
3464 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003465 if (tofree == NULL)
3466 return;
3467
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003468 /* Increase refcount on dictionary, it could get deleted when evaluating
3469 * the arguments. */
3470 if (fudi.fd_dict != NULL)
3471 ++fudi.fd_dict->dv_refcount;
3472
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003473 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003474 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003475 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476
Bram Moolenaar532c7802005-01-27 14:44:31 +00003477 /* Skip white space to allow ":call func ()". Not good, but required for
3478 * backward compatibility. */
3479 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003480 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481
3482 if (*startarg != '(')
3483 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003484 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 goto end;
3486 }
3487
3488 /*
3489 * When skipping, evaluate the function once, to find the end of the
3490 * arguments.
3491 * When the function takes a range, this is discovered after the first
3492 * call, and the loop is broken.
3493 */
3494 if (eap->skip)
3495 {
3496 ++emsg_skip;
3497 lnum = eap->line2; /* do it once, also with an invalid range */
3498 }
3499 else
3500 lnum = eap->line1;
3501 for ( ; lnum <= eap->line2; ++lnum)
3502 {
3503 if (!eap->skip && eap->addr_count > 0)
3504 {
3505 curwin->w_cursor.lnum = lnum;
3506 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003507#ifdef FEAT_VIRTUALEDIT
3508 curwin->w_cursor.coladd = 0;
3509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 }
3511 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003512 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003513 eap->line1, eap->line2, &doesrange,
3514 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 {
3516 failed = TRUE;
3517 break;
3518 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003519
3520 /* Handle a function returning a Funcref, Dictionary or List. */
3521 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3522 {
3523 failed = TRUE;
3524 break;
3525 }
3526
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003527 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 if (doesrange || eap->skip)
3529 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003530
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003532 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003533 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003534 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 if (aborting())
3536 break;
3537 }
3538 if (eap->skip)
3539 --emsg_skip;
3540
3541 if (!failed)
3542 {
3543 /* Check for trailing illegal characters and a following command. */
3544 if (!ends_excmd(*arg))
3545 {
3546 emsg_severe = TRUE;
3547 EMSG(_(e_trailing));
3548 }
3549 else
3550 eap->nextcmd = check_nextcmd(arg);
3551 }
3552
3553end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003554 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003555 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556}
3557
3558/*
3559 * ":unlet[!] var1 ... " command.
3560 */
3561 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003562ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003564 ex_unletlock(eap, eap->arg, 0);
3565}
3566
3567/*
3568 * ":lockvar" and ":unlockvar" commands
3569 */
3570 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003571ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003572{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003574 int deep = 2;
3575
3576 if (eap->forceit)
3577 deep = -1;
3578 else if (vim_isdigit(*arg))
3579 {
3580 deep = getdigits(&arg);
3581 arg = skipwhite(arg);
3582 }
3583
3584 ex_unletlock(eap, arg, deep);
3585}
3586
3587/*
3588 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3589 */
3590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003591ex_unletlock(
3592 exarg_T *eap,
3593 char_u *argstart,
3594 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003595{
3596 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003599 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600
3601 do
3602 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003603 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003604 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003605 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003606 if (lv.ll_name == NULL)
3607 error = TRUE; /* error but continue parsing */
3608 if (name_end == NULL || (!vim_iswhite(*name_end)
3609 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003611 if (name_end != NULL)
3612 {
3613 emsg_severe = TRUE;
3614 EMSG(_(e_trailing));
3615 }
3616 if (!(eap->skip || error))
3617 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 break;
3619 }
3620
3621 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003622 {
3623 if (eap->cmdidx == CMD_unlet)
3624 {
3625 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3626 error = TRUE;
3627 }
3628 else
3629 {
3630 if (do_lock_var(&lv, name_end, deep,
3631 eap->cmdidx == CMD_lockvar) == FAIL)
3632 error = TRUE;
3633 }
3634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003636 if (!eap->skip)
3637 clear_lval(&lv);
3638
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 arg = skipwhite(name_end);
3640 } while (!ends_excmd(*arg));
3641
3642 eap->nextcmd = check_nextcmd(arg);
3643}
3644
Bram Moolenaar8c711452005-01-14 21:53:12 +00003645 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003646do_unlet_var(
3647 lval_T *lp,
3648 char_u *name_end,
3649 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003650{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 int ret = OK;
3652 int cc;
3653
3654 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003655 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003656 cc = *name_end;
3657 *name_end = NUL;
3658
3659 /* Normal name or expanded name. */
3660 if (check_changedtick(lp->ll_name))
3661 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003662 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003663 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003664 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003665 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003666 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003667 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003668 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003669 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003670 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003671 else if (lp->ll_range)
3672 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003673 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003674 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003675 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003676
3677 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3678 {
3679 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003680 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003681 return FAIL;
3682 ll_li = li;
3683 ++ll_n1;
3684 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003685
3686 /* Delete a range of List items. */
3687 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3688 {
3689 li = lp->ll_li->li_next;
3690 listitem_remove(lp->ll_list, lp->ll_li);
3691 lp->ll_li = li;
3692 ++lp->ll_n1;
3693 }
3694 }
3695 else
3696 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003697 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003698 /* unlet a List item. */
3699 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003700 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003701 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003702 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003703 }
3704
3705 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003706}
3707
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708/*
3709 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003710 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 */
3712 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003713do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714{
Bram Moolenaar33570922005-01-25 22:26:29 +00003715 hashtab_T *ht;
3716 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003717 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003718 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003719 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720
Bram Moolenaar33570922005-01-25 22:26:29 +00003721 ht = find_var_ht(name, &varname);
3722 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003724 if (ht == &globvarht)
3725 d = &globvardict;
3726 else if (current_funccal != NULL
3727 && ht == &current_funccal->l_vars.dv_hashtab)
3728 d = &current_funccal->l_vars;
3729 else if (ht == &compat_hashtab)
3730 d = &vimvardict;
3731 else
3732 {
3733 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3734 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3735 }
3736 if (d == NULL)
3737 {
3738 EMSG2(_(e_intern2), "do_unlet()");
3739 return FAIL;
3740 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003741 hi = hash_find(ht, varname);
3742 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003743 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003744 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003745 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003746 || var_check_ro(di->di_flags, name, FALSE)
3747 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003748 return FAIL;
3749
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003750 delete_var(ht, hi);
3751 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003754 if (forceit)
3755 return OK;
3756 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 return FAIL;
3758}
3759
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003760/*
3761 * Lock or unlock variable indicated by "lp".
3762 * "deep" is the levels to go (-1 for unlimited);
3763 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3764 */
3765 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003766do_lock_var(
3767 lval_T *lp,
3768 char_u *name_end,
3769 int deep,
3770 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003771{
3772 int ret = OK;
3773 int cc;
3774 dictitem_T *di;
3775
3776 if (deep == 0) /* nothing to do */
3777 return OK;
3778
3779 if (lp->ll_tv == NULL)
3780 {
3781 cc = *name_end;
3782 *name_end = NUL;
3783
3784 /* Normal name or expanded name. */
3785 if (check_changedtick(lp->ll_name))
3786 ret = FAIL;
3787 else
3788 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003789 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003790 if (di == NULL)
3791 ret = FAIL;
3792 else
3793 {
3794 if (lock)
3795 di->di_flags |= DI_FLAGS_LOCK;
3796 else
3797 di->di_flags &= ~DI_FLAGS_LOCK;
3798 item_lock(&di->di_tv, deep, lock);
3799 }
3800 }
3801 *name_end = cc;
3802 }
3803 else if (lp->ll_range)
3804 {
3805 listitem_T *li = lp->ll_li;
3806
3807 /* (un)lock a range of List items. */
3808 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3809 {
3810 item_lock(&li->li_tv, deep, lock);
3811 li = li->li_next;
3812 ++lp->ll_n1;
3813 }
3814 }
3815 else if (lp->ll_list != NULL)
3816 /* (un)lock a List item. */
3817 item_lock(&lp->ll_li->li_tv, deep, lock);
3818 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003819 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003820 item_lock(&lp->ll_di->di_tv, deep, lock);
3821
3822 return ret;
3823}
3824
3825/*
3826 * Lock or unlock an item. "deep" is nr of levels to go.
3827 */
3828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003829item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003830{
3831 static int recurse = 0;
3832 list_T *l;
3833 listitem_T *li;
3834 dict_T *d;
3835 hashitem_T *hi;
3836 int todo;
3837
3838 if (recurse >= DICT_MAXNEST)
3839 {
3840 EMSG(_("E743: variable nested too deep for (un)lock"));
3841 return;
3842 }
3843 if (deep == 0)
3844 return;
3845 ++recurse;
3846
3847 /* lock/unlock the item itself */
3848 if (lock)
3849 tv->v_lock |= VAR_LOCKED;
3850 else
3851 tv->v_lock &= ~VAR_LOCKED;
3852
3853 switch (tv->v_type)
3854 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003855 case VAR_UNKNOWN:
3856 case VAR_NUMBER:
3857 case VAR_STRING:
3858 case VAR_FUNC:
3859 case VAR_FLOAT:
3860 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003861 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003862 break;
3863
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003864 case VAR_LIST:
3865 if ((l = tv->vval.v_list) != NULL)
3866 {
3867 if (lock)
3868 l->lv_lock |= VAR_LOCKED;
3869 else
3870 l->lv_lock &= ~VAR_LOCKED;
3871 if (deep < 0 || deep > 1)
3872 /* recursive: lock/unlock the items the List contains */
3873 for (li = l->lv_first; li != NULL; li = li->li_next)
3874 item_lock(&li->li_tv, deep - 1, lock);
3875 }
3876 break;
3877 case VAR_DICT:
3878 if ((d = tv->vval.v_dict) != NULL)
3879 {
3880 if (lock)
3881 d->dv_lock |= VAR_LOCKED;
3882 else
3883 d->dv_lock &= ~VAR_LOCKED;
3884 if (deep < 0 || deep > 1)
3885 {
3886 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003887 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003888 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3889 {
3890 if (!HASHITEM_EMPTY(hi))
3891 {
3892 --todo;
3893 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3894 }
3895 }
3896 }
3897 }
3898 }
3899 --recurse;
3900}
3901
Bram Moolenaara40058a2005-07-11 22:42:07 +00003902/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003903 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3904 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003905 */
3906 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003907tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003908{
3909 return (tv->v_lock & VAR_LOCKED)
3910 || (tv->v_type == VAR_LIST
3911 && tv->vval.v_list != NULL
3912 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3913 || (tv->v_type == VAR_DICT
3914 && tv->vval.v_dict != NULL
3915 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3916}
3917
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3919/*
3920 * Delete all "menutrans_" variables.
3921 */
3922 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003923del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924{
Bram Moolenaar33570922005-01-25 22:26:29 +00003925 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003926 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
Bram Moolenaar33570922005-01-25 22:26:29 +00003928 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003929 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003930 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003931 {
3932 if (!HASHITEM_EMPTY(hi))
3933 {
3934 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003935 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3936 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003937 }
3938 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003939 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940}
3941#endif
3942
3943#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3944
3945/*
3946 * Local string buffer for the next two functions to store a variable name
3947 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3948 * get_user_var_name().
3949 */
3950
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003951static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952
3953static char_u *varnamebuf = NULL;
3954static int varnamebuflen = 0;
3955
3956/*
3957 * Function to concatenate a prefix and a variable name.
3958 */
3959 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003960cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961{
3962 int len;
3963
3964 len = (int)STRLEN(name) + 3;
3965 if (len > varnamebuflen)
3966 {
3967 vim_free(varnamebuf);
3968 len += 10; /* some additional space */
3969 varnamebuf = alloc(len);
3970 if (varnamebuf == NULL)
3971 {
3972 varnamebuflen = 0;
3973 return NULL;
3974 }
3975 varnamebuflen = len;
3976 }
3977 *varnamebuf = prefix;
3978 varnamebuf[1] = ':';
3979 STRCPY(varnamebuf + 2, name);
3980 return varnamebuf;
3981}
3982
3983/*
3984 * Function given to ExpandGeneric() to obtain the list of user defined
3985 * (global/buffer/window/built-in) variable names.
3986 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003988get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003990 static long_u gdone;
3991 static long_u bdone;
3992 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003993#ifdef FEAT_WINDOWS
3994 static long_u tdone;
3995#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003996 static int vidx;
3997 static hashitem_T *hi;
3998 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999
4000 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004001 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004002 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004003#ifdef FEAT_WINDOWS
4004 tdone = 0;
4005#endif
4006 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004007
4008 /* Global variables */
4009 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004011 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004012 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004013 else
4014 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004015 while (HASHITEM_EMPTY(hi))
4016 ++hi;
4017 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4018 return cat_prefix_varname('g', hi->hi_key);
4019 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004021
4022 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004023 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004024 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004026 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004027 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004028 else
4029 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004030 while (HASHITEM_EMPTY(hi))
4031 ++hi;
4032 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004034 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004036 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 return (char_u *)"b:changedtick";
4038 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004039
4040 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004041 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004042 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004044 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004045 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004046 else
4047 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004048 while (HASHITEM_EMPTY(hi))
4049 ++hi;
4050 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004052
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004053#ifdef FEAT_WINDOWS
4054 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004055 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004056 if (tdone < ht->ht_used)
4057 {
4058 if (tdone++ == 0)
4059 hi = ht->ht_array;
4060 else
4061 ++hi;
4062 while (HASHITEM_EMPTY(hi))
4063 ++hi;
4064 return cat_prefix_varname('t', hi->hi_key);
4065 }
4066#endif
4067
Bram Moolenaar33570922005-01-25 22:26:29 +00004068 /* v: variables */
4069 if (vidx < VV_LEN)
4070 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071
4072 vim_free(varnamebuf);
4073 varnamebuf = NULL;
4074 varnamebuflen = 0;
4075 return NULL;
4076}
4077
4078#endif /* FEAT_CMDL_COMPL */
4079
4080/*
4081 * types for expressions.
4082 */
4083typedef enum
4084{
4085 TYPE_UNKNOWN = 0
4086 , TYPE_EQUAL /* == */
4087 , TYPE_NEQUAL /* != */
4088 , TYPE_GREATER /* > */
4089 , TYPE_GEQUAL /* >= */
4090 , TYPE_SMALLER /* < */
4091 , TYPE_SEQUAL /* <= */
4092 , TYPE_MATCH /* =~ */
4093 , TYPE_NOMATCH /* !~ */
4094} exptype_T;
4095
4096/*
4097 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004098 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4100 */
4101
4102/*
4103 * Handle zero level expression.
4104 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004105 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004106 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 * Return OK or FAIL.
4108 */
4109 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004110eval0(
4111 char_u *arg,
4112 typval_T *rettv,
4113 char_u **nextcmd,
4114 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115{
4116 int ret;
4117 char_u *p;
4118
4119 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004120 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 if (ret == FAIL || !ends_excmd(*p))
4122 {
4123 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004124 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 /*
4126 * Report the invalid expression unless the expression evaluation has
4127 * been cancelled due to an aborting error, an interrupt, or an
4128 * exception.
4129 */
4130 if (!aborting())
4131 EMSG2(_(e_invexpr2), arg);
4132 ret = FAIL;
4133 }
4134 if (nextcmd != NULL)
4135 *nextcmd = check_nextcmd(p);
4136
4137 return ret;
4138}
4139
4140/*
4141 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004142 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 *
4144 * "arg" must point to the first non-white of the expression.
4145 * "arg" is advanced to the next non-white after the recognized expression.
4146 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004147 * Note: "rettv.v_lock" is not set.
4148 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 * Return OK or FAIL.
4150 */
4151 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004152eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153{
4154 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004155 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156
4157 /*
4158 * Get the first variable.
4159 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004160 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 return FAIL;
4162
4163 if ((*arg)[0] == '?')
4164 {
4165 result = FALSE;
4166 if (evaluate)
4167 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004168 int error = FALSE;
4169
4170 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004172 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004173 if (error)
4174 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 }
4176
4177 /*
4178 * Get the second variable.
4179 */
4180 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004181 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 return FAIL;
4183
4184 /*
4185 * Check for the ":".
4186 */
4187 if ((*arg)[0] != ':')
4188 {
4189 EMSG(_("E109: Missing ':' after '?'"));
4190 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004191 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 return FAIL;
4193 }
4194
4195 /*
4196 * Get the third variable.
4197 */
4198 *arg = skipwhite(*arg + 1);
4199 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4200 {
4201 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004202 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 return FAIL;
4204 }
4205 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004206 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 }
4208
4209 return OK;
4210}
4211
4212/*
4213 * Handle first level expression:
4214 * expr2 || expr2 || expr2 logical OR
4215 *
4216 * "arg" must point to the first non-white of the expression.
4217 * "arg" is advanced to the next non-white after the recognized expression.
4218 *
4219 * Return OK or FAIL.
4220 */
4221 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004222eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223{
Bram Moolenaar33570922005-01-25 22:26:29 +00004224 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 long result;
4226 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004227 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228
4229 /*
4230 * Get the first variable.
4231 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004232 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 return FAIL;
4234
4235 /*
4236 * Repeat until there is no following "||".
4237 */
4238 first = TRUE;
4239 result = FALSE;
4240 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4241 {
4242 if (evaluate && first)
4243 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004244 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004246 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004247 if (error)
4248 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 first = FALSE;
4250 }
4251
4252 /*
4253 * Get the second variable.
4254 */
4255 *arg = skipwhite(*arg + 2);
4256 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4257 return FAIL;
4258
4259 /*
4260 * Compute the result.
4261 */
4262 if (evaluate && !result)
4263 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004264 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004266 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004267 if (error)
4268 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 }
4270 if (evaluate)
4271 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004272 rettv->v_type = VAR_NUMBER;
4273 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 }
4275 }
4276
4277 return OK;
4278}
4279
4280/*
4281 * Handle second level expression:
4282 * expr3 && expr3 && expr3 logical AND
4283 *
4284 * "arg" must point to the first non-white of the expression.
4285 * "arg" is advanced to the next non-white after the recognized expression.
4286 *
4287 * Return OK or FAIL.
4288 */
4289 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004290eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291{
Bram Moolenaar33570922005-01-25 22:26:29 +00004292 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 long result;
4294 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004295 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296
4297 /*
4298 * Get the first variable.
4299 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004300 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 return FAIL;
4302
4303 /*
4304 * Repeat until there is no following "&&".
4305 */
4306 first = TRUE;
4307 result = TRUE;
4308 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4309 {
4310 if (evaluate && first)
4311 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004312 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004314 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004315 if (error)
4316 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 first = FALSE;
4318 }
4319
4320 /*
4321 * Get the second variable.
4322 */
4323 *arg = skipwhite(*arg + 2);
4324 if (eval4(arg, &var2, evaluate && result) == FAIL)
4325 return FAIL;
4326
4327 /*
4328 * Compute the result.
4329 */
4330 if (evaluate && result)
4331 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004332 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004334 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004335 if (error)
4336 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 }
4338 if (evaluate)
4339 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004340 rettv->v_type = VAR_NUMBER;
4341 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 }
4343 }
4344
4345 return OK;
4346}
4347
4348/*
4349 * Handle third level expression:
4350 * var1 == var2
4351 * var1 =~ var2
4352 * var1 != var2
4353 * var1 !~ var2
4354 * var1 > var2
4355 * var1 >= var2
4356 * var1 < var2
4357 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004358 * var1 is var2
4359 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 *
4361 * "arg" must point to the first non-white of the expression.
4362 * "arg" is advanced to the next non-white after the recognized expression.
4363 *
4364 * Return OK or FAIL.
4365 */
4366 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004367eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368{
Bram Moolenaar33570922005-01-25 22:26:29 +00004369 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 char_u *p;
4371 int i;
4372 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004373 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 int len = 2;
4375 long n1, n2;
4376 char_u *s1, *s2;
4377 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4378 regmatch_T regmatch;
4379 int ic;
4380 char_u *save_cpo;
4381
4382 /*
4383 * Get the first variable.
4384 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004385 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 return FAIL;
4387
4388 p = *arg;
4389 switch (p[0])
4390 {
4391 case '=': if (p[1] == '=')
4392 type = TYPE_EQUAL;
4393 else if (p[1] == '~')
4394 type = TYPE_MATCH;
4395 break;
4396 case '!': if (p[1] == '=')
4397 type = TYPE_NEQUAL;
4398 else if (p[1] == '~')
4399 type = TYPE_NOMATCH;
4400 break;
4401 case '>': if (p[1] != '=')
4402 {
4403 type = TYPE_GREATER;
4404 len = 1;
4405 }
4406 else
4407 type = TYPE_GEQUAL;
4408 break;
4409 case '<': if (p[1] != '=')
4410 {
4411 type = TYPE_SMALLER;
4412 len = 1;
4413 }
4414 else
4415 type = TYPE_SEQUAL;
4416 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004417 case 'i': if (p[1] == 's')
4418 {
4419 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4420 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004421 i = p[len];
4422 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004423 {
4424 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4425 type_is = TRUE;
4426 }
4427 }
4428 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 }
4430
4431 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004432 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 */
4434 if (type != TYPE_UNKNOWN)
4435 {
4436 /* extra question mark appended: ignore case */
4437 if (p[len] == '?')
4438 {
4439 ic = TRUE;
4440 ++len;
4441 }
4442 /* extra '#' appended: match case */
4443 else if (p[len] == '#')
4444 {
4445 ic = FALSE;
4446 ++len;
4447 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004448 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 else
4450 ic = p_ic;
4451
4452 /*
4453 * Get the second variable.
4454 */
4455 *arg = skipwhite(p + len);
4456 if (eval5(arg, &var2, evaluate) == FAIL)
4457 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004458 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 return FAIL;
4460 }
4461
4462 if (evaluate)
4463 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004464 if (type_is && rettv->v_type != var2.v_type)
4465 {
4466 /* For "is" a different type always means FALSE, for "notis"
4467 * it means TRUE. */
4468 n1 = (type == TYPE_NEQUAL);
4469 }
4470 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4471 {
4472 if (type_is)
4473 {
4474 n1 = (rettv->v_type == var2.v_type
4475 && rettv->vval.v_list == var2.vval.v_list);
4476 if (type == TYPE_NEQUAL)
4477 n1 = !n1;
4478 }
4479 else if (rettv->v_type != var2.v_type
4480 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4481 {
4482 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004483 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004484 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004485 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004486 clear_tv(rettv);
4487 clear_tv(&var2);
4488 return FAIL;
4489 }
4490 else
4491 {
4492 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004493 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4494 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004495 if (type == TYPE_NEQUAL)
4496 n1 = !n1;
4497 }
4498 }
4499
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004500 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4501 {
4502 if (type_is)
4503 {
4504 n1 = (rettv->v_type == var2.v_type
4505 && rettv->vval.v_dict == var2.vval.v_dict);
4506 if (type == TYPE_NEQUAL)
4507 n1 = !n1;
4508 }
4509 else if (rettv->v_type != var2.v_type
4510 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4511 {
4512 if (rettv->v_type != var2.v_type)
4513 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4514 else
4515 EMSG(_("E736: Invalid operation for Dictionary"));
4516 clear_tv(rettv);
4517 clear_tv(&var2);
4518 return FAIL;
4519 }
4520 else
4521 {
4522 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004523 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4524 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004525 if (type == TYPE_NEQUAL)
4526 n1 = !n1;
4527 }
4528 }
4529
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004530 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4531 {
4532 if (rettv->v_type != var2.v_type
4533 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4534 {
4535 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004536 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004537 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004538 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004539 clear_tv(rettv);
4540 clear_tv(&var2);
4541 return FAIL;
4542 }
4543 else
4544 {
4545 /* Compare two Funcrefs for being equal or unequal. */
4546 if (rettv->vval.v_string == NULL
4547 || var2.vval.v_string == NULL)
4548 n1 = FALSE;
4549 else
4550 n1 = STRCMP(rettv->vval.v_string,
4551 var2.vval.v_string) == 0;
4552 if (type == TYPE_NEQUAL)
4553 n1 = !n1;
4554 }
4555 }
4556
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004557#ifdef FEAT_FLOAT
4558 /*
4559 * If one of the two variables is a float, compare as a float.
4560 * When using "=~" or "!~", always compare as string.
4561 */
4562 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4563 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4564 {
4565 float_T f1, f2;
4566
4567 if (rettv->v_type == VAR_FLOAT)
4568 f1 = rettv->vval.v_float;
4569 else
4570 f1 = get_tv_number(rettv);
4571 if (var2.v_type == VAR_FLOAT)
4572 f2 = var2.vval.v_float;
4573 else
4574 f2 = get_tv_number(&var2);
4575 n1 = FALSE;
4576 switch (type)
4577 {
4578 case TYPE_EQUAL: n1 = (f1 == f2); break;
4579 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4580 case TYPE_GREATER: n1 = (f1 > f2); break;
4581 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4582 case TYPE_SMALLER: n1 = (f1 < f2); break;
4583 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4584 case TYPE_UNKNOWN:
4585 case TYPE_MATCH:
4586 case TYPE_NOMATCH: break; /* avoid gcc warning */
4587 }
4588 }
4589#endif
4590
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 /*
4592 * If one of the two variables is a number, compare as a number.
4593 * When using "=~" or "!~", always compare as string.
4594 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004595 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4597 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004598 n1 = get_tv_number(rettv);
4599 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 switch (type)
4601 {
4602 case TYPE_EQUAL: n1 = (n1 == n2); break;
4603 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4604 case TYPE_GREATER: n1 = (n1 > n2); break;
4605 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4606 case TYPE_SMALLER: n1 = (n1 < n2); break;
4607 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4608 case TYPE_UNKNOWN:
4609 case TYPE_MATCH:
4610 case TYPE_NOMATCH: break; /* avoid gcc warning */
4611 }
4612 }
4613 else
4614 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004615 s1 = get_tv_string_buf(rettv, buf1);
4616 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4618 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4619 else
4620 i = 0;
4621 n1 = FALSE;
4622 switch (type)
4623 {
4624 case TYPE_EQUAL: n1 = (i == 0); break;
4625 case TYPE_NEQUAL: n1 = (i != 0); break;
4626 case TYPE_GREATER: n1 = (i > 0); break;
4627 case TYPE_GEQUAL: n1 = (i >= 0); break;
4628 case TYPE_SMALLER: n1 = (i < 0); break;
4629 case TYPE_SEQUAL: n1 = (i <= 0); break;
4630
4631 case TYPE_MATCH:
4632 case TYPE_NOMATCH:
4633 /* avoid 'l' flag in 'cpoptions' */
4634 save_cpo = p_cpo;
4635 p_cpo = (char_u *)"";
4636 regmatch.regprog = vim_regcomp(s2,
4637 RE_MAGIC + RE_STRING);
4638 regmatch.rm_ic = ic;
4639 if (regmatch.regprog != NULL)
4640 {
4641 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004642 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 if (type == TYPE_NOMATCH)
4644 n1 = !n1;
4645 }
4646 p_cpo = save_cpo;
4647 break;
4648
4649 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4650 }
4651 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004652 clear_tv(rettv);
4653 clear_tv(&var2);
4654 rettv->v_type = VAR_NUMBER;
4655 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 }
4657 }
4658
4659 return OK;
4660}
4661
4662/*
4663 * Handle fourth level expression:
4664 * + number addition
4665 * - number subtraction
4666 * . string concatenation
4667 *
4668 * "arg" must point to the first non-white of the expression.
4669 * "arg" is advanced to the next non-white after the recognized expression.
4670 *
4671 * Return OK or FAIL.
4672 */
4673 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004674eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675{
Bram Moolenaar33570922005-01-25 22:26:29 +00004676 typval_T var2;
4677 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 int op;
4679 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004680#ifdef FEAT_FLOAT
4681 float_T f1 = 0, f2 = 0;
4682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 char_u *s1, *s2;
4684 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4685 char_u *p;
4686
4687 /*
4688 * Get the first variable.
4689 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004690 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 return FAIL;
4692
4693 /*
4694 * Repeat computing, until no '+', '-' or '.' is following.
4695 */
4696 for (;;)
4697 {
4698 op = **arg;
4699 if (op != '+' && op != '-' && op != '.')
4700 break;
4701
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004702 if ((op != '+' || rettv->v_type != VAR_LIST)
4703#ifdef FEAT_FLOAT
4704 && (op == '.' || rettv->v_type != VAR_FLOAT)
4705#endif
4706 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004707 {
4708 /* For "list + ...", an illegal use of the first operand as
4709 * a number cannot be determined before evaluating the 2nd
4710 * operand: if this is also a list, all is ok.
4711 * For "something . ...", "something - ..." or "non-list + ...",
4712 * we know that the first operand needs to be a string or number
4713 * without evaluating the 2nd operand. So check before to avoid
4714 * side effects after an error. */
4715 if (evaluate && get_tv_string_chk(rettv) == NULL)
4716 {
4717 clear_tv(rettv);
4718 return FAIL;
4719 }
4720 }
4721
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 /*
4723 * Get the second variable.
4724 */
4725 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004726 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004728 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 return FAIL;
4730 }
4731
4732 if (evaluate)
4733 {
4734 /*
4735 * Compute the result.
4736 */
4737 if (op == '.')
4738 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004739 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4740 s2 = get_tv_string_buf_chk(&var2, buf2);
4741 if (s2 == NULL) /* type error ? */
4742 {
4743 clear_tv(rettv);
4744 clear_tv(&var2);
4745 return FAIL;
4746 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004747 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004748 clear_tv(rettv);
4749 rettv->v_type = VAR_STRING;
4750 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004752 else if (op == '+' && rettv->v_type == VAR_LIST
4753 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004754 {
4755 /* concatenate Lists */
4756 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4757 &var3) == FAIL)
4758 {
4759 clear_tv(rettv);
4760 clear_tv(&var2);
4761 return FAIL;
4762 }
4763 clear_tv(rettv);
4764 *rettv = var3;
4765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 else
4767 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004768 int error = FALSE;
4769
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004770#ifdef FEAT_FLOAT
4771 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004772 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004773 f1 = rettv->vval.v_float;
4774 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004775 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004776 else
4777#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004778 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004779 n1 = get_tv_number_chk(rettv, &error);
4780 if (error)
4781 {
4782 /* This can only happen for "list + non-list". For
4783 * "non-list + ..." or "something - ...", we returned
4784 * before evaluating the 2nd operand. */
4785 clear_tv(rettv);
4786 return FAIL;
4787 }
4788#ifdef FEAT_FLOAT
4789 if (var2.v_type == VAR_FLOAT)
4790 f1 = n1;
4791#endif
4792 }
4793#ifdef FEAT_FLOAT
4794 if (var2.v_type == VAR_FLOAT)
4795 {
4796 f2 = var2.vval.v_float;
4797 n2 = 0;
4798 }
4799 else
4800#endif
4801 {
4802 n2 = get_tv_number_chk(&var2, &error);
4803 if (error)
4804 {
4805 clear_tv(rettv);
4806 clear_tv(&var2);
4807 return FAIL;
4808 }
4809#ifdef FEAT_FLOAT
4810 if (rettv->v_type == VAR_FLOAT)
4811 f2 = n2;
4812#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004813 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004814 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004815
4816#ifdef FEAT_FLOAT
4817 /* If there is a float on either side the result is a float. */
4818 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4819 {
4820 if (op == '+')
4821 f1 = f1 + f2;
4822 else
4823 f1 = f1 - f2;
4824 rettv->v_type = VAR_FLOAT;
4825 rettv->vval.v_float = f1;
4826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004828#endif
4829 {
4830 if (op == '+')
4831 n1 = n1 + n2;
4832 else
4833 n1 = n1 - n2;
4834 rettv->v_type = VAR_NUMBER;
4835 rettv->vval.v_number = n1;
4836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004838 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 }
4840 }
4841 return OK;
4842}
4843
4844/*
4845 * Handle fifth level expression:
4846 * * number multiplication
4847 * / number division
4848 * % number modulo
4849 *
4850 * "arg" must point to the first non-white of the expression.
4851 * "arg" is advanced to the next non-white after the recognized expression.
4852 *
4853 * Return OK or FAIL.
4854 */
4855 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004856eval6(
4857 char_u **arg,
4858 typval_T *rettv,
4859 int evaluate,
4860 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861{
Bram Moolenaar33570922005-01-25 22:26:29 +00004862 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 int op;
4864 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004865#ifdef FEAT_FLOAT
4866 int use_float = FALSE;
4867 float_T f1 = 0, f2;
4868#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004869 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870
4871 /*
4872 * Get the first variable.
4873 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004874 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 return FAIL;
4876
4877 /*
4878 * Repeat computing, until no '*', '/' or '%' is following.
4879 */
4880 for (;;)
4881 {
4882 op = **arg;
4883 if (op != '*' && op != '/' && op != '%')
4884 break;
4885
4886 if (evaluate)
4887 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004888#ifdef FEAT_FLOAT
4889 if (rettv->v_type == VAR_FLOAT)
4890 {
4891 f1 = rettv->vval.v_float;
4892 use_float = TRUE;
4893 n1 = 0;
4894 }
4895 else
4896#endif
4897 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004898 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004899 if (error)
4900 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 }
4902 else
4903 n1 = 0;
4904
4905 /*
4906 * Get the second variable.
4907 */
4908 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004909 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 return FAIL;
4911
4912 if (evaluate)
4913 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004914#ifdef FEAT_FLOAT
4915 if (var2.v_type == VAR_FLOAT)
4916 {
4917 if (!use_float)
4918 {
4919 f1 = n1;
4920 use_float = TRUE;
4921 }
4922 f2 = var2.vval.v_float;
4923 n2 = 0;
4924 }
4925 else
4926#endif
4927 {
4928 n2 = get_tv_number_chk(&var2, &error);
4929 clear_tv(&var2);
4930 if (error)
4931 return FAIL;
4932#ifdef FEAT_FLOAT
4933 if (use_float)
4934 f2 = n2;
4935#endif
4936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937
4938 /*
4939 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004940 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004942#ifdef FEAT_FLOAT
4943 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004945 if (op == '*')
4946 f1 = f1 * f2;
4947 else if (op == '/')
4948 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004949# ifdef VMS
4950 /* VMS crashes on divide by zero, work around it */
4951 if (f2 == 0.0)
4952 {
4953 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004954 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004955 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004956 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004957 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004958 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004959 }
4960 else
4961 f1 = f1 / f2;
4962# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004963 /* We rely on the floating point library to handle divide
4964 * by zero to result in "inf" and not a crash. */
4965 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004966# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004969 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004970 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004971 return FAIL;
4972 }
4973 rettv->v_type = VAR_FLOAT;
4974 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 }
4976 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004979 if (op == '*')
4980 n1 = n1 * n2;
4981 else if (op == '/')
4982 {
4983 if (n2 == 0) /* give an error message? */
4984 {
4985 if (n1 == 0)
4986 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4987 else if (n1 < 0)
4988 n1 = -0x7fffffffL;
4989 else
4990 n1 = 0x7fffffffL;
4991 }
4992 else
4993 n1 = n1 / n2;
4994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004996 {
4997 if (n2 == 0) /* give an error message? */
4998 n1 = 0;
4999 else
5000 n1 = n1 % n2;
5001 }
5002 rettv->v_type = VAR_NUMBER;
5003 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 }
5006 }
5007
5008 return OK;
5009}
5010
5011/*
5012 * Handle sixth level expression:
5013 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005014 * "string" string constant
5015 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 * &option-name option value
5017 * @r register contents
5018 * identifier variable value
5019 * function() function call
5020 * $VAR environment variable
5021 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005022 * [expr, expr] List
5023 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 *
5025 * Also handle:
5026 * ! in front logical NOT
5027 * - in front unary minus
5028 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005029 * trailing [] subscript in String or List
5030 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 *
5032 * "arg" must point to the first non-white of the expression.
5033 * "arg" is advanced to the next non-white after the recognized expression.
5034 *
5035 * Return OK or FAIL.
5036 */
5037 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005038eval7(
5039 char_u **arg,
5040 typval_T *rettv,
5041 int evaluate,
5042 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 long n;
5045 int len;
5046 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 char_u *start_leader, *end_leader;
5048 int ret = OK;
5049 char_u *alias;
5050
5051 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005052 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005053 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005055 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056
5057 /*
5058 * Skip '!' and '-' characters. They are handled later.
5059 */
5060 start_leader = *arg;
5061 while (**arg == '!' || **arg == '-' || **arg == '+')
5062 *arg = skipwhite(*arg + 1);
5063 end_leader = *arg;
5064
5065 switch (**arg)
5066 {
5067 /*
5068 * Number constant.
5069 */
5070 case '0':
5071 case '1':
5072 case '2':
5073 case '3':
5074 case '4':
5075 case '5':
5076 case '6':
5077 case '7':
5078 case '8':
5079 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005080 {
5081#ifdef FEAT_FLOAT
5082 char_u *p = skipdigits(*arg + 1);
5083 int get_float = FALSE;
5084
5085 /* We accept a float when the format matches
5086 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005087 * strict to avoid backwards compatibility problems.
5088 * Don't look for a float after the "." operator, so that
5089 * ":let vers = 1.2.3" doesn't fail. */
5090 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005092 get_float = TRUE;
5093 p = skipdigits(p + 2);
5094 if (*p == 'e' || *p == 'E')
5095 {
5096 ++p;
5097 if (*p == '-' || *p == '+')
5098 ++p;
5099 if (!vim_isdigit(*p))
5100 get_float = FALSE;
5101 else
5102 p = skipdigits(p + 1);
5103 }
5104 if (ASCII_ISALPHA(*p) || *p == '.')
5105 get_float = FALSE;
5106 }
5107 if (get_float)
5108 {
5109 float_T f;
5110
5111 *arg += string2float(*arg, &f);
5112 if (evaluate)
5113 {
5114 rettv->v_type = VAR_FLOAT;
5115 rettv->vval.v_float = f;
5116 }
5117 }
5118 else
5119#endif
5120 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005121 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005122 *arg += len;
5123 if (evaluate)
5124 {
5125 rettv->v_type = VAR_NUMBER;
5126 rettv->vval.v_number = n;
5127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 }
5129 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131
5132 /*
5133 * String constant: "string".
5134 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005135 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 break;
5137
5138 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005139 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005141 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005142 break;
5143
5144 /*
5145 * List: [expr, expr]
5146 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005147 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 break;
5149
5150 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005151 * Dictionary: {key: val, key: val}
5152 */
5153 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5154 break;
5155
5156 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005157 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005159 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 break;
5161
5162 /*
5163 * Environment variable: $VAR.
5164 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005165 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 break;
5167
5168 /*
5169 * Register contents: @r.
5170 */
5171 case '@': ++*arg;
5172 if (evaluate)
5173 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005174 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005175 rettv->vval.v_string = get_reg_contents(**arg,
5176 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 }
5178 if (**arg != NUL)
5179 ++*arg;
5180 break;
5181
5182 /*
5183 * nested expression: (expression).
5184 */
5185 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005186 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 if (**arg == ')')
5188 ++*arg;
5189 else if (ret == OK)
5190 {
5191 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005192 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 ret = FAIL;
5194 }
5195 break;
5196
Bram Moolenaar8c711452005-01-14 21:53:12 +00005197 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 break;
5199 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005200
5201 if (ret == NOTDONE)
5202 {
5203 /*
5204 * Must be a variable or function name.
5205 * Can also be a curly-braces kind of name: {expr}.
5206 */
5207 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005208 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005209 if (alias != NULL)
5210 s = alias;
5211
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005212 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005213 ret = FAIL;
5214 else
5215 {
5216 if (**arg == '(') /* recursive! */
5217 {
5218 /* If "s" is the name of a variable of type VAR_FUNC
5219 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005220 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005221
5222 /* Invoke the function. */
5223 ret = get_func_tv(s, len, rettv, arg,
5224 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005225 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005226
5227 /* If evaluate is FALSE rettv->v_type was not set in
5228 * get_func_tv, but it's needed in handle_subscript() to parse
5229 * what follows. So set it here. */
5230 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5231 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005232 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005233 rettv->v_type = VAR_FUNC;
5234 }
5235
Bram Moolenaar8c711452005-01-14 21:53:12 +00005236 /* Stop the expression evaluation when immediately
5237 * aborting on error, or when an interrupt occurred or
5238 * an exception was thrown but not caught. */
5239 if (aborting())
5240 {
5241 if (ret == OK)
5242 clear_tv(rettv);
5243 ret = FAIL;
5244 }
5245 }
5246 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005247 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005248 else
5249 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005250 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005251 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 }
5253
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 *arg = skipwhite(*arg);
5255
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005256 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5257 * expr(expr). */
5258 if (ret == OK)
5259 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260
5261 /*
5262 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5263 */
5264 if (ret == OK && evaluate && end_leader > start_leader)
5265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005266 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005267 int val = 0;
5268#ifdef FEAT_FLOAT
5269 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005270
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005271 if (rettv->v_type == VAR_FLOAT)
5272 f = rettv->vval.v_float;
5273 else
5274#endif
5275 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005276 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005278 clear_tv(rettv);
5279 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005281 else
5282 {
5283 while (end_leader > start_leader)
5284 {
5285 --end_leader;
5286 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005287 {
5288#ifdef FEAT_FLOAT
5289 if (rettv->v_type == VAR_FLOAT)
5290 f = !f;
5291 else
5292#endif
5293 val = !val;
5294 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005295 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005296 {
5297#ifdef FEAT_FLOAT
5298 if (rettv->v_type == VAR_FLOAT)
5299 f = -f;
5300 else
5301#endif
5302 val = -val;
5303 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005304 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005305#ifdef FEAT_FLOAT
5306 if (rettv->v_type == VAR_FLOAT)
5307 {
5308 clear_tv(rettv);
5309 rettv->vval.v_float = f;
5310 }
5311 else
5312#endif
5313 {
5314 clear_tv(rettv);
5315 rettv->v_type = VAR_NUMBER;
5316 rettv->vval.v_number = val;
5317 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 }
5320
5321 return ret;
5322}
5323
5324/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005325 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5326 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005327 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5328 */
5329 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005330eval_index(
5331 char_u **arg,
5332 typval_T *rettv,
5333 int evaluate,
5334 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335{
5336 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005337 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005338 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005339 long len = -1;
5340 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005341 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005342 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005343
Bram Moolenaara03f2332016-02-06 18:09:59 +01005344 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005346 case VAR_FUNC:
5347 if (verbose)
5348 EMSG(_("E695: Cannot index a Funcref"));
5349 return FAIL;
5350 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005351#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005352 if (verbose)
5353 EMSG(_(e_float_as_string));
5354 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005355#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005356 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005357 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005358 if (verbose)
5359 EMSG(_("E909: Cannot index a special variable"));
5360 return FAIL;
5361 case VAR_UNKNOWN:
5362 if (evaluate)
5363 return FAIL;
5364 /* FALLTHROUGH */
5365
5366 case VAR_STRING:
5367 case VAR_NUMBER:
5368 case VAR_LIST:
5369 case VAR_DICT:
5370 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005371 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005372
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005373 init_tv(&var1);
5374 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005375 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005376 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005377 /*
5378 * dict.name
5379 */
5380 key = *arg + 1;
5381 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5382 ;
5383 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005385 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386 }
5387 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005388 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005389 /*
5390 * something[idx]
5391 *
5392 * Get the (first) variable from inside the [].
5393 */
5394 *arg = skipwhite(*arg + 1);
5395 if (**arg == ':')
5396 empty1 = TRUE;
5397 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5398 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005399 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5400 {
5401 /* not a number or string */
5402 clear_tv(&var1);
5403 return FAIL;
5404 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005405
5406 /*
5407 * Get the second variable from inside the [:].
5408 */
5409 if (**arg == ':')
5410 {
5411 range = TRUE;
5412 *arg = skipwhite(*arg + 1);
5413 if (**arg == ']')
5414 empty2 = TRUE;
5415 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5416 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005417 if (!empty1)
5418 clear_tv(&var1);
5419 return FAIL;
5420 }
5421 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5422 {
5423 /* not a number or string */
5424 if (!empty1)
5425 clear_tv(&var1);
5426 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005427 return FAIL;
5428 }
5429 }
5430
5431 /* Check for the ']'. */
5432 if (**arg != ']')
5433 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005434 if (verbose)
5435 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005436 clear_tv(&var1);
5437 if (range)
5438 clear_tv(&var2);
5439 return FAIL;
5440 }
5441 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442 }
5443
5444 if (evaluate)
5445 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005446 n1 = 0;
5447 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005449 n1 = get_tv_number(&var1);
5450 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005451 }
5452 if (range)
5453 {
5454 if (empty2)
5455 n2 = -1;
5456 else
5457 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005458 n2 = get_tv_number(&var2);
5459 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 }
5461 }
5462
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005463 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005464 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005465 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005466 case VAR_FUNC:
5467 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005468 case VAR_SPECIAL:
5469 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005470 break; /* not evaluating, skipping over subscript */
5471
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005472 case VAR_NUMBER:
5473 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005474 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475 len = (long)STRLEN(s);
5476 if (range)
5477 {
5478 /* The resulting variable is a substring. If the indexes
5479 * are out of range the result is empty. */
5480 if (n1 < 0)
5481 {
5482 n1 = len + n1;
5483 if (n1 < 0)
5484 n1 = 0;
5485 }
5486 if (n2 < 0)
5487 n2 = len + n2;
5488 else if (n2 >= len)
5489 n2 = len;
5490 if (n1 >= len || n2 < 0 || n1 > n2)
5491 s = NULL;
5492 else
5493 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5494 }
5495 else
5496 {
5497 /* The resulting variable is a string of a single
5498 * character. If the index is too big or negative the
5499 * result is empty. */
5500 if (n1 >= len || n1 < 0)
5501 s = NULL;
5502 else
5503 s = vim_strnsave(s + n1, 1);
5504 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005505 clear_tv(rettv);
5506 rettv->v_type = VAR_STRING;
5507 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005508 break;
5509
5510 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005511 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005512 if (n1 < 0)
5513 n1 = len + n1;
5514 if (!empty1 && (n1 < 0 || n1 >= len))
5515 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005516 /* For a range we allow invalid values and return an empty
5517 * list. A list index out of range is an error. */
5518 if (!range)
5519 {
5520 if (verbose)
5521 EMSGN(_(e_listidx), n1);
5522 return FAIL;
5523 }
5524 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525 }
5526 if (range)
5527 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005528 list_T *l;
5529 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530
5531 if (n2 < 0)
5532 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005533 else if (n2 >= len)
5534 n2 = len - 1;
5535 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005536 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005537 l = list_alloc();
5538 if (l == NULL)
5539 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005540 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541 n1 <= n2; ++n1)
5542 {
5543 if (list_append_tv(l, &item->li_tv) == FAIL)
5544 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005545 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005546 return FAIL;
5547 }
5548 item = item->li_next;
5549 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005550 clear_tv(rettv);
5551 rettv->v_type = VAR_LIST;
5552 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005553 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005554 }
5555 else
5556 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005557 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005558 clear_tv(rettv);
5559 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005560 }
5561 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005562
5563 case VAR_DICT:
5564 if (range)
5565 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005566 if (verbose)
5567 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005568 if (len == -1)
5569 clear_tv(&var1);
5570 return FAIL;
5571 }
5572 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005573 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005574
5575 if (len == -1)
5576 {
5577 key = get_tv_string(&var1);
5578 if (*key == NUL)
5579 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005580 if (verbose)
5581 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005582 clear_tv(&var1);
5583 return FAIL;
5584 }
5585 }
5586
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005587 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005588
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005589 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005590 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005591 if (len == -1)
5592 clear_tv(&var1);
5593 if (item == NULL)
5594 return FAIL;
5595
5596 copy_tv(&item->di_tv, &var1);
5597 clear_tv(rettv);
5598 *rettv = var1;
5599 }
5600 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005601 }
5602 }
5603
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005604 return OK;
5605}
5606
5607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 * Get an option value.
5609 * "arg" points to the '&' or '+' before the option name.
5610 * "arg" is advanced to character after the option name.
5611 * Return OK or FAIL.
5612 */
5613 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005614get_option_tv(
5615 char_u **arg,
5616 typval_T *rettv, /* when NULL, only check if option exists */
5617 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618{
5619 char_u *option_end;
5620 long numval;
5621 char_u *stringval;
5622 int opt_type;
5623 int c;
5624 int working = (**arg == '+'); /* has("+option") */
5625 int ret = OK;
5626 int opt_flags;
5627
5628 /*
5629 * Isolate the option name and find its value.
5630 */
5631 option_end = find_option_end(arg, &opt_flags);
5632 if (option_end == NULL)
5633 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005634 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 EMSG2(_("E112: Option name missing: %s"), *arg);
5636 return FAIL;
5637 }
5638
5639 if (!evaluate)
5640 {
5641 *arg = option_end;
5642 return OK;
5643 }
5644
5645 c = *option_end;
5646 *option_end = NUL;
5647 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005648 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649
5650 if (opt_type == -3) /* invalid name */
5651 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005652 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 EMSG2(_("E113: Unknown option: %s"), *arg);
5654 ret = FAIL;
5655 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005656 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 {
5658 if (opt_type == -2) /* hidden string option */
5659 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005660 rettv->v_type = VAR_STRING;
5661 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 }
5663 else if (opt_type == -1) /* hidden number option */
5664 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005665 rettv->v_type = VAR_NUMBER;
5666 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 }
5668 else if (opt_type == 1) /* number option */
5669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005670 rettv->v_type = VAR_NUMBER;
5671 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 }
5673 else /* string option */
5674 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005675 rettv->v_type = VAR_STRING;
5676 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 }
5678 }
5679 else if (working && (opt_type == -2 || opt_type == -1))
5680 ret = FAIL;
5681
5682 *option_end = c; /* put back for error messages */
5683 *arg = option_end;
5684
5685 return ret;
5686}
5687
5688/*
5689 * Allocate a variable for a string constant.
5690 * Return OK or FAIL.
5691 */
5692 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005693get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694{
5695 char_u *p;
5696 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 int extra = 0;
5698
5699 /*
5700 * Find the end of the string, skipping backslashed characters.
5701 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005702 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 {
5704 if (*p == '\\' && p[1] != NUL)
5705 {
5706 ++p;
5707 /* A "\<x>" form occupies at least 4 characters, and produces up
5708 * to 6 characters: reserve space for 2 extra */
5709 if (*p == '<')
5710 extra += 2;
5711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 }
5713
5714 if (*p != '"')
5715 {
5716 EMSG2(_("E114: Missing quote: %s"), *arg);
5717 return FAIL;
5718 }
5719
5720 /* If only parsing, set *arg and return here */
5721 if (!evaluate)
5722 {
5723 *arg = p + 1;
5724 return OK;
5725 }
5726
5727 /*
5728 * Copy the string into allocated memory, handling backslashed
5729 * characters.
5730 */
5731 name = alloc((unsigned)(p - *arg + extra));
5732 if (name == NULL)
5733 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005734 rettv->v_type = VAR_STRING;
5735 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736
Bram Moolenaar8c711452005-01-14 21:53:12 +00005737 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 {
5739 if (*p == '\\')
5740 {
5741 switch (*++p)
5742 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005743 case 'b': *name++ = BS; ++p; break;
5744 case 'e': *name++ = ESC; ++p; break;
5745 case 'f': *name++ = FF; ++p; break;
5746 case 'n': *name++ = NL; ++p; break;
5747 case 'r': *name++ = CAR; ++p; break;
5748 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749
5750 case 'X': /* hex: "\x1", "\x12" */
5751 case 'x':
5752 case 'u': /* Unicode: "\u0023" */
5753 case 'U':
5754 if (vim_isxdigit(p[1]))
5755 {
5756 int n, nr;
5757 int c = toupper(*p);
5758
5759 if (c == 'X')
5760 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005761 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005763 else
5764 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 nr = 0;
5766 while (--n >= 0 && vim_isxdigit(p[1]))
5767 {
5768 ++p;
5769 nr = (nr << 4) + hex2nr(*p);
5770 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005771 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772#ifdef FEAT_MBYTE
5773 /* For "\u" store the number according to
5774 * 'encoding'. */
5775 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005776 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 else
5778#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 break;
5782
5783 /* octal: "\1", "\12", "\123" */
5784 case '0':
5785 case '1':
5786 case '2':
5787 case '3':
5788 case '4':
5789 case '5':
5790 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 case '7': *name = *p++ - '0';
5792 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005794 *name = (*name << 3) + *p++ - '0';
5795 if (*p >= '0' && *p <= '7')
5796 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 break;
5800
5801 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 if (extra != 0)
5804 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 break;
5807 }
5808 /* FALLTHROUGH */
5809
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 break;
5812 }
5813 }
5814 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005815 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005818 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 *arg = p + 1;
5820
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 return OK;
5822}
5823
5824/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005825 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 * Return OK or FAIL.
5827 */
5828 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005829get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830{
5831 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005832 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005833 int reduce = 0;
5834
5835 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005836 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005837 */
5838 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5839 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005840 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005841 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005842 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005843 break;
5844 ++reduce;
5845 ++p;
5846 }
5847 }
5848
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005850 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005852 return FAIL;
5853 }
5854
Bram Moolenaar8c711452005-01-14 21:53:12 +00005855 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005856 if (!evaluate)
5857 {
5858 *arg = p + 1;
5859 return OK;
5860 }
5861
5862 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005863 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005864 */
5865 str = alloc((unsigned)((p - *arg) - reduce));
5866 if (str == NULL)
5867 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 rettv->v_type = VAR_STRING;
5869 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005870
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005872 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005873 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005874 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005875 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005876 break;
5877 ++p;
5878 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005879 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005880 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882 *arg = p + 1;
5883
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005884 return OK;
5885}
5886
5887/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888 * Allocate a variable for a List and fill it from "*arg".
5889 * Return OK or FAIL.
5890 */
5891 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005892get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005893{
Bram Moolenaar33570922005-01-25 22:26:29 +00005894 list_T *l = NULL;
5895 typval_T tv;
5896 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005897
5898 if (evaluate)
5899 {
5900 l = list_alloc();
5901 if (l == NULL)
5902 return FAIL;
5903 }
5904
5905 *arg = skipwhite(*arg + 1);
5906 while (**arg != ']' && **arg != NUL)
5907 {
5908 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5909 goto failret;
5910 if (evaluate)
5911 {
5912 item = listitem_alloc();
5913 if (item != NULL)
5914 {
5915 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005916 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005917 list_append(l, item);
5918 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005919 else
5920 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921 }
5922
5923 if (**arg == ']')
5924 break;
5925 if (**arg != ',')
5926 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005927 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005928 goto failret;
5929 }
5930 *arg = skipwhite(*arg + 1);
5931 }
5932
5933 if (**arg != ']')
5934 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005935 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005936failret:
5937 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005938 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939 return FAIL;
5940 }
5941
5942 *arg = skipwhite(*arg + 1);
5943 if (evaluate)
5944 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005945 rettv->v_type = VAR_LIST;
5946 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947 ++l->lv_refcount;
5948 }
5949
5950 return OK;
5951}
5952
5953/*
5954 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005955 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005957 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005958list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005960 list_T *l;
5961
5962 l = (list_T *)alloc_clear(sizeof(list_T));
5963 if (l != NULL)
5964 {
5965 /* Prepend the list to the list of lists for garbage collection. */
5966 if (first_list != NULL)
5967 first_list->lv_used_prev = l;
5968 l->lv_used_prev = NULL;
5969 l->lv_used_next = first_list;
5970 first_list = l;
5971 }
5972 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973}
5974
5975/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005976 * Allocate an empty list for a return value.
5977 * Returns OK or FAIL.
5978 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005979 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005980rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005981{
5982 list_T *l = list_alloc();
5983
5984 if (l == NULL)
5985 return FAIL;
5986
5987 rettv->vval.v_list = l;
5988 rettv->v_type = VAR_LIST;
5989 ++l->lv_refcount;
5990 return OK;
5991}
5992
5993/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994 * Unreference a list: decrement the reference count and free it when it
5995 * becomes zero.
5996 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005997 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005998list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005999{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006000 if (l != NULL && --l->lv_refcount <= 0)
6001 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006002}
6003
6004/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006005 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006006 * Ignores the reference count.
6007 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006008 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006009list_free(
6010 list_T *l,
6011 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012{
Bram Moolenaar33570922005-01-25 22:26:29 +00006013 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006014
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006015 /* Remove the list from the list of lists for garbage collection. */
6016 if (l->lv_used_prev == NULL)
6017 first_list = l->lv_used_next;
6018 else
6019 l->lv_used_prev->lv_used_next = l->lv_used_next;
6020 if (l->lv_used_next != NULL)
6021 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6022
Bram Moolenaard9fba312005-06-26 22:34:35 +00006023 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006025 /* Remove the item before deleting it. */
6026 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006027 if (recurse || (item->li_tv.v_type != VAR_LIST
6028 && item->li_tv.v_type != VAR_DICT))
6029 clear_tv(&item->li_tv);
6030 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006031 }
6032 vim_free(l);
6033}
6034
6035/*
6036 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006037 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006039 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006040listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006041{
Bram Moolenaar33570922005-01-25 22:26:29 +00006042 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006043}
6044
6045/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006046 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006047 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006048 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006049listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006050{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006051 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006052 vim_free(item);
6053}
6054
6055/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006056 * Remove a list item from a List and free it. Also clears the value.
6057 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006058 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006059listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006060{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006061 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006062 listitem_free(item);
6063}
6064
6065/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006066 * Get the number of items in a list.
6067 */
6068 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006069list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006070{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006071 if (l == NULL)
6072 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006073 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006074}
6075
6076/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006077 * Return TRUE when two lists have exactly the same values.
6078 */
6079 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006080list_equal(
6081 list_T *l1,
6082 list_T *l2,
6083 int ic, /* ignore case for strings */
6084 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006085{
Bram Moolenaar33570922005-01-25 22:26:29 +00006086 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006087
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006088 if (l1 == NULL || l2 == NULL)
6089 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006090 if (l1 == l2)
6091 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006092 if (list_len(l1) != list_len(l2))
6093 return FALSE;
6094
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006095 for (item1 = l1->lv_first, item2 = l2->lv_first;
6096 item1 != NULL && item2 != NULL;
6097 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006098 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006099 return FALSE;
6100 return item1 == NULL && item2 == NULL;
6101}
6102
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006103/*
6104 * Return the dictitem that an entry in a hashtable points to.
6105 */
6106 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006107dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006108{
6109 return HI2DI(hi);
6110}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006111
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006112/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006113 * Return TRUE when two dictionaries have exactly the same key/values.
6114 */
6115 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006116dict_equal(
6117 dict_T *d1,
6118 dict_T *d2,
6119 int ic, /* ignore case for strings */
6120 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006121{
Bram Moolenaar33570922005-01-25 22:26:29 +00006122 hashitem_T *hi;
6123 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006124 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006125
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006126 if (d1 == NULL || d2 == NULL)
6127 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006128 if (d1 == d2)
6129 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006130 if (dict_len(d1) != dict_len(d2))
6131 return FALSE;
6132
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006133 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006134 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006135 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006136 if (!HASHITEM_EMPTY(hi))
6137 {
6138 item2 = dict_find(d2, hi->hi_key, -1);
6139 if (item2 == NULL)
6140 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006141 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006142 return FALSE;
6143 --todo;
6144 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006145 }
6146 return TRUE;
6147}
6148
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006149static int tv_equal_recurse_limit;
6150
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006151/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006152 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006153 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006154 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006155 */
6156 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006157tv_equal(
6158 typval_T *tv1,
6159 typval_T *tv2,
6160 int ic, /* ignore case */
6161 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006162{
6163 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006164 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006165 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006166 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006167
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006168 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006169 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006170
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006171 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006172 * recursiveness to a limit. We guess they are equal then.
6173 * A fixed limit has the problem of still taking an awful long time.
6174 * Reduce the limit every time running into it. That should work fine for
6175 * deeply linked structures that are not recursively linked and catch
6176 * recursiveness quickly. */
6177 if (!recursive)
6178 tv_equal_recurse_limit = 1000;
6179 if (recursive_cnt >= tv_equal_recurse_limit)
6180 {
6181 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006182 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006183 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006184
6185 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006186 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006187 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006188 ++recursive_cnt;
6189 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6190 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006191 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006192
6193 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006194 ++recursive_cnt;
6195 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6196 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006197 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006198
6199 case VAR_FUNC:
6200 return (tv1->vval.v_string != NULL
6201 && tv2->vval.v_string != NULL
6202 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6203
6204 case VAR_NUMBER:
6205 return tv1->vval.v_number == tv2->vval.v_number;
6206
6207 case VAR_STRING:
6208 s1 = get_tv_string_buf(tv1, buf1);
6209 s2 = get_tv_string_buf(tv2, buf2);
6210 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006211
6212 case VAR_SPECIAL:
6213 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006214
6215 case VAR_FLOAT:
6216#ifdef FEAT_FLOAT
6217 return tv1->vval.v_float == tv2->vval.v_float;
6218#endif
6219 case VAR_JOB:
6220#ifdef FEAT_JOB
6221 return tv1->vval.v_job == tv2->vval.v_job;
6222#endif
6223 case VAR_UNKNOWN:
6224 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006225 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006226
Bram Moolenaara03f2332016-02-06 18:09:59 +01006227 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6228 * does not equal anything, not even itself. */
6229 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006230}
6231
6232/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006233 * Locate item with index "n" in list "l" and return it.
6234 * A negative index is counted from the end; -1 is the last item.
6235 * Returns NULL when "n" is out of range.
6236 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006237 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006238list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006239{
Bram Moolenaar33570922005-01-25 22:26:29 +00006240 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006241 long idx;
6242
6243 if (l == NULL)
6244 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006245
6246 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006247 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006248 n = l->lv_len + n;
6249
6250 /* Check for index out of range. */
6251 if (n < 0 || n >= l->lv_len)
6252 return NULL;
6253
6254 /* When there is a cached index may start search from there. */
6255 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006256 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006257 if (n < l->lv_idx / 2)
6258 {
6259 /* closest to the start of the list */
6260 item = l->lv_first;
6261 idx = 0;
6262 }
6263 else if (n > (l->lv_idx + l->lv_len) / 2)
6264 {
6265 /* closest to the end of the list */
6266 item = l->lv_last;
6267 idx = l->lv_len - 1;
6268 }
6269 else
6270 {
6271 /* closest to the cached index */
6272 item = l->lv_idx_item;
6273 idx = l->lv_idx;
6274 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006275 }
6276 else
6277 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006278 if (n < l->lv_len / 2)
6279 {
6280 /* closest to the start of the list */
6281 item = l->lv_first;
6282 idx = 0;
6283 }
6284 else
6285 {
6286 /* closest to the end of the list */
6287 item = l->lv_last;
6288 idx = l->lv_len - 1;
6289 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006290 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006291
6292 while (n > idx)
6293 {
6294 /* search forward */
6295 item = item->li_next;
6296 ++idx;
6297 }
6298 while (n < idx)
6299 {
6300 /* search backward */
6301 item = item->li_prev;
6302 --idx;
6303 }
6304
6305 /* cache the used index */
6306 l->lv_idx = idx;
6307 l->lv_idx_item = item;
6308
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006309 return item;
6310}
6311
6312/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006313 * Get list item "l[idx]" as a number.
6314 */
6315 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006316list_find_nr(
6317 list_T *l,
6318 long idx,
6319 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006320{
6321 listitem_T *li;
6322
6323 li = list_find(l, idx);
6324 if (li == NULL)
6325 {
6326 if (errorp != NULL)
6327 *errorp = TRUE;
6328 return -1L;
6329 }
6330 return get_tv_number_chk(&li->li_tv, errorp);
6331}
6332
6333/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006334 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6335 */
6336 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006337list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006338{
6339 listitem_T *li;
6340
6341 li = list_find(l, idx - 1);
6342 if (li == NULL)
6343 {
6344 EMSGN(_(e_listidx), idx);
6345 return NULL;
6346 }
6347 return get_tv_string(&li->li_tv);
6348}
6349
6350/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006351 * Locate "item" list "l" and return its index.
6352 * Returns -1 when "item" is not in the list.
6353 */
6354 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006355list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006356{
6357 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006358 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006359
6360 if (l == NULL)
6361 return -1;
6362 idx = 0;
6363 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6364 ++idx;
6365 if (li == NULL)
6366 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006367 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006368}
6369
6370/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006371 * Append item "item" to the end of list "l".
6372 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006373 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006374list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006375{
6376 if (l->lv_last == NULL)
6377 {
6378 /* empty list */
6379 l->lv_first = item;
6380 l->lv_last = item;
6381 item->li_prev = NULL;
6382 }
6383 else
6384 {
6385 l->lv_last->li_next = item;
6386 item->li_prev = l->lv_last;
6387 l->lv_last = item;
6388 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006389 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006390 item->li_next = NULL;
6391}
6392
6393/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006394 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006395 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006396 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006397 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006398list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006399{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006400 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006401
Bram Moolenaar05159a02005-02-26 23:04:13 +00006402 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006403 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006404 copy_tv(tv, &li->li_tv);
6405 list_append(l, li);
6406 return OK;
6407}
6408
6409/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006410 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006411 * Return FAIL when out of memory.
6412 */
6413 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006414list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006415{
6416 listitem_T *li = listitem_alloc();
6417
6418 if (li == NULL)
6419 return FAIL;
6420 li->li_tv.v_type = VAR_DICT;
6421 li->li_tv.v_lock = 0;
6422 li->li_tv.vval.v_dict = dict;
6423 list_append(list, li);
6424 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006425 return OK;
6426}
6427
6428/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006429 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006430 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006431 * Returns FAIL when out of memory.
6432 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006433 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006434list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006435{
6436 listitem_T *li = listitem_alloc();
6437
6438 if (li == NULL)
6439 return FAIL;
6440 list_append(l, li);
6441 li->li_tv.v_type = VAR_STRING;
6442 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006443 if (str == NULL)
6444 li->li_tv.vval.v_string = NULL;
6445 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006446 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006447 return FAIL;
6448 return OK;
6449}
6450
6451/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006452 * Append "n" to list "l".
6453 * Returns FAIL when out of memory.
6454 */
6455 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006456list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006457{
6458 listitem_T *li;
6459
6460 li = listitem_alloc();
6461 if (li == NULL)
6462 return FAIL;
6463 li->li_tv.v_type = VAR_NUMBER;
6464 li->li_tv.v_lock = 0;
6465 li->li_tv.vval.v_number = n;
6466 list_append(l, li);
6467 return OK;
6468}
6469
6470/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006471 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006472 * If "item" is NULL append at the end.
6473 * Return FAIL when out of memory.
6474 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006475 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006476list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006477{
Bram Moolenaar33570922005-01-25 22:26:29 +00006478 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006479
6480 if (ni == NULL)
6481 return FAIL;
6482 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006483 list_insert(l, ni, item);
6484 return OK;
6485}
6486
6487 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006488list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006489{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006490 if (item == NULL)
6491 /* Append new item at end of list. */
6492 list_append(l, ni);
6493 else
6494 {
6495 /* Insert new item before existing item. */
6496 ni->li_prev = item->li_prev;
6497 ni->li_next = item;
6498 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006499 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006500 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006501 ++l->lv_idx;
6502 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006503 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006504 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006505 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006506 l->lv_idx_item = NULL;
6507 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006508 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006509 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006510 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006511}
6512
6513/*
6514 * Extend "l1" with "l2".
6515 * If "bef" is NULL append at the end, otherwise insert before this item.
6516 * Returns FAIL when out of memory.
6517 */
6518 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006519list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006520{
Bram Moolenaar33570922005-01-25 22:26:29 +00006521 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006522 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006523
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006524 /* We also quit the loop when we have inserted the original item count of
6525 * the list, avoid a hang when we extend a list with itself. */
6526 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6528 return FAIL;
6529 return OK;
6530}
6531
6532/*
6533 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6534 * Return FAIL when out of memory.
6535 */
6536 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006537list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006538{
Bram Moolenaar33570922005-01-25 22:26:29 +00006539 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006540
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006541 if (l1 == NULL || l2 == NULL)
6542 return FAIL;
6543
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006544 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006545 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546 if (l == NULL)
6547 return FAIL;
6548 tv->v_type = VAR_LIST;
6549 tv->vval.v_list = l;
6550
6551 /* append all items from the second list */
6552 return list_extend(l, l2, NULL);
6553}
6554
6555/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006556 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006557 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006558 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006559 * Returns NULL when out of memory.
6560 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006561 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006562list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006563{
Bram Moolenaar33570922005-01-25 22:26:29 +00006564 list_T *copy;
6565 listitem_T *item;
6566 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006567
6568 if (orig == NULL)
6569 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006570
6571 copy = list_alloc();
6572 if (copy != NULL)
6573 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006574 if (copyID != 0)
6575 {
6576 /* Do this before adding the items, because one of the items may
6577 * refer back to this list. */
6578 orig->lv_copyID = copyID;
6579 orig->lv_copylist = copy;
6580 }
6581 for (item = orig->lv_first; item != NULL && !got_int;
6582 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006583 {
6584 ni = listitem_alloc();
6585 if (ni == NULL)
6586 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006587 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006588 {
6589 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6590 {
6591 vim_free(ni);
6592 break;
6593 }
6594 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006595 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006596 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006597 list_append(copy, ni);
6598 }
6599 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006600 if (item != NULL)
6601 {
6602 list_unref(copy);
6603 copy = NULL;
6604 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006605 }
6606
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006607 return copy;
6608}
6609
6610/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006611 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006612 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006613 * This used to be called list_remove, but that conflicts with a Sun header
6614 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006615 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006616 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006617vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006618{
Bram Moolenaar33570922005-01-25 22:26:29 +00006619 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006620
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006621 /* notify watchers */
6622 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006623 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006624 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006625 list_fix_watch(l, ip);
6626 if (ip == item2)
6627 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006628 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006629
6630 if (item2->li_next == NULL)
6631 l->lv_last = item->li_prev;
6632 else
6633 item2->li_next->li_prev = item->li_prev;
6634 if (item->li_prev == NULL)
6635 l->lv_first = item2->li_next;
6636 else
6637 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006638 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006639}
6640
6641/*
6642 * Return an allocated string with the string representation of a list.
6643 * May return NULL.
6644 */
6645 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006646list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006647{
6648 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006649
6650 if (tv->vval.v_list == NULL)
6651 return NULL;
6652 ga_init2(&ga, (int)sizeof(char), 80);
6653 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006654 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006655 {
6656 vim_free(ga.ga_data);
6657 return NULL;
6658 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006659 ga_append(&ga, ']');
6660 ga_append(&ga, NUL);
6661 return (char_u *)ga.ga_data;
6662}
6663
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006664typedef struct join_S {
6665 char_u *s;
6666 char_u *tofree;
6667} join_T;
6668
6669 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006670list_join_inner(
6671 garray_T *gap, /* to store the result in */
6672 list_T *l,
6673 char_u *sep,
6674 int echo_style,
6675 int copyID,
6676 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006677{
6678 int i;
6679 join_T *p;
6680 int len;
6681 int sumlen = 0;
6682 int first = TRUE;
6683 char_u *tofree;
6684 char_u numbuf[NUMBUFLEN];
6685 listitem_T *item;
6686 char_u *s;
6687
6688 /* Stringify each item in the list. */
6689 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6690 {
6691 if (echo_style)
6692 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6693 else
6694 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6695 if (s == NULL)
6696 return FAIL;
6697
6698 len = (int)STRLEN(s);
6699 sumlen += len;
6700
Bram Moolenaarcde88542015-08-11 19:14:00 +02006701 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006702 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6703 if (tofree != NULL || s != numbuf)
6704 {
6705 p->s = s;
6706 p->tofree = tofree;
6707 }
6708 else
6709 {
6710 p->s = vim_strnsave(s, len);
6711 p->tofree = p->s;
6712 }
6713
6714 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006715 if (did_echo_string_emsg) /* recursion error, bail out */
6716 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006717 }
6718
6719 /* Allocate result buffer with its total size, avoid re-allocation and
6720 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6721 if (join_gap->ga_len >= 2)
6722 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6723 if (ga_grow(gap, sumlen + 2) == FAIL)
6724 return FAIL;
6725
6726 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6727 {
6728 if (first)
6729 first = FALSE;
6730 else
6731 ga_concat(gap, sep);
6732 p = ((join_T *)join_gap->ga_data) + i;
6733
6734 if (p->s != NULL)
6735 ga_concat(gap, p->s);
6736 line_breakcheck();
6737 }
6738
6739 return OK;
6740}
6741
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006742/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006743 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006744 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006745 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006746 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006747 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006748list_join(
6749 garray_T *gap,
6750 list_T *l,
6751 char_u *sep,
6752 int echo_style,
6753 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006754{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006755 garray_T join_ga;
6756 int retval;
6757 join_T *p;
6758 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006759
Bram Moolenaard39a7512015-04-16 22:51:22 +02006760 if (l->lv_len < 1)
6761 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006762 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6763 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6764
6765 /* Dispose each item in join_ga. */
6766 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006767 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006768 p = (join_T *)join_ga.ga_data;
6769 for (i = 0; i < join_ga.ga_len; ++i)
6770 {
6771 vim_free(p->tofree);
6772 ++p;
6773 }
6774 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006775 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006776
6777 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006778}
6779
6780/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006781 * Return the next (unique) copy ID.
6782 * Used for serializing nested structures.
6783 */
6784 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006785get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006786{
6787 current_copyID += COPYID_INC;
6788 return current_copyID;
6789}
6790
6791/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006792 * Garbage collection for lists and dictionaries.
6793 *
6794 * We use reference counts to be able to free most items right away when they
6795 * are no longer used. But for composite items it's possible that it becomes
6796 * unused while the reference count is > 0: When there is a recursive
6797 * reference. Example:
6798 * :let l = [1, 2, 3]
6799 * :let d = {9: l}
6800 * :let l[1] = d
6801 *
6802 * Since this is quite unusual we handle this with garbage collection: every
6803 * once in a while find out which lists and dicts are not referenced from any
6804 * variable.
6805 *
6806 * Here is a good reference text about garbage collection (refers to Python
6807 * but it applies to all reference-counting mechanisms):
6808 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006809 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006810
6811/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006812 * Do garbage collection for lists and dicts.
6813 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006814 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006815 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006816garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006817{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006818 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006819 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006820 buf_T *buf;
6821 win_T *wp;
6822 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006823 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006824 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006825 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006826#ifdef FEAT_WINDOWS
6827 tabpage_T *tp;
6828#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006829
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006830 /* Only do this once. */
6831 want_garbage_collect = FALSE;
6832 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006833 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006834
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006835 /* We advance by two because we add one for items referenced through
6836 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006837 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006838
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006839 /*
6840 * 1. Go through all accessible variables and mark all lists and dicts
6841 * with copyID.
6842 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006843
6844 /* Don't free variables in the previous_funccal list unless they are only
6845 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006846 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006847 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6848 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006849 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6850 NULL);
6851 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6852 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006853 }
6854
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006855 /* script-local variables */
6856 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006857 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006858
6859 /* buffer-local variables */
6860 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006861 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6862 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006863
6864 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006865 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006866 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6867 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006868#ifdef FEAT_AUTOCMD
6869 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006870 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6871 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006872#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006873
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006874#ifdef FEAT_WINDOWS
6875 /* tabpage-local variables */
6876 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006877 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6878 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006879#endif
6880
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006881 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006882 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006883
6884 /* function-local variables */
6885 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6886 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006887 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6888 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006889 }
6890
Bram Moolenaard812df62008-11-09 12:46:09 +00006891 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006892 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006893
Bram Moolenaar1dced572012-04-05 16:54:08 +02006894#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006895 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006896#endif
6897
Bram Moolenaardb913952012-06-29 12:54:53 +02006898#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006899 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006900#endif
6901
6902#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006903 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006904#endif
6905
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006906#ifdef FEAT_CHANNEL
6907 abort = abort || set_ref_in_channel(copyID);
6908#endif
6909
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006910 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006911 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006912 /*
6913 * 2. Free lists and dictionaries that are not referenced.
6914 */
6915 did_free = free_unref_items(copyID);
6916
6917 /*
6918 * 3. Check if any funccal can be freed now.
6919 */
6920 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006921 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006922 if (can_free_funccal(*pfc, copyID))
6923 {
6924 fc = *pfc;
6925 *pfc = fc->caller;
6926 free_funccal(fc, TRUE);
6927 did_free = TRUE;
6928 did_free_funccal = TRUE;
6929 }
6930 else
6931 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006932 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006933 if (did_free_funccal)
6934 /* When a funccal was freed some more items might be garbage
6935 * collected, so run again. */
6936 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006937 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006938 else if (p_verbose > 0)
6939 {
6940 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6941 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006942
6943 return did_free;
6944}
6945
6946/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006947 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006948 */
6949 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006950free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006951{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006952 dict_T *dd, *dd_next;
6953 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006954 int did_free = FALSE;
6955
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006956 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006957 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006958 */
6959 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006960 {
6961 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006962 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006963 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006964 /* Free the Dictionary and ordinary items it contains, but don't
6965 * recurse into Lists and Dictionaries, they will be in the list
6966 * of dicts or list of lists. */
6967 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006968 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006969 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006970 dd = dd_next;
6971 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006972
6973 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006974 * Go through the list of lists and free items without the copyID.
6975 * But don't free a list that has a watcher (used in a for loop), these
6976 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006977 */
6978 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006979 {
6980 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006981 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6982 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006983 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006984 /* Free the List and ordinary items it contains, but don't recurse
6985 * into Lists and Dictionaries, they will be in the list of dicts
6986 * or list of lists. */
6987 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006989 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006990 ll = ll_next;
6991 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01006992
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993 return did_free;
6994}
6995
6996/*
6997 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006998 * "list_stack" is used to add lists to be marked. Can be NULL.
6999 *
7000 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007001 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007002 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007003set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007004{
7005 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007006 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007007 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007008 hashtab_T *cur_ht;
7009 ht_stack_T *ht_stack = NULL;
7010 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007012 cur_ht = ht;
7013 for (;;)
7014 {
7015 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007017 /* Mark each item in the hashtab. If the item contains a hashtab
7018 * it is added to ht_stack, if it contains a list it is added to
7019 * list_stack. */
7020 todo = (int)cur_ht->ht_used;
7021 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7022 if (!HASHITEM_EMPTY(hi))
7023 {
7024 --todo;
7025 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7026 &ht_stack, list_stack);
7027 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007028 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007029
7030 if (ht_stack == NULL)
7031 break;
7032
7033 /* take an item from the stack */
7034 cur_ht = ht_stack->ht;
7035 tempitem = ht_stack;
7036 ht_stack = ht_stack->prev;
7037 free(tempitem);
7038 }
7039
7040 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007041}
7042
7043/*
7044 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007045 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7046 *
7047 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007048 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007049 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007050set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007051{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007052 listitem_T *li;
7053 int abort = FALSE;
7054 list_T *cur_l;
7055 list_stack_T *list_stack = NULL;
7056 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007057
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007058 cur_l = l;
7059 for (;;)
7060 {
7061 if (!abort)
7062 /* Mark each item in the list. If the item contains a hashtab
7063 * it is added to ht_stack, if it contains a list it is added to
7064 * list_stack. */
7065 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7066 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7067 ht_stack, &list_stack);
7068 if (list_stack == NULL)
7069 break;
7070
7071 /* take an item from the stack */
7072 cur_l = list_stack->list;
7073 tempitem = list_stack;
7074 list_stack = list_stack->prev;
7075 free(tempitem);
7076 }
7077
7078 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007079}
7080
7081/*
7082 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007083 * "list_stack" is used to add lists to be marked. Can be NULL.
7084 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7085 *
7086 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007087 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007088 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007089set_ref_in_item(
7090 typval_T *tv,
7091 int copyID,
7092 ht_stack_T **ht_stack,
7093 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007094{
7095 dict_T *dd;
7096 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007097 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007098
Bram Moolenaara03f2332016-02-06 18:09:59 +01007099 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007100 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007101 dd = tv->vval.v_dict;
7102 if (dd != NULL && dd->dv_copyID != copyID)
7103 {
7104 /* Didn't see this dict yet. */
7105 dd->dv_copyID = copyID;
7106 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007107 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007108 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7109 }
7110 else
7111 {
7112 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7113 if (newitem == NULL)
7114 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007115 else
7116 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007117 newitem->ht = &dd->dv_hashtab;
7118 newitem->prev = *ht_stack;
7119 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007120 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007121 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007122 }
7123 }
7124 else if (tv->v_type == VAR_LIST)
7125 {
7126 ll = tv->vval.v_list;
7127 if (ll != NULL && ll->lv_copyID != copyID)
7128 {
7129 /* Didn't see this list yet. */
7130 ll->lv_copyID = copyID;
7131 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007132 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007133 abort = set_ref_in_list(ll, copyID, ht_stack);
7134 }
7135 else
7136 {
7137 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007138 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007139 if (newitem == NULL)
7140 abort = TRUE;
7141 else
7142 {
7143 newitem->list = ll;
7144 newitem->prev = *list_stack;
7145 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007146 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007147 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007148 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007149 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007150 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007151}
7152
7153/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007154 * Allocate an empty header for a dictionary.
7155 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007156 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007157dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007158{
Bram Moolenaar33570922005-01-25 22:26:29 +00007159 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007160
Bram Moolenaar33570922005-01-25 22:26:29 +00007161 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007162 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007163 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007164 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007165 if (first_dict != NULL)
7166 first_dict->dv_used_prev = d;
7167 d->dv_used_next = first_dict;
7168 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007169 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007170
Bram Moolenaar33570922005-01-25 22:26:29 +00007171 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007172 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007173 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007174 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007175 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007176 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007177 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007178}
7179
7180/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007181 * Allocate an empty dict for a return value.
7182 * Returns OK or FAIL.
7183 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007184 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007185rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007186{
7187 dict_T *d = dict_alloc();
7188
7189 if (d == NULL)
7190 return FAIL;
7191
7192 rettv->vval.v_dict = d;
7193 rettv->v_type = VAR_DICT;
7194 ++d->dv_refcount;
7195 return OK;
7196}
7197
7198
7199/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007200 * Unreference a Dictionary: decrement the reference count and free it when it
7201 * becomes zero.
7202 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007203 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007204dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007205{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007206 if (d != NULL && --d->dv_refcount <= 0)
7207 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007208}
7209
7210/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007211 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007212 * Ignores the reference count.
7213 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007214 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007215dict_free(
7216 dict_T *d,
7217 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007218{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007219 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007220 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007221 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007223 /* Remove the dict from the list of dicts for garbage collection. */
7224 if (d->dv_used_prev == NULL)
7225 first_dict = d->dv_used_next;
7226 else
7227 d->dv_used_prev->dv_used_next = d->dv_used_next;
7228 if (d->dv_used_next != NULL)
7229 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7230
7231 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007232 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007233 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007234 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007235 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007236 if (!HASHITEM_EMPTY(hi))
7237 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007238 /* Remove the item before deleting it, just in case there is
7239 * something recursive causing trouble. */
7240 di = HI2DI(hi);
7241 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007242 if (recurse || (di->di_tv.v_type != VAR_LIST
7243 && di->di_tv.v_type != VAR_DICT))
7244 clear_tv(&di->di_tv);
7245 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007246 --todo;
7247 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007248 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007249 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250 vim_free(d);
7251}
7252
7253/*
7254 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007255 * The "key" is copied to the new item.
7256 * Note that the value of the item "di_tv" still needs to be initialized!
7257 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007258 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007259 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007260dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007261{
Bram Moolenaar33570922005-01-25 22:26:29 +00007262 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007263
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007264 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007265 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007266 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007267 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007268 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007269 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007270 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007271}
7272
7273/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007274 * Make a copy of a Dictionary item.
7275 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007276 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007277dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007278{
Bram Moolenaar33570922005-01-25 22:26:29 +00007279 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007280
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007281 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7282 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007283 if (di != NULL)
7284 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007285 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007286 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007287 copy_tv(&org->di_tv, &di->di_tv);
7288 }
7289 return di;
7290}
7291
7292/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007293 * Remove item "item" from Dictionary "dict" and free it.
7294 */
7295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007296dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007297{
Bram Moolenaar33570922005-01-25 22:26:29 +00007298 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007299
Bram Moolenaar33570922005-01-25 22:26:29 +00007300 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007301 if (HASHITEM_EMPTY(hi))
7302 EMSG2(_(e_intern2), "dictitem_remove()");
7303 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007304 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007305 dictitem_free(item);
7306}
7307
7308/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007309 * Free a dict item. Also clears the value.
7310 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007311 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007312dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007313{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007314 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007315 if (item->di_flags & DI_FLAGS_ALLOC)
7316 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007317}
7318
7319/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007320 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7321 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007322 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007323 * Returns NULL when out of memory.
7324 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007325 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007326dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007327{
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 dict_T *copy;
7329 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007330 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007331 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007332
7333 if (orig == NULL)
7334 return NULL;
7335
7336 copy = dict_alloc();
7337 if (copy != NULL)
7338 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007339 if (copyID != 0)
7340 {
7341 orig->dv_copyID = copyID;
7342 orig->dv_copydict = copy;
7343 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007344 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007345 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007346 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007347 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007348 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007349 --todo;
7350
7351 di = dictitem_alloc(hi->hi_key);
7352 if (di == NULL)
7353 break;
7354 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007355 {
7356 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7357 copyID) == FAIL)
7358 {
7359 vim_free(di);
7360 break;
7361 }
7362 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007363 else
7364 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7365 if (dict_add(copy, di) == FAIL)
7366 {
7367 dictitem_free(di);
7368 break;
7369 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007370 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007371 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007372
Bram Moolenaare9a41262005-01-15 22:18:47 +00007373 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007374 if (todo > 0)
7375 {
7376 dict_unref(copy);
7377 copy = NULL;
7378 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007379 }
7380
7381 return copy;
7382}
7383
7384/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007385 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007386 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007387 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007388 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007389dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007390{
Bram Moolenaar33570922005-01-25 22:26:29 +00007391 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007392}
7393
Bram Moolenaar8c711452005-01-14 21:53:12 +00007394/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007395 * Add a number or string entry to dictionary "d".
7396 * When "str" is NULL use number "nr", otherwise use "str".
7397 * Returns FAIL when out of memory and when key already exists.
7398 */
7399 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007400dict_add_nr_str(
7401 dict_T *d,
7402 char *key,
7403 long nr,
7404 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007405{
7406 dictitem_T *item;
7407
7408 item = dictitem_alloc((char_u *)key);
7409 if (item == NULL)
7410 return FAIL;
7411 item->di_tv.v_lock = 0;
7412 if (str == NULL)
7413 {
7414 item->di_tv.v_type = VAR_NUMBER;
7415 item->di_tv.vval.v_number = nr;
7416 }
7417 else
7418 {
7419 item->di_tv.v_type = VAR_STRING;
7420 item->di_tv.vval.v_string = vim_strsave(str);
7421 }
7422 if (dict_add(d, item) == FAIL)
7423 {
7424 dictitem_free(item);
7425 return FAIL;
7426 }
7427 return OK;
7428}
7429
7430/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007431 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007432 * Returns FAIL when out of memory and when key already exists.
7433 */
7434 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007435dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007436{
7437 dictitem_T *item;
7438
7439 item = dictitem_alloc((char_u *)key);
7440 if (item == NULL)
7441 return FAIL;
7442 item->di_tv.v_lock = 0;
7443 item->di_tv.v_type = VAR_LIST;
7444 item->di_tv.vval.v_list = list;
7445 if (dict_add(d, item) == FAIL)
7446 {
7447 dictitem_free(item);
7448 return FAIL;
7449 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007450 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007451 return OK;
7452}
7453
7454/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007455 * Get the number of items in a Dictionary.
7456 */
7457 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007458dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007459{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007460 if (d == NULL)
7461 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007462 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007463}
7464
7465/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007466 * Find item "key[len]" in Dictionary "d".
7467 * If "len" is negative use strlen(key).
7468 * Returns NULL when not found.
7469 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007470 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007471dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007472{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007473#define AKEYLEN 200
7474 char_u buf[AKEYLEN];
7475 char_u *akey;
7476 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007477 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007478
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007479 if (len < 0)
7480 akey = key;
7481 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007482 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007483 tofree = akey = vim_strnsave(key, len);
7484 if (akey == NULL)
7485 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007486 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007487 else
7488 {
7489 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007490 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007491 akey = buf;
7492 }
7493
Bram Moolenaar33570922005-01-25 22:26:29 +00007494 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007495 vim_free(tofree);
7496 if (HASHITEM_EMPTY(hi))
7497 return NULL;
7498 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007499}
7500
7501/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007502 * Get a string item from a dictionary.
7503 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007504 * Returns NULL if the entry doesn't exist or out of memory.
7505 */
7506 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007507get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007508{
7509 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007510 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007511
7512 di = dict_find(d, key, -1);
7513 if (di == NULL)
7514 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007515 s = get_tv_string(&di->di_tv);
7516 if (save && s != NULL)
7517 s = vim_strsave(s);
7518 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007519}
7520
7521/*
7522 * Get a number item from a dictionary.
7523 * Returns 0 if the entry doesn't exist or out of memory.
7524 */
7525 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007526get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007527{
7528 dictitem_T *di;
7529
7530 di = dict_find(d, key, -1);
7531 if (di == NULL)
7532 return 0;
7533 return get_tv_number(&di->di_tv);
7534}
7535
7536/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537 * Return an allocated string with the string representation of a Dictionary.
7538 * May return NULL.
7539 */
7540 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007541dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007542{
7543 garray_T ga;
7544 int first = TRUE;
7545 char_u *tofree;
7546 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007547 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007548 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007549 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007550 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007551
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007552 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007553 return NULL;
7554 ga_init2(&ga, (int)sizeof(char), 80);
7555 ga_append(&ga, '{');
7556
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007557 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007558 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007559 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007560 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007561 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007562 --todo;
7563
7564 if (first)
7565 first = FALSE;
7566 else
7567 ga_concat(&ga, (char_u *)", ");
7568
7569 tofree = string_quote(hi->hi_key, FALSE);
7570 if (tofree != NULL)
7571 {
7572 ga_concat(&ga, tofree);
7573 vim_free(tofree);
7574 }
7575 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007576 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007577 if (s != NULL)
7578 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007579 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007580 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007581 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007582 line_breakcheck();
7583
Bram Moolenaar8c711452005-01-14 21:53:12 +00007584 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007585 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007586 if (todo > 0)
7587 {
7588 vim_free(ga.ga_data);
7589 return NULL;
7590 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007591
7592 ga_append(&ga, '}');
7593 ga_append(&ga, NUL);
7594 return (char_u *)ga.ga_data;
7595}
7596
7597/*
7598 * Allocate a variable for a Dictionary and fill it from "*arg".
7599 * Return OK or FAIL. Returns NOTDONE for {expr}.
7600 */
7601 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007602get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007603{
Bram Moolenaar33570922005-01-25 22:26:29 +00007604 dict_T *d = NULL;
7605 typval_T tvkey;
7606 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007607 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007608 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007609 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007610 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007611
7612 /*
7613 * First check if it's not a curly-braces thing: {expr}.
7614 * Must do this without evaluating, otherwise a function may be called
7615 * twice. Unfortunately this means we need to call eval1() twice for the
7616 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007617 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007618 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007619 if (*start != '}')
7620 {
7621 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7622 return FAIL;
7623 if (*start == '}')
7624 return NOTDONE;
7625 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007626
7627 if (evaluate)
7628 {
7629 d = dict_alloc();
7630 if (d == NULL)
7631 return FAIL;
7632 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007633 tvkey.v_type = VAR_UNKNOWN;
7634 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007635
7636 *arg = skipwhite(*arg + 1);
7637 while (**arg != '}' && **arg != NUL)
7638 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007639 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007640 goto failret;
7641 if (**arg != ':')
7642 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007643 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007644 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007645 goto failret;
7646 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007647 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007648 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007649 key = get_tv_string_buf_chk(&tvkey, buf);
7650 if (key == NULL || *key == NUL)
7651 {
7652 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7653 if (key != NULL)
7654 EMSG(_(e_emptykey));
7655 clear_tv(&tvkey);
7656 goto failret;
7657 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007659
7660 *arg = skipwhite(*arg + 1);
7661 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7662 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007663 if (evaluate)
7664 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665 goto failret;
7666 }
7667 if (evaluate)
7668 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007669 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007670 if (item != NULL)
7671 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007672 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007673 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007674 clear_tv(&tv);
7675 goto failret;
7676 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007677 item = dictitem_alloc(key);
7678 clear_tv(&tvkey);
7679 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007682 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007683 if (dict_add(d, item) == FAIL)
7684 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007685 }
7686 }
7687
7688 if (**arg == '}')
7689 break;
7690 if (**arg != ',')
7691 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007692 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007693 goto failret;
7694 }
7695 *arg = skipwhite(*arg + 1);
7696 }
7697
7698 if (**arg != '}')
7699 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007700 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007701failret:
7702 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007703 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007704 return FAIL;
7705 }
7706
7707 *arg = skipwhite(*arg + 1);
7708 if (evaluate)
7709 {
7710 rettv->v_type = VAR_DICT;
7711 rettv->vval.v_dict = d;
7712 ++d->dv_refcount;
7713 }
7714
7715 return OK;
7716}
7717
Bram Moolenaar835dc632016-02-07 14:27:38 +01007718#ifdef FEAT_JOB
7719 static void
7720job_free(job_T *job)
7721{
7722 /* TODO: free any handles */
7723
7724 vim_free(job);
7725}
7726
7727 static void
7728job_unref(job_T *job)
7729{
7730 if (job != NULL && --job->jv_refcount <= 0)
7731 job_free(job);
7732}
7733
7734/*
7735 * Allocate a job. Sets the refcount to one.
7736 */
7737 static job_T *
7738job_alloc(void)
7739{
7740 job_T *job;
7741
7742 job = (job_T *)alloc_clear(sizeof(job_T));
7743 if (job != NULL)
7744 {
7745 job->jv_refcount = 1;
7746 }
7747 return job;
7748}
7749
7750#endif
7751
Bram Moolenaar17a13432016-01-24 14:22:10 +01007752 static char *
7753get_var_special_name(int nr)
7754{
7755 switch (nr)
7756 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007757 case VVAL_FALSE: return "v:false";
7758 case VVAL_TRUE: return "v:true";
7759 case VVAL_NONE: return "v:none";
7760 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007761 }
7762 EMSG2(_(e_intern2), "get_var_special_name()");
7763 return "42";
7764}
7765
Bram Moolenaar8c711452005-01-14 21:53:12 +00007766/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007767 * Return a string with the string representation of a variable.
7768 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007769 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007770 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007771 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007772 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007773 */
7774 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007775echo_string(
7776 typval_T *tv,
7777 char_u **tofree,
7778 char_u *numbuf,
7779 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007780{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007781 static int recurse = 0;
7782 char_u *r = NULL;
7783
Bram Moolenaar33570922005-01-25 22:26:29 +00007784 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007785 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007786 if (!did_echo_string_emsg)
7787 {
7788 /* Only give this message once for a recursive call to avoid
7789 * flooding the user with errors. And stop iterating over lists
7790 * and dicts. */
7791 did_echo_string_emsg = TRUE;
7792 EMSG(_("E724: variable nested too deep for displaying"));
7793 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007794 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007795 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007796 }
7797 ++recurse;
7798
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007799 switch (tv->v_type)
7800 {
7801 case VAR_FUNC:
7802 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007803 r = tv->vval.v_string;
7804 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007805
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007806 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007807 if (tv->vval.v_list == NULL)
7808 {
7809 *tofree = NULL;
7810 r = NULL;
7811 }
7812 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7813 {
7814 *tofree = NULL;
7815 r = (char_u *)"[...]";
7816 }
7817 else
7818 {
7819 tv->vval.v_list->lv_copyID = copyID;
7820 *tofree = list2string(tv, copyID);
7821 r = *tofree;
7822 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007823 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007824
Bram Moolenaar8c711452005-01-14 21:53:12 +00007825 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007826 if (tv->vval.v_dict == NULL)
7827 {
7828 *tofree = NULL;
7829 r = NULL;
7830 }
7831 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7832 {
7833 *tofree = NULL;
7834 r = (char_u *)"{...}";
7835 }
7836 else
7837 {
7838 tv->vval.v_dict->dv_copyID = copyID;
7839 *tofree = dict2string(tv, copyID);
7840 r = *tofree;
7841 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007842 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007843
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007844 case VAR_STRING:
7845 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007846 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007847 case VAR_JOB:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007848 *tofree = NULL;
7849 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007850 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007851
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007852 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007853#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007854 *tofree = NULL;
7855 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7856 r = numbuf;
7857 break;
7858#endif
7859
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007860 case VAR_SPECIAL:
7861 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007862 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007863 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007864 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007865
Bram Moolenaar8502c702014-06-17 12:51:16 +02007866 if (--recurse == 0)
7867 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007868 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007869}
7870
7871/*
7872 * Return a string with the string representation of a variable.
7873 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7874 * "numbuf" is used for a number.
7875 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007876 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007877 */
7878 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007879tv2string(
7880 typval_T *tv,
7881 char_u **tofree,
7882 char_u *numbuf,
7883 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007884{
7885 switch (tv->v_type)
7886 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007887 case VAR_FUNC:
7888 *tofree = string_quote(tv->vval.v_string, TRUE);
7889 return *tofree;
7890 case VAR_STRING:
7891 *tofree = string_quote(tv->vval.v_string, FALSE);
7892 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007893#ifdef FEAT_FLOAT
7894 case VAR_FLOAT:
7895 *tofree = NULL;
7896 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7897 return numbuf;
7898#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007899 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007900 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007901 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007902 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007903 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007904 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007905 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007906 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007907 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007908}
7909
7910/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007911 * Return string "str" in ' quotes, doubling ' characters.
7912 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007913 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007914 */
7915 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007916string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007917{
Bram Moolenaar33570922005-01-25 22:26:29 +00007918 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007919 char_u *p, *r, *s;
7920
Bram Moolenaar33570922005-01-25 22:26:29 +00007921 len = (function ? 13 : 3);
7922 if (str != NULL)
7923 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007924 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007925 for (p = str; *p != NUL; mb_ptr_adv(p))
7926 if (*p == '\'')
7927 ++len;
7928 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007929 s = r = alloc(len);
7930 if (r != NULL)
7931 {
7932 if (function)
7933 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007934 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007935 r += 10;
7936 }
7937 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007938 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007939 if (str != NULL)
7940 for (p = str; *p != NUL; )
7941 {
7942 if (*p == '\'')
7943 *r++ = '\'';
7944 MB_COPY_CHAR(p, r);
7945 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007946 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007947 if (function)
7948 *r++ = ')';
7949 *r++ = NUL;
7950 }
7951 return s;
7952}
7953
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007954#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007955/*
7956 * Convert the string "text" to a floating point number.
7957 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7958 * this always uses a decimal point.
7959 * Returns the length of the text that was consumed.
7960 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007961 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007962string2float(
7963 char_u *text,
7964 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007965{
7966 char *s = (char *)text;
7967 float_T f;
7968
7969 f = strtod(s, &s);
7970 *value = f;
7971 return (int)((char_u *)s - text);
7972}
7973#endif
7974
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007975/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 * Get the value of an environment variable.
7977 * "arg" is pointing to the '$'. It is advanced to after the name.
7978 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007979 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 */
7981 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007982get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983{
7984 char_u *string = NULL;
7985 int len;
7986 int cc;
7987 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007988 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989
7990 ++*arg;
7991 name = *arg;
7992 len = get_env_len(arg);
7993 if (evaluate)
7994 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007995 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01007996 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007997
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007998 cc = name[len];
7999 name[len] = NUL;
8000 /* first try vim_getenv(), fast for normal environment vars */
8001 string = vim_getenv(name, &mustfree);
8002 if (string != NULL && *string != NUL)
8003 {
8004 if (!mustfree)
8005 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008007 else
8008 {
8009 if (mustfree)
8010 vim_free(string);
8011
8012 /* next try expanding things like $VIM and ${HOME} */
8013 string = expand_env_save(name - 1);
8014 if (string != NULL && *string == '$')
8015 {
8016 vim_free(string);
8017 string = NULL;
8018 }
8019 }
8020 name[len] = cc;
8021
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008022 rettv->v_type = VAR_STRING;
8023 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 }
8025
8026 return OK;
8027}
8028
8029/*
8030 * Array with names and number of arguments of all internal functions
8031 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8032 */
8033static struct fst
8034{
8035 char *f_name; /* function name */
8036 char f_min_argc; /* minimal number of arguments */
8037 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008038 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008039 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040} functions[] =
8041{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008042#ifdef FEAT_FLOAT
8043 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008044 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008045#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008046 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008047 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008048 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 {"append", 2, 2, f_append},
8050 {"argc", 0, 0, f_argc},
8051 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008052 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008053 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008054#ifdef FEAT_FLOAT
8055 {"asin", 1, 1, f_asin}, /* WJMc */
8056#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008057 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008058 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008059 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008060 {"assert_false", 1, 2, f_assert_false},
8061 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008062#ifdef FEAT_FLOAT
8063 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008064 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008067 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 {"bufexists", 1, 1, f_bufexists},
8069 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8070 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8071 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8072 {"buflisted", 1, 1, f_buflisted},
8073 {"bufloaded", 1, 1, f_bufloaded},
8074 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008075 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {"bufwinnr", 1, 1, f_bufwinnr},
8077 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008078 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008079 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008080 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008081#ifdef FEAT_FLOAT
8082 {"ceil", 1, 1, f_ceil},
8083#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008084#ifdef FEAT_CHANNEL
8085 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008086 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008087 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8088 {"ch_sendraw", 2, 3, f_ch_sendraw},
8089#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008090 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008091 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008093 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008095#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008096 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008097 {"complete_add", 1, 1, f_complete_add},
8098 {"complete_check", 0, 0, f_complete_check},
8099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008101 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008102#ifdef FEAT_FLOAT
8103 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008104 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008105#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008106 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008108 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008109 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008110 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008112 {"diff_filler", 1, 1, f_diff_filler},
8113 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008114 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008116 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 {"eventhandler", 0, 0, f_eventhandler},
8118 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008119 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008121#ifdef FEAT_FLOAT
8122 {"exp", 1, 1, f_exp},
8123#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008124 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008125 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008126 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8128 {"filereadable", 1, 1, f_filereadable},
8129 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008130 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008131 {"finddir", 1, 3, f_finddir},
8132 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008133#ifdef FEAT_FLOAT
8134 {"float2nr", 1, 1, f_float2nr},
8135 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008136 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008137#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008138 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 {"fnamemodify", 2, 2, f_fnamemodify},
8140 {"foldclosed", 1, 1, f_foldclosed},
8141 {"foldclosedend", 1, 1, f_foldclosedend},
8142 {"foldlevel", 1, 1, f_foldlevel},
8143 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008144 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008146 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008147 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008148 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008149 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008150 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 {"getchar", 0, 1, f_getchar},
8152 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008153 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {"getcmdline", 0, 0, f_getcmdline},
8155 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008156 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008157 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008158 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008159 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008160 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008161 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {"getfsize", 1, 1, f_getfsize},
8163 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008164 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008165 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008166 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008167 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008168 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008169 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008170 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008171 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008173 {"gettabvar", 2, 3, f_gettabvar},
8174 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {"getwinposx", 0, 0, f_getwinposx},
8176 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008177 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008178 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008179 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008180 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008182 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008183 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008184 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8186 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8187 {"histadd", 2, 2, f_histadd},
8188 {"histdel", 1, 2, f_histdel},
8189 {"histget", 1, 2, f_histget},
8190 {"histnr", 1, 1, f_histnr},
8191 {"hlID", 1, 1, f_hlID},
8192 {"hlexists", 1, 1, f_hlexists},
8193 {"hostname", 0, 0, f_hostname},
8194 {"iconv", 3, 3, f_iconv},
8195 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008196 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008197 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008199 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 {"inputrestore", 0, 0, f_inputrestore},
8201 {"inputsave", 0, 0, f_inputsave},
8202 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008203 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008204 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008206 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008207 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008208#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +01008209 {"job_start", 1, 2, f_job_start},
8210 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008211 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008212#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008213 {"join", 1, 2, f_join},
Bram Moolenaar595e64e2016-02-07 19:19:53 +01008214 {"jsdecode", 1, 1, f_jsdecode},
8215 {"jsencode", 1, 1, f_jsencode},
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008216 {"jsondecode", 1, 1, f_jsondecode},
8217 {"jsonencode", 1, 1, f_jsonencode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008218 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008220 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 {"libcall", 3, 3, f_libcall},
8222 {"libcallnr", 3, 3, f_libcallnr},
8223 {"line", 1, 1, f_line},
8224 {"line2byte", 1, 1, f_line2byte},
8225 {"lispindent", 1, 1, f_lispindent},
8226 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008227#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008228 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008229 {"log10", 1, 1, f_log10},
8230#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008231#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008232 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008233#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008234 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008235 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008236 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008237 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008238 {"matchadd", 2, 5, f_matchadd},
8239 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008240 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008241 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008242 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008243 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008244 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008245 {"max", 1, 1, f_max},
8246 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008247#ifdef vim_mkdir
8248 {"mkdir", 1, 3, f_mkdir},
8249#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008250 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008251#ifdef FEAT_MZSCHEME
8252 {"mzeval", 1, 1, f_mzeval},
8253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008255 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008256 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008257 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008258#ifdef FEAT_PERL
8259 {"perleval", 1, 1, f_perleval},
8260#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008261#ifdef FEAT_FLOAT
8262 {"pow", 2, 2, f_pow},
8263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008265 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008266 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008267#ifdef FEAT_PYTHON3
8268 {"py3eval", 1, 1, f_py3eval},
8269#endif
8270#ifdef FEAT_PYTHON
8271 {"pyeval", 1, 1, f_pyeval},
8272#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008273 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008274 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008275 {"reltime", 0, 2, f_reltime},
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008276 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008277 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 {"remote_expr", 2, 3, f_remote_expr},
8279 {"remote_foreground", 1, 1, f_remote_foreground},
8280 {"remote_peek", 1, 2, f_remote_peek},
8281 {"remote_read", 1, 1, f_remote_read},
8282 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008283 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008285 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008287 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008288#ifdef FEAT_FLOAT
8289 {"round", 1, 1, f_round},
8290#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008291 {"screenattr", 2, 2, f_screenattr},
8292 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008293 {"screencol", 0, 0, f_screencol},
8294 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008295 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008296 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008297 {"searchpair", 3, 7, f_searchpair},
8298 {"searchpairpos", 3, 7, f_searchpairpos},
8299 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 {"server2client", 2, 2, f_server2client},
8301 {"serverlist", 0, 0, f_serverlist},
8302 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008303 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 {"setcmdpos", 1, 1, f_setcmdpos},
8305 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008306 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008307 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008308 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008309 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008311 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008312 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008314#ifdef FEAT_CRYPT
8315 {"sha256", 1, 1, f_sha256},
8316#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008317 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008318 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008320#ifdef FEAT_FLOAT
8321 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008322 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008323#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008324 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008325 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008326 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008327 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008328 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008329#ifdef FEAT_FLOAT
8330 {"sqrt", 1, 1, f_sqrt},
8331 {"str2float", 1, 1, f_str2float},
8332#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008333 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008334 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008335 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336#ifdef HAVE_STRFTIME
8337 {"strftime", 1, 2, f_strftime},
8338#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008339 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008340 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 {"strlen", 1, 1, f_strlen},
8342 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008343 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008345 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008346 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 {"substitute", 4, 4, f_substitute},
8348 {"synID", 3, 3, f_synID},
8349 {"synIDattr", 2, 3, f_synIDattr},
8350 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008351 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008352 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008353 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008354 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008355 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008356 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008357 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008358 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008359 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008360#ifdef FEAT_FLOAT
8361 {"tan", 1, 1, f_tan},
8362 {"tanh", 1, 1, f_tanh},
8363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008365 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 {"tolower", 1, 1, f_tolower},
8367 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008368 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008369#ifdef FEAT_FLOAT
8370 {"trunc", 1, 1, f_trunc},
8371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008373 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008374 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008375 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008376 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 {"virtcol", 1, 1, f_virtcol},
8378 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008379 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 {"winbufnr", 1, 1, f_winbufnr},
8381 {"wincol", 0, 0, f_wincol},
8382 {"winheight", 1, 1, f_winheight},
8383 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008384 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008386 {"winrestview", 1, 1, f_winrestview},
8387 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008389 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008390 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008391 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392};
8393
8394#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8395
8396/*
8397 * Function given to ExpandGeneric() to obtain the list of internal
8398 * or user defined function names.
8399 */
8400 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008401get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402{
8403 static int intidx = -1;
8404 char_u *name;
8405
8406 if (idx == 0)
8407 intidx = -1;
8408 if (intidx < 0)
8409 {
8410 name = get_user_func_name(xp, idx);
8411 if (name != NULL)
8412 return name;
8413 }
8414 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8415 {
8416 STRCPY(IObuff, functions[intidx].f_name);
8417 STRCAT(IObuff, "(");
8418 if (functions[intidx].f_max_argc == 0)
8419 STRCAT(IObuff, ")");
8420 return IObuff;
8421 }
8422
8423 return NULL;
8424}
8425
8426/*
8427 * Function given to ExpandGeneric() to obtain the list of internal or
8428 * user defined variable or function names.
8429 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008431get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432{
8433 static int intidx = -1;
8434 char_u *name;
8435
8436 if (idx == 0)
8437 intidx = -1;
8438 if (intidx < 0)
8439 {
8440 name = get_function_name(xp, idx);
8441 if (name != NULL)
8442 return name;
8443 }
8444 return get_user_var_name(xp, ++intidx);
8445}
8446
8447#endif /* FEAT_CMDL_COMPL */
8448
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008449#if defined(EBCDIC) || defined(PROTO)
8450/*
8451 * Compare struct fst by function name.
8452 */
8453 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008454compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008455{
8456 struct fst *p1 = (struct fst *)s1;
8457 struct fst *p2 = (struct fst *)s2;
8458
8459 return STRCMP(p1->f_name, p2->f_name);
8460}
8461
8462/*
8463 * Sort the function table by function name.
8464 * The sorting of the table above is ASCII dependant.
8465 * On machines using EBCDIC we have to sort it.
8466 */
8467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008468sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008469{
8470 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8471
8472 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8473}
8474#endif
8475
8476
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477/*
8478 * Find internal function in table above.
8479 * Return index, or -1 if not found
8480 */
8481 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008482find_internal_func(
8483 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484{
8485 int first = 0;
8486 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8487 int cmp;
8488 int x;
8489
8490 /*
8491 * Find the function name in the table. Binary search.
8492 */
8493 while (first <= last)
8494 {
8495 x = first + ((unsigned)(last - first) >> 1);
8496 cmp = STRCMP(name, functions[x].f_name);
8497 if (cmp < 0)
8498 last = x - 1;
8499 else if (cmp > 0)
8500 first = x + 1;
8501 else
8502 return x;
8503 }
8504 return -1;
8505}
8506
8507/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008508 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8509 * name it contains, otherwise return "name".
8510 */
8511 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008512deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008513{
Bram Moolenaar33570922005-01-25 22:26:29 +00008514 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008515 int cc;
8516
8517 cc = name[*lenp];
8518 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008519 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008520 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008521 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008522 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008523 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008524 {
8525 *lenp = 0;
8526 return (char_u *)""; /* just in case */
8527 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008528 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008529 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008530 }
8531
8532 return name;
8533}
8534
8535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 * Allocate a variable for the result of a function.
8537 * Return OK or FAIL.
8538 */
8539 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008540get_func_tv(
8541 char_u *name, /* name of the function */
8542 int len, /* length of "name" */
8543 typval_T *rettv,
8544 char_u **arg, /* argument, pointing to the '(' */
8545 linenr_T firstline, /* first line of range */
8546 linenr_T lastline, /* last line of range */
8547 int *doesrange, /* return: function handled range */
8548 int evaluate,
8549 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550{
8551 char_u *argp;
8552 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008553 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 int argcount = 0; /* number of arguments found */
8555
8556 /*
8557 * Get the arguments.
8558 */
8559 argp = *arg;
8560 while (argcount < MAX_FUNC_ARGS)
8561 {
8562 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8563 if (*argp == ')' || *argp == ',' || *argp == NUL)
8564 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8566 {
8567 ret = FAIL;
8568 break;
8569 }
8570 ++argcount;
8571 if (*argp != ',')
8572 break;
8573 }
8574 if (*argp == ')')
8575 ++argp;
8576 else
8577 ret = FAIL;
8578
8579 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008580 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008581 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008583 {
8584 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008585 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008586 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008587 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589
8590 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008591 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592
8593 *arg = skipwhite(argp);
8594 return ret;
8595}
8596
8597
8598/*
8599 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008600 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008601 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008603 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008604call_func(
8605 char_u *funcname, /* name of the function */
8606 int len, /* length of "name" */
8607 typval_T *rettv, /* return value goes here */
8608 int argcount, /* number of "argvars" */
8609 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008610 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008611 linenr_T firstline, /* first line of range */
8612 linenr_T lastline, /* last line of range */
8613 int *doesrange, /* return: function handled range */
8614 int evaluate,
8615 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616{
8617 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618#define ERROR_UNKNOWN 0
8619#define ERROR_TOOMANY 1
8620#define ERROR_TOOFEW 2
8621#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008622#define ERROR_DICT 4
8623#define ERROR_NONE 5
8624#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 int error = ERROR_NONE;
8626 int i;
8627 int llen;
8628 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629#define FLEN_FIXED 40
8630 char_u fname_buf[FLEN_FIXED + 1];
8631 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008632 char_u *name;
8633
8634 /* Make a copy of the name, if it comes from a funcref variable it could
8635 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008636 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008637 if (name == NULL)
8638 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639
8640 /*
8641 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8642 * Change <SNR>123_name() to K_SNR 123_name().
8643 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 llen = eval_fname_script(name);
8646 if (llen > 0)
8647 {
8648 fname_buf[0] = K_SPECIAL;
8649 fname_buf[1] = KS_EXTRA;
8650 fname_buf[2] = (int)KE_SNR;
8651 i = 3;
8652 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8653 {
8654 if (current_SID <= 0)
8655 error = ERROR_SCRIPT;
8656 else
8657 {
8658 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8659 i = (int)STRLEN(fname_buf);
8660 }
8661 }
8662 if (i + STRLEN(name + llen) < FLEN_FIXED)
8663 {
8664 STRCPY(fname_buf + i, name + llen);
8665 fname = fname_buf;
8666 }
8667 else
8668 {
8669 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8670 if (fname == NULL)
8671 error = ERROR_OTHER;
8672 else
8673 {
8674 mch_memmove(fname, fname_buf, (size_t)i);
8675 STRCPY(fname + i, name + llen);
8676 }
8677 }
8678 }
8679 else
8680 fname = name;
8681
8682 *doesrange = FALSE;
8683
8684
8685 /* execute the function if no errors detected and executing */
8686 if (evaluate && error == ERROR_NONE)
8687 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008688 char_u *rfname = fname;
8689
8690 /* Ignore "g:" before a function name. */
8691 if (fname[0] == 'g' && fname[1] == ':')
8692 rfname = fname + 2;
8693
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008694 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8695 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 error = ERROR_UNKNOWN;
8697
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008698 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 {
8700 /*
8701 * User defined function.
8702 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008703 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008704
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008706 /* Trigger FuncUndefined event, may load the function. */
8707 if (fp == NULL
8708 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008709 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008710 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008712 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008713 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 }
8715#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008716 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008717 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008718 {
8719 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008720 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008721 }
8722
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 if (fp != NULL)
8724 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008725 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008727 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008729 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008731 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008732 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 else
8734 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008735 int did_save_redo = FALSE;
8736
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 /*
8738 * Call the user function.
8739 * Save and restore search patterns, script variables and
8740 * redo buffer.
8741 */
8742 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008743#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008744 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008745#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008746 {
8747 saveRedobuff();
8748 did_save_redo = TRUE;
8749 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008750 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008751 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008752 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008753 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8754 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8755 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008756 /* Function was unreferenced while being used, free it
8757 * now. */
8758 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008759 if (did_save_redo)
8760 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 restore_search_patterns();
8762 error = ERROR_NONE;
8763 }
8764 }
8765 }
8766 else
8767 {
8768 /*
8769 * Find the function name in the table, call its implementation.
8770 */
8771 i = find_internal_func(fname);
8772 if (i >= 0)
8773 {
8774 if (argcount < functions[i].f_min_argc)
8775 error = ERROR_TOOFEW;
8776 else if (argcount > functions[i].f_max_argc)
8777 error = ERROR_TOOMANY;
8778 else
8779 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008780 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008781 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 error = ERROR_NONE;
8783 }
8784 }
8785 }
8786 /*
8787 * The function call (or "FuncUndefined" autocommand sequence) might
8788 * have been aborted by an error, an interrupt, or an explicitly thrown
8789 * exception that has not been caught so far. This situation can be
8790 * tested for by calling aborting(). For an error in an internal
8791 * function or for the "E132" error in call_user_func(), however, the
8792 * throw point at which the "force_abort" flag (temporarily reset by
8793 * emsg()) is normally updated has not been reached yet. We need to
8794 * update that flag first to make aborting() reliable.
8795 */
8796 update_force_abort();
8797 }
8798 if (error == ERROR_NONE)
8799 ret = OK;
8800
8801 /*
8802 * Report an error unless the argument evaluation or function call has been
8803 * cancelled due to an aborting error, an interrupt, or an exception.
8804 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008805 if (!aborting())
8806 {
8807 switch (error)
8808 {
8809 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008810 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008811 break;
8812 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008813 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008814 break;
8815 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008816 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008817 name);
8818 break;
8819 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008820 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008821 name);
8822 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008823 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008824 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008825 name);
8826 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008827 }
8828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 if (fname != name && fname != fname_buf)
8831 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008832 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833
8834 return ret;
8835}
8836
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008837/*
8838 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008839 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008840 */
8841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008842emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008843{
8844 char_u *p;
8845
8846 if (*name == K_SPECIAL)
8847 p = concat_str((char_u *)"<SNR>", name + 3);
8848 else
8849 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008850 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008851 if (p != name)
8852 vim_free(p);
8853}
8854
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008855/*
8856 * Return TRUE for a non-zero Number and a non-empty String.
8857 */
8858 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008859non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008860{
8861 return ((argvars[0].v_type == VAR_NUMBER
8862 && argvars[0].vval.v_number != 0)
8863 || (argvars[0].v_type == VAR_STRING
8864 && argvars[0].vval.v_string != NULL
8865 && *argvars[0].vval.v_string != NUL));
8866}
8867
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868/*********************************************
8869 * Implementation of the built-in functions
8870 */
8871
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008872#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008873static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008874
8875/*
8876 * Get the float value of "argvars[0]" into "f".
8877 * Returns FAIL when the argument is not a Number or Float.
8878 */
8879 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008880get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008881{
8882 if (argvars[0].v_type == VAR_FLOAT)
8883 {
8884 *f = argvars[0].vval.v_float;
8885 return OK;
8886 }
8887 if (argvars[0].v_type == VAR_NUMBER)
8888 {
8889 *f = (float_T)argvars[0].vval.v_number;
8890 return OK;
8891 }
8892 EMSG(_("E808: Number or Float required"));
8893 return FAIL;
8894}
8895
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008896/*
8897 * "abs(expr)" function
8898 */
8899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008900f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008901{
8902 if (argvars[0].v_type == VAR_FLOAT)
8903 {
8904 rettv->v_type = VAR_FLOAT;
8905 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8906 }
8907 else
8908 {
8909 varnumber_T n;
8910 int error = FALSE;
8911
8912 n = get_tv_number_chk(&argvars[0], &error);
8913 if (error)
8914 rettv->vval.v_number = -1;
8915 else if (n > 0)
8916 rettv->vval.v_number = n;
8917 else
8918 rettv->vval.v_number = -n;
8919 }
8920}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008921
8922/*
8923 * "acos()" function
8924 */
8925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008926f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008927{
8928 float_T f;
8929
8930 rettv->v_type = VAR_FLOAT;
8931 if (get_float_arg(argvars, &f) == OK)
8932 rettv->vval.v_float = acos(f);
8933 else
8934 rettv->vval.v_float = 0.0;
8935}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008936#endif
8937
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008939 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 */
8941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008942f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943{
Bram Moolenaar33570922005-01-25 22:26:29 +00008944 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008946 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008947 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008949 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008950 && !tv_check_lock(l->lv_lock,
8951 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008952 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008953 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008954 }
8955 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008956 EMSG(_(e_listreq));
8957}
8958
8959/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008960 * "alloc_fail(id, countdown, repeat)" function
8961 */
8962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008963f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008964{
8965 if (argvars[0].v_type != VAR_NUMBER
8966 || argvars[0].vval.v_number <= 0
8967 || argvars[1].v_type != VAR_NUMBER
8968 || argvars[1].vval.v_number < 0
8969 || argvars[2].v_type != VAR_NUMBER)
8970 EMSG(_(e_invarg));
8971 else
8972 {
8973 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008974 if (alloc_fail_id >= aid_last)
8975 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008976 alloc_fail_countdown = argvars[1].vval.v_number;
8977 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008978 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008979 }
8980}
8981
8982/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008983 * "and(expr, expr)" function
8984 */
8985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008986f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008987{
8988 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8989 & get_tv_number_chk(&argvars[1], NULL);
8990}
8991
8992/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008993 * "append(lnum, string/list)" function
8994 */
8995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008996f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008997{
8998 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008999 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009000 list_T *l = NULL;
9001 listitem_T *li = NULL;
9002 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009003 long added = 0;
9004
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009005 /* When coming here from Insert mode, sync undo, so that this can be
9006 * undone separately from what was previously inserted. */
9007 if (u_sync_once == 2)
9008 {
9009 u_sync_once = 1; /* notify that u_sync() was called */
9010 u_sync(TRUE);
9011 }
9012
Bram Moolenaar0d660222005-01-07 21:51:51 +00009013 lnum = get_tv_lnum(argvars);
9014 if (lnum >= 0
9015 && lnum <= curbuf->b_ml.ml_line_count
9016 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009017 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009018 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009019 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009020 l = argvars[1].vval.v_list;
9021 if (l == NULL)
9022 return;
9023 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009024 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009025 for (;;)
9026 {
9027 if (l == NULL)
9028 tv = &argvars[1]; /* append a string */
9029 else if (li == NULL)
9030 break; /* end of list */
9031 else
9032 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009033 line = get_tv_string_chk(tv);
9034 if (line == NULL) /* type error */
9035 {
9036 rettv->vval.v_number = 1; /* Failed */
9037 break;
9038 }
9039 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009040 ++added;
9041 if (l == NULL)
9042 break;
9043 li = li->li_next;
9044 }
9045
9046 appended_lines_mark(lnum, added);
9047 if (curwin->w_cursor.lnum > lnum)
9048 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009050 else
9051 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052}
9053
9054/*
9055 * "argc()" function
9056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009058f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009060 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061}
9062
9063/*
9064 * "argidx()" function
9065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009067f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009069 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070}
9071
9072/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009073 * "arglistid()" function
9074 */
9075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009076f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009077{
9078 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009079
9080 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009081 wp = find_tabwin(&argvars[0], &argvars[1]);
9082 if (wp != NULL)
9083 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009084}
9085
9086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 * "argv(nr)" function
9088 */
9089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009090f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091{
9092 int idx;
9093
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009094 if (argvars[0].v_type != VAR_UNKNOWN)
9095 {
9096 idx = get_tv_number_chk(&argvars[0], NULL);
9097 if (idx >= 0 && idx < ARGCOUNT)
9098 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9099 else
9100 rettv->vval.v_string = NULL;
9101 rettv->v_type = VAR_STRING;
9102 }
9103 else if (rettv_list_alloc(rettv) == OK)
9104 for (idx = 0; idx < ARGCOUNT; ++idx)
9105 list_append_string(rettv->vval.v_list,
9106 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107}
9108
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009109static void prepare_assert_error(garray_T*gap);
9110static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9111static void assert_error(garray_T *gap);
9112static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009113
9114/*
9115 * Prepare "gap" for an assert error and add the sourcing position.
9116 */
9117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009118prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009119{
9120 char buf[NUMBUFLEN];
9121
9122 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009123 if (sourcing_name != NULL)
9124 {
9125 ga_concat(gap, sourcing_name);
9126 if (sourcing_lnum > 0)
9127 ga_concat(gap, (char_u *)" ");
9128 }
9129 if (sourcing_lnum > 0)
9130 {
9131 sprintf(buf, "line %ld", (long)sourcing_lnum);
9132 ga_concat(gap, (char_u *)buf);
9133 }
9134 if (sourcing_name != NULL || sourcing_lnum > 0)
9135 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009136}
9137
9138/*
9139 * Fill "gap" with information about an assert error.
9140 */
9141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009142fill_assert_error(
9143 garray_T *gap,
9144 typval_T *opt_msg_tv,
9145 char_u *exp_str,
9146 typval_T *exp_tv,
9147 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009148{
9149 char_u numbuf[NUMBUFLEN];
9150 char_u *tofree;
9151
9152 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9153 {
9154 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9155 vim_free(tofree);
9156 }
9157 else
9158 {
9159 ga_concat(gap, (char_u *)"Expected ");
9160 if (exp_str == NULL)
9161 {
9162 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9163 vim_free(tofree);
9164 }
9165 else
9166 ga_concat(gap, exp_str);
9167 ga_concat(gap, (char_u *)" but got ");
9168 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9169 vim_free(tofree);
9170 }
9171}
Bram Moolenaar43345542015-11-29 17:35:35 +01009172
9173/*
9174 * Add an assert error to v:errors.
9175 */
9176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009177assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009178{
9179 struct vimvar *vp = &vimvars[VV_ERRORS];
9180
9181 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9182 /* Make sure v:errors is a list. */
9183 set_vim_var_list(VV_ERRORS, list_alloc());
9184 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9185}
9186
Bram Moolenaar43345542015-11-29 17:35:35 +01009187/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009188 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009189 */
9190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009191f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009192{
9193 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009194
9195 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9196 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009197 prepare_assert_error(&ga);
9198 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9199 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009200 ga_clear(&ga);
9201 }
9202}
9203
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009204/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009205 * "assert_exception(string[, msg])" function
9206 */
9207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009208f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009209{
9210 garray_T ga;
9211 char *error;
9212
9213 error = (char *)get_tv_string_chk(&argvars[0]);
9214 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9215 {
9216 prepare_assert_error(&ga);
9217 ga_concat(&ga, (char_u *)"v:exception is not set");
9218 assert_error(&ga);
9219 ga_clear(&ga);
9220 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009221 else if (error != NULL
9222 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009223 {
9224 prepare_assert_error(&ga);
9225 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9226 &vimvars[VV_EXCEPTION].vv_tv);
9227 assert_error(&ga);
9228 ga_clear(&ga);
9229 }
9230}
9231
9232/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009233 * "assert_fails(cmd [, error])" function
9234 */
9235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009236f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009237{
9238 char_u *cmd = get_tv_string_chk(&argvars[0]);
9239 garray_T ga;
9240
9241 called_emsg = FALSE;
9242 suppress_errthrow = TRUE;
9243 emsg_silent = TRUE;
9244 do_cmdline_cmd(cmd);
9245 if (!called_emsg)
9246 {
9247 prepare_assert_error(&ga);
9248 ga_concat(&ga, (char_u *)"command did not fail: ");
9249 ga_concat(&ga, cmd);
9250 assert_error(&ga);
9251 ga_clear(&ga);
9252 }
9253 else if (argvars[1].v_type != VAR_UNKNOWN)
9254 {
9255 char_u buf[NUMBUFLEN];
9256 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9257
9258 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9259 {
9260 prepare_assert_error(&ga);
9261 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9262 &vimvars[VV_ERRMSG].vv_tv);
9263 assert_error(&ga);
9264 ga_clear(&ga);
9265 }
9266 }
9267
9268 called_emsg = FALSE;
9269 suppress_errthrow = FALSE;
9270 emsg_silent = FALSE;
9271 emsg_on_display = FALSE;
9272 set_vim_var_string(VV_ERRMSG, NULL, 0);
9273}
9274
9275/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009276 * Common for assert_true() and assert_false().
9277 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009279assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009280{
9281 int error = FALSE;
9282 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009283
Bram Moolenaar37127922016-02-06 20:29:28 +01009284 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009285 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009286 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009287 if (argvars[0].v_type != VAR_NUMBER
9288 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9289 || error)
9290 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009291 prepare_assert_error(&ga);
9292 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009293 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009294 NULL, &argvars[0]);
9295 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009296 ga_clear(&ga);
9297 }
9298}
9299
9300/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009301 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009302 */
9303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009304f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009305{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009306 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009307}
9308
9309/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009310 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009311 */
9312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009313f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009314{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009315 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009316}
9317
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009318#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009319/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009320 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009321 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009323f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009324{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009325 float_T f;
9326
9327 rettv->v_type = VAR_FLOAT;
9328 if (get_float_arg(argvars, &f) == OK)
9329 rettv->vval.v_float = asin(f);
9330 else
9331 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009332}
9333
9334/*
9335 * "atan()" function
9336 */
9337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009338f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009339{
9340 float_T f;
9341
9342 rettv->v_type = VAR_FLOAT;
9343 if (get_float_arg(argvars, &f) == OK)
9344 rettv->vval.v_float = atan(f);
9345 else
9346 rettv->vval.v_float = 0.0;
9347}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009348
9349/*
9350 * "atan2()" function
9351 */
9352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009353f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009354{
9355 float_T fx, fy;
9356
9357 rettv->v_type = VAR_FLOAT;
9358 if (get_float_arg(argvars, &fx) == OK
9359 && get_float_arg(&argvars[1], &fy) == OK)
9360 rettv->vval.v_float = atan2(fx, fy);
9361 else
9362 rettv->vval.v_float = 0.0;
9363}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009364#endif
9365
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366/*
9367 * "browse(save, title, initdir, default)" function
9368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009370f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371{
9372#ifdef FEAT_BROWSE
9373 int save;
9374 char_u *title;
9375 char_u *initdir;
9376 char_u *defname;
9377 char_u buf[NUMBUFLEN];
9378 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009379 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009381 save = get_tv_number_chk(&argvars[0], &error);
9382 title = get_tv_string_chk(&argvars[1]);
9383 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9384 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009386 if (error || title == NULL || initdir == NULL || defname == NULL)
9387 rettv->vval.v_string = NULL;
9388 else
9389 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009390 do_browse(save ? BROWSE_SAVE : 0,
9391 title, defname, NULL, initdir, NULL, curbuf);
9392#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009393 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009394#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009395 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009396}
9397
9398/*
9399 * "browsedir(title, initdir)" function
9400 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009402f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009403{
9404#ifdef FEAT_BROWSE
9405 char_u *title;
9406 char_u *initdir;
9407 char_u buf[NUMBUFLEN];
9408
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009409 title = get_tv_string_chk(&argvars[0]);
9410 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009411
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009412 if (title == NULL || initdir == NULL)
9413 rettv->vval.v_string = NULL;
9414 else
9415 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009416 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009418 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009420 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421}
9422
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009423static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009424
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425/*
9426 * Find a buffer by number or exact name.
9427 */
9428 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009429find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430{
9431 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009433 if (avar->v_type == VAR_NUMBER)
9434 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009435 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009437 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009438 if (buf == NULL)
9439 {
9440 /* No full path name match, try a match with a URL or a "nofile"
9441 * buffer, these don't use the full path. */
9442 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9443 if (buf->b_fname != NULL
9444 && (path_with_url(buf->b_fname)
9445#ifdef FEAT_QUICKFIX
9446 || bt_nofile(buf)
9447#endif
9448 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009449 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009450 break;
9451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 }
9453 return buf;
9454}
9455
9456/*
9457 * "bufexists(expr)" function
9458 */
9459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009460f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009462 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463}
9464
9465/*
9466 * "buflisted(expr)" function
9467 */
9468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009469f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470{
9471 buf_T *buf;
9472
9473 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009474 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475}
9476
9477/*
9478 * "bufloaded(expr)" function
9479 */
9480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009481f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482{
9483 buf_T *buf;
9484
9485 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009486 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487}
9488
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009489static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009490
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491/*
9492 * Get buffer by number or pattern.
9493 */
9494 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009495get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009497 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 int save_magic;
9499 char_u *save_cpo;
9500 buf_T *buf;
9501
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009502 if (tv->v_type == VAR_NUMBER)
9503 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009504 if (tv->v_type != VAR_STRING)
9505 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 if (name == NULL || *name == NUL)
9507 return curbuf;
9508 if (name[0] == '$' && name[1] == NUL)
9509 return lastbuf;
9510
9511 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9512 save_magic = p_magic;
9513 p_magic = TRUE;
9514 save_cpo = p_cpo;
9515 p_cpo = (char_u *)"";
9516
9517 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009518 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519
9520 p_magic = save_magic;
9521 p_cpo = save_cpo;
9522
9523 /* If not found, try expanding the name, like done for bufexists(). */
9524 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009525 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526
9527 return buf;
9528}
9529
9530/*
9531 * "bufname(expr)" function
9532 */
9533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009534f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535{
9536 buf_T *buf;
9537
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009538 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009540 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009541 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009543 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009545 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546 --emsg_off;
9547}
9548
9549/*
9550 * "bufnr(expr)" function
9551 */
9552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009553f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554{
9555 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009556 int error = FALSE;
9557 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009559 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009561 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009562 --emsg_off;
9563
9564 /* If the buffer isn't found and the second argument is not zero create a
9565 * new buffer. */
9566 if (buf == NULL
9567 && argvars[1].v_type != VAR_UNKNOWN
9568 && get_tv_number_chk(&argvars[1], &error) != 0
9569 && !error
9570 && (name = get_tv_string_chk(&argvars[0])) != NULL
9571 && !error)
9572 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9573
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009575 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009577 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578}
9579
9580/*
9581 * "bufwinnr(nr)" function
9582 */
9583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009584f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585{
9586#ifdef FEAT_WINDOWS
9587 win_T *wp;
9588 int winnr = 0;
9589#endif
9590 buf_T *buf;
9591
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009592 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009594 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595#ifdef FEAT_WINDOWS
9596 for (wp = firstwin; wp; wp = wp->w_next)
9597 {
9598 ++winnr;
9599 if (wp->w_buffer == buf)
9600 break;
9601 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009602 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009604 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605#endif
9606 --emsg_off;
9607}
9608
9609/*
9610 * "byte2line(byte)" function
9611 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009613f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614{
9615#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009616 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617#else
9618 long boff = 0;
9619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009620 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009622 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009624 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 (linenr_T)0, &boff);
9626#endif
9627}
9628
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009630byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009631{
9632#ifdef FEAT_MBYTE
9633 char_u *t;
9634#endif
9635 char_u *str;
9636 long idx;
9637
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009638 str = get_tv_string_chk(&argvars[0]);
9639 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009640 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009641 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009642 return;
9643
9644#ifdef FEAT_MBYTE
9645 t = str;
9646 for ( ; idx > 0; idx--)
9647 {
9648 if (*t == NUL) /* EOL reached */
9649 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009650 if (enc_utf8 && comp)
9651 t += utf_ptr2len(t);
9652 else
9653 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009654 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009655 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009656#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009657 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009658 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009659#endif
9660}
9661
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009662/*
9663 * "byteidx()" function
9664 */
9665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009666f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009667{
9668 byteidx(argvars, rettv, FALSE);
9669}
9670
9671/*
9672 * "byteidxcomp()" function
9673 */
9674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009675f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009676{
9677 byteidx(argvars, rettv, TRUE);
9678}
9679
Bram Moolenaardb913952012-06-29 12:54:53 +02009680 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009681func_call(
9682 char_u *name,
9683 typval_T *args,
9684 dict_T *selfdict,
9685 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009686{
9687 listitem_T *item;
9688 typval_T argv[MAX_FUNC_ARGS + 1];
9689 int argc = 0;
9690 int dummy;
9691 int r = 0;
9692
9693 for (item = args->vval.v_list->lv_first; item != NULL;
9694 item = item->li_next)
9695 {
9696 if (argc == MAX_FUNC_ARGS)
9697 {
9698 EMSG(_("E699: Too many arguments"));
9699 break;
9700 }
9701 /* Make a copy of each argument. This is needed to be able to set
9702 * v_lock to VAR_FIXED in the copy without changing the original list.
9703 */
9704 copy_tv(&item->li_tv, &argv[argc++]);
9705 }
9706
9707 if (item == NULL)
9708 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9709 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9710 &dummy, TRUE, selfdict);
9711
9712 /* Free the arguments. */
9713 while (argc > 0)
9714 clear_tv(&argv[--argc]);
9715
9716 return r;
9717}
9718
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009719/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009720 * "call(func, arglist)" function
9721 */
9722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009723f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009724{
9725 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009726 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009727
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009728 if (argvars[1].v_type != VAR_LIST)
9729 {
9730 EMSG(_(e_listreq));
9731 return;
9732 }
9733 if (argvars[1].vval.v_list == NULL)
9734 return;
9735
9736 if (argvars[0].v_type == VAR_FUNC)
9737 func = argvars[0].vval.v_string;
9738 else
9739 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009740 if (*func == NUL)
9741 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009742
Bram Moolenaare9a41262005-01-15 22:18:47 +00009743 if (argvars[2].v_type != VAR_UNKNOWN)
9744 {
9745 if (argvars[2].v_type != VAR_DICT)
9746 {
9747 EMSG(_(e_dictreq));
9748 return;
9749 }
9750 selfdict = argvars[2].vval.v_dict;
9751 }
9752
Bram Moolenaardb913952012-06-29 12:54:53 +02009753 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009754}
9755
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009756#ifdef FEAT_FLOAT
9757/*
9758 * "ceil({float})" function
9759 */
9760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009761f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009762{
9763 float_T f;
9764
9765 rettv->v_type = VAR_FLOAT;
9766 if (get_float_arg(argvars, &f) == OK)
9767 rettv->vval.v_float = ceil(f);
9768 else
9769 rettv->vval.v_float = 0.0;
9770}
9771#endif
9772
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009773#ifdef FEAT_CHANNEL
9774/*
9775 * Get the channel index from the handle argument.
9776 * Returns -1 if the handle is invalid or the channel is closed.
9777 */
9778 static int
9779get_channel_arg(typval_T *tv)
9780{
9781 int ch_idx;
9782
9783 if (tv->v_type != VAR_NUMBER)
9784 {
9785 EMSG2(_(e_invarg2), get_tv_string(tv));
9786 return -1;
9787 }
9788 ch_idx = tv->vval.v_number;
9789
9790 if (!channel_is_open(ch_idx))
9791 {
9792 EMSGN(_("E906: not an open channel"), ch_idx);
9793 return -1;
9794 }
9795 return ch_idx;
9796}
9797
9798/*
9799 * "ch_close()" function
9800 */
9801 static void
9802f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9803{
9804 int ch_idx = get_channel_arg(&argvars[0]);
9805
9806 if (ch_idx >= 0)
9807 channel_close(ch_idx);
9808}
9809
9810/*
9811 * Get a callback from "arg". It can be a Funcref or a function name.
9812 * When "arg" is zero return an empty string.
9813 * Return NULL for an invalid argument.
9814 */
9815 static char_u *
9816get_callback(typval_T *arg)
9817{
9818 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9819 return arg->vval.v_string;
9820 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9821 return (char_u *)"";
9822 EMSG(_("E999: Invalid callback argument"));
9823 return NULL;
9824}
9825
9826/*
9827 * "ch_open()" function
9828 */
9829 static void
9830f_ch_open(typval_T *argvars, typval_T *rettv)
9831{
9832 char_u *address;
9833 char_u *mode;
9834 char_u *callback = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009835 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009836 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009837 int port;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009838 int waittime = 0;
9839 int timeout = 2000;
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009840 ch_mode_T ch_mode = MODE_JSON;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009841 int ch_idx;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009842
9843 /* default: fail */
9844 rettv->vval.v_number = -1;
9845
9846 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009847 if (argvars[1].v_type != VAR_UNKNOWN
9848 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009849 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009850 EMSG(_(e_invarg));
9851 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009852 }
9853
9854 /* parse address */
9855 p = vim_strchr(address, ':');
9856 if (p == NULL)
9857 {
9858 EMSG2(_(e_invarg2), address);
9859 return;
9860 }
9861 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009862 port = strtol((char *)p, &rest, 10);
9863 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009864 {
9865 p[-1] = ':';
9866 EMSG2(_(e_invarg2), address);
9867 return;
9868 }
9869
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009870 if (argvars[1].v_type == VAR_DICT)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009871 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009872 /* parse argdict */
9873 dict_T *dict = argvars[1].vval.v_dict;
9874
9875 if (dict_find(dict, (char_u *)"mode", -1) != NULL)
9876 {
9877 mode = get_dict_string(dict, (char_u *)"mode", FALSE);
9878 if (STRCMP(mode, "raw") == 0)
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009879 ch_mode = MODE_RAW;
9880 else if (STRCMP(mode, "js") == 0)
9881 ch_mode = MODE_JS;
9882 else if (STRCMP(mode, "json") == 0)
9883 ch_mode = MODE_JSON;
9884 else
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009885 {
9886 EMSG2(_(e_invarg2), mode);
9887 return;
9888 }
9889 }
9890 if (dict_find(dict, (char_u *)"waittime", -1) != NULL)
9891 waittime = get_dict_number(dict, (char_u *)"waittime");
9892 if (dict_find(dict, (char_u *)"timeout", -1) != NULL)
9893 timeout = get_dict_number(dict, (char_u *)"timeout");
9894 if (dict_find(dict, (char_u *)"callback", -1) != NULL)
9895 callback = get_dict_string(dict, (char_u *)"callback", FALSE);
9896 }
9897 if (waittime < 0 || timeout < 0)
9898 {
9899 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009900 return;
9901 }
9902
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009903 ch_idx = channel_open((char *)address, port, waittime, NULL);
9904 if (ch_idx >= 0)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009905 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009906 channel_set_json_mode(ch_idx, ch_mode);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009907 channel_set_timeout(ch_idx, timeout);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009908 if (callback != NULL && *callback != NUL)
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009909 channel_set_callback(ch_idx, callback);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009910 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009911 rettv->vval.v_number = ch_idx;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009912}
9913
9914/*
9915 * common for "sendexpr()" and "sendraw()"
9916 * Returns the channel index if the caller should read the response.
9917 * Otherwise returns -1.
9918 */
9919 static int
Bram Moolenaara07fec92016-02-05 21:04:08 +01009920send_common(typval_T *argvars, char_u *text, int id, char *fun)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009921{
9922 int ch_idx;
9923 char_u *callback = NULL;
9924
9925 ch_idx = get_channel_arg(&argvars[0]);
9926 if (ch_idx < 0)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01009927 {
9928 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009929 return -1;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01009930 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009931
9932 if (argvars[2].v_type != VAR_UNKNOWN)
9933 {
9934 callback = get_callback(&argvars[2]);
9935 if (callback == NULL)
9936 return -1;
9937 }
Bram Moolenaara07fec92016-02-05 21:04:08 +01009938 /* Set the callback. An empty callback means no callback and not reading
9939 * the response. */
9940 if (callback != NULL && *callback != NUL)
9941 channel_set_req_callback(ch_idx, callback, id);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009942
9943 if (channel_send(ch_idx, text, fun) == OK && callback == NULL)
9944 return ch_idx;
9945 return -1;
9946}
9947
9948/*
9949 * "ch_sendexpr()" function
9950 */
9951 static void
9952f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
9953{
9954 char_u *text;
9955 typval_T *listtv;
9956 int ch_idx;
9957 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01009958 ch_mode_T ch_mode;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009959
9960 /* return an empty string by default */
9961 rettv->v_type = VAR_STRING;
9962 rettv->vval.v_string = NULL;
9963
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01009964 ch_idx = get_channel_arg(&argvars[0]);
9965 if (ch_idx < 0)
9966 {
9967 EMSG(_(e_invarg));
9968 return;
9969 }
9970
9971 ch_mode = channel_get_mode(ch_idx);
9972 if (ch_mode == MODE_RAW)
9973 {
9974 EMSG(_("E912: cannot use ch_sendexpr() with a raw channel"));
9975 return;
9976 }
9977
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009978 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01009979 text = json_encode_nr_expr(id, &argvars[1],
9980 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009981 if (text == NULL)
9982 return;
9983
Bram Moolenaara07fec92016-02-05 21:04:08 +01009984 ch_idx = send_common(argvars, text, id, "sendexpr");
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01009985 vim_free(text);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009986 if (ch_idx >= 0)
9987 {
9988 if (channel_read_json_block(ch_idx, id, &listtv) == OK)
9989 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01009990 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009991
Bram Moolenaar6076fe12016-02-05 22:49:56 +01009992 /* Move the item from the list and then change the type to
9993 * avoid the value being freed. */
9994 *rettv = list->lv_last->li_tv;
9995 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009996 clear_tv(listtv);
9997 }
9998 }
9999}
10000
10001/*
10002 * "ch_sendraw()" function
10003 */
10004 static void
10005f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10006{
10007 char_u buf[NUMBUFLEN];
10008 char_u *text;
10009 int ch_idx;
10010
10011 /* return an empty string by default */
10012 rettv->v_type = VAR_STRING;
10013 rettv->vval.v_string = NULL;
10014
10015 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaara07fec92016-02-05 21:04:08 +010010016 ch_idx = send_common(argvars, text, 0, "sendraw");
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010017 if (ch_idx >= 0)
10018 rettv->vval.v_string = channel_read_block(ch_idx);
10019}
10020#endif
10021
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010022/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010023 * "changenr()" function
10024 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010026f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010027{
10028 rettv->vval.v_number = curbuf->b_u_seq_cur;
10029}
10030
10031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 * "char2nr(string)" function
10033 */
10034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010035f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036{
10037#ifdef FEAT_MBYTE
10038 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010039 {
10040 int utf8 = 0;
10041
10042 if (argvars[1].v_type != VAR_UNKNOWN)
10043 utf8 = get_tv_number_chk(&argvars[1], NULL);
10044
10045 if (utf8)
10046 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10047 else
10048 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 else
10051#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053}
10054
10055/*
10056 * "cindent(lnum)" function
10057 */
10058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010059f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060{
10061#ifdef FEAT_CINDENT
10062 pos_T pos;
10063 linenr_T lnum;
10064
10065 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010066 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10068 {
10069 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010070 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071 curwin->w_cursor = pos;
10072 }
10073 else
10074#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010075 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076}
10077
10078/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010079 * "clearmatches()" function
10080 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010082f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010083{
10084#ifdef FEAT_SEARCH_EXTRA
10085 clear_matches(curwin);
10086#endif
10087}
10088
10089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 * "col(string)" function
10091 */
10092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010093f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094{
10095 colnr_T col = 0;
10096 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010097 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010099 fp = var2fpos(&argvars[0], FALSE, &fnum);
10100 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 {
10102 if (fp->col == MAXCOL)
10103 {
10104 /* '> can be MAXCOL, get the length of the line then */
10105 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010106 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 else
10108 col = MAXCOL;
10109 }
10110 else
10111 {
10112 col = fp->col + 1;
10113#ifdef FEAT_VIRTUALEDIT
10114 /* col(".") when the cursor is on the NUL at the end of the line
10115 * because of "coladd" can be seen as an extra column. */
10116 if (virtual_active() && fp == &curwin->w_cursor)
10117 {
10118 char_u *p = ml_get_cursor();
10119
10120 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10121 curwin->w_virtcol - curwin->w_cursor.coladd))
10122 {
10123# ifdef FEAT_MBYTE
10124 int l;
10125
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010126 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 col += l;
10128# else
10129 if (*p != NUL && p[1] == NUL)
10130 ++col;
10131# endif
10132 }
10133 }
10134#endif
10135 }
10136 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010137 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138}
10139
Bram Moolenaar572cb562005-08-05 21:35:02 +000010140#if defined(FEAT_INS_EXPAND)
10141/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010142 * "complete()" function
10143 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010145f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010146{
10147 int startcol;
10148
10149 if ((State & INSERT) == 0)
10150 {
10151 EMSG(_("E785: complete() can only be used in Insert mode"));
10152 return;
10153 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010154
10155 /* Check for undo allowed here, because if something was already inserted
10156 * the line was already saved for undo and this check isn't done. */
10157 if (!undo_allowed())
10158 return;
10159
Bram Moolenaarade00832006-03-10 21:46:58 +000010160 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10161 {
10162 EMSG(_(e_invarg));
10163 return;
10164 }
10165
10166 startcol = get_tv_number_chk(&argvars[0], NULL);
10167 if (startcol <= 0)
10168 return;
10169
10170 set_completion(startcol - 1, argvars[1].vval.v_list);
10171}
10172
10173/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010174 * "complete_add()" function
10175 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010177f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010178{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010179 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010180}
10181
10182/*
10183 * "complete_check()" function
10184 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010186f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010187{
10188 int saved = RedrawingDisabled;
10189
10190 RedrawingDisabled = 0;
10191 ins_compl_check_keys(0);
10192 rettv->vval.v_number = compl_interrupted;
10193 RedrawingDisabled = saved;
10194}
10195#endif
10196
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197/*
10198 * "confirm(message, buttons[, default [, type]])" function
10199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010201f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202{
10203#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10204 char_u *message;
10205 char_u *buttons = NULL;
10206 char_u buf[NUMBUFLEN];
10207 char_u buf2[NUMBUFLEN];
10208 int def = 1;
10209 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010210 char_u *typestr;
10211 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010213 message = get_tv_string_chk(&argvars[0]);
10214 if (message == NULL)
10215 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010216 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010218 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10219 if (buttons == NULL)
10220 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010221 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010223 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010224 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010226 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10227 if (typestr == NULL)
10228 error = TRUE;
10229 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010231 switch (TOUPPER_ASC(*typestr))
10232 {
10233 case 'E': type = VIM_ERROR; break;
10234 case 'Q': type = VIM_QUESTION; break;
10235 case 'I': type = VIM_INFO; break;
10236 case 'W': type = VIM_WARNING; break;
10237 case 'G': type = VIM_GENERIC; break;
10238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 }
10240 }
10241 }
10242 }
10243
10244 if (buttons == NULL || *buttons == NUL)
10245 buttons = (char_u *)_("&Ok");
10246
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010247 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010248 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010249 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250#endif
10251}
10252
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010253/*
10254 * "copy()" function
10255 */
10256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010257f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010258{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010259 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010260}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010262#ifdef FEAT_FLOAT
10263/*
10264 * "cos()" function
10265 */
10266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010267f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010268{
10269 float_T f;
10270
10271 rettv->v_type = VAR_FLOAT;
10272 if (get_float_arg(argvars, &f) == OK)
10273 rettv->vval.v_float = cos(f);
10274 else
10275 rettv->vval.v_float = 0.0;
10276}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010277
10278/*
10279 * "cosh()" function
10280 */
10281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010282f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010283{
10284 float_T f;
10285
10286 rettv->v_type = VAR_FLOAT;
10287 if (get_float_arg(argvars, &f) == OK)
10288 rettv->vval.v_float = cosh(f);
10289 else
10290 rettv->vval.v_float = 0.0;
10291}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010292#endif
10293
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010295 * "count()" function
10296 */
10297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010298f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010299{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010300 long n = 0;
10301 int ic = FALSE;
10302
Bram Moolenaare9a41262005-01-15 22:18:47 +000010303 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010304 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010305 listitem_T *li;
10306 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010307 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010308
Bram Moolenaare9a41262005-01-15 22:18:47 +000010309 if ((l = argvars[0].vval.v_list) != NULL)
10310 {
10311 li = l->lv_first;
10312 if (argvars[2].v_type != VAR_UNKNOWN)
10313 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010314 int error = FALSE;
10315
10316 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010317 if (argvars[3].v_type != VAR_UNKNOWN)
10318 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010319 idx = get_tv_number_chk(&argvars[3], &error);
10320 if (!error)
10321 {
10322 li = list_find(l, idx);
10323 if (li == NULL)
10324 EMSGN(_(e_listidx), idx);
10325 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010326 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010327 if (error)
10328 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010329 }
10330
10331 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010332 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010333 ++n;
10334 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010335 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010336 else if (argvars[0].v_type == VAR_DICT)
10337 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010338 int todo;
10339 dict_T *d;
10340 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010341
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010342 if ((d = argvars[0].vval.v_dict) != NULL)
10343 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010344 int error = FALSE;
10345
Bram Moolenaare9a41262005-01-15 22:18:47 +000010346 if (argvars[2].v_type != VAR_UNKNOWN)
10347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010348 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010349 if (argvars[3].v_type != VAR_UNKNOWN)
10350 EMSG(_(e_invarg));
10351 }
10352
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010353 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010354 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010355 {
10356 if (!HASHITEM_EMPTY(hi))
10357 {
10358 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010359 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010360 ++n;
10361 }
10362 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010363 }
10364 }
10365 else
10366 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010367 rettv->vval.v_number = n;
10368}
10369
10370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10372 *
10373 * Checks the existence of a cscope connection.
10374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010376f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377{
10378#ifdef FEAT_CSCOPE
10379 int num = 0;
10380 char_u *dbpath = NULL;
10381 char_u *prepend = NULL;
10382 char_u buf[NUMBUFLEN];
10383
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010384 if (argvars[0].v_type != VAR_UNKNOWN
10385 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010387 num = (int)get_tv_number(&argvars[0]);
10388 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010389 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010390 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391 }
10392
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010393 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394#endif
10395}
10396
10397/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010398 * "cursor(lnum, col)" function, or
10399 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010401 * Moves the cursor to the specified line and column.
10402 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010405f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406{
10407 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010408#ifdef FEAT_VIRTUALEDIT
10409 long coladd = 0;
10410#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010411 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010413 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010414 if (argvars[1].v_type == VAR_UNKNOWN)
10415 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010416 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010417 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010418
Bram Moolenaar493c1782014-05-28 14:34:46 +020010419 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010420 {
10421 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010422 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010423 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010424 line = pos.lnum;
10425 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010426#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010427 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010428#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010429 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010430 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010431 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010432 set_curswant = FALSE;
10433 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010434 }
10435 else
10436 {
10437 line = get_tv_lnum(argvars);
10438 col = get_tv_number_chk(&argvars[1], NULL);
10439#ifdef FEAT_VIRTUALEDIT
10440 if (argvars[2].v_type != VAR_UNKNOWN)
10441 coladd = get_tv_number_chk(&argvars[2], NULL);
10442#endif
10443 }
10444 if (line < 0 || col < 0
10445#ifdef FEAT_VIRTUALEDIT
10446 || coladd < 0
10447#endif
10448 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010449 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 if (line > 0)
10451 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 if (col > 0)
10453 curwin->w_cursor.col = col - 1;
10454#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010455 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456#endif
10457
10458 /* Make sure the cursor is in a valid position. */
10459 check_cursor();
10460#ifdef FEAT_MBYTE
10461 /* Correct cursor for multi-byte character. */
10462 if (has_mbyte)
10463 mb_adjust_cursor();
10464#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010465
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010466 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010467 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468}
10469
10470/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010471 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 */
10473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010474f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010476 int noref = 0;
10477
10478 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010479 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010480 if (noref < 0 || noref > 1)
10481 EMSG(_(e_invarg));
10482 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010483 {
10484 current_copyID += COPYID_INC;
10485 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487}
10488
10489/*
10490 * "delete()" function
10491 */
10492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010493f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010495 char_u nbuf[NUMBUFLEN];
10496 char_u *name;
10497 char_u *flags;
10498
10499 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010501 return;
10502
10503 name = get_tv_string(&argvars[0]);
10504 if (name == NULL || *name == NUL)
10505 {
10506 EMSG(_(e_invarg));
10507 return;
10508 }
10509
10510 if (argvars[1].v_type != VAR_UNKNOWN)
10511 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010513 flags = (char_u *)"";
10514
10515 if (*flags == NUL)
10516 /* delete a file */
10517 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10518 else if (STRCMP(flags, "d") == 0)
10519 /* delete an empty directory */
10520 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10521 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010522 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010523 rettv->vval.v_number = delete_recursive(name);
10524 else
10525 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526}
10527
10528/*
10529 * "did_filetype()" function
10530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010532f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533{
10534#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010535 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536#endif
10537}
10538
10539/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010540 * "diff_filler()" function
10541 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010543f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010544{
10545#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010546 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010547#endif
10548}
10549
10550/*
10551 * "diff_hlID()" function
10552 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010554f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010555{
10556#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010557 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010558 static linenr_T prev_lnum = 0;
10559 static int changedtick = 0;
10560 static int fnum = 0;
10561 static int change_start = 0;
10562 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010563 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010564 int filler_lines;
10565 int col;
10566
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010567 if (lnum < 0) /* ignore type error in {lnum} arg */
10568 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010569 if (lnum != prev_lnum
10570 || changedtick != curbuf->b_changedtick
10571 || fnum != curbuf->b_fnum)
10572 {
10573 /* New line, buffer, change: need to get the values. */
10574 filler_lines = diff_check(curwin, lnum);
10575 if (filler_lines < 0)
10576 {
10577 if (filler_lines == -1)
10578 {
10579 change_start = MAXCOL;
10580 change_end = -1;
10581 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10582 hlID = HLF_ADD; /* added line */
10583 else
10584 hlID = HLF_CHD; /* changed line */
10585 }
10586 else
10587 hlID = HLF_ADD; /* added line */
10588 }
10589 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010590 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010591 prev_lnum = lnum;
10592 changedtick = curbuf->b_changedtick;
10593 fnum = curbuf->b_fnum;
10594 }
10595
10596 if (hlID == HLF_CHD || hlID == HLF_TXD)
10597 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010598 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010599 if (col >= change_start && col <= change_end)
10600 hlID = HLF_TXD; /* changed text */
10601 else
10602 hlID = HLF_CHD; /* changed line */
10603 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010604 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010605#endif
10606}
10607
10608/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010609 * "empty({expr})" function
10610 */
10611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010612f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010613{
10614 int n;
10615
10616 switch (argvars[0].v_type)
10617 {
10618 case VAR_STRING:
10619 case VAR_FUNC:
10620 n = argvars[0].vval.v_string == NULL
10621 || *argvars[0].vval.v_string == NUL;
10622 break;
10623 case VAR_NUMBER:
10624 n = argvars[0].vval.v_number == 0;
10625 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010626 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010627#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010628 n = argvars[0].vval.v_float == 0.0;
10629 break;
10630#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010631 case VAR_LIST:
10632 n = argvars[0].vval.v_list == NULL
10633 || argvars[0].vval.v_list->lv_first == NULL;
10634 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010635 case VAR_DICT:
10636 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010637 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010638 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010639 case VAR_SPECIAL:
10640 n = argvars[0].vval.v_number != VVAL_TRUE;
10641 break;
10642
Bram Moolenaar835dc632016-02-07 14:27:38 +010010643 case VAR_JOB:
10644#ifdef FEAT_JOB
10645 n = argvars[0].vval.v_job->jv_status != JOB_STARTED;
10646 break;
10647#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010648 case VAR_UNKNOWN:
10649 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10650 n = TRUE;
10651 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010652 }
10653
10654 rettv->vval.v_number = n;
10655}
10656
10657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 * "escape({string}, {chars})" function
10659 */
10660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010661f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662{
10663 char_u buf[NUMBUFLEN];
10664
Bram Moolenaar758711c2005-02-02 23:11:38 +000010665 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10666 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010667 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668}
10669
10670/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010671 * "eval()" function
10672 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010674f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010675{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010676 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010677
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010678 s = get_tv_string_chk(&argvars[0]);
10679 if (s != NULL)
10680 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010681
Bram Moolenaar615b9972015-01-14 17:15:05 +010010682 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010683 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10684 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010685 if (p != NULL && !aborting())
10686 EMSG2(_(e_invexpr2), p);
10687 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010688 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010689 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010690 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010691 else if (*s != NUL)
10692 EMSG(_(e_trailing));
10693}
10694
10695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696 * "eventhandler()" function
10697 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010699f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010701 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702}
10703
10704/*
10705 * "executable()" function
10706 */
10707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010708f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010710 char_u *name = get_tv_string(&argvars[0]);
10711
10712 /* Check in $PATH and also check directly if there is a directory name. */
10713 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10714 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010715}
10716
10717/*
10718 * "exepath()" function
10719 */
10720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010721f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010722{
10723 char_u *p = NULL;
10724
Bram Moolenaarb5971142015-03-21 17:32:19 +010010725 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010726 rettv->v_type = VAR_STRING;
10727 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728}
10729
10730/*
10731 * "exists()" function
10732 */
10733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010734f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735{
10736 char_u *p;
10737 char_u *name;
10738 int n = FALSE;
10739 int len = 0;
10740
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010741 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 if (*p == '$') /* environment variable */
10743 {
10744 /* first try "normal" environment variables (fast) */
10745 if (mch_getenv(p + 1) != NULL)
10746 n = TRUE;
10747 else
10748 {
10749 /* try expanding things like $VIM and ${HOME} */
10750 p = expand_env_save(p);
10751 if (p != NULL && *p != '$')
10752 n = TRUE;
10753 vim_free(p);
10754 }
10755 }
10756 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010757 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010758 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010759 if (*skipwhite(p) != NUL)
10760 n = FALSE; /* trailing garbage */
10761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762 else if (*p == '*') /* internal or user defined function */
10763 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010764 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 }
10766 else if (*p == ':')
10767 {
10768 n = cmd_exists(p + 1);
10769 }
10770 else if (*p == '#')
10771 {
10772#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010773 if (p[1] == '#')
10774 n = autocmd_supported(p + 2);
10775 else
10776 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777#endif
10778 }
10779 else /* internal variable */
10780 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010781 char_u *tofree;
10782 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010784 /* get_name_len() takes care of expanding curly braces */
10785 name = p;
10786 len = get_name_len(&p, &tofree, TRUE, FALSE);
10787 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010789 if (tofree != NULL)
10790 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010791 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010792 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010794 /* handle d.key, l[idx], f(expr) */
10795 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10796 if (n)
10797 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798 }
10799 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010800 if (*p != NUL)
10801 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010803 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804 }
10805
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010806 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807}
10808
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010809#ifdef FEAT_FLOAT
10810/*
10811 * "exp()" function
10812 */
10813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010814f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010815{
10816 float_T f;
10817
10818 rettv->v_type = VAR_FLOAT;
10819 if (get_float_arg(argvars, &f) == OK)
10820 rettv->vval.v_float = exp(f);
10821 else
10822 rettv->vval.v_float = 0.0;
10823}
10824#endif
10825
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826/*
10827 * "expand()" function
10828 */
10829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010830f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831{
10832 char_u *s;
10833 int len;
10834 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010835 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010837 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010838 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010840 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010841 if (argvars[1].v_type != VAR_UNKNOWN
10842 && argvars[2].v_type != VAR_UNKNOWN
10843 && get_tv_number_chk(&argvars[2], &error)
10844 && !error)
10845 {
10846 rettv->v_type = VAR_LIST;
10847 rettv->vval.v_list = NULL;
10848 }
10849
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010850 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 if (*s == '%' || *s == '#' || *s == '<')
10852 {
10853 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010854 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010856 if (rettv->v_type == VAR_LIST)
10857 {
10858 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10859 list_append_string(rettv->vval.v_list, result, -1);
10860 else
10861 vim_free(result);
10862 }
10863 else
10864 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865 }
10866 else
10867 {
10868 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010869 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010870 if (argvars[1].v_type != VAR_UNKNOWN
10871 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010872 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010873 if (!error)
10874 {
10875 ExpandInit(&xpc);
10876 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010877 if (p_wic)
10878 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010879 if (rettv->v_type == VAR_STRING)
10880 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10881 options, WILD_ALL);
10882 else if (rettv_list_alloc(rettv) != FAIL)
10883 {
10884 int i;
10885
10886 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10887 for (i = 0; i < xpc.xp_numfiles; i++)
10888 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10889 ExpandCleanup(&xpc);
10890 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010891 }
10892 else
10893 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 }
10895}
10896
10897/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010898 * Go over all entries in "d2" and add them to "d1".
10899 * When "action" is "error" then a duplicate key is an error.
10900 * When "action" is "force" then a duplicate key is overwritten.
10901 * Otherwise duplicate keys are ignored ("action" is "keep").
10902 */
10903 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010904dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020010905{
10906 dictitem_T *di1;
10907 hashitem_T *hi2;
10908 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010909 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010910
10911 todo = (int)d2->dv_hashtab.ht_used;
10912 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10913 {
10914 if (!HASHITEM_EMPTY(hi2))
10915 {
10916 --todo;
10917 di1 = dict_find(d1, hi2->hi_key, -1);
10918 if (d1->dv_scope != 0)
10919 {
10920 /* Disallow replacing a builtin function in l: and g:.
10921 * Check the key to be valid when adding to any
10922 * scope. */
10923 if (d1->dv_scope == VAR_DEF_SCOPE
10924 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10925 && var_check_func_name(hi2->hi_key,
10926 di1 == NULL))
10927 break;
10928 if (!valid_varname(hi2->hi_key))
10929 break;
10930 }
10931 if (di1 == NULL)
10932 {
10933 di1 = dictitem_copy(HI2DI(hi2));
10934 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10935 dictitem_free(di1);
10936 }
10937 else if (*action == 'e')
10938 {
10939 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10940 break;
10941 }
10942 else if (*action == 'f' && HI2DI(hi2) != di1)
10943 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010944 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10945 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010946 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010947 clear_tv(&di1->di_tv);
10948 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10949 }
10950 }
10951 }
10952}
10953
10954/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010955 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010956 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010957 */
10958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010959f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010960{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010961 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010962
Bram Moolenaare9a41262005-01-15 22:18:47 +000010963 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010964 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010965 list_T *l1, *l2;
10966 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010967 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010968 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010969
Bram Moolenaare9a41262005-01-15 22:18:47 +000010970 l1 = argvars[0].vval.v_list;
10971 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010972 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010973 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010974 {
10975 if (argvars[2].v_type != VAR_UNKNOWN)
10976 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010977 before = get_tv_number_chk(&argvars[2], &error);
10978 if (error)
10979 return; /* type error; errmsg already given */
10980
Bram Moolenaar758711c2005-02-02 23:11:38 +000010981 if (before == l1->lv_len)
10982 item = NULL;
10983 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010984 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010985 item = list_find(l1, before);
10986 if (item == NULL)
10987 {
10988 EMSGN(_(e_listidx), before);
10989 return;
10990 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010991 }
10992 }
10993 else
10994 item = NULL;
10995 list_extend(l1, l2, item);
10996
Bram Moolenaare9a41262005-01-15 22:18:47 +000010997 copy_tv(&argvars[0], rettv);
10998 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010999 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011000 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11001 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011002 dict_T *d1, *d2;
11003 char_u *action;
11004 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011005
11006 d1 = argvars[0].vval.v_dict;
11007 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011008 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011009 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011010 {
11011 /* Check the third argument. */
11012 if (argvars[2].v_type != VAR_UNKNOWN)
11013 {
11014 static char *(av[]) = {"keep", "force", "error"};
11015
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011016 action = get_tv_string_chk(&argvars[2]);
11017 if (action == NULL)
11018 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011019 for (i = 0; i < 3; ++i)
11020 if (STRCMP(action, av[i]) == 0)
11021 break;
11022 if (i == 3)
11023 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011024 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011025 return;
11026 }
11027 }
11028 else
11029 action = (char_u *)"force";
11030
Bram Moolenaara9922d62013-05-30 13:01:18 +020011031 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011032
Bram Moolenaare9a41262005-01-15 22:18:47 +000011033 copy_tv(&argvars[0], rettv);
11034 }
11035 }
11036 else
11037 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011038}
11039
11040/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011041 * "feedkeys()" function
11042 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011044f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011045{
11046 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011047 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011048 char_u *keys, *flags;
11049 char_u nbuf[NUMBUFLEN];
11050 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011051 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011052 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011053
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011054 /* This is not allowed in the sandbox. If the commands would still be
11055 * executed in the sandbox it would be OK, but it probably happens later,
11056 * when "sandbox" is no longer set. */
11057 if (check_secure())
11058 return;
11059
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011060 keys = get_tv_string(&argvars[0]);
11061 if (*keys != NUL)
11062 {
11063 if (argvars[1].v_type != VAR_UNKNOWN)
11064 {
11065 flags = get_tv_string_buf(&argvars[1], nbuf);
11066 for ( ; *flags != NUL; ++flags)
11067 {
11068 switch (*flags)
11069 {
11070 case 'n': remap = FALSE; break;
11071 case 'm': remap = TRUE; break;
11072 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011073 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011074 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011075 }
11076 }
11077 }
11078
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011079 /* Need to escape K_SPECIAL and CSI before putting the string in the
11080 * typeahead buffer. */
11081 keys_esc = vim_strsave_escape_csi(keys);
11082 if (keys_esc != NULL)
11083 {
11084 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011085 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011086 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011087 if (vgetc_busy)
11088 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011089 if (execute)
11090 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011091 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011092 }
11093}
11094
11095/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096 * "filereadable()" function
11097 */
11098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011099f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011101 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102 char_u *p;
11103 int n;
11104
Bram Moolenaarc236c162008-07-13 17:41:49 +000011105#ifndef O_NONBLOCK
11106# define O_NONBLOCK 0
11107#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011108 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011109 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11110 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111 {
11112 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011113 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114 }
11115 else
11116 n = FALSE;
11117
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011118 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119}
11120
11121/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011122 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123 * rights to write into.
11124 */
11125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011126f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011128 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129}
11130
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011131 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011132findfilendir(
11133 typval_T *argvars UNUSED,
11134 typval_T *rettv,
11135 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011136{
11137#ifdef FEAT_SEARCHPATH
11138 char_u *fname;
11139 char_u *fresult = NULL;
11140 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11141 char_u *p;
11142 char_u pathbuf[NUMBUFLEN];
11143 int count = 1;
11144 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011145 int error = FALSE;
11146#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011147
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011148 rettv->vval.v_string = NULL;
11149 rettv->v_type = VAR_STRING;
11150
11151#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011152 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011153
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011154 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011155 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011156 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11157 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011158 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011159 else
11160 {
11161 if (*p != NUL)
11162 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011163
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011164 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011165 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011166 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011167 }
11168
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011169 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11170 error = TRUE;
11171
11172 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011173 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011174 do
11175 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011176 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011177 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011178 fresult = find_file_in_path_option(first ? fname : NULL,
11179 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011180 0, first, path,
11181 find_what,
11182 curbuf->b_ffname,
11183 find_what == FINDFILE_DIR
11184 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011185 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011186
11187 if (fresult != NULL && rettv->v_type == VAR_LIST)
11188 list_append_string(rettv->vval.v_list, fresult, -1);
11189
11190 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011191 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011192
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011193 if (rettv->v_type == VAR_STRING)
11194 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011195#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011196}
11197
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011198static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11199static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011200
11201/*
11202 * Implementation of map() and filter().
11203 */
11204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011205filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011206{
11207 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011208 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011209 listitem_T *li, *nli;
11210 list_T *l = NULL;
11211 dictitem_T *di;
11212 hashtab_T *ht;
11213 hashitem_T *hi;
11214 dict_T *d = NULL;
11215 typval_T save_val;
11216 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011217 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011218 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011219 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011220 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011221 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011222 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011223 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011224
Bram Moolenaare9a41262005-01-15 22:18:47 +000011225 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011226 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011227 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011228 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011229 return;
11230 }
11231 else if (argvars[0].v_type == VAR_DICT)
11232 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011233 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011234 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011235 return;
11236 }
11237 else
11238 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011239 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011240 return;
11241 }
11242
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011243 expr = get_tv_string_buf_chk(&argvars[1], buf);
11244 /* On type errors, the preceding call has already displayed an error
11245 * message. Avoid a misleading error message for an empty string that
11246 * was not passed as argument. */
11247 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011249 prepare_vimvar(VV_VAL, &save_val);
11250 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011251
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011252 /* We reset "did_emsg" to be able to detect whether an error
11253 * occurred during evaluation of the expression. */
11254 save_did_emsg = did_emsg;
11255 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011256
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011257 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011258 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011259 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011260 vimvars[VV_KEY].vv_type = VAR_STRING;
11261
11262 ht = &d->dv_hashtab;
11263 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011264 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011265 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011266 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011267 if (!HASHITEM_EMPTY(hi))
11268 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011269 int r;
11270
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011271 --todo;
11272 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011273 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011274 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11275 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011276 break;
11277 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011278 r = filter_map_one(&di->di_tv, expr, map, &rem);
11279 clear_tv(&vimvars[VV_KEY].vv_tv);
11280 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011281 break;
11282 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011283 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011284 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11285 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011286 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011287 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011288 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011289 }
11290 }
11291 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011292 }
11293 else
11294 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011295 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11296
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011297 for (li = l->lv_first; li != NULL; li = nli)
11298 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011299 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011300 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011301 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011302 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011303 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011304 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011305 break;
11306 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011307 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011308 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011309 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011310 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011311
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011312 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011313 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011314
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011315 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011316 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011317
11318 copy_tv(&argvars[0], rettv);
11319}
11320
11321 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011322filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011323{
Bram Moolenaar33570922005-01-25 22:26:29 +000011324 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011325 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011326 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011327
Bram Moolenaar33570922005-01-25 22:26:29 +000011328 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011329 s = expr;
11330 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011331 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011332 if (*s != NUL) /* check for trailing chars after expr */
11333 {
11334 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011335 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011336 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011337 }
11338 if (map)
11339 {
11340 /* map(): replace the list item value */
11341 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011342 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011343 *tv = rettv;
11344 }
11345 else
11346 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011347 int error = FALSE;
11348
Bram Moolenaare9a41262005-01-15 22:18:47 +000011349 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011350 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011351 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011352 /* On type error, nothing has been removed; return FAIL to stop the
11353 * loop. The error message was given by get_tv_number_chk(). */
11354 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011355 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011356 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011357 retval = OK;
11358theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011359 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011360 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011361}
11362
11363/*
11364 * "filter()" function
11365 */
11366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011367f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011368{
11369 filter_map(argvars, rettv, FALSE);
11370}
11371
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011372/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011373 * "finddir({fname}[, {path}[, {count}]])" function
11374 */
11375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011376f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011377{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011378 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011379}
11380
11381/*
11382 * "findfile({fname}[, {path}[, {count}]])" function
11383 */
11384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011385f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011386{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011387 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011388}
11389
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011390#ifdef FEAT_FLOAT
11391/*
11392 * "float2nr({float})" function
11393 */
11394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011395f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011396{
11397 float_T f;
11398
11399 if (get_float_arg(argvars, &f) == OK)
11400 {
11401 if (f < -0x7fffffff)
11402 rettv->vval.v_number = -0x7fffffff;
11403 else if (f > 0x7fffffff)
11404 rettv->vval.v_number = 0x7fffffff;
11405 else
11406 rettv->vval.v_number = (varnumber_T)f;
11407 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011408}
11409
11410/*
11411 * "floor({float})" function
11412 */
11413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011414f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011415{
11416 float_T f;
11417
11418 rettv->v_type = VAR_FLOAT;
11419 if (get_float_arg(argvars, &f) == OK)
11420 rettv->vval.v_float = floor(f);
11421 else
11422 rettv->vval.v_float = 0.0;
11423}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011424
11425/*
11426 * "fmod()" function
11427 */
11428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011429f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011430{
11431 float_T fx, fy;
11432
11433 rettv->v_type = VAR_FLOAT;
11434 if (get_float_arg(argvars, &fx) == OK
11435 && get_float_arg(&argvars[1], &fy) == OK)
11436 rettv->vval.v_float = fmod(fx, fy);
11437 else
11438 rettv->vval.v_float = 0.0;
11439}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011440#endif
11441
Bram Moolenaar0d660222005-01-07 21:51:51 +000011442/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011443 * "fnameescape({string})" function
11444 */
11445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011446f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011447{
11448 rettv->vval.v_string = vim_strsave_fnameescape(
11449 get_tv_string(&argvars[0]), FALSE);
11450 rettv->v_type = VAR_STRING;
11451}
11452
11453/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454 * "fnamemodify({fname}, {mods})" function
11455 */
11456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011457f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458{
11459 char_u *fname;
11460 char_u *mods;
11461 int usedlen = 0;
11462 int len;
11463 char_u *fbuf = NULL;
11464 char_u buf[NUMBUFLEN];
11465
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011466 fname = get_tv_string_chk(&argvars[0]);
11467 mods = get_tv_string_buf_chk(&argvars[1], buf);
11468 if (fname == NULL || mods == NULL)
11469 fname = NULL;
11470 else
11471 {
11472 len = (int)STRLEN(fname);
11473 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011476 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011478 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011480 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011481 vim_free(fbuf);
11482}
11483
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011484static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485
11486/*
11487 * "foldclosed()" function
11488 */
11489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011490foldclosed_both(
11491 typval_T *argvars UNUSED,
11492 typval_T *rettv,
11493 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494{
11495#ifdef FEAT_FOLDING
11496 linenr_T lnum;
11497 linenr_T first, last;
11498
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011499 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011500 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11501 {
11502 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11503 {
11504 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011505 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011507 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011508 return;
11509 }
11510 }
11511#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011512 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513}
11514
11515/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011516 * "foldclosed()" function
11517 */
11518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011519f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011520{
11521 foldclosed_both(argvars, rettv, FALSE);
11522}
11523
11524/*
11525 * "foldclosedend()" function
11526 */
11527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011528f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011529{
11530 foldclosed_both(argvars, rettv, TRUE);
11531}
11532
11533/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534 * "foldlevel()" function
11535 */
11536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011537f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011538{
11539#ifdef FEAT_FOLDING
11540 linenr_T lnum;
11541
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011542 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011543 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011544 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011546}
11547
11548/*
11549 * "foldtext()" function
11550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011552f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553{
11554#ifdef FEAT_FOLDING
11555 linenr_T lnum;
11556 char_u *s;
11557 char_u *r;
11558 int len;
11559 char *txt;
11560#endif
11561
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011562 rettv->v_type = VAR_STRING;
11563 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011565 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11566 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11567 <= curbuf->b_ml.ml_line_count
11568 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569 {
11570 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011571 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11572 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573 {
11574 if (!linewhite(lnum))
11575 break;
11576 ++lnum;
11577 }
11578
11579 /* Find interesting text in this line. */
11580 s = skipwhite(ml_get(lnum));
11581 /* skip C comment-start */
11582 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011583 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011585 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011586 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011587 {
11588 s = skipwhite(ml_get(lnum + 1));
11589 if (*s == '*')
11590 s = skipwhite(s + 1);
11591 }
11592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011593 txt = _("+-%s%3ld lines: ");
11594 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011595 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011596 + 20 /* for %3ld */
11597 + STRLEN(s))); /* concatenated */
11598 if (r != NULL)
11599 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011600 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11601 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11602 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603 len = (int)STRLEN(r);
11604 STRCAT(r, s);
11605 /* remove 'foldmarker' and 'commentstring' */
11606 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011607 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608 }
11609 }
11610#endif
11611}
11612
11613/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011614 * "foldtextresult(lnum)" function
11615 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011617f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011618{
11619#ifdef FEAT_FOLDING
11620 linenr_T lnum;
11621 char_u *text;
11622 char_u buf[51];
11623 foldinfo_T foldinfo;
11624 int fold_count;
11625#endif
11626
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011627 rettv->v_type = VAR_STRING;
11628 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011629#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011630 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011631 /* treat illegal types and illegal string values for {lnum} the same */
11632 if (lnum < 0)
11633 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011634 fold_count = foldedCount(curwin, lnum, &foldinfo);
11635 if (fold_count > 0)
11636 {
11637 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11638 &foldinfo, buf);
11639 if (text == buf)
11640 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011641 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011642 }
11643#endif
11644}
11645
11646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011647 * "foreground()" function
11648 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011650f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011651{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652#ifdef FEAT_GUI
11653 if (gui.in_use)
11654 gui_mch_set_foreground();
11655#else
11656# ifdef WIN32
11657 win32_set_foreground();
11658# endif
11659#endif
11660}
11661
11662/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011663 * "function()" function
11664 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011666f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011667{
11668 char_u *s;
11669
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011670 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011671 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011672 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011673 /* Don't check an autoload name for existence here. */
11674 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011675 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011676 else
11677 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011678 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011679 {
11680 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011681 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011682
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011683 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11684 * also be called from another script. Using trans_function_name()
11685 * would also work, but some plugins depend on the name being
11686 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011687 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011688 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011689 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011690 if (rettv->vval.v_string != NULL)
11691 {
11692 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011693 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011694 }
11695 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011696 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011697 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011698 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011699 }
11700}
11701
11702/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011703 * "garbagecollect()" function
11704 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011706f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011707{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011708 /* This is postponed until we are back at the toplevel, because we may be
11709 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11710 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011711
11712 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11713 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011714}
11715
11716/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011717 * "get()" function
11718 */
11719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011720f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011721{
Bram Moolenaar33570922005-01-25 22:26:29 +000011722 listitem_T *li;
11723 list_T *l;
11724 dictitem_T *di;
11725 dict_T *d;
11726 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011727
Bram Moolenaare9a41262005-01-15 22:18:47 +000011728 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011729 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011730 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011731 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011732 int error = FALSE;
11733
11734 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11735 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011736 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011737 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011738 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011739 else if (argvars[0].v_type == VAR_DICT)
11740 {
11741 if ((d = argvars[0].vval.v_dict) != NULL)
11742 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011743 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011744 if (di != NULL)
11745 tv = &di->di_tv;
11746 }
11747 }
11748 else
11749 EMSG2(_(e_listdictarg), "get()");
11750
11751 if (tv == NULL)
11752 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011753 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011754 copy_tv(&argvars[2], rettv);
11755 }
11756 else
11757 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011758}
11759
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011760static 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 +000011761
11762/*
11763 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011764 * Return a range (from start to end) of lines in rettv from the specified
11765 * buffer.
11766 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011767 */
11768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011769get_buffer_lines(
11770 buf_T *buf,
11771 linenr_T start,
11772 linenr_T end,
11773 int retlist,
11774 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011775{
11776 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011777
Bram Moolenaar959a1432013-12-14 12:17:38 +010011778 rettv->v_type = VAR_STRING;
11779 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011780 if (retlist && rettv_list_alloc(rettv) == FAIL)
11781 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011782
11783 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11784 return;
11785
11786 if (!retlist)
11787 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011788 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11789 p = ml_get_buf(buf, start, FALSE);
11790 else
11791 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011792 rettv->vval.v_string = vim_strsave(p);
11793 }
11794 else
11795 {
11796 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011797 return;
11798
11799 if (start < 1)
11800 start = 1;
11801 if (end > buf->b_ml.ml_line_count)
11802 end = buf->b_ml.ml_line_count;
11803 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011804 if (list_append_string(rettv->vval.v_list,
11805 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011806 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011807 }
11808}
11809
11810/*
11811 * "getbufline()" function
11812 */
11813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011814f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011815{
11816 linenr_T lnum;
11817 linenr_T end;
11818 buf_T *buf;
11819
11820 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11821 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011822 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011823 --emsg_off;
11824
Bram Moolenaar661b1822005-07-28 22:36:45 +000011825 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011826 if (argvars[2].v_type == VAR_UNKNOWN)
11827 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011828 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011829 end = get_tv_lnum_buf(&argvars[2], buf);
11830
Bram Moolenaar342337a2005-07-21 21:11:17 +000011831 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011832}
11833
Bram Moolenaar0d660222005-01-07 21:51:51 +000011834/*
11835 * "getbufvar()" function
11836 */
11837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011838f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011839{
11840 buf_T *buf;
11841 buf_T *save_curbuf;
11842 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011843 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011844 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011845
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011846 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11847 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011848 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011849 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011850
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011851 rettv->v_type = VAR_STRING;
11852 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011853
11854 if (buf != NULL && varname != NULL)
11855 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011856 /* set curbuf to be our buf, temporarily */
11857 save_curbuf = curbuf;
11858 curbuf = buf;
11859
Bram Moolenaar0d660222005-01-07 21:51:51 +000011860 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011861 {
11862 if (get_option_tv(&varname, rettv, TRUE) == OK)
11863 done = TRUE;
11864 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011865 else if (STRCMP(varname, "changedtick") == 0)
11866 {
11867 rettv->v_type = VAR_NUMBER;
11868 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011869 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011870 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011871 else
11872 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011873 /* Look up the variable. */
11874 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11875 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11876 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011877 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011878 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011879 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011880 done = TRUE;
11881 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011882 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011883
11884 /* restore previous notion of curbuf */
11885 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011886 }
11887
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011888 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11889 /* use the default value */
11890 copy_tv(&argvars[2], rettv);
11891
Bram Moolenaar0d660222005-01-07 21:51:51 +000011892 --emsg_off;
11893}
11894
11895/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896 * "getchar()" function
11897 */
11898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011899f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011900{
11901 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011902 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011903
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011904 /* Position the cursor. Needed after a message that ends in a space. */
11905 windgoto(msg_row, msg_col);
11906
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907 ++no_mapping;
11908 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011909 for (;;)
11910 {
11911 if (argvars[0].v_type == VAR_UNKNOWN)
11912 /* getchar(): blocking wait. */
11913 n = safe_vgetc();
11914 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11915 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011916 n = vpeekc_any();
11917 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011918 /* illegal argument or getchar(0) and no char avail: return zero */
11919 n = 0;
11920 else
11921 /* getchar(0) and char avail: return char */
11922 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011923
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011924 if (n == K_IGNORE)
11925 continue;
11926 break;
11927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928 --no_mapping;
11929 --allow_keys;
11930
Bram Moolenaar219b8702006-11-01 14:32:36 +000011931 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11932 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11933 vimvars[VV_MOUSE_COL].vv_nr = 0;
11934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011935 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936 if (IS_SPECIAL(n) || mod_mask != 0)
11937 {
11938 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11939 int i = 0;
11940
11941 /* Turn a special key into three bytes, plus modifier. */
11942 if (mod_mask != 0)
11943 {
11944 temp[i++] = K_SPECIAL;
11945 temp[i++] = KS_MODIFIER;
11946 temp[i++] = mod_mask;
11947 }
11948 if (IS_SPECIAL(n))
11949 {
11950 temp[i++] = K_SPECIAL;
11951 temp[i++] = K_SECOND(n);
11952 temp[i++] = K_THIRD(n);
11953 }
11954#ifdef FEAT_MBYTE
11955 else if (has_mbyte)
11956 i += (*mb_char2bytes)(n, temp + i);
11957#endif
11958 else
11959 temp[i++] = n;
11960 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011961 rettv->v_type = VAR_STRING;
11962 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011963
11964#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011965 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011966 {
11967 int row = mouse_row;
11968 int col = mouse_col;
11969 win_T *win;
11970 linenr_T lnum;
11971# ifdef FEAT_WINDOWS
11972 win_T *wp;
11973# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011974 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011975
11976 if (row >= 0 && col >= 0)
11977 {
11978 /* Find the window at the mouse coordinates and compute the
11979 * text position. */
11980 win = mouse_find_win(&row, &col);
11981 (void)mouse_comp_pos(win, &row, &col, &lnum);
11982# ifdef FEAT_WINDOWS
11983 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011984 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011985# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011986 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011987 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11988 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11989 }
11990 }
11991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992 }
11993}
11994
11995/*
11996 * "getcharmod()" function
11997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011999f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012001 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002}
12003
12004/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012005 * "getcharsearch()" function
12006 */
12007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012008f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012009{
12010 if (rettv_dict_alloc(rettv) != FAIL)
12011 {
12012 dict_T *dict = rettv->vval.v_dict;
12013
12014 dict_add_nr_str(dict, "char", 0L, last_csearch());
12015 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12016 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12017 }
12018}
12019
12020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021 * "getcmdline()" function
12022 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012024f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012025{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012026 rettv->v_type = VAR_STRING;
12027 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028}
12029
12030/*
12031 * "getcmdpos()" function
12032 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012034f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012035{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012036 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037}
12038
12039/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012040 * "getcmdtype()" function
12041 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012043f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012044{
12045 rettv->v_type = VAR_STRING;
12046 rettv->vval.v_string = alloc(2);
12047 if (rettv->vval.v_string != NULL)
12048 {
12049 rettv->vval.v_string[0] = get_cmdline_type();
12050 rettv->vval.v_string[1] = NUL;
12051 }
12052}
12053
12054/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012055 * "getcmdwintype()" function
12056 */
12057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012058f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012059{
12060 rettv->v_type = VAR_STRING;
12061 rettv->vval.v_string = NULL;
12062#ifdef FEAT_CMDWIN
12063 rettv->vval.v_string = alloc(2);
12064 if (rettv->vval.v_string != NULL)
12065 {
12066 rettv->vval.v_string[0] = cmdwin_type;
12067 rettv->vval.v_string[1] = NUL;
12068 }
12069#endif
12070}
12071
12072/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073 * "getcwd()" function
12074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012076f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012078 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012079 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012081 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012082 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012083
12084 wp = find_tabwin(&argvars[0], &argvars[1]);
12085 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012087 if (wp->w_localdir != NULL)
12088 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12089 else if(globaldir != NULL)
12090 rettv->vval.v_string = vim_strsave(globaldir);
12091 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012092 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012093 cwd = alloc(MAXPATHL);
12094 if (cwd != NULL)
12095 {
12096 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12097 rettv->vval.v_string = vim_strsave(cwd);
12098 vim_free(cwd);
12099 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012100 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012101#ifdef BACKSLASH_IN_FILENAME
12102 if (rettv->vval.v_string != NULL)
12103 slash_adjust(rettv->vval.v_string);
12104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012105 }
12106}
12107
12108/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012109 * "getfontname()" function
12110 */
12111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012112f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012113{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012114 rettv->v_type = VAR_STRING;
12115 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012116#ifdef FEAT_GUI
12117 if (gui.in_use)
12118 {
12119 GuiFont font;
12120 char_u *name = NULL;
12121
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012122 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012123 {
12124 /* Get the "Normal" font. Either the name saved by
12125 * hl_set_font_name() or from the font ID. */
12126 font = gui.norm_font;
12127 name = hl_get_font_name();
12128 }
12129 else
12130 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012131 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012132 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12133 return;
12134 font = gui_mch_get_font(name, FALSE);
12135 if (font == NOFONT)
12136 return; /* Invalid font name, return empty string. */
12137 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012138 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012139 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012140 gui_mch_free_font(font);
12141 }
12142#endif
12143}
12144
12145/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012146 * "getfperm({fname})" function
12147 */
12148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012149f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012150{
12151 char_u *fname;
12152 struct stat st;
12153 char_u *perm = NULL;
12154 char_u flags[] = "rwx";
12155 int i;
12156
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012157 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012159 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012160 if (mch_stat((char *)fname, &st) >= 0)
12161 {
12162 perm = vim_strsave((char_u *)"---------");
12163 if (perm != NULL)
12164 {
12165 for (i = 0; i < 9; i++)
12166 {
12167 if (st.st_mode & (1 << (8 - i)))
12168 perm[i] = flags[i % 3];
12169 }
12170 }
12171 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012172 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012173}
12174
12175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012176 * "getfsize({fname})" function
12177 */
12178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012179f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012180{
12181 char_u *fname;
12182 struct stat st;
12183
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012184 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012186 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012187
12188 if (mch_stat((char *)fname, &st) >= 0)
12189 {
12190 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012191 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012192 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012193 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012194 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012195
12196 /* non-perfect check for overflow */
12197 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12198 rettv->vval.v_number = -2;
12199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200 }
12201 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012202 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012203}
12204
12205/*
12206 * "getftime({fname})" function
12207 */
12208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012209f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012210{
12211 char_u *fname;
12212 struct stat st;
12213
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012214 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215
12216 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012217 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012219 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220}
12221
12222/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012223 * "getftype({fname})" function
12224 */
12225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012226f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012227{
12228 char_u *fname;
12229 struct stat st;
12230 char_u *type = NULL;
12231 char *t;
12232
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012233 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012234
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012235 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012236 if (mch_lstat((char *)fname, &st) >= 0)
12237 {
12238#ifdef S_ISREG
12239 if (S_ISREG(st.st_mode))
12240 t = "file";
12241 else if (S_ISDIR(st.st_mode))
12242 t = "dir";
12243# ifdef S_ISLNK
12244 else if (S_ISLNK(st.st_mode))
12245 t = "link";
12246# endif
12247# ifdef S_ISBLK
12248 else if (S_ISBLK(st.st_mode))
12249 t = "bdev";
12250# endif
12251# ifdef S_ISCHR
12252 else if (S_ISCHR(st.st_mode))
12253 t = "cdev";
12254# endif
12255# ifdef S_ISFIFO
12256 else if (S_ISFIFO(st.st_mode))
12257 t = "fifo";
12258# endif
12259# ifdef S_ISSOCK
12260 else if (S_ISSOCK(st.st_mode))
12261 t = "fifo";
12262# endif
12263 else
12264 t = "other";
12265#else
12266# ifdef S_IFMT
12267 switch (st.st_mode & S_IFMT)
12268 {
12269 case S_IFREG: t = "file"; break;
12270 case S_IFDIR: t = "dir"; break;
12271# ifdef S_IFLNK
12272 case S_IFLNK: t = "link"; break;
12273# endif
12274# ifdef S_IFBLK
12275 case S_IFBLK: t = "bdev"; break;
12276# endif
12277# ifdef S_IFCHR
12278 case S_IFCHR: t = "cdev"; break;
12279# endif
12280# ifdef S_IFIFO
12281 case S_IFIFO: t = "fifo"; break;
12282# endif
12283# ifdef S_IFSOCK
12284 case S_IFSOCK: t = "socket"; break;
12285# endif
12286 default: t = "other";
12287 }
12288# else
12289 if (mch_isdir(fname))
12290 t = "dir";
12291 else
12292 t = "file";
12293# endif
12294#endif
12295 type = vim_strsave((char_u *)t);
12296 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012297 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012298}
12299
12300/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012301 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012302 */
12303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012304f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012305{
12306 linenr_T lnum;
12307 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012308 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012309
12310 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012311 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012312 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012313 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012314 retlist = FALSE;
12315 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012316 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012317 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012318 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012319 retlist = TRUE;
12320 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012321
Bram Moolenaar342337a2005-07-21 21:11:17 +000012322 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012323}
12324
12325/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012326 * "getmatches()" function
12327 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012329f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012330{
12331#ifdef FEAT_SEARCH_EXTRA
12332 dict_T *dict;
12333 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012334 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012335
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012336 if (rettv_list_alloc(rettv) == OK)
12337 {
12338 while (cur != NULL)
12339 {
12340 dict = dict_alloc();
12341 if (dict == NULL)
12342 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012343 if (cur->match.regprog == NULL)
12344 {
12345 /* match added with matchaddpos() */
12346 for (i = 0; i < MAXPOSMATCH; ++i)
12347 {
12348 llpos_T *llpos;
12349 char buf[6];
12350 list_T *l;
12351
12352 llpos = &cur->pos.pos[i];
12353 if (llpos->lnum == 0)
12354 break;
12355 l = list_alloc();
12356 if (l == NULL)
12357 break;
12358 list_append_number(l, (varnumber_T)llpos->lnum);
12359 if (llpos->col > 0)
12360 {
12361 list_append_number(l, (varnumber_T)llpos->col);
12362 list_append_number(l, (varnumber_T)llpos->len);
12363 }
12364 sprintf(buf, "pos%d", i + 1);
12365 dict_add_list(dict, buf, l);
12366 }
12367 }
12368 else
12369 {
12370 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12371 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012372 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012373 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12374 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012375# ifdef FEAT_CONCEAL
12376 if (cur->conceal_char)
12377 {
12378 char_u buf[MB_MAXBYTES + 1];
12379
12380 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12381 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12382 }
12383# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012384 list_append_dict(rettv->vval.v_list, dict);
12385 cur = cur->next;
12386 }
12387 }
12388#endif
12389}
12390
12391/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012392 * "getpid()" function
12393 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012395f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012396{
12397 rettv->vval.v_number = mch_get_pid();
12398}
12399
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012400static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012401
12402/*
12403 * "getcurpos()" function
12404 */
12405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012406f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012407{
12408 getpos_both(argvars, rettv, TRUE);
12409}
12410
Bram Moolenaar18081e32008-02-20 19:11:07 +000012411/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012412 * "getpos(string)" function
12413 */
12414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012415f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012416{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012417 getpos_both(argvars, rettv, FALSE);
12418}
12419
12420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012421getpos_both(
12422 typval_T *argvars,
12423 typval_T *rettv,
12424 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012425{
Bram Moolenaara5525202006-03-02 22:52:09 +000012426 pos_T *fp;
12427 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012428 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012429
12430 if (rettv_list_alloc(rettv) == OK)
12431 {
12432 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012433 if (getcurpos)
12434 fp = &curwin->w_cursor;
12435 else
12436 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012437 if (fnum != -1)
12438 list_append_number(l, (varnumber_T)fnum);
12439 else
12440 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012441 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12442 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012443 list_append_number(l, (fp != NULL)
12444 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012445 : (varnumber_T)0);
12446 list_append_number(l,
12447#ifdef FEAT_VIRTUALEDIT
12448 (fp != NULL) ? (varnumber_T)fp->coladd :
12449#endif
12450 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012451 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012452 list_append_number(l, curwin->w_curswant == MAXCOL ?
12453 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012454 }
12455 else
12456 rettv->vval.v_number = FALSE;
12457}
12458
12459/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012460 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012461 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012463f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012464{
12465#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012466 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012467#endif
12468
Bram Moolenaar2641f772005-03-25 21:58:17 +000012469#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012470 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012471 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012472 wp = NULL;
12473 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12474 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012475 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012476 if (wp == NULL)
12477 return;
12478 }
12479
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012480 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012481 }
12482#endif
12483}
12484
12485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486 * "getreg()" function
12487 */
12488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012489f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012490{
12491 char_u *strregname;
12492 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012493 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012494 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012495 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012496
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012497 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012499 strregname = get_tv_string_chk(&argvars[0]);
12500 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012501 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012502 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012503 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012504 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12505 return_list = get_tv_number_chk(&argvars[2], &error);
12506 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012508 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012509 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012510
12511 if (error)
12512 return;
12513
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514 regname = (strregname == NULL ? '"' : *strregname);
12515 if (regname == 0)
12516 regname = '"';
12517
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012518 if (return_list)
12519 {
12520 rettv->v_type = VAR_LIST;
12521 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12522 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012523 if (rettv->vval.v_list != NULL)
12524 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012525 }
12526 else
12527 {
12528 rettv->v_type = VAR_STRING;
12529 rettv->vval.v_string = get_reg_contents(regname,
12530 arg2 ? GREG_EXPR_SRC : 0);
12531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532}
12533
12534/*
12535 * "getregtype()" function
12536 */
12537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012538f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012539{
12540 char_u *strregname;
12541 int regname;
12542 char_u buf[NUMBUFLEN + 2];
12543 long reglen = 0;
12544
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012545 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012546 {
12547 strregname = get_tv_string_chk(&argvars[0]);
12548 if (strregname == NULL) /* type error; errmsg already given */
12549 {
12550 rettv->v_type = VAR_STRING;
12551 rettv->vval.v_string = NULL;
12552 return;
12553 }
12554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555 else
12556 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012557 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012558
12559 regname = (strregname == NULL ? '"' : *strregname);
12560 if (regname == 0)
12561 regname = '"';
12562
12563 buf[0] = NUL;
12564 buf[1] = NUL;
12565 switch (get_reg_type(regname, &reglen))
12566 {
12567 case MLINE: buf[0] = 'V'; break;
12568 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012569 case MBLOCK:
12570 buf[0] = Ctrl_V;
12571 sprintf((char *)buf + 1, "%ld", reglen + 1);
12572 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012573 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012574 rettv->v_type = VAR_STRING;
12575 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012576}
12577
12578/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012579 * "gettabvar()" function
12580 */
12581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012582f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012583{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012584 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012585 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012586 dictitem_T *v;
12587 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012588 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012589
12590 rettv->v_type = VAR_STRING;
12591 rettv->vval.v_string = NULL;
12592
12593 varname = get_tv_string_chk(&argvars[1]);
12594 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12595 if (tp != NULL && varname != NULL)
12596 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012597 /* Set tp to be our tabpage, temporarily. Also set the window to the
12598 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012599 if (switch_win(&oldcurwin, &oldtabpage,
12600 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012601 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012602 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012603 /* look up the variable */
12604 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12605 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12606 if (v != NULL)
12607 {
12608 copy_tv(&v->di_tv, rettv);
12609 done = TRUE;
12610 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012611 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012612
12613 /* restore previous notion of curwin */
12614 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012615 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012616
12617 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012618 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012619}
12620
12621/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012622 * "gettabwinvar()" function
12623 */
12624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012625f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012626{
12627 getwinvar(argvars, rettv, 1);
12628}
12629
12630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012631 * "getwinposx()" function
12632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012634f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012635{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012636 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012637#ifdef FEAT_GUI
12638 if (gui.in_use)
12639 {
12640 int x, y;
12641
12642 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012643 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012644 }
12645#endif
12646}
12647
12648/*
12649 * "getwinposy()" function
12650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012652f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012653{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012654 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012655#ifdef FEAT_GUI
12656 if (gui.in_use)
12657 {
12658 int x, y;
12659
12660 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012661 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012662 }
12663#endif
12664}
12665
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012666/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012667 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012668 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012669 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012670find_win_by_nr(
12671 typval_T *vp,
12672 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012673{
12674#ifdef FEAT_WINDOWS
12675 win_T *wp;
12676#endif
12677 int nr;
12678
12679 nr = get_tv_number_chk(vp, NULL);
12680
12681#ifdef FEAT_WINDOWS
12682 if (nr < 0)
12683 return NULL;
12684 if (nr == 0)
12685 return curwin;
12686
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012687 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12688 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012689 if (--nr <= 0)
12690 break;
12691 return wp;
12692#else
12693 if (nr == 0 || nr == 1)
12694 return curwin;
12695 return NULL;
12696#endif
12697}
12698
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012700 * Find window specified by "wvp" in tabpage "tvp".
12701 */
12702 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012703find_tabwin(
12704 typval_T *wvp, /* VAR_UNKNOWN for current window */
12705 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012706{
12707 win_T *wp = NULL;
12708 tabpage_T *tp = NULL;
12709 long n;
12710
12711 if (wvp->v_type != VAR_UNKNOWN)
12712 {
12713 if (tvp->v_type != VAR_UNKNOWN)
12714 {
12715 n = get_tv_number(tvp);
12716 if (n >= 0)
12717 tp = find_tabpage(n);
12718 }
12719 else
12720 tp = curtab;
12721
12722 if (tp != NULL)
12723 wp = find_win_by_nr(wvp, tp);
12724 }
12725 else
12726 wp = curwin;
12727
12728 return wp;
12729}
12730
12731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732 * "getwinvar()" function
12733 */
12734 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012735f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012737 getwinvar(argvars, rettv, 0);
12738}
12739
12740/*
12741 * getwinvar() and gettabwinvar()
12742 */
12743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012744getwinvar(
12745 typval_T *argvars,
12746 typval_T *rettv,
12747 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012748{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012749 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012751 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012752 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012753 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012754#ifdef FEAT_WINDOWS
12755 win_T *oldcurwin;
12756 tabpage_T *oldtabpage;
12757 int need_switch_win;
12758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012759
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012760#ifdef FEAT_WINDOWS
12761 if (off == 1)
12762 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12763 else
12764 tp = curtab;
12765#endif
12766 win = find_win_by_nr(&argvars[off], tp);
12767 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012768 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012770 rettv->v_type = VAR_STRING;
12771 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772
12773 if (win != NULL && varname != NULL)
12774 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012775#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012776 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012777 * otherwise the window is not valid. Only do this when needed,
12778 * autocommands get blocked. */
12779 need_switch_win = !(tp == curtab && win == curwin);
12780 if (!need_switch_win
12781 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12782#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012783 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012784 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012785 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012786 if (get_option_tv(&varname, rettv, 1) == OK)
12787 done = TRUE;
12788 }
12789 else
12790 {
12791 /* Look up the variable. */
12792 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12793 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12794 varname, FALSE);
12795 if (v != NULL)
12796 {
12797 copy_tv(&v->di_tv, rettv);
12798 done = TRUE;
12799 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012801 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012802
Bram Moolenaarba117c22015-09-29 16:53:22 +020012803#ifdef FEAT_WINDOWS
12804 if (need_switch_win)
12805 /* restore previous notion of curwin */
12806 restore_win(oldcurwin, oldtabpage, TRUE);
12807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808 }
12809
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012810 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12811 /* use the default return value */
12812 copy_tv(&argvars[off + 2], rettv);
12813
Bram Moolenaar071d4272004-06-13 20:20:40 +000012814 --emsg_off;
12815}
12816
12817/*
12818 * "glob()" function
12819 */
12820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012821f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012823 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012825 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012827 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012828 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012829 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012830 if (argvars[1].v_type != VAR_UNKNOWN)
12831 {
12832 if (get_tv_number_chk(&argvars[1], &error))
12833 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012834 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012835 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012836 if (get_tv_number_chk(&argvars[2], &error))
12837 {
12838 rettv->v_type = VAR_LIST;
12839 rettv->vval.v_list = NULL;
12840 }
12841 if (argvars[3].v_type != VAR_UNKNOWN
12842 && get_tv_number_chk(&argvars[3], &error))
12843 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012844 }
12845 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012846 if (!error)
12847 {
12848 ExpandInit(&xpc);
12849 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012850 if (p_wic)
12851 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012852 if (rettv->v_type == VAR_STRING)
12853 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012854 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012855 else if (rettv_list_alloc(rettv) != FAIL)
12856 {
12857 int i;
12858
12859 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12860 NULL, options, WILD_ALL_KEEP);
12861 for (i = 0; i < xpc.xp_numfiles; i++)
12862 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12863
12864 ExpandCleanup(&xpc);
12865 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012866 }
12867 else
12868 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869}
12870
12871/*
12872 * "globpath()" function
12873 */
12874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012875f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012877 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012879 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012880 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012881 garray_T ga;
12882 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012884 /* When the optional second argument is non-zero, don't remove matches
12885 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012886 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012887 if (argvars[2].v_type != VAR_UNKNOWN)
12888 {
12889 if (get_tv_number_chk(&argvars[2], &error))
12890 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012891 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012892 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012893 if (get_tv_number_chk(&argvars[3], &error))
12894 {
12895 rettv->v_type = VAR_LIST;
12896 rettv->vval.v_list = NULL;
12897 }
12898 if (argvars[4].v_type != VAR_UNKNOWN
12899 && get_tv_number_chk(&argvars[4], &error))
12900 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012901 }
12902 }
12903 if (file != NULL && !error)
12904 {
12905 ga_init2(&ga, (int)sizeof(char_u *), 10);
12906 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12907 if (rettv->v_type == VAR_STRING)
12908 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12909 else if (rettv_list_alloc(rettv) != FAIL)
12910 for (i = 0; i < ga.ga_len; ++i)
12911 list_append_string(rettv->vval.v_list,
12912 ((char_u **)(ga.ga_data))[i], -1);
12913 ga_clear_strings(&ga);
12914 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012915 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012916 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917}
12918
12919/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012920 * "glob2regpat()" function
12921 */
12922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012923f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012924{
12925 char_u *pat = get_tv_string_chk(&argvars[0]);
12926
12927 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010012928 rettv->vval.v_string = (pat == NULL)
12929 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012930}
12931
12932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012933 * "has()" function
12934 */
12935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012936f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937{
12938 int i;
12939 char_u *name;
12940 int n = FALSE;
12941 static char *(has_list[]) =
12942 {
12943#ifdef AMIGA
12944 "amiga",
12945# ifdef FEAT_ARP
12946 "arp",
12947# endif
12948#endif
12949#ifdef __BEOS__
12950 "beos",
12951#endif
12952#ifdef MSDOS
12953# ifdef DJGPP
12954 "dos32",
12955# else
12956 "dos16",
12957# endif
12958#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012959#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960 "mac",
12961#endif
12962#if defined(MACOS_X_UNIX)
12963 "macunix",
12964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965#ifdef __QNX__
12966 "qnx",
12967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012968#ifdef UNIX
12969 "unix",
12970#endif
12971#ifdef VMS
12972 "vms",
12973#endif
12974#ifdef WIN16
12975 "win16",
12976#endif
12977#ifdef WIN32
12978 "win32",
12979#endif
12980#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12981 "win32unix",
12982#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012983#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012984 "win64",
12985#endif
12986#ifdef EBCDIC
12987 "ebcdic",
12988#endif
12989#ifndef CASE_INSENSITIVE_FILENAME
12990 "fname_case",
12991#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012992#ifdef HAVE_ACL
12993 "acl",
12994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012995#ifdef FEAT_ARABIC
12996 "arabic",
12997#endif
12998#ifdef FEAT_AUTOCMD
12999 "autocmd",
13000#endif
13001#ifdef FEAT_BEVAL
13002 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013003# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13004 "balloon_multiline",
13005# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013006#endif
13007#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13008 "builtin_terms",
13009# ifdef ALL_BUILTIN_TCAPS
13010 "all_builtin_terms",
13011# endif
13012#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013013#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13014 || defined(FEAT_GUI_W32) \
13015 || defined(FEAT_GUI_MOTIF))
13016 "browsefilter",
13017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013018#ifdef FEAT_BYTEOFF
13019 "byte_offset",
13020#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013021#ifdef FEAT_CHANNEL
13022 "channel",
13023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013024#ifdef FEAT_CINDENT
13025 "cindent",
13026#endif
13027#ifdef FEAT_CLIENTSERVER
13028 "clientserver",
13029#endif
13030#ifdef FEAT_CLIPBOARD
13031 "clipboard",
13032#endif
13033#ifdef FEAT_CMDL_COMPL
13034 "cmdline_compl",
13035#endif
13036#ifdef FEAT_CMDHIST
13037 "cmdline_hist",
13038#endif
13039#ifdef FEAT_COMMENTS
13040 "comments",
13041#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013042#ifdef FEAT_CONCEAL
13043 "conceal",
13044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013045#ifdef FEAT_CRYPT
13046 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013047 "crypt-blowfish",
13048 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049#endif
13050#ifdef FEAT_CSCOPE
13051 "cscope",
13052#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013053#ifdef FEAT_CURSORBIND
13054 "cursorbind",
13055#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013056#ifdef CURSOR_SHAPE
13057 "cursorshape",
13058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013059#ifdef DEBUG
13060 "debug",
13061#endif
13062#ifdef FEAT_CON_DIALOG
13063 "dialog_con",
13064#endif
13065#ifdef FEAT_GUI_DIALOG
13066 "dialog_gui",
13067#endif
13068#ifdef FEAT_DIFF
13069 "diff",
13070#endif
13071#ifdef FEAT_DIGRAPHS
13072 "digraphs",
13073#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013074#ifdef FEAT_DIRECTX
13075 "directx",
13076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013077#ifdef FEAT_DND
13078 "dnd",
13079#endif
13080#ifdef FEAT_EMACS_TAGS
13081 "emacs_tags",
13082#endif
13083 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013084 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085#ifdef FEAT_SEARCH_EXTRA
13086 "extra_search",
13087#endif
13088#ifdef FEAT_FKMAP
13089 "farsi",
13090#endif
13091#ifdef FEAT_SEARCHPATH
13092 "file_in_path",
13093#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013094#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013095 "filterpipe",
13096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013097#ifdef FEAT_FIND_ID
13098 "find_in_path",
13099#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013100#ifdef FEAT_FLOAT
13101 "float",
13102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013103#ifdef FEAT_FOLDING
13104 "folding",
13105#endif
13106#ifdef FEAT_FOOTER
13107 "footer",
13108#endif
13109#if !defined(USE_SYSTEM) && defined(UNIX)
13110 "fork",
13111#endif
13112#ifdef FEAT_GETTEXT
13113 "gettext",
13114#endif
13115#ifdef FEAT_GUI
13116 "gui",
13117#endif
13118#ifdef FEAT_GUI_ATHENA
13119# ifdef FEAT_GUI_NEXTAW
13120 "gui_neXtaw",
13121# else
13122 "gui_athena",
13123# endif
13124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013125#ifdef FEAT_GUI_GTK
13126 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013127 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013128#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013129#ifdef FEAT_GUI_GNOME
13130 "gui_gnome",
13131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132#ifdef FEAT_GUI_MAC
13133 "gui_mac",
13134#endif
13135#ifdef FEAT_GUI_MOTIF
13136 "gui_motif",
13137#endif
13138#ifdef FEAT_GUI_PHOTON
13139 "gui_photon",
13140#endif
13141#ifdef FEAT_GUI_W16
13142 "gui_win16",
13143#endif
13144#ifdef FEAT_GUI_W32
13145 "gui_win32",
13146#endif
13147#ifdef FEAT_HANGULIN
13148 "hangul_input",
13149#endif
13150#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13151 "iconv",
13152#endif
13153#ifdef FEAT_INS_EXPAND
13154 "insert_expand",
13155#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013156#ifdef FEAT_JOB
13157 "job",
13158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159#ifdef FEAT_JUMPLIST
13160 "jumplist",
13161#endif
13162#ifdef FEAT_KEYMAP
13163 "keymap",
13164#endif
13165#ifdef FEAT_LANGMAP
13166 "langmap",
13167#endif
13168#ifdef FEAT_LIBCALL
13169 "libcall",
13170#endif
13171#ifdef FEAT_LINEBREAK
13172 "linebreak",
13173#endif
13174#ifdef FEAT_LISP
13175 "lispindent",
13176#endif
13177#ifdef FEAT_LISTCMDS
13178 "listcmds",
13179#endif
13180#ifdef FEAT_LOCALMAP
13181 "localmap",
13182#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013183#ifdef FEAT_LUA
13184# ifndef DYNAMIC_LUA
13185 "lua",
13186# endif
13187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188#ifdef FEAT_MENU
13189 "menu",
13190#endif
13191#ifdef FEAT_SESSION
13192 "mksession",
13193#endif
13194#ifdef FEAT_MODIFY_FNAME
13195 "modify_fname",
13196#endif
13197#ifdef FEAT_MOUSE
13198 "mouse",
13199#endif
13200#ifdef FEAT_MOUSESHAPE
13201 "mouseshape",
13202#endif
13203#if defined(UNIX) || defined(VMS)
13204# ifdef FEAT_MOUSE_DEC
13205 "mouse_dec",
13206# endif
13207# ifdef FEAT_MOUSE_GPM
13208 "mouse_gpm",
13209# endif
13210# ifdef FEAT_MOUSE_JSB
13211 "mouse_jsbterm",
13212# endif
13213# ifdef FEAT_MOUSE_NET
13214 "mouse_netterm",
13215# endif
13216# ifdef FEAT_MOUSE_PTERM
13217 "mouse_pterm",
13218# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013219# ifdef FEAT_MOUSE_SGR
13220 "mouse_sgr",
13221# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013222# ifdef FEAT_SYSMOUSE
13223 "mouse_sysmouse",
13224# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013225# ifdef FEAT_MOUSE_URXVT
13226 "mouse_urxvt",
13227# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228# ifdef FEAT_MOUSE_XTERM
13229 "mouse_xterm",
13230# endif
13231#endif
13232#ifdef FEAT_MBYTE
13233 "multi_byte",
13234#endif
13235#ifdef FEAT_MBYTE_IME
13236 "multi_byte_ime",
13237#endif
13238#ifdef FEAT_MULTI_LANG
13239 "multi_lang",
13240#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013241#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013242#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013243 "mzscheme",
13244#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246#ifdef FEAT_OLE
13247 "ole",
13248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013249#ifdef FEAT_PATH_EXTRA
13250 "path_extra",
13251#endif
13252#ifdef FEAT_PERL
13253#ifndef DYNAMIC_PERL
13254 "perl",
13255#endif
13256#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013257#ifdef FEAT_PERSISTENT_UNDO
13258 "persistent_undo",
13259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260#ifdef FEAT_PYTHON
13261#ifndef DYNAMIC_PYTHON
13262 "python",
13263#endif
13264#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013265#ifdef FEAT_PYTHON3
13266#ifndef DYNAMIC_PYTHON3
13267 "python3",
13268#endif
13269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013270#ifdef FEAT_POSTSCRIPT
13271 "postscript",
13272#endif
13273#ifdef FEAT_PRINTER
13274 "printer",
13275#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013276#ifdef FEAT_PROFILE
13277 "profile",
13278#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013279#ifdef FEAT_RELTIME
13280 "reltime",
13281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282#ifdef FEAT_QUICKFIX
13283 "quickfix",
13284#endif
13285#ifdef FEAT_RIGHTLEFT
13286 "rightleft",
13287#endif
13288#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13289 "ruby",
13290#endif
13291#ifdef FEAT_SCROLLBIND
13292 "scrollbind",
13293#endif
13294#ifdef FEAT_CMDL_INFO
13295 "showcmd",
13296 "cmdline_info",
13297#endif
13298#ifdef FEAT_SIGNS
13299 "signs",
13300#endif
13301#ifdef FEAT_SMARTINDENT
13302 "smartindent",
13303#endif
13304#ifdef FEAT_SNIFF
13305 "sniff",
13306#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013307#ifdef STARTUPTIME
13308 "startuptime",
13309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310#ifdef FEAT_STL_OPT
13311 "statusline",
13312#endif
13313#ifdef FEAT_SUN_WORKSHOP
13314 "sun_workshop",
13315#endif
13316#ifdef FEAT_NETBEANS_INTG
13317 "netbeans_intg",
13318#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013319#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013320 "spell",
13321#endif
13322#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013323 "syntax",
13324#endif
13325#if defined(USE_SYSTEM) || !defined(UNIX)
13326 "system",
13327#endif
13328#ifdef FEAT_TAG_BINS
13329 "tag_binary",
13330#endif
13331#ifdef FEAT_TAG_OLDSTATIC
13332 "tag_old_static",
13333#endif
13334#ifdef FEAT_TAG_ANYWHITE
13335 "tag_any_white",
13336#endif
13337#ifdef FEAT_TCL
13338# ifndef DYNAMIC_TCL
13339 "tcl",
13340# endif
13341#endif
13342#ifdef TERMINFO
13343 "terminfo",
13344#endif
13345#ifdef FEAT_TERMRESPONSE
13346 "termresponse",
13347#endif
13348#ifdef FEAT_TEXTOBJ
13349 "textobjects",
13350#endif
13351#ifdef HAVE_TGETENT
13352 "tgetent",
13353#endif
13354#ifdef FEAT_TITLE
13355 "title",
13356#endif
13357#ifdef FEAT_TOOLBAR
13358 "toolbar",
13359#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013360#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13361 "unnamedplus",
13362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013363#ifdef FEAT_USR_CMDS
13364 "user-commands", /* was accidentally included in 5.4 */
13365 "user_commands",
13366#endif
13367#ifdef FEAT_VIMINFO
13368 "viminfo",
13369#endif
13370#ifdef FEAT_VERTSPLIT
13371 "vertsplit",
13372#endif
13373#ifdef FEAT_VIRTUALEDIT
13374 "virtualedit",
13375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013376 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013377#ifdef FEAT_VISUALEXTRA
13378 "visualextra",
13379#endif
13380#ifdef FEAT_VREPLACE
13381 "vreplace",
13382#endif
13383#ifdef FEAT_WILDIGN
13384 "wildignore",
13385#endif
13386#ifdef FEAT_WILDMENU
13387 "wildmenu",
13388#endif
13389#ifdef FEAT_WINDOWS
13390 "windows",
13391#endif
13392#ifdef FEAT_WAK
13393 "winaltkeys",
13394#endif
13395#ifdef FEAT_WRITEBACKUP
13396 "writebackup",
13397#endif
13398#ifdef FEAT_XIM
13399 "xim",
13400#endif
13401#ifdef FEAT_XFONTSET
13402 "xfontset",
13403#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013404#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013405 "xpm",
13406 "xpm_w32", /* for backward compatibility */
13407#else
13408# if defined(HAVE_XPM)
13409 "xpm",
13410# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412#ifdef USE_XSMP
13413 "xsmp",
13414#endif
13415#ifdef USE_XSMP_INTERACT
13416 "xsmp_interact",
13417#endif
13418#ifdef FEAT_XCLIPBOARD
13419 "xterm_clipboard",
13420#endif
13421#ifdef FEAT_XTERM_SAVE
13422 "xterm_save",
13423#endif
13424#if defined(UNIX) && defined(FEAT_X11)
13425 "X11",
13426#endif
13427 NULL
13428 };
13429
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013430 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013431 for (i = 0; has_list[i] != NULL; ++i)
13432 if (STRICMP(name, has_list[i]) == 0)
13433 {
13434 n = TRUE;
13435 break;
13436 }
13437
13438 if (n == FALSE)
13439 {
13440 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013441 {
13442 if (name[5] == '-'
13443 && STRLEN(name) > 11
13444 && vim_isdigit(name[6])
13445 && vim_isdigit(name[8])
13446 && vim_isdigit(name[10]))
13447 {
13448 int major = atoi((char *)name + 6);
13449 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013450
13451 /* Expect "patch-9.9.01234". */
13452 n = (major < VIM_VERSION_MAJOR
13453 || (major == VIM_VERSION_MAJOR
13454 && (minor < VIM_VERSION_MINOR
13455 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013456 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013457 }
13458 else
13459 n = has_patch(atoi((char *)name + 5));
13460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013461 else if (STRICMP(name, "vim_starting") == 0)
13462 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013463#ifdef FEAT_MBYTE
13464 else if (STRICMP(name, "multi_byte_encoding") == 0)
13465 n = has_mbyte;
13466#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013467#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13468 else if (STRICMP(name, "balloon_multiline") == 0)
13469 n = multiline_balloon_available();
13470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013471#ifdef DYNAMIC_TCL
13472 else if (STRICMP(name, "tcl") == 0)
13473 n = tcl_enabled(FALSE);
13474#endif
13475#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13476 else if (STRICMP(name, "iconv") == 0)
13477 n = iconv_enabled(FALSE);
13478#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013479#ifdef DYNAMIC_LUA
13480 else if (STRICMP(name, "lua") == 0)
13481 n = lua_enabled(FALSE);
13482#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013483#ifdef DYNAMIC_MZSCHEME
13484 else if (STRICMP(name, "mzscheme") == 0)
13485 n = mzscheme_enabled(FALSE);
13486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013487#ifdef DYNAMIC_RUBY
13488 else if (STRICMP(name, "ruby") == 0)
13489 n = ruby_enabled(FALSE);
13490#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013491#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492#ifdef DYNAMIC_PYTHON
13493 else if (STRICMP(name, "python") == 0)
13494 n = python_enabled(FALSE);
13495#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013496#endif
13497#ifdef FEAT_PYTHON3
13498#ifdef DYNAMIC_PYTHON3
13499 else if (STRICMP(name, "python3") == 0)
13500 n = python3_enabled(FALSE);
13501#endif
13502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013503#ifdef DYNAMIC_PERL
13504 else if (STRICMP(name, "perl") == 0)
13505 n = perl_enabled(FALSE);
13506#endif
13507#ifdef FEAT_GUI
13508 else if (STRICMP(name, "gui_running") == 0)
13509 n = (gui.in_use || gui.starting);
13510# ifdef FEAT_GUI_W32
13511 else if (STRICMP(name, "gui_win32s") == 0)
13512 n = gui_is_win32s();
13513# endif
13514# ifdef FEAT_BROWSE
13515 else if (STRICMP(name, "browse") == 0)
13516 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13517# endif
13518#endif
13519#ifdef FEAT_SYN_HL
13520 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013521 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013522#endif
13523#if defined(WIN3264)
13524 else if (STRICMP(name, "win95") == 0)
13525 n = mch_windows95();
13526#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013527#ifdef FEAT_NETBEANS_INTG
13528 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013529 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531 }
13532
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013533 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534}
13535
13536/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013537 * "has_key()" function
13538 */
13539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013540f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013541{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013542 if (argvars[0].v_type != VAR_DICT)
13543 {
13544 EMSG(_(e_dictreq));
13545 return;
13546 }
13547 if (argvars[0].vval.v_dict == NULL)
13548 return;
13549
13550 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013551 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013552}
13553
13554/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013555 * "haslocaldir()" function
13556 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013558f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013559{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013560 win_T *wp = NULL;
13561
13562 wp = find_tabwin(&argvars[0], &argvars[1]);
13563 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013564}
13565
13566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013567 * "hasmapto()" function
13568 */
13569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013570f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571{
13572 char_u *name;
13573 char_u *mode;
13574 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013575 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013577 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013578 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579 mode = (char_u *)"nvo";
13580 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013581 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013582 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013583 if (argvars[2].v_type != VAR_UNKNOWN)
13584 abbr = get_tv_number(&argvars[2]);
13585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586
Bram Moolenaar2c932302006-03-18 21:42:09 +000013587 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013588 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013590 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591}
13592
13593/*
13594 * "histadd()" function
13595 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013597f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598{
13599#ifdef FEAT_CMDHIST
13600 int histype;
13601 char_u *str;
13602 char_u buf[NUMBUFLEN];
13603#endif
13604
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013605 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606 if (check_restricted() || check_secure())
13607 return;
13608#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013609 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13610 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013611 if (histype >= 0)
13612 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013613 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013614 if (*str != NUL)
13615 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013616 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013618 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619 return;
13620 }
13621 }
13622#endif
13623}
13624
13625/*
13626 * "histdel()" function
13627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013629f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013630{
13631#ifdef FEAT_CMDHIST
13632 int n;
13633 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013634 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013636 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13637 if (str == NULL)
13638 n = 0;
13639 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013641 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013642 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013644 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013645 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646 else
13647 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013648 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013649 get_tv_string_buf(&argvars[1], buf));
13650 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651#endif
13652}
13653
13654/*
13655 * "histget()" function
13656 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013658f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659{
13660#ifdef FEAT_CMDHIST
13661 int type;
13662 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013663 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013665 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13666 if (str == NULL)
13667 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013669 {
13670 type = get_histtype(str);
13671 if (argvars[1].v_type == VAR_UNKNOWN)
13672 idx = get_history_idx(type);
13673 else
13674 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13675 /* -1 on type error */
13676 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013679 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013681 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682}
13683
13684/*
13685 * "histnr()" function
13686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013688f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689{
13690 int i;
13691
13692#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013693 char_u *history = get_tv_string_chk(&argvars[0]);
13694
13695 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696 if (i >= HIST_CMD && i < HIST_COUNT)
13697 i = get_history_idx(i);
13698 else
13699#endif
13700 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013701 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702}
13703
13704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705 * "highlightID(name)" function
13706 */
13707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013708f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013710 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711}
13712
13713/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013714 * "highlight_exists()" function
13715 */
13716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013717f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013718{
13719 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13720}
13721
13722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723 * "hostname()" function
13724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013726f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727{
13728 char_u hostname[256];
13729
13730 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013731 rettv->v_type = VAR_STRING;
13732 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733}
13734
13735/*
13736 * iconv() function
13737 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013739f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740{
13741#ifdef FEAT_MBYTE
13742 char_u buf1[NUMBUFLEN];
13743 char_u buf2[NUMBUFLEN];
13744 char_u *from, *to, *str;
13745 vimconv_T vimconv;
13746#endif
13747
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013748 rettv->v_type = VAR_STRING;
13749 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013750
13751#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013752 str = get_tv_string(&argvars[0]);
13753 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13754 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755 vimconv.vc_type = CONV_NONE;
13756 convert_setup(&vimconv, from, to);
13757
13758 /* If the encodings are equal, no conversion needed. */
13759 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013760 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013762 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013763
13764 convert_setup(&vimconv, NULL, NULL);
13765 vim_free(from);
13766 vim_free(to);
13767#endif
13768}
13769
13770/*
13771 * "indent()" function
13772 */
13773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013774f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775{
13776 linenr_T lnum;
13777
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013778 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013780 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013781 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013782 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013783}
13784
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013785/*
13786 * "index()" function
13787 */
13788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013789f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013790{
Bram Moolenaar33570922005-01-25 22:26:29 +000013791 list_T *l;
13792 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013793 long idx = 0;
13794 int ic = FALSE;
13795
13796 rettv->vval.v_number = -1;
13797 if (argvars[0].v_type != VAR_LIST)
13798 {
13799 EMSG(_(e_listreq));
13800 return;
13801 }
13802 l = argvars[0].vval.v_list;
13803 if (l != NULL)
13804 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013805 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013806 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013807 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013808 int error = FALSE;
13809
Bram Moolenaar758711c2005-02-02 23:11:38 +000013810 /* Start at specified item. Use the cached index that list_find()
13811 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013812 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013813 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013814 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013815 ic = get_tv_number_chk(&argvars[3], &error);
13816 if (error)
13817 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013818 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013819
Bram Moolenaar758711c2005-02-02 23:11:38 +000013820 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013821 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013822 {
13823 rettv->vval.v_number = idx;
13824 break;
13825 }
13826 }
13827}
13828
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829static int inputsecret_flag = 0;
13830
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013831static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013832
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013834 * This function is used by f_input() and f_inputdialog() functions. The third
13835 * argument to f_input() specifies the type of completion to use at the
13836 * prompt. The third argument to f_inputdialog() specifies the value to return
13837 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838 */
13839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013840get_user_input(
13841 typval_T *argvars,
13842 typval_T *rettv,
13843 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013845 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846 char_u *p = NULL;
13847 int c;
13848 char_u buf[NUMBUFLEN];
13849 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013850 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013851 int xp_type = EXPAND_NOTHING;
13852 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013854 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013855 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013856
13857#ifdef NO_CONSOLE_INPUT
13858 /* While starting up, there is no place to enter text. */
13859 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013861#endif
13862
13863 cmd_silent = FALSE; /* Want to see the prompt. */
13864 if (prompt != NULL)
13865 {
13866 /* Only the part of the message after the last NL is considered as
13867 * prompt for the command line */
13868 p = vim_strrchr(prompt, '\n');
13869 if (p == NULL)
13870 p = prompt;
13871 else
13872 {
13873 ++p;
13874 c = *p;
13875 *p = NUL;
13876 msg_start();
13877 msg_clr_eos();
13878 msg_puts_attr(prompt, echo_attr);
13879 msg_didout = FALSE;
13880 msg_starthere();
13881 *p = c;
13882 }
13883 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013885 if (argvars[1].v_type != VAR_UNKNOWN)
13886 {
13887 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13888 if (defstr != NULL)
13889 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013890
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013891 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013892 {
13893 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013894 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013895 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013896
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013897 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013898 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013899
Bram Moolenaar4463f292005-09-25 22:20:24 +000013900 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13901 if (xp_name == NULL)
13902 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013903
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013904 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013905
Bram Moolenaar4463f292005-09-25 22:20:24 +000013906 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13907 &xp_arg) == FAIL)
13908 return;
13909 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013910 }
13911
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013912 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013913 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013914 int save_ex_normal_busy = ex_normal_busy;
13915 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013916 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013917 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13918 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013919 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013920 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013921 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013922 && argvars[1].v_type != VAR_UNKNOWN
13923 && argvars[2].v_type != VAR_UNKNOWN)
13924 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13925 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013926
13927 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013929 /* since the user typed this, no need to wait for return */
13930 need_wait_return = FALSE;
13931 msg_didout = FALSE;
13932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933 cmd_silent = cmd_silent_save;
13934}
13935
13936/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013937 * "input()" function
13938 * Also handles inputsecret() when inputsecret is set.
13939 */
13940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013941f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013942{
13943 get_user_input(argvars, rettv, FALSE);
13944}
13945
13946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947 * "inputdialog()" function
13948 */
13949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013950f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951{
13952#if defined(FEAT_GUI_TEXTDIALOG)
13953 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13954 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13955 {
13956 char_u *message;
13957 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013958 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013960 message = get_tv_string_chk(&argvars[0]);
13961 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013962 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013963 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013964 else
13965 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013966 if (message != NULL && defstr != NULL
13967 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013968 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013969 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013970 else
13971 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013972 if (message != NULL && defstr != NULL
13973 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013974 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013975 rettv->vval.v_string = vim_strsave(
13976 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013978 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013979 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013980 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013981 }
13982 else
13983#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013984 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013985}
13986
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013987/*
13988 * "inputlist()" function
13989 */
13990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013991f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013992{
13993 listitem_T *li;
13994 int selected;
13995 int mouse_used;
13996
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013997#ifdef NO_CONSOLE_INPUT
13998 /* While starting up, there is no place to enter text. */
13999 if (no_console_input())
14000 return;
14001#endif
14002 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14003 {
14004 EMSG2(_(e_listarg), "inputlist()");
14005 return;
14006 }
14007
14008 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014009 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014010 lines_left = Rows; /* avoid more prompt */
14011 msg_scroll = TRUE;
14012 msg_clr_eos();
14013
14014 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14015 {
14016 msg_puts(get_tv_string(&li->li_tv));
14017 msg_putchar('\n');
14018 }
14019
14020 /* Ask for choice. */
14021 selected = prompt_for_number(&mouse_used);
14022 if (mouse_used)
14023 selected -= lines_left;
14024
14025 rettv->vval.v_number = selected;
14026}
14027
14028
Bram Moolenaar071d4272004-06-13 20:20:40 +000014029static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14030
14031/*
14032 * "inputrestore()" function
14033 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014035f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014036{
14037 if (ga_userinput.ga_len > 0)
14038 {
14039 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014040 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14041 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014042 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014043 }
14044 else if (p_verbose > 1)
14045 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014046 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014047 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048 }
14049}
14050
14051/*
14052 * "inputsave()" function
14053 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014055f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014056{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014057 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014058 if (ga_grow(&ga_userinput, 1) == OK)
14059 {
14060 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14061 + ga_userinput.ga_len);
14062 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014063 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014064 }
14065 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014066 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014067}
14068
14069/*
14070 * "inputsecret()" function
14071 */
14072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014073f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014074{
14075 ++cmdline_star;
14076 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014077 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014078 --cmdline_star;
14079 --inputsecret_flag;
14080}
14081
14082/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014083 * "insert()" function
14084 */
14085 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014086f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014087{
14088 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014089 listitem_T *item;
14090 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014091 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014092
14093 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014094 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014095 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014096 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014097 {
14098 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014099 before = get_tv_number_chk(&argvars[2], &error);
14100 if (error)
14101 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014102
Bram Moolenaar758711c2005-02-02 23:11:38 +000014103 if (before == l->lv_len)
14104 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014105 else
14106 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014107 item = list_find(l, before);
14108 if (item == NULL)
14109 {
14110 EMSGN(_(e_listidx), before);
14111 l = NULL;
14112 }
14113 }
14114 if (l != NULL)
14115 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014116 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014117 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014118 }
14119 }
14120}
14121
14122/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014123 * "invert(expr)" function
14124 */
14125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014126f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014127{
14128 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14129}
14130
14131/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132 * "isdirectory()" function
14133 */
14134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014135f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014137 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014138}
14139
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014140/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014141 * "islocked()" function
14142 */
14143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014144f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014145{
14146 lval_T lv;
14147 char_u *end;
14148 dictitem_T *di;
14149
14150 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014151 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14152 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014153 if (end != NULL && lv.ll_name != NULL)
14154 {
14155 if (*end != NUL)
14156 EMSG(_(e_trailing));
14157 else
14158 {
14159 if (lv.ll_tv == NULL)
14160 {
14161 if (check_changedtick(lv.ll_name))
14162 rettv->vval.v_number = 1; /* always locked */
14163 else
14164 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014165 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014166 if (di != NULL)
14167 {
14168 /* Consider a variable locked when:
14169 * 1. the variable itself is locked
14170 * 2. the value of the variable is locked.
14171 * 3. the List or Dict value is locked.
14172 */
14173 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14174 || tv_islocked(&di->di_tv));
14175 }
14176 }
14177 }
14178 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014179 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014180 else if (lv.ll_newkey != NULL)
14181 EMSG2(_(e_dictkey), lv.ll_newkey);
14182 else if (lv.ll_list != NULL)
14183 /* List item. */
14184 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14185 else
14186 /* Dictionary item. */
14187 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14188 }
14189 }
14190
14191 clear_lval(&lv);
14192}
14193
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014194static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014195
14196/*
14197 * Turn a dict into a list:
14198 * "what" == 0: list of keys
14199 * "what" == 1: list of values
14200 * "what" == 2: list of items
14201 */
14202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014203dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014204{
Bram Moolenaar33570922005-01-25 22:26:29 +000014205 list_T *l2;
14206 dictitem_T *di;
14207 hashitem_T *hi;
14208 listitem_T *li;
14209 listitem_T *li2;
14210 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014211 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014212
Bram Moolenaar8c711452005-01-14 21:53:12 +000014213 if (argvars[0].v_type != VAR_DICT)
14214 {
14215 EMSG(_(e_dictreq));
14216 return;
14217 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014218 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014219 return;
14220
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014221 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014222 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014223
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014224 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014225 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014226 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014227 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014228 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014229 --todo;
14230 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014231
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014232 li = listitem_alloc();
14233 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014234 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014235 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014236
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014237 if (what == 0)
14238 {
14239 /* keys() */
14240 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014241 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014242 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14243 }
14244 else if (what == 1)
14245 {
14246 /* values() */
14247 copy_tv(&di->di_tv, &li->li_tv);
14248 }
14249 else
14250 {
14251 /* items() */
14252 l2 = list_alloc();
14253 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014254 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014255 li->li_tv.vval.v_list = l2;
14256 if (l2 == NULL)
14257 break;
14258 ++l2->lv_refcount;
14259
14260 li2 = listitem_alloc();
14261 if (li2 == NULL)
14262 break;
14263 list_append(l2, li2);
14264 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014265 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014266 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14267
14268 li2 = listitem_alloc();
14269 if (li2 == NULL)
14270 break;
14271 list_append(l2, li2);
14272 copy_tv(&di->di_tv, &li2->li_tv);
14273 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014274 }
14275 }
14276}
14277
14278/*
14279 * "items(dict)" function
14280 */
14281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014282f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014283{
14284 dict_list(argvars, rettv, 2);
14285}
14286
Bram Moolenaar835dc632016-02-07 14:27:38 +010014287#ifdef FEAT_JOB
14288/*
14289 * "job_start()" function
14290 */
14291 static void
14292f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
14293{
14294 job_T *job;
14295 char_u *cmd = NULL;
14296#if defined(UNIX)
14297# define USE_ARGV
14298 char **argv = NULL;
14299 int argc = 0;
14300#else
14301 garray_T ga;
14302#endif
14303
14304 rettv->v_type = VAR_JOB;
14305 job = job_alloc();
14306 rettv->vval.v_job = job;
14307 if (job == NULL)
14308 return;
14309
14310 rettv->vval.v_job->jv_status = JOB_FAILED;
14311#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010014312 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014313#endif
14314
14315 if (argvars[0].v_type == VAR_STRING)
14316 {
14317 /* Command is a string. */
14318 cmd = argvars[0].vval.v_string;
14319#ifdef USE_ARGV
14320 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
14321 return;
14322 argv[argc] = NULL;
14323#endif
14324 }
14325 else if (argvars[0].v_type != VAR_LIST
14326 || argvars[0].vval.v_list == NULL
14327 || argvars[0].vval.v_list->lv_len < 1)
14328 {
14329 EMSG(_(e_invarg));
14330 return;
14331 }
14332 else
14333 {
14334 list_T *l = argvars[0].vval.v_list;
14335 listitem_T *li;
14336 char_u *s;
14337
14338#ifdef USE_ARGV
14339 /* Pass argv[] to mch_call_shell(). */
14340 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
14341 if (argv == NULL)
14342 return;
14343#endif
14344 for (li = l->lv_first; li != NULL; li = li->li_next)
14345 {
14346 s = get_tv_string_chk(&li->li_tv);
14347 if (s == NULL)
14348 goto theend;
14349#ifdef USE_ARGV
14350 argv[argc++] = (char *)s;
14351#else
14352 if (li != l->lv_first)
14353 {
14354 s = vim_strsave_shellescape(s, FALSE, TRUE);
14355 if (s == NULL)
14356 goto theend;
14357 }
14358 ga_concat(&ga, s);
14359 vim_free(s);
14360 if (li->li_next != NULL)
14361 ga_append(&ga, ' ');
14362#endif
14363 }
14364#ifdef USE_ARGV
14365 argv[argc] = NULL;
14366#else
14367 cmd = ga.ga_data;
14368#endif
14369 }
14370#ifdef USE_ARGV
14371 mch_start_job(argv, job);
14372#else
14373 mch_start_job(cmd, job);
14374#endif
14375
14376theend:
14377#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010014378 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014379#else
14380 vim_free(ga.ga_data);
14381#endif
14382}
14383
14384/*
14385 * "job_status()" function
14386 */
14387 static void
14388f_job_status(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14389{
14390 char *result;
14391
14392 if (argvars[0].v_type != VAR_JOB)
14393 EMSG(_(e_invarg));
14394 else
14395 {
14396 job_T *job = argvars[0].vval.v_job;
14397
14398 if (job->jv_status == JOB_ENDED)
14399 /* No need to check, dead is dead. */
14400 result = "dead";
14401 else if (job->jv_status == JOB_FAILED)
14402 result = "fail";
14403 else
14404 result = mch_job_status(job);
14405 rettv->v_type = VAR_STRING;
14406 rettv->vval.v_string = vim_strsave((char_u *)result);
14407 }
14408}
14409
14410/*
14411 * "job_stop()" function
14412 */
14413 static void
14414f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14415{
14416 if (argvars[0].v_type != VAR_JOB)
14417 EMSG(_(e_invarg));
14418 else
14419 {
14420 char_u *arg;
14421
14422 if (argvars[1].v_type == VAR_UNKNOWN)
14423 arg = (char_u *)"";
14424 else
14425 {
14426 arg = get_tv_string_chk(&argvars[1]);
14427 if (arg == NULL)
14428 {
14429 EMSG(_(e_invarg));
14430 return;
14431 }
14432 }
14433 if (mch_stop_job(argvars[0].vval.v_job, arg) == FAIL)
14434 rettv->vval.v_number = 0;
14435 else
14436 rettv->vval.v_number = 1;
14437 }
14438}
14439#endif
14440
Bram Moolenaar071d4272004-06-13 20:20:40 +000014441/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014442 * "join()" function
14443 */
14444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014445f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014446{
14447 garray_T ga;
14448 char_u *sep;
14449
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014450 if (argvars[0].v_type != VAR_LIST)
14451 {
14452 EMSG(_(e_listreq));
14453 return;
14454 }
14455 if (argvars[0].vval.v_list == NULL)
14456 return;
14457 if (argvars[1].v_type == VAR_UNKNOWN)
14458 sep = (char_u *)" ";
14459 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014460 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014461
14462 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014463
14464 if (sep != NULL)
14465 {
14466 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014467 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014468 ga_append(&ga, NUL);
14469 rettv->vval.v_string = (char_u *)ga.ga_data;
14470 }
14471 else
14472 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014473}
14474
14475/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014476 * "jsdecode()" function
14477 */
14478 static void
14479f_jsdecode(typval_T *argvars, typval_T *rettv)
14480{
14481 js_read_T reader;
14482
14483 reader.js_buf = get_tv_string(&argvars[0]);
14484 reader.js_fill = NULL;
14485 reader.js_used = 0;
14486 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14487 EMSG(_(e_invarg));
14488}
14489
14490/*
14491 * "jsencode()" function
14492 */
14493 static void
14494f_jsencode(typval_T *argvars, typval_T *rettv)
14495{
14496 rettv->v_type = VAR_STRING;
14497 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14498}
14499
14500/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014501 * "jsondecode()" function
14502 */
14503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014504f_jsondecode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014505{
14506 js_read_T reader;
14507
14508 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014509 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014510 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014511 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014512 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014513}
14514
14515/*
14516 * "jsonencode()" function
14517 */
14518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014519f_jsonencode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014520{
14521 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014522 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014523}
14524
14525/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014526 * "keys()" function
14527 */
14528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014529f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014530{
14531 dict_list(argvars, rettv, 0);
14532}
14533
14534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535 * "last_buffer_nr()" function.
14536 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014538f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539{
14540 int n = 0;
14541 buf_T *buf;
14542
14543 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14544 if (n < buf->b_fnum)
14545 n = buf->b_fnum;
14546
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014547 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014548}
14549
14550/*
14551 * "len()" function
14552 */
14553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014554f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014555{
14556 switch (argvars[0].v_type)
14557 {
14558 case VAR_STRING:
14559 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014560 rettv->vval.v_number = (varnumber_T)STRLEN(
14561 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014562 break;
14563 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014564 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014565 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014566 case VAR_DICT:
14567 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14568 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014569 case VAR_UNKNOWN:
14570 case VAR_SPECIAL:
14571 case VAR_FLOAT:
14572 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014573 case VAR_JOB:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014574 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014575 break;
14576 }
14577}
14578
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014579static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014580
14581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014582libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014583{
14584#ifdef FEAT_LIBCALL
14585 char_u *string_in;
14586 char_u **string_result;
14587 int nr_result;
14588#endif
14589
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014590 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014591 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014592 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014593
14594 if (check_restricted() || check_secure())
14595 return;
14596
14597#ifdef FEAT_LIBCALL
14598 /* The first two args must be strings, otherwise its meaningless */
14599 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14600 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014601 string_in = NULL;
14602 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014603 string_in = argvars[2].vval.v_string;
14604 if (type == VAR_NUMBER)
14605 string_result = NULL;
14606 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014607 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014608 if (mch_libcall(argvars[0].vval.v_string,
14609 argvars[1].vval.v_string,
14610 string_in,
14611 argvars[2].vval.v_number,
14612 string_result,
14613 &nr_result) == OK
14614 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014615 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014616 }
14617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014618}
14619
14620/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014621 * "libcall()" function
14622 */
14623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014624f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014625{
14626 libcall_common(argvars, rettv, VAR_STRING);
14627}
14628
14629/*
14630 * "libcallnr()" function
14631 */
14632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014633f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014634{
14635 libcall_common(argvars, rettv, VAR_NUMBER);
14636}
14637
14638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014639 * "line(string)" function
14640 */
14641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014642f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014643{
14644 linenr_T lnum = 0;
14645 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014646 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014647
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014648 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014649 if (fp != NULL)
14650 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014651 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014652}
14653
14654/*
14655 * "line2byte(lnum)" function
14656 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014658f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014659{
14660#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014661 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014662#else
14663 linenr_T lnum;
14664
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014665 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014666 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014667 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014668 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014669 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14670 if (rettv->vval.v_number >= 0)
14671 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014672#endif
14673}
14674
14675/*
14676 * "lispindent(lnum)" function
14677 */
14678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014679f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014680{
14681#ifdef FEAT_LISP
14682 pos_T pos;
14683 linenr_T lnum;
14684
14685 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014686 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014687 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14688 {
14689 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014690 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014691 curwin->w_cursor = pos;
14692 }
14693 else
14694#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014695 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014696}
14697
14698/*
14699 * "localtime()" function
14700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014702f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014703{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014704 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014705}
14706
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014707static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014708
14709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014710get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014711{
14712 char_u *keys;
14713 char_u *which;
14714 char_u buf[NUMBUFLEN];
14715 char_u *keys_buf = NULL;
14716 char_u *rhs;
14717 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014718 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014719 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014720 mapblock_T *mp;
14721 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014722
14723 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014724 rettv->v_type = VAR_STRING;
14725 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014726
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014727 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014728 if (*keys == NUL)
14729 return;
14730
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014731 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014732 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014733 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014734 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014735 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014736 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014737 if (argvars[3].v_type != VAR_UNKNOWN)
14738 get_dict = get_tv_number(&argvars[3]);
14739 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014741 else
14742 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014743 if (which == NULL)
14744 return;
14745
Bram Moolenaar071d4272004-06-13 20:20:40 +000014746 mode = get_map_mode(&which, 0);
14747
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014748 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014749 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014751
14752 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014753 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014754 /* Return a string. */
14755 if (rhs != NULL)
14756 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757
Bram Moolenaarbd743252010-10-20 21:23:33 +020014758 }
14759 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14760 {
14761 /* Return a dictionary. */
14762 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14763 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14764 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765
Bram Moolenaarbd743252010-10-20 21:23:33 +020014766 dict_add_nr_str(dict, "lhs", 0L, lhs);
14767 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14768 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14769 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14770 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14771 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14772 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014773 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014774 dict_add_nr_str(dict, "mode", 0L, mapmode);
14775
14776 vim_free(lhs);
14777 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014778 }
14779}
14780
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014781#ifdef FEAT_FLOAT
14782/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014783 * "log()" function
14784 */
14785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014786f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014787{
14788 float_T f;
14789
14790 rettv->v_type = VAR_FLOAT;
14791 if (get_float_arg(argvars, &f) == OK)
14792 rettv->vval.v_float = log(f);
14793 else
14794 rettv->vval.v_float = 0.0;
14795}
14796
14797/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014798 * "log10()" function
14799 */
14800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014801f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014802{
14803 float_T f;
14804
14805 rettv->v_type = VAR_FLOAT;
14806 if (get_float_arg(argvars, &f) == OK)
14807 rettv->vval.v_float = log10(f);
14808 else
14809 rettv->vval.v_float = 0.0;
14810}
14811#endif
14812
Bram Moolenaar1dced572012-04-05 16:54:08 +020014813#ifdef FEAT_LUA
14814/*
14815 * "luaeval()" function
14816 */
14817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014818f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020014819{
14820 char_u *str;
14821 char_u buf[NUMBUFLEN];
14822
14823 str = get_tv_string_buf(&argvars[0], buf);
14824 do_luaeval(str, argvars + 1, rettv);
14825}
14826#endif
14827
Bram Moolenaar071d4272004-06-13 20:20:40 +000014828/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014829 * "map()" function
14830 */
14831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014832f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014833{
14834 filter_map(argvars, rettv, TRUE);
14835}
14836
14837/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014838 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014839 */
14840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014841f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014842{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014843 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844}
14845
14846/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014847 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014848 */
14849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014850f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014851{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014852 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014853}
14854
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014855static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014856
14857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014858find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014859{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014860 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014861 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014862 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014863 char_u *pat;
14864 regmatch_T regmatch;
14865 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014866 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014867 char_u *save_cpo;
14868 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014869 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014870 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014871 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014872 list_T *l = NULL;
14873 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014874 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014875 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014876
14877 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14878 save_cpo = p_cpo;
14879 p_cpo = (char_u *)"";
14880
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014881 rettv->vval.v_number = -1;
14882 if (type == 3)
14883 {
14884 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014885 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014886 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014887 }
14888 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014889 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014890 rettv->v_type = VAR_STRING;
14891 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014893
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014894 if (argvars[0].v_type == VAR_LIST)
14895 {
14896 if ((l = argvars[0].vval.v_list) == NULL)
14897 goto theend;
14898 li = l->lv_first;
14899 }
14900 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014901 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014902 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014903 len = (long)STRLEN(str);
14904 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014905
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014906 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14907 if (pat == NULL)
14908 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014909
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014910 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014911 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014912 int error = FALSE;
14913
14914 start = get_tv_number_chk(&argvars[2], &error);
14915 if (error)
14916 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014917 if (l != NULL)
14918 {
14919 li = list_find(l, start);
14920 if (li == NULL)
14921 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014922 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014923 }
14924 else
14925 {
14926 if (start < 0)
14927 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014928 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014929 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014930 /* When "count" argument is there ignore matches before "start",
14931 * otherwise skip part of the string. Differs when pattern is "^"
14932 * or "\<". */
14933 if (argvars[3].v_type != VAR_UNKNOWN)
14934 startcol = start;
14935 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014936 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014937 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014938 len -= start;
14939 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014940 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014941
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014942 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014943 nth = get_tv_number_chk(&argvars[3], &error);
14944 if (error)
14945 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014946 }
14947
14948 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14949 if (regmatch.regprog != NULL)
14950 {
14951 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014952
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014953 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014954 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014955 if (l != NULL)
14956 {
14957 if (li == NULL)
14958 {
14959 match = FALSE;
14960 break;
14961 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014962 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014963 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014964 if (str == NULL)
14965 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014966 }
14967
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014968 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014969
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014970 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014971 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014972 if (l == NULL && !match)
14973 break;
14974
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014975 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014976 if (l != NULL)
14977 {
14978 li = li->li_next;
14979 ++idx;
14980 }
14981 else
14982 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014983#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014984 startcol = (colnr_T)(regmatch.startp[0]
14985 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014986#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014987 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014988#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014989 if (startcol > (colnr_T)len
14990 || str + startcol <= regmatch.startp[0])
14991 {
14992 match = FALSE;
14993 break;
14994 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014995 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014996 }
14997
14998 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014999 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015000 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015001 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015002 int i;
15003
15004 /* return list with matched string and submatches */
15005 for (i = 0; i < NSUBEXP; ++i)
15006 {
15007 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015008 {
15009 if (list_append_string(rettv->vval.v_list,
15010 (char_u *)"", 0) == FAIL)
15011 break;
15012 }
15013 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015014 regmatch.startp[i],
15015 (int)(regmatch.endp[i] - regmatch.startp[i]))
15016 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015017 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015018 }
15019 }
15020 else if (type == 2)
15021 {
15022 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015023 if (l != NULL)
15024 copy_tv(&li->li_tv, rettv);
15025 else
15026 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015027 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015028 }
15029 else if (l != NULL)
15030 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015031 else
15032 {
15033 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015034 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015035 (varnumber_T)(regmatch.startp[0] - str);
15036 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015037 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015038 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015039 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015040 }
15041 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015042 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015043 }
15044
15045theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015046 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015047 p_cpo = save_cpo;
15048}
15049
15050/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015051 * "match()" function
15052 */
15053 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015054f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015055{
15056 find_some_match(argvars, rettv, 1);
15057}
15058
15059/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015060 * "matchadd()" function
15061 */
15062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015063f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015064{
15065#ifdef FEAT_SEARCH_EXTRA
15066 char_u buf[NUMBUFLEN];
15067 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15068 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15069 int prio = 10; /* default priority */
15070 int id = -1;
15071 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015072 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015073
15074 rettv->vval.v_number = -1;
15075
15076 if (grp == NULL || pat == NULL)
15077 return;
15078 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015079 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015080 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015081 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015082 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015083 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015084 if (argvars[4].v_type != VAR_UNKNOWN)
15085 {
15086 if (argvars[4].v_type != VAR_DICT)
15087 {
15088 EMSG(_(e_dictreq));
15089 return;
15090 }
15091 if (dict_find(argvars[4].vval.v_dict,
15092 (char_u *)"conceal", -1) != NULL)
15093 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15094 (char_u *)"conceal", FALSE);
15095 }
15096 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015097 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015098 if (error == TRUE)
15099 return;
15100 if (id >= 1 && id <= 3)
15101 {
15102 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15103 return;
15104 }
15105
Bram Moolenaar6561d522015-07-21 15:48:27 +020015106 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15107 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015108#endif
15109}
15110
15111/*
15112 * "matchaddpos()" function
15113 */
15114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015115f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015116{
15117#ifdef FEAT_SEARCH_EXTRA
15118 char_u buf[NUMBUFLEN];
15119 char_u *group;
15120 int prio = 10;
15121 int id = -1;
15122 int error = FALSE;
15123 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015124 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015125
15126 rettv->vval.v_number = -1;
15127
15128 group = get_tv_string_buf_chk(&argvars[0], buf);
15129 if (group == NULL)
15130 return;
15131
15132 if (argvars[1].v_type != VAR_LIST)
15133 {
15134 EMSG2(_(e_listarg), "matchaddpos()");
15135 return;
15136 }
15137 l = argvars[1].vval.v_list;
15138 if (l == NULL)
15139 return;
15140
15141 if (argvars[2].v_type != VAR_UNKNOWN)
15142 {
15143 prio = get_tv_number_chk(&argvars[2], &error);
15144 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015145 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015146 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015147 if (argvars[4].v_type != VAR_UNKNOWN)
15148 {
15149 if (argvars[4].v_type != VAR_DICT)
15150 {
15151 EMSG(_(e_dictreq));
15152 return;
15153 }
15154 if (dict_find(argvars[4].vval.v_dict,
15155 (char_u *)"conceal", -1) != NULL)
15156 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15157 (char_u *)"conceal", FALSE);
15158 }
15159 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015160 }
15161 if (error == TRUE)
15162 return;
15163
15164 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15165 if (id == 1 || id == 2)
15166 {
15167 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15168 return;
15169 }
15170
Bram Moolenaar6561d522015-07-21 15:48:27 +020015171 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15172 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015173#endif
15174}
15175
15176/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015177 * "matcharg()" function
15178 */
15179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015180f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015181{
15182 if (rettv_list_alloc(rettv) == OK)
15183 {
15184#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015185 int id = get_tv_number(&argvars[0]);
15186 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015187
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015188 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015189 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015190 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15191 {
15192 list_append_string(rettv->vval.v_list,
15193 syn_id2name(m->hlg_id), -1);
15194 list_append_string(rettv->vval.v_list, m->pattern, -1);
15195 }
15196 else
15197 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015198 list_append_string(rettv->vval.v_list, NULL, -1);
15199 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015200 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015201 }
15202#endif
15203 }
15204}
15205
15206/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015207 * "matchdelete()" function
15208 */
15209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015210f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015211{
15212#ifdef FEAT_SEARCH_EXTRA
15213 rettv->vval.v_number = match_delete(curwin,
15214 (int)get_tv_number(&argvars[0]), TRUE);
15215#endif
15216}
15217
15218/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015219 * "matchend()" function
15220 */
15221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015222f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015223{
15224 find_some_match(argvars, rettv, 0);
15225}
15226
15227/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015228 * "matchlist()" function
15229 */
15230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015231f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015232{
15233 find_some_match(argvars, rettv, 3);
15234}
15235
15236/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015237 * "matchstr()" function
15238 */
15239 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015240f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015241{
15242 find_some_match(argvars, rettv, 2);
15243}
15244
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015245static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015246
15247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015248max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015249{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015250 long n = 0;
15251 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015252 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015253
15254 if (argvars[0].v_type == VAR_LIST)
15255 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015256 list_T *l;
15257 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015258
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015259 l = argvars[0].vval.v_list;
15260 if (l != NULL)
15261 {
15262 li = l->lv_first;
15263 if (li != NULL)
15264 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015265 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015266 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015267 {
15268 li = li->li_next;
15269 if (li == NULL)
15270 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015271 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015272 if (domax ? i > n : i < n)
15273 n = i;
15274 }
15275 }
15276 }
15277 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015278 else if (argvars[0].v_type == VAR_DICT)
15279 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015280 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015281 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015282 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015283 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015284
15285 d = argvars[0].vval.v_dict;
15286 if (d != NULL)
15287 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015288 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015289 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015290 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015291 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015292 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015293 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015294 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015295 if (first)
15296 {
15297 n = i;
15298 first = FALSE;
15299 }
15300 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015301 n = i;
15302 }
15303 }
15304 }
15305 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015306 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015307 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015308 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015309}
15310
15311/*
15312 * "max()" function
15313 */
15314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015315f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015316{
15317 max_min(argvars, rettv, TRUE);
15318}
15319
15320/*
15321 * "min()" function
15322 */
15323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015324f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015325{
15326 max_min(argvars, rettv, FALSE);
15327}
15328
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015329static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015330
15331/*
15332 * Create the directory in which "dir" is located, and higher levels when
15333 * needed.
15334 */
15335 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015336mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015337{
15338 char_u *p;
15339 char_u *updir;
15340 int r = FAIL;
15341
15342 /* Get end of directory name in "dir".
15343 * We're done when it's "/" or "c:/". */
15344 p = gettail_sep(dir);
15345 if (p <= get_past_head(dir))
15346 return OK;
15347
15348 /* If the directory exists we're done. Otherwise: create it.*/
15349 updir = vim_strnsave(dir, (int)(p - dir));
15350 if (updir == NULL)
15351 return FAIL;
15352 if (mch_isdir(updir))
15353 r = OK;
15354 else if (mkdir_recurse(updir, prot) == OK)
15355 r = vim_mkdir_emsg(updir, prot);
15356 vim_free(updir);
15357 return r;
15358}
15359
15360#ifdef vim_mkdir
15361/*
15362 * "mkdir()" function
15363 */
15364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015365f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015366{
15367 char_u *dir;
15368 char_u buf[NUMBUFLEN];
15369 int prot = 0755;
15370
15371 rettv->vval.v_number = FAIL;
15372 if (check_restricted() || check_secure())
15373 return;
15374
15375 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015376 if (*dir == NUL)
15377 rettv->vval.v_number = FAIL;
15378 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015379 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015380 if (*gettail(dir) == NUL)
15381 /* remove trailing slashes */
15382 *gettail_sep(dir) = NUL;
15383
15384 if (argvars[1].v_type != VAR_UNKNOWN)
15385 {
15386 if (argvars[2].v_type != VAR_UNKNOWN)
15387 prot = get_tv_number_chk(&argvars[2], NULL);
15388 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15389 mkdir_recurse(dir, prot);
15390 }
15391 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015392 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015393}
15394#endif
15395
Bram Moolenaar0d660222005-01-07 21:51:51 +000015396/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015397 * "mode()" function
15398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015400f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015402 char_u buf[3];
15403
15404 buf[1] = NUL;
15405 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406
Bram Moolenaar071d4272004-06-13 20:20:40 +000015407 if (VIsual_active)
15408 {
15409 if (VIsual_select)
15410 buf[0] = VIsual_mode + 's' - 'v';
15411 else
15412 buf[0] = VIsual_mode;
15413 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015414 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015415 || State == CONFIRM)
15416 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015417 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015418 if (State == ASKMORE)
15419 buf[1] = 'm';
15420 else if (State == CONFIRM)
15421 buf[1] = '?';
15422 }
15423 else if (State == EXTERNCMD)
15424 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015425 else if (State & INSERT)
15426 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015427#ifdef FEAT_VREPLACE
15428 if (State & VREPLACE_FLAG)
15429 {
15430 buf[0] = 'R';
15431 buf[1] = 'v';
15432 }
15433 else
15434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015435 if (State & REPLACE_FLAG)
15436 buf[0] = 'R';
15437 else
15438 buf[0] = 'i';
15439 }
15440 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015441 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015442 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015443 if (exmode_active)
15444 buf[1] = 'v';
15445 }
15446 else if (exmode_active)
15447 {
15448 buf[0] = 'c';
15449 buf[1] = 'e';
15450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015452 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015453 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015454 if (finish_op)
15455 buf[1] = 'o';
15456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015458 /* Clear out the minor mode when the argument is not a non-zero number or
15459 * non-empty string. */
15460 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015461 buf[1] = NUL;
15462
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015463 rettv->vval.v_string = vim_strsave(buf);
15464 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015465}
15466
Bram Moolenaar429fa852013-04-15 12:27:36 +020015467#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015468/*
15469 * "mzeval()" function
15470 */
15471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015472f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015473{
15474 char_u *str;
15475 char_u buf[NUMBUFLEN];
15476
15477 str = get_tv_string_buf(&argvars[0], buf);
15478 do_mzeval(str, rettv);
15479}
Bram Moolenaar75676462013-01-30 14:55:42 +010015480
15481 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015482mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015483{
15484 typval_T argvars[3];
15485
15486 argvars[0].v_type = VAR_STRING;
15487 argvars[0].vval.v_string = name;
15488 copy_tv(args, &argvars[1]);
15489 argvars[2].v_type = VAR_UNKNOWN;
15490 f_call(argvars, rettv);
15491 clear_tv(&argvars[1]);
15492}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015493#endif
15494
Bram Moolenaar071d4272004-06-13 20:20:40 +000015495/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015496 * "nextnonblank()" function
15497 */
15498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015499f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015500{
15501 linenr_T lnum;
15502
15503 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15504 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015505 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015506 {
15507 lnum = 0;
15508 break;
15509 }
15510 if (*skipwhite(ml_get(lnum)) != NUL)
15511 break;
15512 }
15513 rettv->vval.v_number = lnum;
15514}
15515
15516/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015517 * "nr2char()" function
15518 */
15519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015520f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015521{
15522 char_u buf[NUMBUFLEN];
15523
15524#ifdef FEAT_MBYTE
15525 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015526 {
15527 int utf8 = 0;
15528
15529 if (argvars[1].v_type != VAR_UNKNOWN)
15530 utf8 = get_tv_number_chk(&argvars[1], NULL);
15531 if (utf8)
15532 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15533 else
15534 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015536 else
15537#endif
15538 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015539 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015540 buf[1] = NUL;
15541 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015542 rettv->v_type = VAR_STRING;
15543 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015544}
15545
15546/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015547 * "or(expr, expr)" function
15548 */
15549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015550f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015551{
15552 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15553 | get_tv_number_chk(&argvars[1], NULL);
15554}
15555
15556/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015557 * "pathshorten()" function
15558 */
15559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015560f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015561{
15562 char_u *p;
15563
15564 rettv->v_type = VAR_STRING;
15565 p = get_tv_string_chk(&argvars[0]);
15566 if (p == NULL)
15567 rettv->vval.v_string = NULL;
15568 else
15569 {
15570 p = vim_strsave(p);
15571 rettv->vval.v_string = p;
15572 if (p != NULL)
15573 shorten_dir(p);
15574 }
15575}
15576
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015577#ifdef FEAT_PERL
15578/*
15579 * "perleval()" function
15580 */
15581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015582f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015583{
15584 char_u *str;
15585 char_u buf[NUMBUFLEN];
15586
15587 str = get_tv_string_buf(&argvars[0], buf);
15588 do_perleval(str, rettv);
15589}
15590#endif
15591
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015592#ifdef FEAT_FLOAT
15593/*
15594 * "pow()" function
15595 */
15596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015597f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015598{
15599 float_T fx, fy;
15600
15601 rettv->v_type = VAR_FLOAT;
15602 if (get_float_arg(argvars, &fx) == OK
15603 && get_float_arg(&argvars[1], &fy) == OK)
15604 rettv->vval.v_float = pow(fx, fy);
15605 else
15606 rettv->vval.v_float = 0.0;
15607}
15608#endif
15609
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015610/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015611 * "prevnonblank()" function
15612 */
15613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015614f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015615{
15616 linenr_T lnum;
15617
15618 lnum = get_tv_lnum(argvars);
15619 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15620 lnum = 0;
15621 else
15622 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15623 --lnum;
15624 rettv->vval.v_number = lnum;
15625}
15626
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015627/* This dummy va_list is here because:
15628 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15629 * - locally in the function results in a "used before set" warning
15630 * - using va_start() to initialize it gives "function with fixed args" error */
15631static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015632
Bram Moolenaar8c711452005-01-14 21:53:12 +000015633/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015634 * "printf()" function
15635 */
15636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015637f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015638{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015639 char_u buf[NUMBUFLEN];
15640 int len;
15641 char_u *s;
15642 int saved_did_emsg = did_emsg;
15643 char *fmt;
15644
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015645 rettv->v_type = VAR_STRING;
15646 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015647
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015648 /* Get the required length, allocate the buffer and do it for real. */
15649 did_emsg = FALSE;
15650 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15651 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15652 if (!did_emsg)
15653 {
15654 s = alloc(len + 1);
15655 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015656 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015657 rettv->vval.v_string = s;
15658 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015659 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015660 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015661 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015662}
15663
15664/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015665 * "pumvisible()" function
15666 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015668f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015669{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015670#ifdef FEAT_INS_EXPAND
15671 if (pum_visible())
15672 rettv->vval.v_number = 1;
15673#endif
15674}
15675
Bram Moolenaardb913952012-06-29 12:54:53 +020015676#ifdef FEAT_PYTHON3
15677/*
15678 * "py3eval()" function
15679 */
15680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015681f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015682{
15683 char_u *str;
15684 char_u buf[NUMBUFLEN];
15685
15686 str = get_tv_string_buf(&argvars[0], buf);
15687 do_py3eval(str, rettv);
15688}
15689#endif
15690
15691#ifdef FEAT_PYTHON
15692/*
15693 * "pyeval()" function
15694 */
15695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015696f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015697{
15698 char_u *str;
15699 char_u buf[NUMBUFLEN];
15700
15701 str = get_tv_string_buf(&argvars[0], buf);
15702 do_pyeval(str, rettv);
15703}
15704#endif
15705
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015706/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015707 * "range()" function
15708 */
15709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015710f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015711{
15712 long start;
15713 long end;
15714 long stride = 1;
15715 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015716 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015717
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015718 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015719 if (argvars[1].v_type == VAR_UNKNOWN)
15720 {
15721 end = start - 1;
15722 start = 0;
15723 }
15724 else
15725 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015726 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015727 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015728 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015729 }
15730
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015731 if (error)
15732 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015733 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015734 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015735 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015736 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015737 else
15738 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015739 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015740 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015741 if (list_append_number(rettv->vval.v_list,
15742 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015743 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015744 }
15745}
15746
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015747/*
15748 * "readfile()" function
15749 */
15750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015751f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015752{
15753 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015754 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015755 char_u *fname;
15756 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015757 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15758 int io_size = sizeof(buf);
15759 int readlen; /* size of last fread() */
15760 char_u *prev = NULL; /* previously read bytes, if any */
15761 long prevlen = 0; /* length of data in prev */
15762 long prevsize = 0; /* size of prev buffer */
15763 long maxline = MAXLNUM;
15764 long cnt = 0;
15765 char_u *p; /* position in buf */
15766 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015767
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015768 if (argvars[1].v_type != VAR_UNKNOWN)
15769 {
15770 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15771 binary = TRUE;
15772 if (argvars[2].v_type != VAR_UNKNOWN)
15773 maxline = get_tv_number(&argvars[2]);
15774 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015775
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015776 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015777 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015778
15779 /* Always open the file in binary mode, library functions have a mind of
15780 * their own about CR-LF conversion. */
15781 fname = get_tv_string(&argvars[0]);
15782 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15783 {
15784 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15785 return;
15786 }
15787
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015788 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015789 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015790 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015791
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015792 /* This for loop processes what was read, but is also entered at end
15793 * of file so that either:
15794 * - an incomplete line gets written
15795 * - a "binary" file gets an empty line at the end if it ends in a
15796 * newline. */
15797 for (p = buf, start = buf;
15798 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15799 ++p)
15800 {
15801 if (*p == '\n' || readlen <= 0)
15802 {
15803 listitem_T *li;
15804 char_u *s = NULL;
15805 long_u len = p - start;
15806
15807 /* Finished a line. Remove CRs before NL. */
15808 if (readlen > 0 && !binary)
15809 {
15810 while (len > 0 && start[len - 1] == '\r')
15811 --len;
15812 /* removal may cross back to the "prev" string */
15813 if (len == 0)
15814 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15815 --prevlen;
15816 }
15817 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015818 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015819 else
15820 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015821 /* Change "prev" buffer to be the right size. This way
15822 * the bytes are only copied once, and very long lines are
15823 * allocated only once. */
15824 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015825 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015826 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015827 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015828 prev = NULL; /* the list will own the string */
15829 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015830 }
15831 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015832 if (s == NULL)
15833 {
15834 do_outofmem_msg((long_u) prevlen + len + 1);
15835 failed = TRUE;
15836 break;
15837 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015838
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015839 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015840 {
15841 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015842 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015843 break;
15844 }
15845 li->li_tv.v_type = VAR_STRING;
15846 li->li_tv.v_lock = 0;
15847 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015848 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015849
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015850 start = p + 1; /* step over newline */
15851 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015852 break;
15853 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015854 else if (*p == NUL)
15855 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015856#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015857 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15858 * when finding the BF and check the previous two bytes. */
15859 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015860 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015861 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15862 * + 1, these may be in the "prev" string. */
15863 char_u back1 = p >= buf + 1 ? p[-1]
15864 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15865 char_u back2 = p >= buf + 2 ? p[-2]
15866 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15867 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015868
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015869 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015870 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015871 char_u *dest = p - 2;
15872
15873 /* Usually a BOM is at the beginning of a file, and so at
15874 * the beginning of a line; then we can just step over it.
15875 */
15876 if (start == dest)
15877 start = p + 1;
15878 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015879 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015880 /* have to shuffle buf to close gap */
15881 int adjust_prevlen = 0;
15882
15883 if (dest < buf)
15884 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015885 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015886 dest = buf;
15887 }
15888 if (readlen > p - buf + 1)
15889 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15890 readlen -= 3 - adjust_prevlen;
15891 prevlen -= adjust_prevlen;
15892 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015893 }
15894 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015895 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015896#endif
15897 } /* for */
15898
15899 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15900 break;
15901 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015902 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015903 /* There's part of a line in buf, store it in "prev". */
15904 if (p - start + prevlen >= prevsize)
15905 {
15906 /* need bigger "prev" buffer */
15907 char_u *newprev;
15908
15909 /* A common use case is ordinary text files and "prev" gets a
15910 * fragment of a line, so the first allocation is made
15911 * small, to avoid repeatedly 'allocing' large and
15912 * 'reallocing' small. */
15913 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015914 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015915 else
15916 {
15917 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015918 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015919 prevsize = grow50pc > growmin ? grow50pc : growmin;
15920 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015921 newprev = prev == NULL ? alloc(prevsize)
15922 : vim_realloc(prev, prevsize);
15923 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015924 {
15925 do_outofmem_msg((long_u)prevsize);
15926 failed = TRUE;
15927 break;
15928 }
15929 prev = newprev;
15930 }
15931 /* Add the line part to end of "prev". */
15932 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015933 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015934 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015935 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015936
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015937 /*
15938 * For a negative line count use only the lines at the end of the file,
15939 * free the rest.
15940 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015941 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015942 while (cnt > -maxline)
15943 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015944 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015945 --cnt;
15946 }
15947
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015948 if (failed)
15949 {
15950 list_free(rettv->vval.v_list, TRUE);
15951 /* readfile doc says an empty list is returned on error */
15952 rettv->vval.v_list = list_alloc();
15953 }
15954
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015955 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015956 fclose(fd);
15957}
15958
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015959#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015960static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015961
15962/*
15963 * Convert a List to proftime_T.
15964 * Return FAIL when there is something wrong.
15965 */
15966 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015967list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015968{
15969 long n1, n2;
15970 int error = FALSE;
15971
15972 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15973 || arg->vval.v_list->lv_len != 2)
15974 return FAIL;
15975 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15976 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15977# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015978 tm->HighPart = n1;
15979 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015980# else
15981 tm->tv_sec = n1;
15982 tm->tv_usec = n2;
15983# endif
15984 return error ? FAIL : OK;
15985}
15986#endif /* FEAT_RELTIME */
15987
15988/*
15989 * "reltime()" function
15990 */
15991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015992f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015993{
15994#ifdef FEAT_RELTIME
15995 proftime_T res;
15996 proftime_T start;
15997
15998 if (argvars[0].v_type == VAR_UNKNOWN)
15999 {
16000 /* No arguments: get current time. */
16001 profile_start(&res);
16002 }
16003 else if (argvars[1].v_type == VAR_UNKNOWN)
16004 {
16005 if (list2proftime(&argvars[0], &res) == FAIL)
16006 return;
16007 profile_end(&res);
16008 }
16009 else
16010 {
16011 /* Two arguments: compute the difference. */
16012 if (list2proftime(&argvars[0], &start) == FAIL
16013 || list2proftime(&argvars[1], &res) == FAIL)
16014 return;
16015 profile_sub(&res, &start);
16016 }
16017
16018 if (rettv_list_alloc(rettv) == OK)
16019 {
16020 long n1, n2;
16021
16022# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016023 n1 = res.HighPart;
16024 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016025# else
16026 n1 = res.tv_sec;
16027 n2 = res.tv_usec;
16028# endif
16029 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16030 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16031 }
16032#endif
16033}
16034
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016035#ifdef FEAT_FLOAT
16036/*
16037 * "reltimefloat()" function
16038 */
16039 static void
16040f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16041{
16042# ifdef FEAT_RELTIME
16043 proftime_T tm;
16044# endif
16045
16046 rettv->v_type = VAR_FLOAT;
16047 rettv->vval.v_float = 0;
16048# ifdef FEAT_RELTIME
16049 if (list2proftime(&argvars[0], &tm) == OK)
16050 rettv->vval.v_float = profile_float(&tm);
16051# endif
16052}
16053#endif
16054
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016055/*
16056 * "reltimestr()" function
16057 */
16058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016059f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016060{
16061#ifdef FEAT_RELTIME
16062 proftime_T tm;
16063#endif
16064
16065 rettv->v_type = VAR_STRING;
16066 rettv->vval.v_string = NULL;
16067#ifdef FEAT_RELTIME
16068 if (list2proftime(&argvars[0], &tm) == OK)
16069 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16070#endif
16071}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016072
Bram Moolenaar0d660222005-01-07 21:51:51 +000016073#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016074static void make_connection(void);
16075static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016076
16077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016078make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016079{
16080 if (X_DISPLAY == NULL
16081# ifdef FEAT_GUI
16082 && !gui.in_use
16083# endif
16084 )
16085 {
16086 x_force_connect = TRUE;
16087 setup_term_clip();
16088 x_force_connect = FALSE;
16089 }
16090}
16091
16092 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016093check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016094{
16095 make_connection();
16096 if (X_DISPLAY == NULL)
16097 {
16098 EMSG(_("E240: No connection to Vim server"));
16099 return FAIL;
16100 }
16101 return OK;
16102}
16103#endif
16104
16105#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016106static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016107
16108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016109remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016110{
16111 char_u *server_name;
16112 char_u *keys;
16113 char_u *r = NULL;
16114 char_u buf[NUMBUFLEN];
16115# ifdef WIN32
16116 HWND w;
16117# else
16118 Window w;
16119# endif
16120
16121 if (check_restricted() || check_secure())
16122 return;
16123
16124# ifdef FEAT_X11
16125 if (check_connection() == FAIL)
16126 return;
16127# endif
16128
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016129 server_name = get_tv_string_chk(&argvars[0]);
16130 if (server_name == NULL)
16131 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016132 keys = get_tv_string_buf(&argvars[1], buf);
16133# ifdef WIN32
16134 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16135# else
16136 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16137 < 0)
16138# endif
16139 {
16140 if (r != NULL)
16141 EMSG(r); /* sending worked but evaluation failed */
16142 else
16143 EMSG2(_("E241: Unable to send to %s"), server_name);
16144 return;
16145 }
16146
16147 rettv->vval.v_string = r;
16148
16149 if (argvars[2].v_type != VAR_UNKNOWN)
16150 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016151 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016152 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016153 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016154
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016155 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016156 v.di_tv.v_type = VAR_STRING;
16157 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016158 idvar = get_tv_string_chk(&argvars[2]);
16159 if (idvar != NULL)
16160 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016161 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016162 }
16163}
16164#endif
16165
16166/*
16167 * "remote_expr()" function
16168 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016170f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016171{
16172 rettv->v_type = VAR_STRING;
16173 rettv->vval.v_string = NULL;
16174#ifdef FEAT_CLIENTSERVER
16175 remote_common(argvars, rettv, TRUE);
16176#endif
16177}
16178
16179/*
16180 * "remote_foreground()" function
16181 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016183f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016184{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016185#ifdef FEAT_CLIENTSERVER
16186# ifdef WIN32
16187 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016188 {
16189 char_u *server_name = get_tv_string_chk(&argvars[0]);
16190
16191 if (server_name != NULL)
16192 serverForeground(server_name);
16193 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016194# else
16195 /* Send a foreground() expression to the server. */
16196 argvars[1].v_type = VAR_STRING;
16197 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16198 argvars[2].v_type = VAR_UNKNOWN;
16199 remote_common(argvars, rettv, TRUE);
16200 vim_free(argvars[1].vval.v_string);
16201# endif
16202#endif
16203}
16204
Bram Moolenaar0d660222005-01-07 21:51:51 +000016205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016206f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016207{
16208#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016209 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016210 char_u *s = NULL;
16211# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016212 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016213# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016214 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016215
16216 if (check_restricted() || check_secure())
16217 {
16218 rettv->vval.v_number = -1;
16219 return;
16220 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016221 serverid = get_tv_string_chk(&argvars[0]);
16222 if (serverid == NULL)
16223 {
16224 rettv->vval.v_number = -1;
16225 return; /* type error; errmsg already given */
16226 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016227# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016228 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016229 if (n == 0)
16230 rettv->vval.v_number = -1;
16231 else
16232 {
16233 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16234 rettv->vval.v_number = (s != NULL);
16235 }
16236# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016237 if (check_connection() == FAIL)
16238 return;
16239
16240 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016241 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016242# endif
16243
16244 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16245 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016246 char_u *retvar;
16247
Bram Moolenaar33570922005-01-25 22:26:29 +000016248 v.di_tv.v_type = VAR_STRING;
16249 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016250 retvar = get_tv_string_chk(&argvars[1]);
16251 if (retvar != NULL)
16252 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016253 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016254 }
16255#else
16256 rettv->vval.v_number = -1;
16257#endif
16258}
16259
Bram Moolenaar0d660222005-01-07 21:51:51 +000016260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016261f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016262{
16263 char_u *r = NULL;
16264
16265#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016266 char_u *serverid = get_tv_string_chk(&argvars[0]);
16267
16268 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016269 {
16270# ifdef WIN32
16271 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016272 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016273
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016274 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016275 if (n != 0)
16276 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16277 if (r == NULL)
16278# else
16279 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016280 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016281# endif
16282 EMSG(_("E277: Unable to read a server reply"));
16283 }
16284#endif
16285 rettv->v_type = VAR_STRING;
16286 rettv->vval.v_string = r;
16287}
16288
16289/*
16290 * "remote_send()" function
16291 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016293f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016294{
16295 rettv->v_type = VAR_STRING;
16296 rettv->vval.v_string = NULL;
16297#ifdef FEAT_CLIENTSERVER
16298 remote_common(argvars, rettv, FALSE);
16299#endif
16300}
16301
16302/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016303 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016304 */
16305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016306f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016307{
Bram Moolenaar33570922005-01-25 22:26:29 +000016308 list_T *l;
16309 listitem_T *item, *item2;
16310 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016311 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016312 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016313 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016314 dict_T *d;
16315 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016316 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016317
Bram Moolenaar8c711452005-01-14 21:53:12 +000016318 if (argvars[0].v_type == VAR_DICT)
16319 {
16320 if (argvars[2].v_type != VAR_UNKNOWN)
16321 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016322 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016323 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016324 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016325 key = get_tv_string_chk(&argvars[1]);
16326 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016328 di = dict_find(d, key, -1);
16329 if (di == NULL)
16330 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016331 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16332 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016333 {
16334 *rettv = di->di_tv;
16335 init_tv(&di->di_tv);
16336 dictitem_remove(d, di);
16337 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016338 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016339 }
16340 }
16341 else if (argvars[0].v_type != VAR_LIST)
16342 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016343 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016344 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016345 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016346 int error = FALSE;
16347
16348 idx = get_tv_number_chk(&argvars[1], &error);
16349 if (error)
16350 ; /* type error: do nothing, errmsg already given */
16351 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016352 EMSGN(_(e_listidx), idx);
16353 else
16354 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016355 if (argvars[2].v_type == VAR_UNKNOWN)
16356 {
16357 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016358 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016359 *rettv = item->li_tv;
16360 vim_free(item);
16361 }
16362 else
16363 {
16364 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016365 end = get_tv_number_chk(&argvars[2], &error);
16366 if (error)
16367 ; /* type error: do nothing */
16368 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016369 EMSGN(_(e_listidx), end);
16370 else
16371 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016372 int cnt = 0;
16373
16374 for (li = item; li != NULL; li = li->li_next)
16375 {
16376 ++cnt;
16377 if (li == item2)
16378 break;
16379 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016380 if (li == NULL) /* didn't find "item2" after "item" */
16381 EMSG(_(e_invrange));
16382 else
16383 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016384 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016385 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016386 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016387 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016388 l->lv_first = item;
16389 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016390 item->li_prev = NULL;
16391 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016392 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016393 }
16394 }
16395 }
16396 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016397 }
16398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016399}
16400
16401/*
16402 * "rename({from}, {to})" function
16403 */
16404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016405f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016406{
16407 char_u buf[NUMBUFLEN];
16408
16409 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016410 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016411 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016412 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16413 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016414}
16415
16416/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016417 * "repeat()" function
16418 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016420f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016421{
16422 char_u *p;
16423 int n;
16424 int slen;
16425 int len;
16426 char_u *r;
16427 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016428
16429 n = get_tv_number(&argvars[1]);
16430 if (argvars[0].v_type == VAR_LIST)
16431 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016432 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016433 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016434 if (list_extend(rettv->vval.v_list,
16435 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016436 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016437 }
16438 else
16439 {
16440 p = get_tv_string(&argvars[0]);
16441 rettv->v_type = VAR_STRING;
16442 rettv->vval.v_string = NULL;
16443
16444 slen = (int)STRLEN(p);
16445 len = slen * n;
16446 if (len <= 0)
16447 return;
16448
16449 r = alloc(len + 1);
16450 if (r != NULL)
16451 {
16452 for (i = 0; i < n; i++)
16453 mch_memmove(r + i * slen, p, (size_t)slen);
16454 r[len] = NUL;
16455 }
16456
16457 rettv->vval.v_string = r;
16458 }
16459}
16460
16461/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016462 * "resolve()" function
16463 */
16464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016465f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016466{
16467 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016468#ifdef HAVE_READLINK
16469 char_u *buf = NULL;
16470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016471
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016472 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016473#ifdef FEAT_SHORTCUT
16474 {
16475 char_u *v = NULL;
16476
16477 v = mch_resolve_shortcut(p);
16478 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016479 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016480 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016481 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016482 }
16483#else
16484# ifdef HAVE_READLINK
16485 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016486 char_u *cpy;
16487 int len;
16488 char_u *remain = NULL;
16489 char_u *q;
16490 int is_relative_to_current = FALSE;
16491 int has_trailing_pathsep = FALSE;
16492 int limit = 100;
16493
16494 p = vim_strsave(p);
16495
16496 if (p[0] == '.' && (vim_ispathsep(p[1])
16497 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16498 is_relative_to_current = TRUE;
16499
16500 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016501 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016502 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016504 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016506
16507 q = getnextcomp(p);
16508 if (*q != NUL)
16509 {
16510 /* Separate the first path component in "p", and keep the
16511 * remainder (beginning with the path separator). */
16512 remain = vim_strsave(q - 1);
16513 q[-1] = NUL;
16514 }
16515
Bram Moolenaard9462e32011-04-11 21:35:11 +020016516 buf = alloc(MAXPATHL + 1);
16517 if (buf == NULL)
16518 goto fail;
16519
Bram Moolenaar071d4272004-06-13 20:20:40 +000016520 for (;;)
16521 {
16522 for (;;)
16523 {
16524 len = readlink((char *)p, (char *)buf, MAXPATHL);
16525 if (len <= 0)
16526 break;
16527 buf[len] = NUL;
16528
16529 if (limit-- == 0)
16530 {
16531 vim_free(p);
16532 vim_free(remain);
16533 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016534 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016535 goto fail;
16536 }
16537
16538 /* Ensure that the result will have a trailing path separator
16539 * if the argument has one. */
16540 if (remain == NULL && has_trailing_pathsep)
16541 add_pathsep(buf);
16542
16543 /* Separate the first path component in the link value and
16544 * concatenate the remainders. */
16545 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16546 if (*q != NUL)
16547 {
16548 if (remain == NULL)
16549 remain = vim_strsave(q - 1);
16550 else
16551 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016552 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553 if (cpy != NULL)
16554 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555 vim_free(remain);
16556 remain = cpy;
16557 }
16558 }
16559 q[-1] = NUL;
16560 }
16561
16562 q = gettail(p);
16563 if (q > p && *q == NUL)
16564 {
16565 /* Ignore trailing path separator. */
16566 q[-1] = NUL;
16567 q = gettail(p);
16568 }
16569 if (q > p && !mch_isFullName(buf))
16570 {
16571 /* symlink is relative to directory of argument */
16572 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16573 if (cpy != NULL)
16574 {
16575 STRCPY(cpy, p);
16576 STRCPY(gettail(cpy), buf);
16577 vim_free(p);
16578 p = cpy;
16579 }
16580 }
16581 else
16582 {
16583 vim_free(p);
16584 p = vim_strsave(buf);
16585 }
16586 }
16587
16588 if (remain == NULL)
16589 break;
16590
16591 /* Append the first path component of "remain" to "p". */
16592 q = getnextcomp(remain + 1);
16593 len = q - remain - (*q != NUL);
16594 cpy = vim_strnsave(p, STRLEN(p) + len);
16595 if (cpy != NULL)
16596 {
16597 STRNCAT(cpy, remain, len);
16598 vim_free(p);
16599 p = cpy;
16600 }
16601 /* Shorten "remain". */
16602 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016603 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016604 else
16605 {
16606 vim_free(remain);
16607 remain = NULL;
16608 }
16609 }
16610
16611 /* If the result is a relative path name, make it explicitly relative to
16612 * the current directory if and only if the argument had this form. */
16613 if (!vim_ispathsep(*p))
16614 {
16615 if (is_relative_to_current
16616 && *p != NUL
16617 && !(p[0] == '.'
16618 && (p[1] == NUL
16619 || vim_ispathsep(p[1])
16620 || (p[1] == '.'
16621 && (p[2] == NUL
16622 || vim_ispathsep(p[2]))))))
16623 {
16624 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016625 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016626 if (cpy != NULL)
16627 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016628 vim_free(p);
16629 p = cpy;
16630 }
16631 }
16632 else if (!is_relative_to_current)
16633 {
16634 /* Strip leading "./". */
16635 q = p;
16636 while (q[0] == '.' && vim_ispathsep(q[1]))
16637 q += 2;
16638 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016639 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016640 }
16641 }
16642
16643 /* Ensure that the result will have no trailing path separator
16644 * if the argument had none. But keep "/" or "//". */
16645 if (!has_trailing_pathsep)
16646 {
16647 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016648 if (after_pathsep(p, q))
16649 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016650 }
16651
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016652 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016653 }
16654# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016655 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016656# endif
16657#endif
16658
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016659 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016660
16661#ifdef HAVE_READLINK
16662fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016663 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016664#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016665 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016666}
16667
16668/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016669 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016670 */
16671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016672f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016673{
Bram Moolenaar33570922005-01-25 22:26:29 +000016674 list_T *l;
16675 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016676
Bram Moolenaar0d660222005-01-07 21:51:51 +000016677 if (argvars[0].v_type != VAR_LIST)
16678 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016679 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016680 && !tv_check_lock(l->lv_lock,
16681 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016682 {
16683 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016684 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016685 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016686 while (li != NULL)
16687 {
16688 ni = li->li_prev;
16689 list_append(l, li);
16690 li = ni;
16691 }
16692 rettv->vval.v_list = l;
16693 rettv->v_type = VAR_LIST;
16694 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016695 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016697}
16698
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016699#define SP_NOMOVE 0x01 /* don't move cursor */
16700#define SP_REPEAT 0x02 /* repeat to find outer pair */
16701#define SP_RETCOUNT 0x04 /* return matchcount */
16702#define SP_SETPCMARK 0x08 /* set previous context mark */
16703#define SP_START 0x10 /* accept match at start position */
16704#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16705#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016706#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016707
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016708static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016709
16710/*
16711 * Get flags for a search function.
16712 * Possibly sets "p_ws".
16713 * Returns BACKWARD, FORWARD or zero (for an error).
16714 */
16715 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016716get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016717{
16718 int dir = FORWARD;
16719 char_u *flags;
16720 char_u nbuf[NUMBUFLEN];
16721 int mask;
16722
16723 if (varp->v_type != VAR_UNKNOWN)
16724 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016725 flags = get_tv_string_buf_chk(varp, nbuf);
16726 if (flags == NULL)
16727 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016728 while (*flags != NUL)
16729 {
16730 switch (*flags)
16731 {
16732 case 'b': dir = BACKWARD; break;
16733 case 'w': p_ws = TRUE; break;
16734 case 'W': p_ws = FALSE; break;
16735 default: mask = 0;
16736 if (flagsp != NULL)
16737 switch (*flags)
16738 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016739 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016740 case 'e': mask = SP_END; break;
16741 case 'm': mask = SP_RETCOUNT; break;
16742 case 'n': mask = SP_NOMOVE; break;
16743 case 'p': mask = SP_SUBPAT; break;
16744 case 'r': mask = SP_REPEAT; break;
16745 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016746 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016747 }
16748 if (mask == 0)
16749 {
16750 EMSG2(_(e_invarg2), flags);
16751 dir = 0;
16752 }
16753 else
16754 *flagsp |= mask;
16755 }
16756 if (dir == 0)
16757 break;
16758 ++flags;
16759 }
16760 }
16761 return dir;
16762}
16763
Bram Moolenaar071d4272004-06-13 20:20:40 +000016764/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016765 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016766 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016767 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016768search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016769{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016770 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016771 char_u *pat;
16772 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016773 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016774 int save_p_ws = p_ws;
16775 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016776 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016777 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016778 proftime_T tm;
16779#ifdef FEAT_RELTIME
16780 long time_limit = 0;
16781#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016782 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016783 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016784
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016785 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016786 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016787 if (dir == 0)
16788 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016789 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016790 if (flags & SP_START)
16791 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016792 if (flags & SP_END)
16793 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016794 if (flags & SP_COLUMN)
16795 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016796
Bram Moolenaar76929292008-01-06 19:07:36 +000016797 /* Optional arguments: line number to stop searching and timeout. */
16798 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016799 {
16800 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16801 if (lnum_stop < 0)
16802 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016803#ifdef FEAT_RELTIME
16804 if (argvars[3].v_type != VAR_UNKNOWN)
16805 {
16806 time_limit = get_tv_number_chk(&argvars[3], NULL);
16807 if (time_limit < 0)
16808 goto theend;
16809 }
16810#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016811 }
16812
Bram Moolenaar76929292008-01-06 19:07:36 +000016813#ifdef FEAT_RELTIME
16814 /* Set the time limit, if there is one. */
16815 profile_setlimit(time_limit, &tm);
16816#endif
16817
Bram Moolenaar231334e2005-07-25 20:46:57 +000016818 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016819 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016820 * Check to make sure only those flags are set.
16821 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16822 * flags cannot be set. Check for that condition also.
16823 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016824 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016825 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016826 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016827 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016828 goto theend;
16829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016831 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016832 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016833 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016834 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016835 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016836 if (flags & SP_SUBPAT)
16837 retval = subpatnum;
16838 else
16839 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016840 if (flags & SP_SETPCMARK)
16841 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016842 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016843 if (match_pos != NULL)
16844 {
16845 /* Store the match cursor position */
16846 match_pos->lnum = pos.lnum;
16847 match_pos->col = pos.col + 1;
16848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016849 /* "/$" will put the cursor after the end of the line, may need to
16850 * correct that here */
16851 check_cursor();
16852 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016853
16854 /* If 'n' flag is used: restore cursor position. */
16855 if (flags & SP_NOMOVE)
16856 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016857 else
16858 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016859theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016860 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016861
16862 return retval;
16863}
16864
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016865#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016866
16867/*
16868 * round() is not in C90, use ceil() or floor() instead.
16869 */
16870 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010016871vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016872{
16873 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16874}
16875
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016876/*
16877 * "round({float})" function
16878 */
16879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016880f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016881{
16882 float_T f;
16883
16884 rettv->v_type = VAR_FLOAT;
16885 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016886 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016887 else
16888 rettv->vval.v_float = 0.0;
16889}
16890#endif
16891
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016892/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016893 * "screenattr()" function
16894 */
16895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016896f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016897{
16898 int row;
16899 int col;
16900 int c;
16901
16902 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16903 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16904 if (row < 0 || row >= screen_Rows
16905 || col < 0 || col >= screen_Columns)
16906 c = -1;
16907 else
16908 c = ScreenAttrs[LineOffset[row] + col];
16909 rettv->vval.v_number = c;
16910}
16911
16912/*
16913 * "screenchar()" function
16914 */
16915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016916f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016917{
16918 int row;
16919 int col;
16920 int off;
16921 int c;
16922
16923 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16924 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16925 if (row < 0 || row >= screen_Rows
16926 || col < 0 || col >= screen_Columns)
16927 c = -1;
16928 else
16929 {
16930 off = LineOffset[row] + col;
16931#ifdef FEAT_MBYTE
16932 if (enc_utf8 && ScreenLinesUC[off] != 0)
16933 c = ScreenLinesUC[off];
16934 else
16935#endif
16936 c = ScreenLines[off];
16937 }
16938 rettv->vval.v_number = c;
16939}
16940
16941/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016942 * "screencol()" function
16943 *
16944 * First column is 1 to be consistent with virtcol().
16945 */
16946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016947f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016948{
16949 rettv->vval.v_number = screen_screencol() + 1;
16950}
16951
16952/*
16953 * "screenrow()" function
16954 */
16955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016956f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016957{
16958 rettv->vval.v_number = screen_screenrow() + 1;
16959}
16960
16961/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016962 * "search()" function
16963 */
16964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016965f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016966{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016967 int flags = 0;
16968
16969 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016970}
16971
Bram Moolenaar071d4272004-06-13 20:20:40 +000016972/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016973 * "searchdecl()" function
16974 */
16975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016976f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016977{
16978 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016979 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016980 int error = FALSE;
16981 char_u *name;
16982
16983 rettv->vval.v_number = 1; /* default: FAIL */
16984
16985 name = get_tv_string_chk(&argvars[0]);
16986 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016987 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016988 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016989 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16990 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16991 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016992 if (!error && name != NULL)
16993 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016994 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016995}
16996
16997/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016998 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017000 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017001searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002{
17003 char_u *spat, *mpat, *epat;
17004 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017005 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006 int dir;
17007 int flags = 0;
17008 char_u nbuf1[NUMBUFLEN];
17009 char_u nbuf2[NUMBUFLEN];
17010 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017011 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017012 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017013 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017016 spat = get_tv_string_chk(&argvars[0]);
17017 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17018 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17019 if (spat == NULL || mpat == NULL || epat == NULL)
17020 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021
Bram Moolenaar071d4272004-06-13 20:20:40 +000017022 /* Handle the optional fourth argument: flags */
17023 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017024 if (dir == 0)
17025 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017026
17027 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017028 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17029 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017030 if ((flags & (SP_END | SP_SUBPAT)) != 0
17031 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017032 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017033 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017034 goto theend;
17035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017036
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017037 /* Using 'r' implies 'W', otherwise it doesn't work. */
17038 if (flags & SP_REPEAT)
17039 p_ws = FALSE;
17040
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017041 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017042 if (argvars[3].v_type == VAR_UNKNOWN
17043 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044 skip = (char_u *)"";
17045 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017046 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017047 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017048 if (argvars[5].v_type != VAR_UNKNOWN)
17049 {
17050 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17051 if (lnum_stop < 0)
17052 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017053#ifdef FEAT_RELTIME
17054 if (argvars[6].v_type != VAR_UNKNOWN)
17055 {
17056 time_limit = get_tv_number_chk(&argvars[6], NULL);
17057 if (time_limit < 0)
17058 goto theend;
17059 }
17060#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017061 }
17062 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017063 if (skip == NULL)
17064 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017065
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017066 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017067 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017068
17069theend:
17070 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017071
17072 return retval;
17073}
17074
17075/*
17076 * "searchpair()" function
17077 */
17078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017079f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017080{
17081 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17082}
17083
17084/*
17085 * "searchpairpos()" function
17086 */
17087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017088f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017089{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017090 pos_T match_pos;
17091 int lnum = 0;
17092 int col = 0;
17093
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017094 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017095 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017096
17097 if (searchpair_cmn(argvars, &match_pos) > 0)
17098 {
17099 lnum = match_pos.lnum;
17100 col = match_pos.col;
17101 }
17102
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017103 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17104 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017105}
17106
17107/*
17108 * Search for a start/middle/end thing.
17109 * Used by searchpair(), see its documentation for the details.
17110 * Returns 0 or -1 for no match,
17111 */
17112 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017113do_searchpair(
17114 char_u *spat, /* start pattern */
17115 char_u *mpat, /* middle pattern */
17116 char_u *epat, /* end pattern */
17117 int dir, /* BACKWARD or FORWARD */
17118 char_u *skip, /* skip expression */
17119 int flags, /* SP_SETPCMARK and other SP_ values */
17120 pos_T *match_pos,
17121 linenr_T lnum_stop, /* stop at this line if not zero */
17122 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017123{
17124 char_u *save_cpo;
17125 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17126 long retval = 0;
17127 pos_T pos;
17128 pos_T firstpos;
17129 pos_T foundpos;
17130 pos_T save_cursor;
17131 pos_T save_pos;
17132 int n;
17133 int r;
17134 int nest = 1;
17135 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017136 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017137 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017138
17139 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17140 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017141 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017142
Bram Moolenaar76929292008-01-06 19:07:36 +000017143#ifdef FEAT_RELTIME
17144 /* Set the time limit, if there is one. */
17145 profile_setlimit(time_limit, &tm);
17146#endif
17147
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017148 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17149 * start/middle/end (pat3, for the top pair). */
17150 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17151 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17152 if (pat2 == NULL || pat3 == NULL)
17153 goto theend;
17154 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17155 if (*mpat == NUL)
17156 STRCPY(pat3, pat2);
17157 else
17158 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17159 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017160 if (flags & SP_START)
17161 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017162
Bram Moolenaar071d4272004-06-13 20:20:40 +000017163 save_cursor = curwin->w_cursor;
17164 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017165 clearpos(&firstpos);
17166 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167 pat = pat3;
17168 for (;;)
17169 {
17170 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017171 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17173 /* didn't find it or found the first match again: FAIL */
17174 break;
17175
17176 if (firstpos.lnum == 0)
17177 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017178 if (equalpos(pos, foundpos))
17179 {
17180 /* Found the same position again. Can happen with a pattern that
17181 * has "\zs" at the end and searching backwards. Advance one
17182 * character and try again. */
17183 if (dir == BACKWARD)
17184 decl(&pos);
17185 else
17186 incl(&pos);
17187 }
17188 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017190 /* clear the start flag to avoid getting stuck here */
17191 options &= ~SEARCH_START;
17192
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193 /* If the skip pattern matches, ignore this match. */
17194 if (*skip != NUL)
17195 {
17196 save_pos = curwin->w_cursor;
17197 curwin->w_cursor = pos;
17198 r = eval_to_bool(skip, &err, NULL, FALSE);
17199 curwin->w_cursor = save_pos;
17200 if (err)
17201 {
17202 /* Evaluating {skip} caused an error, break here. */
17203 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017204 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017205 break;
17206 }
17207 if (r)
17208 continue;
17209 }
17210
17211 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17212 {
17213 /* Found end when searching backwards or start when searching
17214 * forward: nested pair. */
17215 ++nest;
17216 pat = pat2; /* nested, don't search for middle */
17217 }
17218 else
17219 {
17220 /* Found end when searching forward or start when searching
17221 * backward: end of (nested) pair; or found middle in outer pair. */
17222 if (--nest == 1)
17223 pat = pat3; /* outer level, search for middle */
17224 }
17225
17226 if (nest == 0)
17227 {
17228 /* Found the match: return matchcount or line number. */
17229 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017230 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017231 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017232 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017233 if (flags & SP_SETPCMARK)
17234 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017235 curwin->w_cursor = pos;
17236 if (!(flags & SP_REPEAT))
17237 break;
17238 nest = 1; /* search for next unmatched */
17239 }
17240 }
17241
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017242 if (match_pos != NULL)
17243 {
17244 /* Store the match cursor position */
17245 match_pos->lnum = curwin->w_cursor.lnum;
17246 match_pos->col = curwin->w_cursor.col + 1;
17247 }
17248
Bram Moolenaar071d4272004-06-13 20:20:40 +000017249 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017250 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017251 curwin->w_cursor = save_cursor;
17252
17253theend:
17254 vim_free(pat2);
17255 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017256 if (p_cpo == empty_option)
17257 p_cpo = save_cpo;
17258 else
17259 /* Darn, evaluating the {skip} expression changed the value. */
17260 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017261
17262 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017263}
17264
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017265/*
17266 * "searchpos()" function
17267 */
17268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017269f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017270{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017271 pos_T match_pos;
17272 int lnum = 0;
17273 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017274 int n;
17275 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017276
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017277 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017278 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017279
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017280 n = search_cmn(argvars, &match_pos, &flags);
17281 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017282 {
17283 lnum = match_pos.lnum;
17284 col = match_pos.col;
17285 }
17286
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017287 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17288 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017289 if (flags & SP_SUBPAT)
17290 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017291}
17292
Bram Moolenaar0d660222005-01-07 21:51:51 +000017293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017294f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017295{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017296#ifdef FEAT_CLIENTSERVER
17297 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017298 char_u *server = get_tv_string_chk(&argvars[0]);
17299 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300
Bram Moolenaar0d660222005-01-07 21:51:51 +000017301 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017302 if (server == NULL || reply == NULL)
17303 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017304 if (check_restricted() || check_secure())
17305 return;
17306# ifdef FEAT_X11
17307 if (check_connection() == FAIL)
17308 return;
17309# endif
17310
17311 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017313 EMSG(_("E258: Unable to send to client"));
17314 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017315 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017316 rettv->vval.v_number = 0;
17317#else
17318 rettv->vval.v_number = -1;
17319#endif
17320}
17321
Bram Moolenaar0d660222005-01-07 21:51:51 +000017322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017323f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017324{
17325 char_u *r = NULL;
17326
17327#ifdef FEAT_CLIENTSERVER
17328# ifdef WIN32
17329 r = serverGetVimNames();
17330# else
17331 make_connection();
17332 if (X_DISPLAY != NULL)
17333 r = serverGetVimNames(X_DISPLAY);
17334# endif
17335#endif
17336 rettv->v_type = VAR_STRING;
17337 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017338}
17339
17340/*
17341 * "setbufvar()" function
17342 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017344f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017345{
17346 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017347 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017348 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017349 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017350 char_u nbuf[NUMBUFLEN];
17351
17352 if (check_restricted() || check_secure())
17353 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017354 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17355 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017356 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357 varp = &argvars[2];
17358
17359 if (buf != NULL && varname != NULL && varp != NULL)
17360 {
17361 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363
17364 if (*varname == '&')
17365 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017366 long numval;
17367 char_u *strval;
17368 int error = FALSE;
17369
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017371 numval = get_tv_number_chk(varp, &error);
17372 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017373 if (!error && strval != NULL)
17374 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017375 }
17376 else
17377 {
17378 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17379 if (bufvarname != NULL)
17380 {
17381 STRCPY(bufvarname, "b:");
17382 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017383 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384 vim_free(bufvarname);
17385 }
17386 }
17387
17388 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391}
17392
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017394f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017395{
17396 dict_T *d;
17397 dictitem_T *di;
17398 char_u *csearch;
17399
17400 if (argvars[0].v_type != VAR_DICT)
17401 {
17402 EMSG(_(e_dictreq));
17403 return;
17404 }
17405
17406 if ((d = argvars[0].vval.v_dict) != NULL)
17407 {
17408 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17409 if (csearch != NULL)
17410 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017411#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017412 if (enc_utf8)
17413 {
17414 int pcc[MAX_MCO];
17415 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017416
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017417 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17418 }
17419 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017420#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017421 set_last_csearch(PTR2CHAR(csearch),
17422 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017423 }
17424
17425 di = dict_find(d, (char_u *)"forward", -1);
17426 if (di != NULL)
17427 set_csearch_direction(get_tv_number(&di->di_tv)
17428 ? FORWARD : BACKWARD);
17429
17430 di = dict_find(d, (char_u *)"until", -1);
17431 if (di != NULL)
17432 set_csearch_until(!!get_tv_number(&di->di_tv));
17433 }
17434}
17435
Bram Moolenaar071d4272004-06-13 20:20:40 +000017436/*
17437 * "setcmdpos()" function
17438 */
17439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017440f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017441{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017442 int pos = (int)get_tv_number(&argvars[0]) - 1;
17443
17444 if (pos >= 0)
17445 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017446}
17447
17448/*
17449 * "setline()" function
17450 */
17451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017452f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453{
17454 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017455 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017456 list_T *l = NULL;
17457 listitem_T *li = NULL;
17458 long added = 0;
17459 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017460
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017461 lnum = get_tv_lnum(&argvars[0]);
17462 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017463 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017464 l = argvars[1].vval.v_list;
17465 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017466 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017467 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017468 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017469
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017470 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017471 for (;;)
17472 {
17473 if (l != NULL)
17474 {
17475 /* list argument, get next string */
17476 if (li == NULL)
17477 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017478 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017479 li = li->li_next;
17480 }
17481
17482 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017483 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017484 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017485
17486 /* When coming here from Insert mode, sync undo, so that this can be
17487 * undone separately from what was previously inserted. */
17488 if (u_sync_once == 2)
17489 {
17490 u_sync_once = 1; /* notify that u_sync() was called */
17491 u_sync(TRUE);
17492 }
17493
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017494 if (lnum <= curbuf->b_ml.ml_line_count)
17495 {
17496 /* existing line, replace it */
17497 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17498 {
17499 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017500 if (lnum == curwin->w_cursor.lnum)
17501 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017502 rettv->vval.v_number = 0; /* OK */
17503 }
17504 }
17505 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17506 {
17507 /* lnum is one past the last line, append the line */
17508 ++added;
17509 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17510 rettv->vval.v_number = 0; /* OK */
17511 }
17512
17513 if (l == NULL) /* only one string argument */
17514 break;
17515 ++lnum;
17516 }
17517
17518 if (added > 0)
17519 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017520}
17521
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017522static 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 +000017523
Bram Moolenaar071d4272004-06-13 20:20:40 +000017524/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017525 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017526 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017528set_qf_ll_list(
17529 win_T *wp UNUSED,
17530 typval_T *list_arg UNUSED,
17531 typval_T *action_arg UNUSED,
17532 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017533{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017534#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017535 char_u *act;
17536 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017537#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017538
Bram Moolenaar2641f772005-03-25 21:58:17 +000017539 rettv->vval.v_number = -1;
17540
17541#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017542 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017543 EMSG(_(e_listreq));
17544 else
17545 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017546 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017547
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017548 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017549 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017550 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017551 if (act == NULL)
17552 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017553 if (*act == 'a' || *act == 'r')
17554 action = *act;
17555 }
17556
Bram Moolenaar81484f42012-12-05 15:16:47 +010017557 if (l != NULL && set_errorlist(wp, l, action,
17558 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017559 rettv->vval.v_number = 0;
17560 }
17561#endif
17562}
17563
17564/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017565 * "setloclist()" function
17566 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017568f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017569{
17570 win_T *win;
17571
17572 rettv->vval.v_number = -1;
17573
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017574 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017575 if (win != NULL)
17576 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17577}
17578
17579/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017580 * "setmatches()" function
17581 */
17582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017583f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017584{
17585#ifdef FEAT_SEARCH_EXTRA
17586 list_T *l;
17587 listitem_T *li;
17588 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017589 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017590
17591 rettv->vval.v_number = -1;
17592 if (argvars[0].v_type != VAR_LIST)
17593 {
17594 EMSG(_(e_listreq));
17595 return;
17596 }
17597 if ((l = argvars[0].vval.v_list) != NULL)
17598 {
17599
17600 /* To some extent make sure that we are dealing with a list from
17601 * "getmatches()". */
17602 li = l->lv_first;
17603 while (li != NULL)
17604 {
17605 if (li->li_tv.v_type != VAR_DICT
17606 || (d = li->li_tv.vval.v_dict) == NULL)
17607 {
17608 EMSG(_(e_invarg));
17609 return;
17610 }
17611 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017612 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17613 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017614 && dict_find(d, (char_u *)"priority", -1) != NULL
17615 && dict_find(d, (char_u *)"id", -1) != NULL))
17616 {
17617 EMSG(_(e_invarg));
17618 return;
17619 }
17620 li = li->li_next;
17621 }
17622
17623 clear_matches(curwin);
17624 li = l->lv_first;
17625 while (li != NULL)
17626 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017627 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017628 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017629 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017630 char_u *group;
17631 int priority;
17632 int id;
17633 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017634
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017635 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017636 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17637 {
17638 if (s == NULL)
17639 {
17640 s = list_alloc();
17641 if (s == NULL)
17642 return;
17643 }
17644
17645 /* match from matchaddpos() */
17646 for (i = 1; i < 9; i++)
17647 {
17648 sprintf((char *)buf, (char *)"pos%d", i);
17649 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17650 {
17651 if (di->di_tv.v_type != VAR_LIST)
17652 return;
17653
17654 list_append_tv(s, &di->di_tv);
17655 s->lv_refcount++;
17656 }
17657 else
17658 break;
17659 }
17660 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017661
17662 group = get_dict_string(d, (char_u *)"group", FALSE);
17663 priority = (int)get_dict_number(d, (char_u *)"priority");
17664 id = (int)get_dict_number(d, (char_u *)"id");
17665 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17666 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17667 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017668 if (i == 0)
17669 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017670 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017671 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017672 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017673 }
17674 else
17675 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017676 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017677 list_unref(s);
17678 s = NULL;
17679 }
17680
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017681 li = li->li_next;
17682 }
17683 rettv->vval.v_number = 0;
17684 }
17685#endif
17686}
17687
17688/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017689 * "setpos()" function
17690 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017692f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017693{
17694 pos_T pos;
17695 int fnum;
17696 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017697 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017698
Bram Moolenaar08250432008-02-13 11:42:46 +000017699 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017700 name = get_tv_string_chk(argvars);
17701 if (name != NULL)
17702 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017703 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017704 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017705 if (--pos.col < 0)
17706 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017707 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017708 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017709 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017710 if (fnum == curbuf->b_fnum)
17711 {
17712 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017713 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017714 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017715 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017716 curwin->w_set_curswant = FALSE;
17717 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017718 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017719 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017720 }
17721 else
17722 EMSG(_(e_invarg));
17723 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017724 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17725 {
17726 /* set mark */
17727 if (setmark_pos(name[1], &pos, fnum) == OK)
17728 rettv->vval.v_number = 0;
17729 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017730 else
17731 EMSG(_(e_invarg));
17732 }
17733 }
17734}
17735
17736/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017737 * "setqflist()" function
17738 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017740f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017741{
17742 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17743}
17744
17745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746 * "setreg()" function
17747 */
17748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017749f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750{
17751 int regname;
17752 char_u *strregname;
17753 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017754 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017755 int append;
17756 char_u yank_type;
17757 long block_len;
17758
17759 block_len = -1;
17760 yank_type = MAUTO;
17761 append = FALSE;
17762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017763 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017764 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017766 if (strregname == NULL)
17767 return; /* type error; errmsg already given */
17768 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017769 if (regname == 0 || regname == '@')
17770 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017771
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017772 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017774 stropt = get_tv_string_chk(&argvars[2]);
17775 if (stropt == NULL)
17776 return; /* type error */
17777 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778 switch (*stropt)
17779 {
17780 case 'a': case 'A': /* append */
17781 append = TRUE;
17782 break;
17783 case 'v': case 'c': /* character-wise selection */
17784 yank_type = MCHAR;
17785 break;
17786 case 'V': case 'l': /* line-wise selection */
17787 yank_type = MLINE;
17788 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017789 case 'b': case Ctrl_V: /* block-wise selection */
17790 yank_type = MBLOCK;
17791 if (VIM_ISDIGIT(stropt[1]))
17792 {
17793 ++stropt;
17794 block_len = getdigits(&stropt) - 1;
17795 --stropt;
17796 }
17797 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798 }
17799 }
17800
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017801 if (argvars[1].v_type == VAR_LIST)
17802 {
17803 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017804 char_u **allocval;
17805 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017806 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017807 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017808 int len = argvars[1].vval.v_list->lv_len;
17809 listitem_T *li;
17810
Bram Moolenaar7d647822014-04-05 21:28:56 +020017811 /* First half: use for pointers to result lines; second half: use for
17812 * pointers to allocated copies. */
17813 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017814 if (lstval == NULL)
17815 return;
17816 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017817 allocval = lstval + len + 2;
17818 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017819
17820 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17821 li = li->li_next)
17822 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017823 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017824 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017825 goto free_lstval;
17826 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017827 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017828 /* Need to make a copy, next get_tv_string_buf_chk() will
17829 * overwrite the string. */
17830 strval = vim_strsave(buf);
17831 if (strval == NULL)
17832 goto free_lstval;
17833 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017834 }
17835 *curval++ = strval;
17836 }
17837 *curval++ = NULL;
17838
17839 write_reg_contents_lst(regname, lstval, -1,
17840 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017841free_lstval:
17842 while (curallocval > allocval)
17843 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017844 vim_free(lstval);
17845 }
17846 else
17847 {
17848 strval = get_tv_string_chk(&argvars[1]);
17849 if (strval == NULL)
17850 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017851 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017852 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017853 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017854 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017855}
17856
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017857/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017858 * "settabvar()" function
17859 */
17860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017861f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017862{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017863#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017864 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017865 tabpage_T *tp;
17866#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017867 char_u *varname, *tabvarname;
17868 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017869
17870 rettv->vval.v_number = 0;
17871
17872 if (check_restricted() || check_secure())
17873 return;
17874
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017875#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017876 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017877#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017878 varname = get_tv_string_chk(&argvars[1]);
17879 varp = &argvars[2];
17880
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017881 if (varname != NULL && varp != NULL
17882#ifdef FEAT_WINDOWS
17883 && tp != NULL
17884#endif
17885 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017886 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017887#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017888 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017889 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017890#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017891
17892 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17893 if (tabvarname != NULL)
17894 {
17895 STRCPY(tabvarname, "t:");
17896 STRCPY(tabvarname + 2, varname);
17897 set_var(tabvarname, varp, TRUE);
17898 vim_free(tabvarname);
17899 }
17900
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017901#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017902 /* Restore current tabpage */
17903 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017904 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017905#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017906 }
17907}
17908
17909/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017910 * "settabwinvar()" function
17911 */
17912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017913f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017914{
17915 setwinvar(argvars, rettv, 1);
17916}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017917
17918/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017919 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017922f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017924 setwinvar(argvars, rettv, 0);
17925}
17926
17927/*
17928 * "setwinvar()" and "settabwinvar()" functions
17929 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017930
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017932setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017933{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017934 win_T *win;
17935#ifdef FEAT_WINDOWS
17936 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017937 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017938 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017939#endif
17940 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017941 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017942 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017943 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017944
17945 if (check_restricted() || check_secure())
17946 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017947
17948#ifdef FEAT_WINDOWS
17949 if (off == 1)
17950 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17951 else
17952 tp = curtab;
17953#endif
17954 win = find_win_by_nr(&argvars[off], tp);
17955 varname = get_tv_string_chk(&argvars[off + 1]);
17956 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017957
17958 if (win != NULL && varname != NULL && varp != NULL)
17959 {
17960#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017961 need_switch_win = !(tp == curtab && win == curwin);
17962 if (!need_switch_win
17963 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017965 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017966 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017967 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017968 long numval;
17969 char_u *strval;
17970 int error = FALSE;
17971
17972 ++varname;
17973 numval = get_tv_number_chk(varp, &error);
17974 strval = get_tv_string_buf_chk(varp, nbuf);
17975 if (!error && strval != NULL)
17976 set_option_value(varname, numval, strval, OPT_LOCAL);
17977 }
17978 else
17979 {
17980 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17981 if (winvarname != NULL)
17982 {
17983 STRCPY(winvarname, "w:");
17984 STRCPY(winvarname + 2, varname);
17985 set_var(winvarname, varp, TRUE);
17986 vim_free(winvarname);
17987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017988 }
17989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017990#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017991 if (need_switch_win)
17992 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017993#endif
17994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017995}
17996
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017997#ifdef FEAT_CRYPT
17998/*
17999 * "sha256({string})" function
18000 */
18001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018002f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018003{
18004 char_u *p;
18005
18006 p = get_tv_string(&argvars[0]);
18007 rettv->vval.v_string = vim_strsave(
18008 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18009 rettv->v_type = VAR_STRING;
18010}
18011#endif /* FEAT_CRYPT */
18012
Bram Moolenaar071d4272004-06-13 20:20:40 +000018013/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018014 * "shellescape({string})" function
18015 */
18016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018017f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018018{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018019 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018020 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018021 rettv->v_type = VAR_STRING;
18022}
18023
18024/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018025 * shiftwidth() function
18026 */
18027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018028f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018029{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018030 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018031}
18032
18033/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018034 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018035 */
18036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018037f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018038{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018039 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040
Bram Moolenaar0d660222005-01-07 21:51:51 +000018041 p = get_tv_string(&argvars[0]);
18042 rettv->vval.v_string = vim_strsave(p);
18043 simplify_filename(rettv->vval.v_string); /* simplify in place */
18044 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018045}
18046
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018047#ifdef FEAT_FLOAT
18048/*
18049 * "sin()" function
18050 */
18051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018052f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018053{
18054 float_T f;
18055
18056 rettv->v_type = VAR_FLOAT;
18057 if (get_float_arg(argvars, &f) == OK)
18058 rettv->vval.v_float = sin(f);
18059 else
18060 rettv->vval.v_float = 0.0;
18061}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018062
18063/*
18064 * "sinh()" function
18065 */
18066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018067f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018068{
18069 float_T f;
18070
18071 rettv->v_type = VAR_FLOAT;
18072 if (get_float_arg(argvars, &f) == OK)
18073 rettv->vval.v_float = sinh(f);
18074 else
18075 rettv->vval.v_float = 0.0;
18076}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018077#endif
18078
Bram Moolenaar0d660222005-01-07 21:51:51 +000018079static int
18080#ifdef __BORLANDC__
18081 _RTLENTRYF
18082#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018083 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018084static int
18085#ifdef __BORLANDC__
18086 _RTLENTRYF
18087#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018088 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018089
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018090/* struct used in the array that's given to qsort() */
18091typedef struct
18092{
18093 listitem_T *item;
18094 int idx;
18095} sortItem_T;
18096
Bram Moolenaar0d660222005-01-07 21:51:51 +000018097static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018098static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018099static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018100#ifdef FEAT_FLOAT
18101static int item_compare_float;
18102#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018103static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018104static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018105static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018106static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018107static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018108#define ITEM_COMPARE_FAIL 999
18109
Bram Moolenaar071d4272004-06-13 20:20:40 +000018110/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018111 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018112 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018113 static int
18114#ifdef __BORLANDC__
18115_RTLENTRYF
18116#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018117item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018119 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018120 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018121 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018122 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018123 int res;
18124 char_u numbuf1[NUMBUFLEN];
18125 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018126
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018127 si1 = (sortItem_T *)s1;
18128 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018129 tv1 = &si1->item->li_tv;
18130 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018131
18132 if (item_compare_numbers)
18133 {
18134 long v1 = get_tv_number(tv1);
18135 long v2 = get_tv_number(tv2);
18136
18137 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18138 }
18139
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018140#ifdef FEAT_FLOAT
18141 if (item_compare_float)
18142 {
18143 float_T v1 = get_tv_float(tv1);
18144 float_T v2 = get_tv_float(tv2);
18145
18146 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18147 }
18148#endif
18149
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018150 /* tv2string() puts quotes around a string and allocates memory. Don't do
18151 * that for string variables. Use a single quote when comparing with a
18152 * non-string to do what the docs promise. */
18153 if (tv1->v_type == VAR_STRING)
18154 {
18155 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18156 p1 = (char_u *)"'";
18157 else
18158 p1 = tv1->vval.v_string;
18159 }
18160 else
18161 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18162 if (tv2->v_type == VAR_STRING)
18163 {
18164 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18165 p2 = (char_u *)"'";
18166 else
18167 p2 = tv2->vval.v_string;
18168 }
18169 else
18170 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018171 if (p1 == NULL)
18172 p1 = (char_u *)"";
18173 if (p2 == NULL)
18174 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018175 if (!item_compare_numeric)
18176 {
18177 if (item_compare_ic)
18178 res = STRICMP(p1, p2);
18179 else
18180 res = STRCMP(p1, p2);
18181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018182 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018183 {
18184 double n1, n2;
18185 n1 = strtod((char *)p1, (char **)&p1);
18186 n2 = strtod((char *)p2, (char **)&p2);
18187 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18188 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018189
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018190 /* When the result would be zero, compare the item indexes. Makes the
18191 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018192 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018193 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018194
Bram Moolenaar0d660222005-01-07 21:51:51 +000018195 vim_free(tofree1);
18196 vim_free(tofree2);
18197 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018198}
18199
18200 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018201#ifdef __BORLANDC__
18202_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018203#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018204item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018205{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018206 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018207 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018208 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018209 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018210 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018211
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018212 /* shortcut after failure in previous call; compare all items equal */
18213 if (item_compare_func_err)
18214 return 0;
18215
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018216 si1 = (sortItem_T *)s1;
18217 si2 = (sortItem_T *)s2;
18218
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018219 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018220 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018221 copy_tv(&si1->item->li_tv, &argv[0]);
18222 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018223
18224 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018225 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018226 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18227 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018228 clear_tv(&argv[0]);
18229 clear_tv(&argv[1]);
18230
18231 if (res == FAIL)
18232 res = ITEM_COMPARE_FAIL;
18233 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018234 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18235 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018236 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018237 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018238
18239 /* When the result would be zero, compare the pointers themselves. Makes
18240 * the sort stable. */
18241 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018242 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018243
Bram Moolenaar0d660222005-01-07 21:51:51 +000018244 return res;
18245}
18246
18247/*
18248 * "sort({list})" function
18249 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018251do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018252{
Bram Moolenaar33570922005-01-25 22:26:29 +000018253 list_T *l;
18254 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018255 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018256 long len;
18257 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258
Bram Moolenaar0d660222005-01-07 21:51:51 +000018259 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018260 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261 else
18262 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018263 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018264 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018265 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18266 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018267 return;
18268 rettv->vval.v_list = l;
18269 rettv->v_type = VAR_LIST;
18270 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018271
Bram Moolenaar0d660222005-01-07 21:51:51 +000018272 len = list_len(l);
18273 if (len <= 1)
18274 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018275
Bram Moolenaar0d660222005-01-07 21:51:51 +000018276 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018277 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018278 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018279#ifdef FEAT_FLOAT
18280 item_compare_float = FALSE;
18281#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018282 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018283 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018284 if (argvars[1].v_type != VAR_UNKNOWN)
18285 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018286 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018287 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018288 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018289 else
18290 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018291 int error = FALSE;
18292
18293 i = get_tv_number_chk(&argvars[1], &error);
18294 if (error)
18295 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018296 if (i == 1)
18297 item_compare_ic = TRUE;
18298 else
18299 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018300 if (item_compare_func != NULL)
18301 {
18302 if (STRCMP(item_compare_func, "n") == 0)
18303 {
18304 item_compare_func = NULL;
18305 item_compare_numeric = TRUE;
18306 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018307 else if (STRCMP(item_compare_func, "N") == 0)
18308 {
18309 item_compare_func = NULL;
18310 item_compare_numbers = TRUE;
18311 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018312#ifdef FEAT_FLOAT
18313 else if (STRCMP(item_compare_func, "f") == 0)
18314 {
18315 item_compare_func = NULL;
18316 item_compare_float = TRUE;
18317 }
18318#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020018319 else if (STRCMP(item_compare_func, "i") == 0)
18320 {
18321 item_compare_func = NULL;
18322 item_compare_ic = TRUE;
18323 }
18324 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018325 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018326
18327 if (argvars[2].v_type != VAR_UNKNOWN)
18328 {
18329 /* optional third argument: {dict} */
18330 if (argvars[2].v_type != VAR_DICT)
18331 {
18332 EMSG(_(e_dictreq));
18333 return;
18334 }
18335 item_compare_selfdict = argvars[2].vval.v_dict;
18336 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018338
Bram Moolenaar0d660222005-01-07 21:51:51 +000018339 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018340 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018341 if (ptrs == NULL)
18342 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018343
Bram Moolenaar327aa022014-03-25 18:24:23 +010018344 i = 0;
18345 if (sort)
18346 {
18347 /* sort(): ptrs will be the list to sort */
18348 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018349 {
18350 ptrs[i].item = li;
18351 ptrs[i].idx = i;
18352 ++i;
18353 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018354
18355 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018356 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018357 /* test the compare function */
18358 if (item_compare_func != NULL
18359 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018360 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018361 EMSG(_("E702: Sort compare function failed"));
18362 else
18363 {
18364 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018365 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018366 item_compare_func == NULL ? item_compare : item_compare2);
18367
18368 if (!item_compare_func_err)
18369 {
18370 /* Clear the List and append the items in sorted order. */
18371 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18372 l->lv_len = 0;
18373 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018374 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018375 }
18376 }
18377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018378 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018379 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018380 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018381
18382 /* f_uniq(): ptrs will be a stack of items to remove */
18383 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018384 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018385 item_compare_func_ptr = item_compare_func
18386 ? item_compare2 : item_compare;
18387
18388 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18389 li = li->li_next)
18390 {
18391 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18392 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018393 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018394 if (item_compare_func_err)
18395 {
18396 EMSG(_("E882: Uniq compare function failed"));
18397 break;
18398 }
18399 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018400
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018401 if (!item_compare_func_err)
18402 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018403 while (--i >= 0)
18404 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018405 li = ptrs[i].item->li_next;
18406 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018407 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018408 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018409 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018410 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018411 list_fix_watch(l, li);
18412 listitem_free(li);
18413 l->lv_len--;
18414 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018415 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018416 }
18417
18418 vim_free(ptrs);
18419 }
18420}
18421
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018422/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018423 * "sort({list})" function
18424 */
18425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018426f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018427{
18428 do_sort_uniq(argvars, rettv, TRUE);
18429}
18430
18431/*
18432 * "uniq({list})" function
18433 */
18434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018435f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018436{
18437 do_sort_uniq(argvars, rettv, FALSE);
18438}
18439
18440/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018441 * "soundfold({word})" function
18442 */
18443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018444f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018445{
18446 char_u *s;
18447
18448 rettv->v_type = VAR_STRING;
18449 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018450#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018451 rettv->vval.v_string = eval_soundfold(s);
18452#else
18453 rettv->vval.v_string = vim_strsave(s);
18454#endif
18455}
18456
18457/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018458 * "spellbadword()" function
18459 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018461f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018462{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018463 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018464 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018465 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018466
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018467 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018468 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018469
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018470#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018471 if (argvars[0].v_type == VAR_UNKNOWN)
18472 {
18473 /* Find the start and length of the badly spelled word. */
18474 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18475 if (len != 0)
18476 word = ml_get_cursor();
18477 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018478 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018479 {
18480 char_u *str = get_tv_string_chk(&argvars[0]);
18481 int capcol = -1;
18482
18483 if (str != NULL)
18484 {
18485 /* Check the argument for spelling. */
18486 while (*str != NUL)
18487 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018488 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018489 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018490 {
18491 word = str;
18492 break;
18493 }
18494 str += len;
18495 }
18496 }
18497 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018498#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018499
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018500 list_append_string(rettv->vval.v_list, word, len);
18501 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018502 attr == HLF_SPB ? "bad" :
18503 attr == HLF_SPR ? "rare" :
18504 attr == HLF_SPL ? "local" :
18505 attr == HLF_SPC ? "caps" :
18506 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018507}
18508
18509/*
18510 * "spellsuggest()" function
18511 */
18512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018513f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018514{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018515#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018516 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018517 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018518 int maxcount;
18519 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018520 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018521 listitem_T *li;
18522 int need_capital = FALSE;
18523#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018524
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018525 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018526 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018527
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018528#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018529 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018530 {
18531 str = get_tv_string(&argvars[0]);
18532 if (argvars[1].v_type != VAR_UNKNOWN)
18533 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018534 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018535 if (maxcount <= 0)
18536 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018537 if (argvars[2].v_type != VAR_UNKNOWN)
18538 {
18539 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18540 if (typeerr)
18541 return;
18542 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018543 }
18544 else
18545 maxcount = 25;
18546
Bram Moolenaar4770d092006-01-12 23:22:24 +000018547 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018548
18549 for (i = 0; i < ga.ga_len; ++i)
18550 {
18551 str = ((char_u **)ga.ga_data)[i];
18552
18553 li = listitem_alloc();
18554 if (li == NULL)
18555 vim_free(str);
18556 else
18557 {
18558 li->li_tv.v_type = VAR_STRING;
18559 li->li_tv.v_lock = 0;
18560 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018561 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018562 }
18563 }
18564 ga_clear(&ga);
18565 }
18566#endif
18567}
18568
Bram Moolenaar0d660222005-01-07 21:51:51 +000018569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018570f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018571{
18572 char_u *str;
18573 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018574 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018575 regmatch_T regmatch;
18576 char_u patbuf[NUMBUFLEN];
18577 char_u *save_cpo;
18578 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018579 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018580 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018581 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018582
18583 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18584 save_cpo = p_cpo;
18585 p_cpo = (char_u *)"";
18586
18587 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018588 if (argvars[1].v_type != VAR_UNKNOWN)
18589 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018590 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18591 if (pat == NULL)
18592 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018593 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018594 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018595 }
18596 if (pat == NULL || *pat == NUL)
18597 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018598
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018599 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018600 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018601 if (typeerr)
18602 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018603
Bram Moolenaar0d660222005-01-07 21:51:51 +000018604 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18605 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018606 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018607 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018608 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018609 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018610 if (*str == NUL)
18611 match = FALSE; /* empty item at the end */
18612 else
18613 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018614 if (match)
18615 end = regmatch.startp[0];
18616 else
18617 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018618 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18619 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018620 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018621 if (list_append_string(rettv->vval.v_list, str,
18622 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018623 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018624 }
18625 if (!match)
18626 break;
18627 /* Advance to just after the match. */
18628 if (regmatch.endp[0] > str)
18629 col = 0;
18630 else
18631 {
18632 /* Don't get stuck at the same match. */
18633#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018634 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018635#else
18636 col = 1;
18637#endif
18638 }
18639 str = regmatch.endp[0];
18640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641
Bram Moolenaar473de612013-06-08 18:19:48 +020018642 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018644
Bram Moolenaar0d660222005-01-07 21:51:51 +000018645 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018646}
18647
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018648#ifdef FEAT_FLOAT
18649/*
18650 * "sqrt()" function
18651 */
18652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018653f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018654{
18655 float_T f;
18656
18657 rettv->v_type = VAR_FLOAT;
18658 if (get_float_arg(argvars, &f) == OK)
18659 rettv->vval.v_float = sqrt(f);
18660 else
18661 rettv->vval.v_float = 0.0;
18662}
18663
18664/*
18665 * "str2float()" function
18666 */
18667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018668f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018669{
18670 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18671
18672 if (*p == '+')
18673 p = skipwhite(p + 1);
18674 (void)string2float(p, &rettv->vval.v_float);
18675 rettv->v_type = VAR_FLOAT;
18676}
18677#endif
18678
Bram Moolenaar2c932302006-03-18 21:42:09 +000018679/*
18680 * "str2nr()" function
18681 */
18682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018683f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018684{
18685 int base = 10;
18686 char_u *p;
18687 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018688 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018689
18690 if (argvars[1].v_type != VAR_UNKNOWN)
18691 {
18692 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018693 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018694 {
18695 EMSG(_(e_invarg));
18696 return;
18697 }
18698 }
18699
18700 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018701 if (*p == '+')
18702 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018703 switch (base)
18704 {
18705 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18706 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18707 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18708 default: what = 0;
18709 }
18710 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018711 rettv->vval.v_number = n;
18712}
18713
Bram Moolenaar071d4272004-06-13 20:20:40 +000018714#ifdef HAVE_STRFTIME
18715/*
18716 * "strftime({format}[, {time}])" function
18717 */
18718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018719f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018720{
18721 char_u result_buf[256];
18722 struct tm *curtime;
18723 time_t seconds;
18724 char_u *p;
18725
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018726 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018727
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018728 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018729 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018730 seconds = time(NULL);
18731 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018732 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018733 curtime = localtime(&seconds);
18734 /* MSVC returns NULL for an invalid value of seconds. */
18735 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018736 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737 else
18738 {
18739# ifdef FEAT_MBYTE
18740 vimconv_T conv;
18741 char_u *enc;
18742
18743 conv.vc_type = CONV_NONE;
18744 enc = enc_locale();
18745 convert_setup(&conv, p_enc, enc);
18746 if (conv.vc_type != CONV_NONE)
18747 p = string_convert(&conv, p, NULL);
18748# endif
18749 if (p != NULL)
18750 (void)strftime((char *)result_buf, sizeof(result_buf),
18751 (char *)p, curtime);
18752 else
18753 result_buf[0] = NUL;
18754
18755# ifdef FEAT_MBYTE
18756 if (conv.vc_type != CONV_NONE)
18757 vim_free(p);
18758 convert_setup(&conv, enc, p_enc);
18759 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018760 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761 else
18762# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018763 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018764
18765# ifdef FEAT_MBYTE
18766 /* Release conversion descriptors */
18767 convert_setup(&conv, NULL, NULL);
18768 vim_free(enc);
18769# endif
18770 }
18771}
18772#endif
18773
18774/*
18775 * "stridx()" function
18776 */
18777 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018778f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779{
18780 char_u buf[NUMBUFLEN];
18781 char_u *needle;
18782 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018783 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018784 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018785 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018787 needle = get_tv_string_chk(&argvars[1]);
18788 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018789 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018790 if (needle == NULL || haystack == NULL)
18791 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792
Bram Moolenaar33570922005-01-25 22:26:29 +000018793 if (argvars[2].v_type != VAR_UNKNOWN)
18794 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018795 int error = FALSE;
18796
18797 start_idx = get_tv_number_chk(&argvars[2], &error);
18798 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018799 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018800 if (start_idx >= 0)
18801 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018802 }
18803
18804 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18805 if (pos != NULL)
18806 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807}
18808
18809/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018810 * "string()" function
18811 */
18812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018813f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018814{
18815 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018816 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018817
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018818 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018819 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018820 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018821 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018822 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823}
18824
18825/*
18826 * "strlen()" function
18827 */
18828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018829f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018830{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018831 rettv->vval.v_number = (varnumber_T)(STRLEN(
18832 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018833}
18834
18835/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018836 * "strchars()" function
18837 */
18838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018839f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018840{
18841 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018842 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018843#ifdef FEAT_MBYTE
18844 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018845 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018846#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018847
18848 if (argvars[1].v_type != VAR_UNKNOWN)
18849 skipcc = get_tv_number_chk(&argvars[1], NULL);
18850 if (skipcc < 0 || skipcc > 1)
18851 EMSG(_(e_invarg));
18852 else
18853 {
18854#ifdef FEAT_MBYTE
18855 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18856 while (*s != NUL)
18857 {
18858 func_mb_ptr2char_adv(&s);
18859 ++len;
18860 }
18861 rettv->vval.v_number = len;
18862#else
18863 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18864#endif
18865 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018866}
18867
18868/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018869 * "strdisplaywidth()" function
18870 */
18871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018872f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020018873{
18874 char_u *s = get_tv_string(&argvars[0]);
18875 int col = 0;
18876
18877 if (argvars[1].v_type != VAR_UNKNOWN)
18878 col = get_tv_number(&argvars[1]);
18879
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018880 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018881}
18882
18883/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018884 * "strwidth()" function
18885 */
18886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018887f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018888{
18889 char_u *s = get_tv_string(&argvars[0]);
18890
18891 rettv->vval.v_number = (varnumber_T)(
18892#ifdef FEAT_MBYTE
18893 mb_string2cells(s, -1)
18894#else
18895 STRLEN(s)
18896#endif
18897 );
18898}
18899
18900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018901 * "strpart()" function
18902 */
18903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018904f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905{
18906 char_u *p;
18907 int n;
18908 int len;
18909 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018910 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018912 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018913 slen = (int)STRLEN(p);
18914
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018915 n = get_tv_number_chk(&argvars[1], &error);
18916 if (error)
18917 len = 0;
18918 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018919 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018920 else
18921 len = slen - n; /* default len: all bytes that are available. */
18922
18923 /*
18924 * Only return the overlap between the specified part and the actual
18925 * string.
18926 */
18927 if (n < 0)
18928 {
18929 len += n;
18930 n = 0;
18931 }
18932 else if (n > slen)
18933 n = slen;
18934 if (len < 0)
18935 len = 0;
18936 else if (n + len > slen)
18937 len = slen - n;
18938
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018939 rettv->v_type = VAR_STRING;
18940 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018941}
18942
18943/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018944 * "strridx()" function
18945 */
18946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018947f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018948{
18949 char_u buf[NUMBUFLEN];
18950 char_u *needle;
18951 char_u *haystack;
18952 char_u *rest;
18953 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018954 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018955
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018956 needle = get_tv_string_chk(&argvars[1]);
18957 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018958
18959 rettv->vval.v_number = -1;
18960 if (needle == NULL || haystack == NULL)
18961 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018962
18963 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018964 if (argvars[2].v_type != VAR_UNKNOWN)
18965 {
18966 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018967 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018968 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018969 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018970 }
18971 else
18972 end_idx = haystack_len;
18973
Bram Moolenaar0d660222005-01-07 21:51:51 +000018974 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018975 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018976 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018977 lastmatch = haystack + end_idx;
18978 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018979 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018980 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018981 for (rest = haystack; *rest != '\0'; ++rest)
18982 {
18983 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018984 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018985 break;
18986 lastmatch = rest;
18987 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018988 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018989
18990 if (lastmatch == NULL)
18991 rettv->vval.v_number = -1;
18992 else
18993 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18994}
18995
18996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018997 * "strtrans()" function
18998 */
18999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019000f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019002 rettv->v_type = VAR_STRING;
19003 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019004}
19005
19006/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019007 * "submatch()" function
19008 */
19009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019010f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019011{
Bram Moolenaar41571762014-04-02 19:00:58 +020019012 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019013 int no;
19014 int retList = 0;
19015
19016 no = (int)get_tv_number_chk(&argvars[0], &error);
19017 if (error)
19018 return;
19019 error = FALSE;
19020 if (argvars[1].v_type != VAR_UNKNOWN)
19021 retList = get_tv_number_chk(&argvars[1], &error);
19022 if (error)
19023 return;
19024
19025 if (retList == 0)
19026 {
19027 rettv->v_type = VAR_STRING;
19028 rettv->vval.v_string = reg_submatch(no);
19029 }
19030 else
19031 {
19032 rettv->v_type = VAR_LIST;
19033 rettv->vval.v_list = reg_submatch_list(no);
19034 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019035}
19036
19037/*
19038 * "substitute()" function
19039 */
19040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019041f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019042{
19043 char_u patbuf[NUMBUFLEN];
19044 char_u subbuf[NUMBUFLEN];
19045 char_u flagsbuf[NUMBUFLEN];
19046
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019047 char_u *str = get_tv_string_chk(&argvars[0]);
19048 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19049 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19050 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19051
Bram Moolenaar0d660222005-01-07 21:51:51 +000019052 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019053 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19054 rettv->vval.v_string = NULL;
19055 else
19056 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019057}
19058
19059/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019060 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019063f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019064{
19065 int id = 0;
19066#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019067 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019068 long col;
19069 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019070 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019072 lnum = get_tv_lnum(argvars); /* -1 on type error */
19073 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19074 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019075
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019076 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019077 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019078 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019079#endif
19080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019081 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082}
19083
19084/*
19085 * "synIDattr(id, what [, mode])" function
19086 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019088f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019089{
19090 char_u *p = NULL;
19091#ifdef FEAT_SYN_HL
19092 int id;
19093 char_u *what;
19094 char_u *mode;
19095 char_u modebuf[NUMBUFLEN];
19096 int modec;
19097
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019098 id = get_tv_number(&argvars[0]);
19099 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019100 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019101 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019102 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019104 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019105 modec = 0; /* replace invalid with current */
19106 }
19107 else
19108 {
19109#ifdef FEAT_GUI
19110 if (gui.in_use)
19111 modec = 'g';
19112 else
19113#endif
19114 if (t_colors > 1)
19115 modec = 'c';
19116 else
19117 modec = 't';
19118 }
19119
19120
19121 switch (TOLOWER_ASC(what[0]))
19122 {
19123 case 'b':
19124 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19125 p = highlight_color(id, what, modec);
19126 else /* bold */
19127 p = highlight_has_attr(id, HL_BOLD, modec);
19128 break;
19129
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019130 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019131 p = highlight_color(id, what, modec);
19132 break;
19133
19134 case 'i':
19135 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19136 p = highlight_has_attr(id, HL_INVERSE, modec);
19137 else /* italic */
19138 p = highlight_has_attr(id, HL_ITALIC, modec);
19139 break;
19140
19141 case 'n': /* name */
19142 p = get_highlight_name(NULL, id - 1);
19143 break;
19144
19145 case 'r': /* reverse */
19146 p = highlight_has_attr(id, HL_INVERSE, modec);
19147 break;
19148
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019149 case 's':
19150 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19151 p = highlight_color(id, what, modec);
19152 else /* standout */
19153 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019154 break;
19155
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019156 case 'u':
19157 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19158 /* underline */
19159 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19160 else
19161 /* undercurl */
19162 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019163 break;
19164 }
19165
19166 if (p != NULL)
19167 p = vim_strsave(p);
19168#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019169 rettv->v_type = VAR_STRING;
19170 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019171}
19172
19173/*
19174 * "synIDtrans(id)" function
19175 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019177f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019178{
19179 int id;
19180
19181#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019182 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019183
19184 if (id > 0)
19185 id = syn_get_final_id(id);
19186 else
19187#endif
19188 id = 0;
19189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019190 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019191}
19192
19193/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019194 * "synconcealed(lnum, col)" function
19195 */
19196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019197f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019198{
19199#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19200 long lnum;
19201 long col;
19202 int syntax_flags = 0;
19203 int cchar;
19204 int matchid = 0;
19205 char_u str[NUMBUFLEN];
19206#endif
19207
19208 rettv->v_type = VAR_LIST;
19209 rettv->vval.v_list = NULL;
19210
19211#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19212 lnum = get_tv_lnum(argvars); /* -1 on type error */
19213 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19214
19215 vim_memset(str, NUL, sizeof(str));
19216
19217 if (rettv_list_alloc(rettv) != FAIL)
19218 {
19219 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19220 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19221 && curwin->w_p_cole > 0)
19222 {
19223 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19224 syntax_flags = get_syntax_info(&matchid);
19225
19226 /* get the conceal character */
19227 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19228 {
19229 cchar = syn_get_sub_char();
19230 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19231 cchar = lcs_conceal;
19232 if (cchar != NUL)
19233 {
19234# ifdef FEAT_MBYTE
19235 if (has_mbyte)
19236 (*mb_char2bytes)(cchar, str);
19237 else
19238# endif
19239 str[0] = cchar;
19240 }
19241 }
19242 }
19243
19244 list_append_number(rettv->vval.v_list,
19245 (syntax_flags & HL_CONCEAL) != 0);
19246 /* -1 to auto-determine strlen */
19247 list_append_string(rettv->vval.v_list, str, -1);
19248 list_append_number(rettv->vval.v_list, matchid);
19249 }
19250#endif
19251}
19252
19253/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019254 * "synstack(lnum, col)" function
19255 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019257f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019258{
19259#ifdef FEAT_SYN_HL
19260 long lnum;
19261 long col;
19262 int i;
19263 int id;
19264#endif
19265
19266 rettv->v_type = VAR_LIST;
19267 rettv->vval.v_list = NULL;
19268
19269#ifdef FEAT_SYN_HL
19270 lnum = get_tv_lnum(argvars); /* -1 on type error */
19271 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19272
19273 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019274 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019275 && rettv_list_alloc(rettv) != FAIL)
19276 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019277 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019278 for (i = 0; ; ++i)
19279 {
19280 id = syn_get_stack_item(i);
19281 if (id < 0)
19282 break;
19283 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19284 break;
19285 }
19286 }
19287#endif
19288}
19289
Bram Moolenaar071d4272004-06-13 20:20:40 +000019290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019291get_cmd_output_as_rettv(
19292 typval_T *argvars,
19293 typval_T *rettv,
19294 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019295{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019296 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019297 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019298 char_u *infile = NULL;
19299 char_u buf[NUMBUFLEN];
19300 int err = FALSE;
19301 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019302 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019303 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019305 rettv->v_type = VAR_STRING;
19306 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019307 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019308 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019309
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019310 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019311 {
19312 /*
19313 * Write the string to a temp file, to be used for input of the shell
19314 * command.
19315 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019316 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019317 {
19318 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019319 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019320 }
19321
19322 fd = mch_fopen((char *)infile, WRITEBIN);
19323 if (fd == NULL)
19324 {
19325 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019326 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019327 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019328 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019329 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019330 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19331 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019332 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019333 else
19334 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019335 size_t len;
19336
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019337 p = get_tv_string_buf_chk(&argvars[1], buf);
19338 if (p == NULL)
19339 {
19340 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019341 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019342 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019343 len = STRLEN(p);
19344 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019345 err = TRUE;
19346 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019347 if (fclose(fd) != 0)
19348 err = TRUE;
19349 if (err)
19350 {
19351 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019352 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019353 }
19354 }
19355
Bram Moolenaar52a72462014-08-29 15:53:52 +020019356 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19357 * echoes typeahead, that messes up the display. */
19358 if (!msg_silent)
19359 flags += SHELL_COOKED;
19360
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019361 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019362 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019363 int len;
19364 listitem_T *li;
19365 char_u *s = NULL;
19366 char_u *start;
19367 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019368 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369
Bram Moolenaar52a72462014-08-29 15:53:52 +020019370 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019371 if (res == NULL)
19372 goto errret;
19373
19374 list = list_alloc();
19375 if (list == NULL)
19376 goto errret;
19377
19378 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019380 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019381 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019382 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019383 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019384
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019385 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019386 if (s == NULL)
19387 goto errret;
19388
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019389 for (p = s; start < end; ++p, ++start)
19390 *p = *start == NUL ? NL : *start;
19391 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019392
19393 li = listitem_alloc();
19394 if (li == NULL)
19395 {
19396 vim_free(s);
19397 goto errret;
19398 }
19399 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019400 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019401 li->li_tv.vval.v_string = s;
19402 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019403 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019404
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019405 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019406 rettv->v_type = VAR_LIST;
19407 rettv->vval.v_list = list;
19408 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019409 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019410 else
19411 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019412 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019413#ifdef USE_CR
19414 /* translate <CR> into <NL> */
19415 if (res != NULL)
19416 {
19417 char_u *s;
19418
19419 for (s = res; *s; ++s)
19420 {
19421 if (*s == CAR)
19422 *s = NL;
19423 }
19424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019425#else
19426# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019427 /* translate <CR><NL> into <NL> */
19428 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019429 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019430 char_u *s, *d;
19431
19432 d = res;
19433 for (s = res; *s; ++s)
19434 {
19435 if (s[0] == CAR && s[1] == NL)
19436 ++s;
19437 *d++ = *s;
19438 }
19439 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019441# endif
19442#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019443 rettv->vval.v_string = res;
19444 res = NULL;
19445 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019446
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019447errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019448 if (infile != NULL)
19449 {
19450 mch_remove(infile);
19451 vim_free(infile);
19452 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019453 if (res != NULL)
19454 vim_free(res);
19455 if (list != NULL)
19456 list_free(list, TRUE);
19457}
19458
19459/*
19460 * "system()" function
19461 */
19462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019463f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019464{
19465 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19466}
19467
19468/*
19469 * "systemlist()" function
19470 */
19471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019472f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019473{
19474 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019475}
19476
19477/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019478 * "tabpagebuflist()" function
19479 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019481f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019482{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019483#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019484 tabpage_T *tp;
19485 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019486
19487 if (argvars[0].v_type == VAR_UNKNOWN)
19488 wp = firstwin;
19489 else
19490 {
19491 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19492 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019493 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019494 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019495 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019496 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019497 for (; wp != NULL; wp = wp->w_next)
19498 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019499 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019500 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019501 }
19502#endif
19503}
19504
19505
19506/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019507 * "tabpagenr()" function
19508 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019510f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019511{
19512 int nr = 1;
19513#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019514 char_u *arg;
19515
19516 if (argvars[0].v_type != VAR_UNKNOWN)
19517 {
19518 arg = get_tv_string_chk(&argvars[0]);
19519 nr = 0;
19520 if (arg != NULL)
19521 {
19522 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019523 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019524 else
19525 EMSG2(_(e_invexpr2), arg);
19526 }
19527 }
19528 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019529 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019530#endif
19531 rettv->vval.v_number = nr;
19532}
19533
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019534
19535#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019536static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019537
19538/*
19539 * Common code for tabpagewinnr() and winnr().
19540 */
19541 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019542get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019543{
19544 win_T *twin;
19545 int nr = 1;
19546 win_T *wp;
19547 char_u *arg;
19548
19549 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19550 if (argvar->v_type != VAR_UNKNOWN)
19551 {
19552 arg = get_tv_string_chk(argvar);
19553 if (arg == NULL)
19554 nr = 0; /* type error; errmsg already given */
19555 else if (STRCMP(arg, "$") == 0)
19556 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19557 else if (STRCMP(arg, "#") == 0)
19558 {
19559 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19560 if (twin == NULL)
19561 nr = 0;
19562 }
19563 else
19564 {
19565 EMSG2(_(e_invexpr2), arg);
19566 nr = 0;
19567 }
19568 }
19569
19570 if (nr > 0)
19571 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19572 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019573 {
19574 if (wp == NULL)
19575 {
19576 /* didn't find it in this tabpage */
19577 nr = 0;
19578 break;
19579 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019580 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019581 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019582 return nr;
19583}
19584#endif
19585
19586/*
19587 * "tabpagewinnr()" function
19588 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019590f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019591{
19592 int nr = 1;
19593#ifdef FEAT_WINDOWS
19594 tabpage_T *tp;
19595
19596 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19597 if (tp == NULL)
19598 nr = 0;
19599 else
19600 nr = get_winnr(tp, &argvars[1]);
19601#endif
19602 rettv->vval.v_number = nr;
19603}
19604
19605
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019606/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019607 * "tagfiles()" function
19608 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019610f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019611{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019612 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019613 tagname_T tn;
19614 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019615
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019616 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019617 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019618 fname = alloc(MAXPATHL);
19619 if (fname == NULL)
19620 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019621
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019622 for (first = TRUE; ; first = FALSE)
19623 if (get_tagfname(&tn, first, fname) == FAIL
19624 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019625 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019626 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019627 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019628}
19629
19630/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019631 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019632 */
19633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019634f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019635{
19636 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019637
19638 tag_pattern = get_tv_string(&argvars[0]);
19639
19640 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019641 if (*tag_pattern == NUL)
19642 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019643
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019644 if (rettv_list_alloc(rettv) == OK)
19645 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019646}
19647
19648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649 * "tempname()" function
19650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019652f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653{
19654 static int x = 'A';
19655
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019656 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019657 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019658
19659 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19660 * names. Skip 'I' and 'O', they are used for shell redirection. */
19661 do
19662 {
19663 if (x == 'Z')
19664 x = '0';
19665 else if (x == '9')
19666 x = 'A';
19667 else
19668 {
19669#ifdef EBCDIC
19670 if (x == 'I')
19671 x = 'J';
19672 else if (x == 'R')
19673 x = 'S';
19674 else
19675#endif
19676 ++x;
19677 }
19678 } while (x == 'I' || x == 'O');
19679}
19680
19681/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019682 * "test(list)" function: Just checking the walls...
19683 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019685f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019686{
19687 /* Used for unit testing. Change the code below to your liking. */
19688#if 0
19689 listitem_T *li;
19690 list_T *l;
19691 char_u *bad, *good;
19692
19693 if (argvars[0].v_type != VAR_LIST)
19694 return;
19695 l = argvars[0].vval.v_list;
19696 if (l == NULL)
19697 return;
19698 li = l->lv_first;
19699 if (li == NULL)
19700 return;
19701 bad = get_tv_string(&li->li_tv);
19702 li = li->li_next;
19703 if (li == NULL)
19704 return;
19705 good = get_tv_string(&li->li_tv);
19706 rettv->vval.v_number = test_edit_score(bad, good);
19707#endif
19708}
19709
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019710#ifdef FEAT_FLOAT
19711/*
19712 * "tan()" function
19713 */
19714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019715f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019716{
19717 float_T f;
19718
19719 rettv->v_type = VAR_FLOAT;
19720 if (get_float_arg(argvars, &f) == OK)
19721 rettv->vval.v_float = tan(f);
19722 else
19723 rettv->vval.v_float = 0.0;
19724}
19725
19726/*
19727 * "tanh()" function
19728 */
19729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019730f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019731{
19732 float_T f;
19733
19734 rettv->v_type = VAR_FLOAT;
19735 if (get_float_arg(argvars, &f) == OK)
19736 rettv->vval.v_float = tanh(f);
19737 else
19738 rettv->vval.v_float = 0.0;
19739}
19740#endif
19741
Bram Moolenaard52d9742005-08-21 22:20:28 +000019742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019743 * "tolower(string)" function
19744 */
19745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019746f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019747{
19748 char_u *p;
19749
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019750 p = vim_strsave(get_tv_string(&argvars[0]));
19751 rettv->v_type = VAR_STRING;
19752 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019753
19754 if (p != NULL)
19755 while (*p != NUL)
19756 {
19757#ifdef FEAT_MBYTE
19758 int l;
19759
19760 if (enc_utf8)
19761 {
19762 int c, lc;
19763
19764 c = utf_ptr2char(p);
19765 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019766 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767 /* TODO: reallocate string when byte count changes. */
19768 if (utf_char2len(lc) == l)
19769 utf_char2bytes(lc, p);
19770 p += l;
19771 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019772 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773 p += l; /* skip multi-byte character */
19774 else
19775#endif
19776 {
19777 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19778 ++p;
19779 }
19780 }
19781}
19782
19783/*
19784 * "toupper(string)" function
19785 */
19786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019787f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019789 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019790 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791}
19792
19793/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019794 * "tr(string, fromstr, tostr)" function
19795 */
19796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019797f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019798{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019799 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019800 char_u *fromstr;
19801 char_u *tostr;
19802 char_u *p;
19803#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019804 int inlen;
19805 int fromlen;
19806 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019807 int idx;
19808 char_u *cpstr;
19809 int cplen;
19810 int first = TRUE;
19811#endif
19812 char_u buf[NUMBUFLEN];
19813 char_u buf2[NUMBUFLEN];
19814 garray_T ga;
19815
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019816 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019817 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19818 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019819
19820 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019821 rettv->v_type = VAR_STRING;
19822 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019823 if (fromstr == NULL || tostr == NULL)
19824 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019825 ga_init2(&ga, (int)sizeof(char), 80);
19826
19827#ifdef FEAT_MBYTE
19828 if (!has_mbyte)
19829#endif
19830 /* not multi-byte: fromstr and tostr must be the same length */
19831 if (STRLEN(fromstr) != STRLEN(tostr))
19832 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019833#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019834error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019835#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019836 EMSG2(_(e_invarg2), fromstr);
19837 ga_clear(&ga);
19838 return;
19839 }
19840
19841 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019842 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019843 {
19844#ifdef FEAT_MBYTE
19845 if (has_mbyte)
19846 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019847 inlen = (*mb_ptr2len)(in_str);
19848 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019849 cplen = inlen;
19850 idx = 0;
19851 for (p = fromstr; *p != NUL; p += fromlen)
19852 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019853 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019854 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019855 {
19856 for (p = tostr; *p != NUL; p += tolen)
19857 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019858 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019859 if (idx-- == 0)
19860 {
19861 cplen = tolen;
19862 cpstr = p;
19863 break;
19864 }
19865 }
19866 if (*p == NUL) /* tostr is shorter than fromstr */
19867 goto error;
19868 break;
19869 }
19870 ++idx;
19871 }
19872
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019873 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019874 {
19875 /* Check that fromstr and tostr have the same number of
19876 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019877 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019878 first = FALSE;
19879 for (p = tostr; *p != NUL; p += tolen)
19880 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019881 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019882 --idx;
19883 }
19884 if (idx != 0)
19885 goto error;
19886 }
19887
Bram Moolenaarcde88542015-08-11 19:14:00 +020019888 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019889 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019890 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019891
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019892 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019893 }
19894 else
19895#endif
19896 {
19897 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019898 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019899 if (p != NULL)
19900 ga_append(&ga, tostr[p - fromstr]);
19901 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019902 ga_append(&ga, *in_str);
19903 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019904 }
19905 }
19906
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019907 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019908 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019909 ga_append(&ga, NUL);
19910
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019911 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019912}
19913
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019914#ifdef FEAT_FLOAT
19915/*
19916 * "trunc({float})" function
19917 */
19918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019919f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019920{
19921 float_T f;
19922
19923 rettv->v_type = VAR_FLOAT;
19924 if (get_float_arg(argvars, &f) == OK)
19925 /* trunc() is not in C90, use floor() or ceil() instead. */
19926 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19927 else
19928 rettv->vval.v_float = 0.0;
19929}
19930#endif
19931
Bram Moolenaar8299df92004-07-10 09:47:34 +000019932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019933 * "type(expr)" function
19934 */
19935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019936f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019938 int n;
19939
19940 switch (argvars[0].v_type)
19941 {
19942 case VAR_NUMBER: n = 0; break;
19943 case VAR_STRING: n = 1; break;
19944 case VAR_FUNC: n = 2; break;
19945 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019946 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019947 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010019948 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010019949 if (argvars[0].vval.v_number == VVAL_FALSE
19950 || argvars[0].vval.v_number == VVAL_TRUE)
19951 n = 6;
19952 else
19953 n = 7;
19954 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010019955 case VAR_JOB: n = 8; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010019956 case VAR_UNKNOWN:
19957 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
19958 n = -1;
19959 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019960 }
19961 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019962}
19963
19964/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019965 * "undofile(name)" function
19966 */
19967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019968f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019969{
19970 rettv->v_type = VAR_STRING;
19971#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019972 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019973 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019974
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019975 if (*fname == NUL)
19976 {
19977 /* If there is no file name there will be no undo file. */
19978 rettv->vval.v_string = NULL;
19979 }
19980 else
19981 {
19982 char_u *ffname = FullName_save(fname, FALSE);
19983
19984 if (ffname != NULL)
19985 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19986 vim_free(ffname);
19987 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019988 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019989#else
19990 rettv->vval.v_string = NULL;
19991#endif
19992}
19993
19994/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019995 * "undotree()" function
19996 */
19997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019998f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020019999{
20000 if (rettv_dict_alloc(rettv) == OK)
20001 {
20002 dict_T *dict = rettv->vval.v_dict;
20003 list_T *list;
20004
Bram Moolenaar730cde92010-06-27 05:18:54 +020020005 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020006 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020007 dict_add_nr_str(dict, "save_last",
20008 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020009 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20010 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020011 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020012
20013 list = list_alloc();
20014 if (list != NULL)
20015 {
20016 u_eval_tree(curbuf->b_u_oldhead, list);
20017 dict_add_list(dict, "entries", list);
20018 }
20019 }
20020}
20021
20022/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020023 * "values(dict)" function
20024 */
20025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020026f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020027{
20028 dict_list(argvars, rettv, 1);
20029}
20030
20031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020032 * "virtcol(string)" function
20033 */
20034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020035f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020036{
20037 colnr_T vcol = 0;
20038 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020039 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020040
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020041 fp = var2fpos(&argvars[0], FALSE, &fnum);
20042 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20043 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044 {
20045 getvvcol(curwin, fp, NULL, NULL, &vcol);
20046 ++vcol;
20047 }
20048
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020049 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020050}
20051
20052/*
20053 * "visualmode()" function
20054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020056f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058 char_u str[2];
20059
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020060 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020061 str[0] = curbuf->b_visual_mode_eval;
20062 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020063 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064
20065 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020066 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020068}
20069
20070/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020071 * "wildmenumode()" function
20072 */
20073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020074f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020075{
20076#ifdef FEAT_WILDMENU
20077 if (wild_menu_showing)
20078 rettv->vval.v_number = 1;
20079#endif
20080}
20081
20082/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020083 * "winbufnr(nr)" function
20084 */
20085 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020086f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020087{
20088 win_T *wp;
20089
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020090 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020092 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020094 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095}
20096
20097/*
20098 * "wincol()" function
20099 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020101f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102{
20103 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020104 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105}
20106
20107/*
20108 * "winheight(nr)" function
20109 */
20110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020111f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112{
20113 win_T *wp;
20114
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020115 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020116 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020117 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020118 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020119 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020120}
20121
20122/*
20123 * "winline()" function
20124 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020126f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020127{
20128 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020129 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130}
20131
20132/*
20133 * "winnr()" function
20134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020136f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137{
20138 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020139
Bram Moolenaar071d4272004-06-13 20:20:40 +000020140#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020141 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020143 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144}
20145
20146/*
20147 * "winrestcmd()" function
20148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020150f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020151{
20152#ifdef FEAT_WINDOWS
20153 win_T *wp;
20154 int winnr = 1;
20155 garray_T ga;
20156 char_u buf[50];
20157
20158 ga_init2(&ga, (int)sizeof(char), 70);
20159 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20160 {
20161 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20162 ga_concat(&ga, buf);
20163# ifdef FEAT_VERTSPLIT
20164 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20165 ga_concat(&ga, buf);
20166# endif
20167 ++winnr;
20168 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020169 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020171 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020172#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020173 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020174#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020175 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176}
20177
20178/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020179 * "winrestview()" function
20180 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020182f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020183{
20184 dict_T *dict;
20185
20186 if (argvars[0].v_type != VAR_DICT
20187 || (dict = argvars[0].vval.v_dict) == NULL)
20188 EMSG(_(e_invarg));
20189 else
20190 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020191 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20192 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20193 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20194 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020195#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020196 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20197 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020198#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020199 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20200 {
20201 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20202 curwin->w_set_curswant = FALSE;
20203 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020204
Bram Moolenaar82c25852014-05-28 16:47:16 +020020205 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20206 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020207#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020208 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20209 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020210#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020211 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20212 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20213 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20214 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020215
20216 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020217 win_new_height(curwin, curwin->w_height);
20218# ifdef FEAT_VERTSPLIT
20219 win_new_width(curwin, W_WIDTH(curwin));
20220# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020221 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020222
Bram Moolenaarb851a962014-10-31 15:45:52 +010020223 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020224 curwin->w_topline = 1;
20225 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20226 curwin->w_topline = curbuf->b_ml.ml_line_count;
20227#ifdef FEAT_DIFF
20228 check_topfill(curwin, TRUE);
20229#endif
20230 }
20231}
20232
20233/*
20234 * "winsaveview()" function
20235 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020237f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020238{
20239 dict_T *dict;
20240
Bram Moolenaara800b422010-06-27 01:15:55 +020020241 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020242 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020243 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020244
20245 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20246 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20247#ifdef FEAT_VIRTUALEDIT
20248 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20249#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020250 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020251 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20252
20253 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20254#ifdef FEAT_DIFF
20255 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20256#endif
20257 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20258 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20259}
20260
20261/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020262 * "winwidth(nr)" function
20263 */
20264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020265f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020266{
20267 win_T *wp;
20268
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020269 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020270 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020271 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020272 else
20273#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020274 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020276 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277#endif
20278}
20279
Bram Moolenaar071d4272004-06-13 20:20:40 +000020280/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020281 * "wordcount()" function
20282 */
20283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020284f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020285{
20286 if (rettv_dict_alloc(rettv) == FAIL)
20287 return;
20288 cursor_pos_info(rettv->vval.v_dict);
20289}
20290
20291/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020292 * Write list of strings to file
20293 */
20294 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020295write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020296{
20297 listitem_T *li;
20298 int c;
20299 int ret = OK;
20300 char_u *s;
20301
20302 for (li = list->lv_first; li != NULL; li = li->li_next)
20303 {
20304 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20305 {
20306 if (*s == '\n')
20307 c = putc(NUL, fd);
20308 else
20309 c = putc(*s, fd);
20310 if (c == EOF)
20311 {
20312 ret = FAIL;
20313 break;
20314 }
20315 }
20316 if (!binary || li->li_next != NULL)
20317 if (putc('\n', fd) == EOF)
20318 {
20319 ret = FAIL;
20320 break;
20321 }
20322 if (ret == FAIL)
20323 {
20324 EMSG(_(e_write));
20325 break;
20326 }
20327 }
20328 return ret;
20329}
20330
20331/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020332 * "writefile()" function
20333 */
20334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020335f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020336{
20337 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020338 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020339 char_u *fname;
20340 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020341 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020342
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020343 if (check_restricted() || check_secure())
20344 return;
20345
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020346 if (argvars[0].v_type != VAR_LIST)
20347 {
20348 EMSG2(_(e_listarg), "writefile()");
20349 return;
20350 }
20351 if (argvars[0].vval.v_list == NULL)
20352 return;
20353
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020354 if (argvars[2].v_type != VAR_UNKNOWN)
20355 {
20356 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20357 binary = TRUE;
20358 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20359 append = TRUE;
20360 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020361
20362 /* Always open the file in binary mode, library functions have a mind of
20363 * their own about CR-LF conversion. */
20364 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020365 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20366 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020367 {
20368 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20369 ret = -1;
20370 }
20371 else
20372 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020373 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20374 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020375 fclose(fd);
20376 }
20377
20378 rettv->vval.v_number = ret;
20379}
20380
20381/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020382 * "xor(expr, expr)" function
20383 */
20384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020385f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020386{
20387 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20388 ^ get_tv_number_chk(&argvars[1], NULL);
20389}
20390
20391
20392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020393 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020394 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020395 */
20396 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020397var2fpos(
20398 typval_T *varp,
20399 int dollar_lnum, /* TRUE when $ is last line */
20400 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020401{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020402 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020403 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020404 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020405
Bram Moolenaara5525202006-03-02 22:52:09 +000020406 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020407 if (varp->v_type == VAR_LIST)
20408 {
20409 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020410 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020411 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020412 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020413
20414 l = varp->vval.v_list;
20415 if (l == NULL)
20416 return NULL;
20417
20418 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020419 pos.lnum = list_find_nr(l, 0L, &error);
20420 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020421 return NULL; /* invalid line number */
20422
20423 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020424 pos.col = list_find_nr(l, 1L, &error);
20425 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020426 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020427 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020428
20429 /* We accept "$" for the column number: last column. */
20430 li = list_find(l, 1L);
20431 if (li != NULL && li->li_tv.v_type == VAR_STRING
20432 && li->li_tv.vval.v_string != NULL
20433 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20434 pos.col = len + 1;
20435
Bram Moolenaara5525202006-03-02 22:52:09 +000020436 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020437 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020438 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020439 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020440
Bram Moolenaara5525202006-03-02 22:52:09 +000020441#ifdef FEAT_VIRTUALEDIT
20442 /* Get the virtual offset. Defaults to zero. */
20443 pos.coladd = list_find_nr(l, 2L, &error);
20444 if (error)
20445 pos.coladd = 0;
20446#endif
20447
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020448 return &pos;
20449 }
20450
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020451 name = get_tv_string_chk(varp);
20452 if (name == NULL)
20453 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020454 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020455 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020456 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20457 {
20458 if (VIsual_active)
20459 return &VIsual;
20460 return &curwin->w_cursor;
20461 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020462 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020463 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020464 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020465 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20466 return NULL;
20467 return pp;
20468 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020469
20470#ifdef FEAT_VIRTUALEDIT
20471 pos.coladd = 0;
20472#endif
20473
Bram Moolenaar477933c2007-07-17 14:32:23 +000020474 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020475 {
20476 pos.col = 0;
20477 if (name[1] == '0') /* "w0": first visible line */
20478 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020479 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020480 pos.lnum = curwin->w_topline;
20481 return &pos;
20482 }
20483 else if (name[1] == '$') /* "w$": last visible line */
20484 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020485 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020486 pos.lnum = curwin->w_botline - 1;
20487 return &pos;
20488 }
20489 }
20490 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020491 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020492 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020493 {
20494 pos.lnum = curbuf->b_ml.ml_line_count;
20495 pos.col = 0;
20496 }
20497 else
20498 {
20499 pos.lnum = curwin->w_cursor.lnum;
20500 pos.col = (colnr_T)STRLEN(ml_get_curline());
20501 }
20502 return &pos;
20503 }
20504 return NULL;
20505}
20506
20507/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020508 * Convert list in "arg" into a position and optional file number.
20509 * When "fnump" is NULL there is no file number, only 3 items.
20510 * Note that the column is passed on as-is, the caller may want to decrement
20511 * it to use 1 for the first column.
20512 * Return FAIL when conversion is not possible, doesn't check the position for
20513 * validity.
20514 */
20515 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020516list2fpos(
20517 typval_T *arg,
20518 pos_T *posp,
20519 int *fnump,
20520 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020521{
20522 list_T *l = arg->vval.v_list;
20523 long i = 0;
20524 long n;
20525
Bram Moolenaar493c1782014-05-28 14:34:46 +020020526 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20527 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020528 if (arg->v_type != VAR_LIST
20529 || l == NULL
20530 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020531 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020532 return FAIL;
20533
20534 if (fnump != NULL)
20535 {
20536 n = list_find_nr(l, i++, NULL); /* fnum */
20537 if (n < 0)
20538 return FAIL;
20539 if (n == 0)
20540 n = curbuf->b_fnum; /* current buffer */
20541 *fnump = n;
20542 }
20543
20544 n = list_find_nr(l, i++, NULL); /* lnum */
20545 if (n < 0)
20546 return FAIL;
20547 posp->lnum = n;
20548
20549 n = list_find_nr(l, i++, NULL); /* col */
20550 if (n < 0)
20551 return FAIL;
20552 posp->col = n;
20553
20554#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020555 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020556 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020557 posp->coladd = 0;
20558 else
20559 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020560#endif
20561
Bram Moolenaar493c1782014-05-28 14:34:46 +020020562 if (curswantp != NULL)
20563 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20564
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020565 return OK;
20566}
20567
20568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020569 * Get the length of an environment variable name.
20570 * Advance "arg" to the first character after the name.
20571 * Return 0 for error.
20572 */
20573 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020574get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020575{
20576 char_u *p;
20577 int len;
20578
20579 for (p = *arg; vim_isIDc(*p); ++p)
20580 ;
20581 if (p == *arg) /* no name found */
20582 return 0;
20583
20584 len = (int)(p - *arg);
20585 *arg = p;
20586 return len;
20587}
20588
20589/*
20590 * Get the length of the name of a function or internal variable.
20591 * "arg" is advanced to the first non-white character after the name.
20592 * Return 0 if something is wrong.
20593 */
20594 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020595get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020596{
20597 char_u *p;
20598 int len;
20599
20600 /* Find the end of the name. */
20601 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020602 {
20603 if (*p == ':')
20604 {
20605 /* "s:" is start of "s:var", but "n:" is not and can be used in
20606 * slice "[n:]". Also "xx:" is not a namespace. */
20607 len = (int)(p - *arg);
20608 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20609 || len > 1)
20610 break;
20611 }
20612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020613 if (p == *arg) /* no name found */
20614 return 0;
20615
20616 len = (int)(p - *arg);
20617 *arg = skipwhite(p);
20618
20619 return len;
20620}
20621
20622/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020623 * Get the length of the name of a variable or function.
20624 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020625 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020626 * Return -1 if curly braces expansion failed.
20627 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020628 * If the name contains 'magic' {}'s, expand them and return the
20629 * expanded name in an allocated string via 'alias' - caller must free.
20630 */
20631 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020632get_name_len(
20633 char_u **arg,
20634 char_u **alias,
20635 int evaluate,
20636 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020637{
20638 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020639 char_u *p;
20640 char_u *expr_start;
20641 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020642
20643 *alias = NULL; /* default to no alias */
20644
20645 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20646 && (*arg)[2] == (int)KE_SNR)
20647 {
20648 /* hard coded <SNR>, already translated */
20649 *arg += 3;
20650 return get_id_len(arg) + 3;
20651 }
20652 len = eval_fname_script(*arg);
20653 if (len > 0)
20654 {
20655 /* literal "<SID>", "s:" or "<SNR>" */
20656 *arg += len;
20657 }
20658
Bram Moolenaar071d4272004-06-13 20:20:40 +000020659 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020660 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020662 p = find_name_end(*arg, &expr_start, &expr_end,
20663 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020664 if (expr_start != NULL)
20665 {
20666 char_u *temp_string;
20667
20668 if (!evaluate)
20669 {
20670 len += (int)(p - *arg);
20671 *arg = skipwhite(p);
20672 return len;
20673 }
20674
20675 /*
20676 * Include any <SID> etc in the expanded string:
20677 * Thus the -len here.
20678 */
20679 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20680 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020681 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020682 *alias = temp_string;
20683 *arg = skipwhite(p);
20684 return (int)STRLEN(temp_string);
20685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020686
20687 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020688 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020689 EMSG2(_(e_invexpr2), *arg);
20690
20691 return len;
20692}
20693
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020694/*
20695 * Find the end of a variable or function name, taking care of magic braces.
20696 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20697 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020698 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020699 * Return a pointer to just after the name. Equal to "arg" if there is no
20700 * valid name.
20701 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020703find_name_end(
20704 char_u *arg,
20705 char_u **expr_start,
20706 char_u **expr_end,
20707 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020709 int mb_nest = 0;
20710 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020711 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020712 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020713
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020714 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020715 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020716 *expr_start = NULL;
20717 *expr_end = NULL;
20718 }
20719
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020720 /* Quick check for valid starting character. */
20721 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20722 return arg;
20723
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020724 for (p = arg; *p != NUL
20725 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020726 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020727 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020728 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020729 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020730 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020731 if (*p == '\'')
20732 {
20733 /* skip over 'string' to avoid counting [ and ] inside it. */
20734 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20735 ;
20736 if (*p == NUL)
20737 break;
20738 }
20739 else if (*p == '"')
20740 {
20741 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20742 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20743 if (*p == '\\' && p[1] != NUL)
20744 ++p;
20745 if (*p == NUL)
20746 break;
20747 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020748 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20749 {
20750 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020751 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020752 len = (int)(p - arg);
20753 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020754 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020755 break;
20756 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020757
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020758 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020759 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020760 if (*p == '[')
20761 ++br_nest;
20762 else if (*p == ']')
20763 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020764 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020765
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020766 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020767 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020768 if (*p == '{')
20769 {
20770 mb_nest++;
20771 if (expr_start != NULL && *expr_start == NULL)
20772 *expr_start = p;
20773 }
20774 else if (*p == '}')
20775 {
20776 mb_nest--;
20777 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20778 *expr_end = p;
20779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020781 }
20782
20783 return p;
20784}
20785
20786/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020787 * Expands out the 'magic' {}'s in a variable/function name.
20788 * Note that this can call itself recursively, to deal with
20789 * constructs like foo{bar}{baz}{bam}
20790 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20791 * "in_start" ^
20792 * "expr_start" ^
20793 * "expr_end" ^
20794 * "in_end" ^
20795 *
20796 * Returns a new allocated string, which the caller must free.
20797 * Returns NULL for failure.
20798 */
20799 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020800make_expanded_name(
20801 char_u *in_start,
20802 char_u *expr_start,
20803 char_u *expr_end,
20804 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020805{
20806 char_u c1;
20807 char_u *retval = NULL;
20808 char_u *temp_result;
20809 char_u *nextcmd = NULL;
20810
20811 if (expr_end == NULL || in_end == NULL)
20812 return NULL;
20813 *expr_start = NUL;
20814 *expr_end = NUL;
20815 c1 = *in_end;
20816 *in_end = NUL;
20817
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020818 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020819 if (temp_result != NULL && nextcmd == NULL)
20820 {
20821 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20822 + (in_end - expr_end) + 1));
20823 if (retval != NULL)
20824 {
20825 STRCPY(retval, in_start);
20826 STRCAT(retval, temp_result);
20827 STRCAT(retval, expr_end + 1);
20828 }
20829 }
20830 vim_free(temp_result);
20831
20832 *in_end = c1; /* put char back for error messages */
20833 *expr_start = '{';
20834 *expr_end = '}';
20835
20836 if (retval != NULL)
20837 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020838 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020839 if (expr_start != NULL)
20840 {
20841 /* Further expansion! */
20842 temp_result = make_expanded_name(retval, expr_start,
20843 expr_end, temp_result);
20844 vim_free(retval);
20845 retval = temp_result;
20846 }
20847 }
20848
20849 return retval;
20850}
20851
20852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020853 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020854 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020855 */
20856 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020857eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020858{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020859 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20860}
20861
20862/*
20863 * Return TRUE if character "c" can be used as the first character in a
20864 * variable or function name (excluding '{' and '}').
20865 */
20866 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020867eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020868{
20869 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870}
20871
20872/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020873 * Set number v: variable to "val".
20874 */
20875 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020876set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020877{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020878 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020879}
20880
20881/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020882 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020883 */
20884 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010020885get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020886{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020887 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020888}
20889
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020890/*
20891 * Get string v: variable value. Uses a static buffer, can only be used once.
20892 */
20893 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020894get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020895{
20896 return get_tv_string(&vimvars[idx].vv_tv);
20897}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020898
Bram Moolenaar071d4272004-06-13 20:20:40 +000020899/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020900 * Get List v: variable value. Caller must take care of reference count when
20901 * needed.
20902 */
20903 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020904get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000020905{
20906 return vimvars[idx].vv_list;
20907}
20908
20909/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020910 * Set v:char to character "c".
20911 */
20912 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020913set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020914{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020915 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020916
20917#ifdef FEAT_MBYTE
20918 if (has_mbyte)
20919 buf[(*mb_char2bytes)(c, buf)] = NUL;
20920 else
20921#endif
20922 {
20923 buf[0] = c;
20924 buf[1] = NUL;
20925 }
20926 set_vim_var_string(VV_CHAR, buf, -1);
20927}
20928
20929/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020930 * Set v:count to "count" and v:count1 to "count1".
20931 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020932 */
20933 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020934set_vcount(
20935 long count,
20936 long count1,
20937 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020938{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020939 if (set_prevcount)
20940 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020941 vimvars[VV_COUNT].vv_nr = count;
20942 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020943}
20944
20945/*
20946 * Set string v: variable to a copy of "val".
20947 */
20948 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020949set_vim_var_string(
20950 int idx,
20951 char_u *val,
20952 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020953{
Bram Moolenaara542c682016-01-31 16:28:04 +010020954 clear_tv(&vimvars[idx].vv_di.di_tv);
20955 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020956 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020957 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020959 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020960 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020961 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020962}
20963
20964/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020965 * Set List v: variable to "val".
20966 */
20967 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020968set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000020969{
Bram Moolenaara542c682016-01-31 16:28:04 +010020970 clear_tv(&vimvars[idx].vv_di.di_tv);
20971 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000020972 vimvars[idx].vv_list = val;
20973 if (val != NULL)
20974 ++val->lv_refcount;
20975}
20976
20977/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020978 * Set Dictionary v: variable to "val".
20979 */
20980 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020981set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020020982{
20983 int todo;
20984 hashitem_T *hi;
20985
Bram Moolenaara542c682016-01-31 16:28:04 +010020986 clear_tv(&vimvars[idx].vv_di.di_tv);
20987 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020020988 vimvars[idx].vv_dict = val;
20989 if (val != NULL)
20990 {
20991 ++val->dv_refcount;
20992
20993 /* Set readonly */
20994 todo = (int)val->dv_hashtab.ht_used;
20995 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20996 {
20997 if (HASHITEM_EMPTY(hi))
20998 continue;
20999 --todo;
21000 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21001 }
21002 }
21003}
21004
21005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021006 * Set v:register if needed.
21007 */
21008 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021009set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010{
21011 char_u regname;
21012
21013 if (c == 0 || c == ' ')
21014 regname = '"';
21015 else
21016 regname = c;
21017 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021018 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021019 set_vim_var_string(VV_REG, &regname, 1);
21020}
21021
21022/*
21023 * Get or set v:exception. If "oldval" == NULL, return the current value.
21024 * Otherwise, restore the value to "oldval" and return NULL.
21025 * Must always be called in pairs to save and restore v:exception! Does not
21026 * take care of memory allocations.
21027 */
21028 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021029v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021030{
21031 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021032 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021033
Bram Moolenaare9a41262005-01-15 22:18:47 +000021034 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021035 return NULL;
21036}
21037
21038/*
21039 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21040 * Otherwise, restore the value to "oldval" and return NULL.
21041 * Must always be called in pairs to save and restore v:throwpoint! Does not
21042 * take care of memory allocations.
21043 */
21044 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021045v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021046{
21047 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021048 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021049
Bram Moolenaare9a41262005-01-15 22:18:47 +000021050 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021051 return NULL;
21052}
21053
21054#if defined(FEAT_AUTOCMD) || defined(PROTO)
21055/*
21056 * Set v:cmdarg.
21057 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21058 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21059 * Must always be called in pairs!
21060 */
21061 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021062set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021063{
21064 char_u *oldval;
21065 char_u *newval;
21066 unsigned len;
21067
Bram Moolenaare9a41262005-01-15 22:18:47 +000021068 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021069 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021070 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021071 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021072 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021073 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021074 }
21075
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021076 if (eap->force_bin == FORCE_BIN)
21077 len = 6;
21078 else if (eap->force_bin == FORCE_NOBIN)
21079 len = 8;
21080 else
21081 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021082
21083 if (eap->read_edit)
21084 len += 7;
21085
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021086 if (eap->force_ff != 0)
21087 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21088# ifdef FEAT_MBYTE
21089 if (eap->force_enc != 0)
21090 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021091 if (eap->bad_char != 0)
21092 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021093# endif
21094
21095 newval = alloc(len + 1);
21096 if (newval == NULL)
21097 return NULL;
21098
21099 if (eap->force_bin == FORCE_BIN)
21100 sprintf((char *)newval, " ++bin");
21101 else if (eap->force_bin == FORCE_NOBIN)
21102 sprintf((char *)newval, " ++nobin");
21103 else
21104 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021105
21106 if (eap->read_edit)
21107 STRCAT(newval, " ++edit");
21108
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021109 if (eap->force_ff != 0)
21110 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21111 eap->cmd + eap->force_ff);
21112# ifdef FEAT_MBYTE
21113 if (eap->force_enc != 0)
21114 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21115 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021116 if (eap->bad_char == BAD_KEEP)
21117 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21118 else if (eap->bad_char == BAD_DROP)
21119 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21120 else if (eap->bad_char != 0)
21121 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021122# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021123 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021124 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125}
21126#endif
21127
21128/*
21129 * Get the value of internal variable "name".
21130 * Return OK or FAIL.
21131 */
21132 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021133get_var_tv(
21134 char_u *name,
21135 int len, /* length of "name" */
21136 typval_T *rettv, /* NULL when only checking existence */
21137 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21138 int verbose, /* may give error message */
21139 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021140{
21141 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021142 typval_T *tv = NULL;
21143 typval_T atv;
21144 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021145 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146
21147 /* truncate the name, so that we can use strcmp() */
21148 cc = name[len];
21149 name[len] = NUL;
21150
21151 /*
21152 * Check for "b:changedtick".
21153 */
21154 if (STRCMP(name, "b:changedtick") == 0)
21155 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021156 atv.v_type = VAR_NUMBER;
21157 atv.vval.v_number = curbuf->b_changedtick;
21158 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021159 }
21160
21161 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162 * Check for user-defined variables.
21163 */
21164 else
21165 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021166 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021167 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021168 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021169 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021170 if (dip != NULL)
21171 *dip = v;
21172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021173 }
21174
Bram Moolenaare9a41262005-01-15 22:18:47 +000021175 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021176 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021177 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021178 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021179 ret = FAIL;
21180 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021181 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021182 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021183
21184 name[len] = cc;
21185
21186 return ret;
21187}
21188
21189/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021190 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21191 * Also handle function call with Funcref variable: func(expr)
21192 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21193 */
21194 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021195handle_subscript(
21196 char_u **arg,
21197 typval_T *rettv,
21198 int evaluate, /* do more than finding the end */
21199 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021200{
21201 int ret = OK;
21202 dict_T *selfdict = NULL;
21203 char_u *s;
21204 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021205 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021206
21207 while (ret == OK
21208 && (**arg == '['
21209 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021210 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021211 && !vim_iswhite(*(*arg - 1)))
21212 {
21213 if (**arg == '(')
21214 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021215 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021216 if (evaluate)
21217 {
21218 functv = *rettv;
21219 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021220
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021221 /* Invoke the function. Recursive! */
21222 s = functv.vval.v_string;
21223 }
21224 else
21225 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021226 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021227 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21228 &len, evaluate, selfdict);
21229
21230 /* Clear the funcref afterwards, so that deleting it while
21231 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021232 if (evaluate)
21233 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021234
21235 /* Stop the expression evaluation when immediately aborting on
21236 * error, or when an interrupt occurred or an exception was thrown
21237 * but not caught. */
21238 if (aborting())
21239 {
21240 if (ret == OK)
21241 clear_tv(rettv);
21242 ret = FAIL;
21243 }
21244 dict_unref(selfdict);
21245 selfdict = NULL;
21246 }
21247 else /* **arg == '[' || **arg == '.' */
21248 {
21249 dict_unref(selfdict);
21250 if (rettv->v_type == VAR_DICT)
21251 {
21252 selfdict = rettv->vval.v_dict;
21253 if (selfdict != NULL)
21254 ++selfdict->dv_refcount;
21255 }
21256 else
21257 selfdict = NULL;
21258 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21259 {
21260 clear_tv(rettv);
21261 ret = FAIL;
21262 }
21263 }
21264 }
21265 dict_unref(selfdict);
21266 return ret;
21267}
21268
21269/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021270 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021271 * value).
21272 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021273 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021274alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021275{
Bram Moolenaar33570922005-01-25 22:26:29 +000021276 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021277}
21278
21279/*
21280 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021281 * The string "s" must have been allocated, it is consumed.
21282 * Return NULL for out of memory, the variable otherwise.
21283 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021284 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021285alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021286{
Bram Moolenaar33570922005-01-25 22:26:29 +000021287 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021288
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021289 rettv = alloc_tv();
21290 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021291 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021292 rettv->v_type = VAR_STRING;
21293 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021294 }
21295 else
21296 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021297 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021298}
21299
21300/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021301 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021302 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021303 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021304free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021305{
21306 if (varp != NULL)
21307 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021308 switch (varp->v_type)
21309 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021310 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021311 func_unref(varp->vval.v_string);
21312 /*FALLTHROUGH*/
21313 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021314 vim_free(varp->vval.v_string);
21315 break;
21316 case VAR_LIST:
21317 list_unref(varp->vval.v_list);
21318 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021319 case VAR_DICT:
21320 dict_unref(varp->vval.v_dict);
21321 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021322 case VAR_JOB:
21323#ifdef FEAT_JOB
21324 job_unref(varp->vval.v_job);
21325 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021326#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021327 case VAR_NUMBER:
21328 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021329 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021330 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021331 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021333 vim_free(varp);
21334 }
21335}
21336
21337/*
21338 * Free the memory for a variable value and set the value to NULL or 0.
21339 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021340 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021341clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021342{
21343 if (varp != NULL)
21344 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021345 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021346 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021347 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021348 func_unref(varp->vval.v_string);
21349 /*FALLTHROUGH*/
21350 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021351 vim_free(varp->vval.v_string);
21352 varp->vval.v_string = NULL;
21353 break;
21354 case VAR_LIST:
21355 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021356 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021357 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021358 case VAR_DICT:
21359 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021360 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021361 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021362 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021363 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021364 varp->vval.v_number = 0;
21365 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021366 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021367#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021368 varp->vval.v_float = 0.0;
21369 break;
21370#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021371 case VAR_JOB:
21372#ifdef FEAT_JOB
21373 job_unref(varp->vval.v_job);
21374 varp->vval.v_job = NULL;
21375#endif
21376 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021377 case VAR_UNKNOWN:
21378 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021379 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021380 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021381 }
21382}
21383
21384/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021385 * Set the value of a variable to NULL without freeing items.
21386 */
21387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021388init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021389{
21390 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021391 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021392}
21393
21394/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021395 * Get the number value of a variable.
21396 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021397 * For incompatible types, return 0.
21398 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21399 * caller of incompatible types: it sets *denote to TRUE if "denote"
21400 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021401 */
21402 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021403get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021404{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021405 int error = FALSE;
21406
21407 return get_tv_number_chk(varp, &error); /* return 0L on error */
21408}
21409
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021410 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021411get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021412{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021413 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021415 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021416 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021417 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021418 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021419 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021420#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021421 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021422 break;
21423#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021424 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021425 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021426 break;
21427 case VAR_STRING:
21428 if (varp->vval.v_string != NULL)
21429 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021430 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021431 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021432 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021433 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021434 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021435 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021436 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021437 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021438 case VAR_SPECIAL:
21439 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21440 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021441 case VAR_JOB:
21442#ifdef FEAT_JOB
21443 EMSG(_("E910: Using a Job as a Number"));
21444 break;
21445#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021446 case VAR_UNKNOWN:
21447 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021448 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021449 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021450 if (denote == NULL) /* useful for values that must be unsigned */
21451 n = -1;
21452 else
21453 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021454 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455}
21456
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021457#ifdef FEAT_FLOAT
21458 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021459get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021460{
21461 switch (varp->v_type)
21462 {
21463 case VAR_NUMBER:
21464 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021465 case VAR_FLOAT:
21466 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021467 case VAR_FUNC:
21468 EMSG(_("E891: Using a Funcref as a Float"));
21469 break;
21470 case VAR_STRING:
21471 EMSG(_("E892: Using a String as a Float"));
21472 break;
21473 case VAR_LIST:
21474 EMSG(_("E893: Using a List as a Float"));
21475 break;
21476 case VAR_DICT:
21477 EMSG(_("E894: Using a Dictionary as a Float"));
21478 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021479 case VAR_SPECIAL:
21480 EMSG(_("E907: Using a special value as a Float"));
21481 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021482 case VAR_JOB:
21483# ifdef FEAT_JOB
21484 EMSG(_("E911: Using a Job as a Float"));
21485 break;
21486# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021487 case VAR_UNKNOWN:
21488 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021489 break;
21490 }
21491 return 0;
21492}
21493#endif
21494
Bram Moolenaar071d4272004-06-13 20:20:40 +000021495/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021496 * Get the lnum from the first argument.
21497 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021498 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499 */
21500 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021501get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021502{
Bram Moolenaar33570922005-01-25 22:26:29 +000021503 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021504 linenr_T lnum;
21505
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021506 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021507 if (lnum == 0) /* no valid number, try using line() */
21508 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021509 rettv.v_type = VAR_NUMBER;
21510 f_line(argvars, &rettv);
21511 lnum = rettv.vval.v_number;
21512 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021513 }
21514 return lnum;
21515}
21516
21517/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021518 * Get the lnum from the first argument.
21519 * Also accepts "$", then "buf" is used.
21520 * Returns 0 on error.
21521 */
21522 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021523get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021524{
21525 if (argvars[0].v_type == VAR_STRING
21526 && argvars[0].vval.v_string != NULL
21527 && argvars[0].vval.v_string[0] == '$'
21528 && buf != NULL)
21529 return buf->b_ml.ml_line_count;
21530 return get_tv_number_chk(&argvars[0], NULL);
21531}
21532
21533/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021534 * Get the string value of a variable.
21535 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021536 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21537 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021538 * If the String variable has never been set, return an empty string.
21539 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021540 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21541 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021542 */
21543 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021544get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021545{
21546 static char_u mybuf[NUMBUFLEN];
21547
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021548 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021549}
21550
21551 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021552get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021553{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021554 char_u *res = get_tv_string_buf_chk(varp, buf);
21555
21556 return res != NULL ? res : (char_u *)"";
21557}
21558
Bram Moolenaar7d647822014-04-05 21:28:56 +020021559/*
21560 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21561 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021562 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021563get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021564{
21565 static char_u mybuf[NUMBUFLEN];
21566
21567 return get_tv_string_buf_chk(varp, mybuf);
21568}
21569
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021570 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021571get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021572{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021573 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021574 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021575 case VAR_NUMBER:
21576 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21577 return buf;
21578 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021579 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021580 break;
21581 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021582 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021583 break;
21584 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021585 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021586 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021587 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021588#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021589 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021590 break;
21591#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021592 case VAR_STRING:
21593 if (varp->vval.v_string != NULL)
21594 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021595 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021596 case VAR_SPECIAL:
21597 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21598 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021599 case VAR_JOB:
21600#ifdef FEAT_JOB
21601 {
21602 job_T *job = varp->vval.v_job;
21603 char *status = job->jv_status == JOB_FAILED ? "fail"
21604 : job->jv_status == JOB_ENDED ? "dead"
21605 : "run";
21606# ifdef UNIX
21607 vim_snprintf((char *)buf, NUMBUFLEN,
21608 "process %ld %s", (long)job->jv_pid, status);
21609# else
21610 /* TODO */
21611 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
21612# endif
21613 return buf;
21614 }
21615#endif
21616 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021617 case VAR_UNKNOWN:
21618 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021619 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021621 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021622}
21623
21624/*
21625 * Find variable "name" in the list of variables.
21626 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021627 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021628 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021629 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021631 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021632find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021633{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021635 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021636
Bram Moolenaara7043832005-01-21 11:56:39 +000021637 ht = find_var_ht(name, &varname);
21638 if (htp != NULL)
21639 *htp = ht;
21640 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021641 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021642 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021643}
21644
21645/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021646 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021647 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021648 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021649 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021650find_var_in_ht(
21651 hashtab_T *ht,
21652 int htname,
21653 char_u *varname,
21654 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021655{
Bram Moolenaar33570922005-01-25 22:26:29 +000021656 hashitem_T *hi;
21657
21658 if (*varname == NUL)
21659 {
21660 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021661 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021662 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021663 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021664 case 'g': return &globvars_var;
21665 case 'v': return &vimvars_var;
21666 case 'b': return &curbuf->b_bufvar;
21667 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021668#ifdef FEAT_WINDOWS
21669 case 't': return &curtab->tp_winvar;
21670#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021671 case 'l': return current_funccal == NULL
21672 ? NULL : &current_funccal->l_vars_var;
21673 case 'a': return current_funccal == NULL
21674 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021675 }
21676 return NULL;
21677 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021678
21679 hi = hash_find(ht, varname);
21680 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021681 {
21682 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021683 * worked find the variable again. Don't auto-load a script if it was
21684 * loaded already, otherwise it would be loaded every time when
21685 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021686 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021687 {
21688 /* Note: script_autoload() may make "hi" invalid. It must either
21689 * be obtained again or not used. */
21690 if (!script_autoload(varname, FALSE) || aborting())
21691 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021692 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021693 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021694 if (HASHITEM_EMPTY(hi))
21695 return NULL;
21696 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021697 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021698}
21699
21700/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021701 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021702 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021703 * Set "varname" to the start of name without ':'.
21704 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021705 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021706find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021707{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021708 hashitem_T *hi;
21709
Bram Moolenaar73627d02015-08-11 15:46:09 +020021710 if (name[0] == NUL)
21711 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021712 if (name[1] != ':')
21713 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021714 /* The name must not start with a colon or #. */
21715 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021716 return NULL;
21717 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021718
21719 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021720 hi = hash_find(&compat_hashtab, name);
21721 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021722 return &compat_hashtab;
21723
Bram Moolenaar071d4272004-06-13 20:20:40 +000021724 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021725 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021726 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021727 }
21728 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021729 if (*name == 'g') /* global variable */
21730 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021731 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21732 */
21733 if (vim_strchr(name + 2, ':') != NULL
21734 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021735 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021736 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021737 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021738 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021739 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021740#ifdef FEAT_WINDOWS
21741 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021742 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021743#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021744 if (*name == 'v') /* v: variable */
21745 return &vimvarht;
21746 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021747 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021748 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021749 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021750 if (*name == 's' /* script variable */
21751 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21752 return &SCRIPT_VARS(current_SID);
21753 return NULL;
21754}
21755
21756/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021757 * Get function call environment based on bactrace debug level
21758 */
21759 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021760get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021761{
21762 int i;
21763 funccall_T *funccal;
21764 funccall_T *temp_funccal;
21765
21766 funccal = current_funccal;
21767 if (debug_backtrace_level > 0)
21768 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021769 for (i = 0; i < debug_backtrace_level; i++)
21770 {
21771 temp_funccal = funccal->caller;
21772 if (temp_funccal)
21773 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021774 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021775 /* backtrace level overflow. reset to max */
21776 debug_backtrace_level = i;
21777 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021778 }
21779 return funccal;
21780}
21781
21782/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021783 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021784 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021785 * Returns NULL when it doesn't exist.
21786 */
21787 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021788get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021789{
Bram Moolenaar33570922005-01-25 22:26:29 +000021790 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021791
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021792 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021793 if (v == NULL)
21794 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021795 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021796}
21797
21798/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021799 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021800 * sourcing this script and when executing functions defined in the script.
21801 */
21802 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021803new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021804{
Bram Moolenaara7043832005-01-21 11:56:39 +000021805 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021806 hashtab_T *ht;
21807 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021808
Bram Moolenaar071d4272004-06-13 20:20:40 +000021809 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21810 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021811 /* Re-allocating ga_data means that an ht_array pointing to
21812 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021813 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021814 for (i = 1; i <= ga_scripts.ga_len; ++i)
21815 {
21816 ht = &SCRIPT_VARS(i);
21817 if (ht->ht_mask == HT_INIT_SIZE - 1)
21818 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021819 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021820 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021821 }
21822
Bram Moolenaar071d4272004-06-13 20:20:40 +000021823 while (ga_scripts.ga_len < id)
21824 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021825 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021826 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021827 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021828 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021829 }
21830 }
21831}
21832
21833/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021834 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21835 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021836 */
21837 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021838init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021839{
Bram Moolenaar33570922005-01-25 22:26:29 +000021840 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021841 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021842 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021843 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021844 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021845 dict_var->di_tv.vval.v_dict = dict;
21846 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021847 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021848 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21849 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021850}
21851
21852/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021853 * Unreference a dictionary initialized by init_var_dict().
21854 */
21855 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021856unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020021857{
21858 /* Now the dict needs to be freed if no one else is using it, go back to
21859 * normal reference counting. */
21860 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21861 dict_unref(dict);
21862}
21863
21864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021865 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021866 * Frees all allocated variables and the value they contain.
21867 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021868 */
21869 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021870vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021871{
21872 vars_clear_ext(ht, TRUE);
21873}
21874
21875/*
21876 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21877 */
21878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021879vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021880{
Bram Moolenaara7043832005-01-21 11:56:39 +000021881 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021882 hashitem_T *hi;
21883 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021884
Bram Moolenaar33570922005-01-25 22:26:29 +000021885 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021886 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021887 for (hi = ht->ht_array; todo > 0; ++hi)
21888 {
21889 if (!HASHITEM_EMPTY(hi))
21890 {
21891 --todo;
21892
Bram Moolenaar33570922005-01-25 22:26:29 +000021893 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021894 * ht_array might change then. hash_clear() takes care of it
21895 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021896 v = HI2DI(hi);
21897 if (free_val)
21898 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021899 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021900 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021901 }
21902 }
21903 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021904 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021905}
21906
Bram Moolenaara7043832005-01-21 11:56:39 +000021907/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021908 * Delete a variable from hashtab "ht" at item "hi".
21909 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021910 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021912delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021913{
Bram Moolenaar33570922005-01-25 22:26:29 +000021914 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021915
21916 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021917 clear_tv(&di->di_tv);
21918 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021919}
21920
21921/*
21922 * List the value of one internal variable.
21923 */
21924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021925list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021926{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021927 char_u *tofree;
21928 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021929 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021930
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021931 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000021932 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021933 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021934 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021935}
21936
Bram Moolenaar071d4272004-06-13 20:20:40 +000021937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021938list_one_var_a(
21939 char_u *prefix,
21940 char_u *name,
21941 int type,
21942 char_u *string,
21943 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021944{
Bram Moolenaar31859182007-08-14 20:41:13 +000021945 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21946 msg_start();
21947 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021948 if (name != NULL) /* "a:" vars don't have a name stored */
21949 msg_puts(name);
21950 msg_putchar(' ');
21951 msg_advance(22);
21952 if (type == VAR_NUMBER)
21953 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021954 else if (type == VAR_FUNC)
21955 msg_putchar('*');
21956 else if (type == VAR_LIST)
21957 {
21958 msg_putchar('[');
21959 if (*string == '[')
21960 ++string;
21961 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021962 else if (type == VAR_DICT)
21963 {
21964 msg_putchar('{');
21965 if (*string == '{')
21966 ++string;
21967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021968 else
21969 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021970
Bram Moolenaar071d4272004-06-13 20:20:40 +000021971 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021972
21973 if (type == VAR_FUNC)
21974 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021975 if (*first)
21976 {
21977 msg_clr_eos();
21978 *first = FALSE;
21979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021980}
21981
21982/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021983 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021984 * If the variable already exists, the value is updated.
21985 * Otherwise the variable is created.
21986 */
21987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021988set_var(
21989 char_u *name,
21990 typval_T *tv,
21991 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021992{
Bram Moolenaar33570922005-01-25 22:26:29 +000021993 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021994 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021995 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021996
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021997 ht = find_var_ht(name, &varname);
21998 if (ht == NULL || *varname == NUL)
21999 {
22000 EMSG2(_(e_illvar), name);
22001 return;
22002 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022003 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022004
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022005 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22006 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022007
Bram Moolenaar33570922005-01-25 22:26:29 +000022008 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022009 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022010 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022011 if (var_check_ro(v->di_flags, name, FALSE)
22012 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022013 return;
22014 if (v->di_tv.v_type != tv->v_type
22015 && !((v->di_tv.v_type == VAR_STRING
22016 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022017 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022018 || tv->v_type == VAR_NUMBER))
22019#ifdef FEAT_FLOAT
22020 && !((v->di_tv.v_type == VAR_NUMBER
22021 || v->di_tv.v_type == VAR_FLOAT)
22022 && (tv->v_type == VAR_NUMBER
22023 || tv->v_type == VAR_FLOAT))
22024#endif
22025 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022026 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022027 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022028 return;
22029 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022030
22031 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022032 * Handle setting internal v: variables separately where needed to
22033 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022034 */
22035 if (ht == &vimvarht)
22036 {
22037 if (v->di_tv.v_type == VAR_STRING)
22038 {
22039 vim_free(v->di_tv.vval.v_string);
22040 if (copy || tv->v_type != VAR_STRING)
22041 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22042 else
22043 {
22044 /* Take over the string to avoid an extra alloc/free. */
22045 v->di_tv.vval.v_string = tv->vval.v_string;
22046 tv->vval.v_string = NULL;
22047 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022048 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022049 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022050 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022051 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022052 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022053 if (STRCMP(varname, "searchforward") == 0)
22054 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022055#ifdef FEAT_SEARCH_EXTRA
22056 else if (STRCMP(varname, "hlsearch") == 0)
22057 {
22058 no_hlsearch = !v->di_tv.vval.v_number;
22059 redraw_all_later(SOME_VALID);
22060 }
22061#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022062 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022063 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022064 else if (v->di_tv.v_type != tv->v_type)
22065 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022066 }
22067
22068 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022069 }
22070 else /* add a new variable */
22071 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022072 /* Can't add "v:" variable. */
22073 if (ht == &vimvarht)
22074 {
22075 EMSG2(_(e_illvar), name);
22076 return;
22077 }
22078
Bram Moolenaar92124a32005-06-17 22:03:40 +000022079 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022080 if (!valid_varname(varname))
22081 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022082
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022083 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22084 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022085 if (v == NULL)
22086 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022087 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022088 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022089 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022090 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022091 return;
22092 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022093 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022094 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022095
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022096 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022097 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022098 else
22099 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022100 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022101 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022102 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022104}
22105
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022106/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022107 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022108 * Also give an error message.
22109 */
22110 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022111var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022112{
22113 if (flags & DI_FLAGS_RO)
22114 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022115 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022116 return TRUE;
22117 }
22118 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22119 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022120 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022121 return TRUE;
22122 }
22123 return FALSE;
22124}
22125
22126/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022127 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22128 * Also give an error message.
22129 */
22130 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022131var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022132{
22133 if (flags & DI_FLAGS_FIX)
22134 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022135 EMSG2(_("E795: Cannot delete variable %s"),
22136 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022137 return TRUE;
22138 }
22139 return FALSE;
22140}
22141
22142/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022143 * Check if a funcref is assigned to a valid variable name.
22144 * Return TRUE and give an error if not.
22145 */
22146 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022147var_check_func_name(
22148 char_u *name, /* points to start of variable name */
22149 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022150{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022151 /* Allow for w: b: s: and t:. */
22152 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022153 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22154 ? name[2] : name[0]))
22155 {
22156 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22157 name);
22158 return TRUE;
22159 }
22160 /* Don't allow hiding a function. When "v" is not NULL we might be
22161 * assigning another function to the same var, the type is checked
22162 * below. */
22163 if (new_var && function_exists(name))
22164 {
22165 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22166 name);
22167 return TRUE;
22168 }
22169 return FALSE;
22170}
22171
22172/*
22173 * Check if a variable name is valid.
22174 * Return FALSE and give an error if not.
22175 */
22176 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022177valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022178{
22179 char_u *p;
22180
22181 for (p = varname; *p != NUL; ++p)
22182 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22183 && *p != AUTOLOAD_CHAR)
22184 {
22185 EMSG2(_(e_illvar), varname);
22186 return FALSE;
22187 }
22188 return TRUE;
22189}
22190
22191/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022192 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022193 * Also give an error message, using "name" or _("name") when use_gettext is
22194 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022195 */
22196 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022197tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022198{
22199 if (lock & VAR_LOCKED)
22200 {
22201 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022202 name == NULL ? (char_u *)_("Unknown")
22203 : use_gettext ? (char_u *)_(name)
22204 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022205 return TRUE;
22206 }
22207 if (lock & VAR_FIXED)
22208 {
22209 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022210 name == NULL ? (char_u *)_("Unknown")
22211 : use_gettext ? (char_u *)_(name)
22212 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022213 return TRUE;
22214 }
22215 return FALSE;
22216}
22217
22218/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022219 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022220 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022221 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022222 * It is OK for "from" and "to" to point to the same item. This is used to
22223 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022224 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022225 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022226copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022227{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022228 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022229 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022230 switch (from->v_type)
22231 {
22232 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022233 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022234 to->vval.v_number = from->vval.v_number;
22235 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022236 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022237#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022238 to->vval.v_float = from->vval.v_float;
22239 break;
22240#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022241 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010022242#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010022243 to->vval.v_job = from->vval.v_job;
22244 ++to->vval.v_job->jv_refcount;
22245 break;
22246#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022247 case VAR_STRING:
22248 case VAR_FUNC:
22249 if (from->vval.v_string == NULL)
22250 to->vval.v_string = NULL;
22251 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022252 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022253 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022254 if (from->v_type == VAR_FUNC)
22255 func_ref(to->vval.v_string);
22256 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022257 break;
22258 case VAR_LIST:
22259 if (from->vval.v_list == NULL)
22260 to->vval.v_list = NULL;
22261 else
22262 {
22263 to->vval.v_list = from->vval.v_list;
22264 ++to->vval.v_list->lv_refcount;
22265 }
22266 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022267 case VAR_DICT:
22268 if (from->vval.v_dict == NULL)
22269 to->vval.v_dict = NULL;
22270 else
22271 {
22272 to->vval.v_dict = from->vval.v_dict;
22273 ++to->vval.v_dict->dv_refcount;
22274 }
22275 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022276 case VAR_UNKNOWN:
22277 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022278 break;
22279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280}
22281
22282/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022283 * Make a copy of an item.
22284 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022285 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22286 * reference to an already copied list/dict can be used.
22287 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022288 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022289 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022290item_copy(
22291 typval_T *from,
22292 typval_T *to,
22293 int deep,
22294 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022295{
22296 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022297 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022298
Bram Moolenaar33570922005-01-25 22:26:29 +000022299 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022300 {
22301 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022302 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022303 }
22304 ++recurse;
22305
22306 switch (from->v_type)
22307 {
22308 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022309 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022310 case VAR_STRING:
22311 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010022312 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022313 case VAR_JOB:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022314 copy_tv(from, to);
22315 break;
22316 case VAR_LIST:
22317 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022318 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022319 if (from->vval.v_list == NULL)
22320 to->vval.v_list = NULL;
22321 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22322 {
22323 /* use the copy made earlier */
22324 to->vval.v_list = from->vval.v_list->lv_copylist;
22325 ++to->vval.v_list->lv_refcount;
22326 }
22327 else
22328 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22329 if (to->vval.v_list == NULL)
22330 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022331 break;
22332 case VAR_DICT:
22333 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022334 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022335 if (from->vval.v_dict == NULL)
22336 to->vval.v_dict = NULL;
22337 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22338 {
22339 /* use the copy made earlier */
22340 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22341 ++to->vval.v_dict->dv_refcount;
22342 }
22343 else
22344 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22345 if (to->vval.v_dict == NULL)
22346 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022347 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022348 case VAR_UNKNOWN:
22349 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022350 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022351 }
22352 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022353 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022354}
22355
22356/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022357 * ":echo expr1 ..." print each argument separated with a space, add a
22358 * newline at the end.
22359 * ":echon expr1 ..." print each argument plain.
22360 */
22361 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022362ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022363{
22364 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022365 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022366 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022367 char_u *p;
22368 int needclr = TRUE;
22369 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022370 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022371
22372 if (eap->skip)
22373 ++emsg_skip;
22374 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22375 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022376 /* If eval1() causes an error message the text from the command may
22377 * still need to be cleared. E.g., "echo 22,44". */
22378 need_clr_eos = needclr;
22379
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022381 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022382 {
22383 /*
22384 * Report the invalid expression unless the expression evaluation
22385 * has been cancelled due to an aborting error, an interrupt, or an
22386 * exception.
22387 */
22388 if (!aborting())
22389 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022390 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022391 break;
22392 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022393 need_clr_eos = FALSE;
22394
Bram Moolenaar071d4272004-06-13 20:20:40 +000022395 if (!eap->skip)
22396 {
22397 if (atstart)
22398 {
22399 atstart = FALSE;
22400 /* Call msg_start() after eval1(), evaluating the expression
22401 * may cause a message to appear. */
22402 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022403 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022404 /* Mark the saved text as finishing the line, so that what
22405 * follows is displayed on a new line when scrolling back
22406 * at the more prompt. */
22407 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022408 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022410 }
22411 else if (eap->cmdidx == CMD_echo)
22412 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022413 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022414 if (p != NULL)
22415 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022416 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022417 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022418 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022419 if (*p != TAB && needclr)
22420 {
22421 /* remove any text still there from the command */
22422 msg_clr_eos();
22423 needclr = FALSE;
22424 }
22425 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022426 }
22427 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022428 {
22429#ifdef FEAT_MBYTE
22430 if (has_mbyte)
22431 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022432 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022433
22434 (void)msg_outtrans_len_attr(p, i, echo_attr);
22435 p += i - 1;
22436 }
22437 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022438#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022439 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022441 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022442 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022443 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022444 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022445 arg = skipwhite(arg);
22446 }
22447 eap->nextcmd = check_nextcmd(arg);
22448
22449 if (eap->skip)
22450 --emsg_skip;
22451 else
22452 {
22453 /* remove text that may still be there from the command */
22454 if (needclr)
22455 msg_clr_eos();
22456 if (eap->cmdidx == CMD_echo)
22457 msg_end();
22458 }
22459}
22460
22461/*
22462 * ":echohl {name}".
22463 */
22464 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022465ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022466{
22467 int id;
22468
22469 id = syn_name2id(eap->arg);
22470 if (id == 0)
22471 echo_attr = 0;
22472 else
22473 echo_attr = syn_id2attr(id);
22474}
22475
22476/*
22477 * ":execute expr1 ..." execute the result of an expression.
22478 * ":echomsg expr1 ..." Print a message
22479 * ":echoerr expr1 ..." Print an error
22480 * Each gets spaces around each argument and a newline at the end for
22481 * echo commands
22482 */
22483 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022484ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022485{
22486 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022487 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022488 int ret = OK;
22489 char_u *p;
22490 garray_T ga;
22491 int len;
22492 int save_did_emsg;
22493
22494 ga_init2(&ga, 1, 80);
22495
22496 if (eap->skip)
22497 ++emsg_skip;
22498 while (*arg != NUL && *arg != '|' && *arg != '\n')
22499 {
22500 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022501 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022502 {
22503 /*
22504 * Report the invalid expression unless the expression evaluation
22505 * has been cancelled due to an aborting error, an interrupt, or an
22506 * exception.
22507 */
22508 if (!aborting())
22509 EMSG2(_(e_invexpr2), p);
22510 ret = FAIL;
22511 break;
22512 }
22513
22514 if (!eap->skip)
22515 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022516 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022517 len = (int)STRLEN(p);
22518 if (ga_grow(&ga, len + 2) == FAIL)
22519 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022520 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022521 ret = FAIL;
22522 break;
22523 }
22524 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022525 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022526 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022527 ga.ga_len += len;
22528 }
22529
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022530 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022531 arg = skipwhite(arg);
22532 }
22533
22534 if (ret != FAIL && ga.ga_data != NULL)
22535 {
22536 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022537 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022538 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022539 out_flush();
22540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022541 else if (eap->cmdidx == CMD_echoerr)
22542 {
22543 /* We don't want to abort following commands, restore did_emsg. */
22544 save_did_emsg = did_emsg;
22545 EMSG((char_u *)ga.ga_data);
22546 if (!force_abort)
22547 did_emsg = save_did_emsg;
22548 }
22549 else if (eap->cmdidx == CMD_execute)
22550 do_cmdline((char_u *)ga.ga_data,
22551 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22552 }
22553
22554 ga_clear(&ga);
22555
22556 if (eap->skip)
22557 --emsg_skip;
22558
22559 eap->nextcmd = check_nextcmd(arg);
22560}
22561
22562/*
22563 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22564 * "arg" points to the "&" or '+' when called, to "option" when returning.
22565 * Returns NULL when no option name found. Otherwise pointer to the char
22566 * after the option name.
22567 */
22568 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022569find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022570{
22571 char_u *p = *arg;
22572
22573 ++p;
22574 if (*p == 'g' && p[1] == ':')
22575 {
22576 *opt_flags = OPT_GLOBAL;
22577 p += 2;
22578 }
22579 else if (*p == 'l' && p[1] == ':')
22580 {
22581 *opt_flags = OPT_LOCAL;
22582 p += 2;
22583 }
22584 else
22585 *opt_flags = 0;
22586
22587 if (!ASCII_ISALPHA(*p))
22588 return NULL;
22589 *arg = p;
22590
22591 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22592 p += 4; /* termcap option */
22593 else
22594 while (ASCII_ISALPHA(*p))
22595 ++p;
22596 return p;
22597}
22598
22599/*
22600 * ":function"
22601 */
22602 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022603ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022604{
22605 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022606 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022607 int j;
22608 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022609 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022610 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022611 char_u *name = NULL;
22612 char_u *p;
22613 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022614 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022615 garray_T newargs;
22616 garray_T newlines;
22617 int varargs = FALSE;
22618 int mustend = FALSE;
22619 int flags = 0;
22620 ufunc_T *fp;
22621 int indent;
22622 int nesting;
22623 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022624 dictitem_T *v;
22625 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022626 static int func_nr = 0; /* number for nameless function */
22627 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022628 hashtab_T *ht;
22629 int todo;
22630 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022631 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022632
22633 /*
22634 * ":function" without argument: list functions.
22635 */
22636 if (ends_excmd(*eap->arg))
22637 {
22638 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022639 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022640 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022641 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022642 {
22643 if (!HASHITEM_EMPTY(hi))
22644 {
22645 --todo;
22646 fp = HI2UF(hi);
22647 if (!isdigit(*fp->uf_name))
22648 list_func_head(fp, FALSE);
22649 }
22650 }
22651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022652 eap->nextcmd = check_nextcmd(eap->arg);
22653 return;
22654 }
22655
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022656 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022657 * ":function /pat": list functions matching pattern.
22658 */
22659 if (*eap->arg == '/')
22660 {
22661 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22662 if (!eap->skip)
22663 {
22664 regmatch_T regmatch;
22665
22666 c = *p;
22667 *p = NUL;
22668 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22669 *p = c;
22670 if (regmatch.regprog != NULL)
22671 {
22672 regmatch.rm_ic = p_ic;
22673
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022674 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022675 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22676 {
22677 if (!HASHITEM_EMPTY(hi))
22678 {
22679 --todo;
22680 fp = HI2UF(hi);
22681 if (!isdigit(*fp->uf_name)
22682 && vim_regexec(&regmatch, fp->uf_name, 0))
22683 list_func_head(fp, FALSE);
22684 }
22685 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022686 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022687 }
22688 }
22689 if (*p == '/')
22690 ++p;
22691 eap->nextcmd = check_nextcmd(p);
22692 return;
22693 }
22694
22695 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022696 * Get the function name. There are these situations:
22697 * func normal function name
22698 * "name" == func, "fudi.fd_dict" == NULL
22699 * dict.func new dictionary entry
22700 * "name" == NULL, "fudi.fd_dict" set,
22701 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22702 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022703 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022704 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22705 * dict.func existing dict entry that's not a Funcref
22706 * "name" == NULL, "fudi.fd_dict" set,
22707 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022708 * s:func script-local function name
22709 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022711 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022712 name = trans_function_name(&p, eap->skip, 0, &fudi);
22713 paren = (vim_strchr(p, '(') != NULL);
22714 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022715 {
22716 /*
22717 * Return on an invalid expression in braces, unless the expression
22718 * evaluation has been cancelled due to an aborting error, an
22719 * interrupt, or an exception.
22720 */
22721 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022722 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022723 if (!eap->skip && fudi.fd_newkey != NULL)
22724 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022725 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022728 else
22729 eap->skip = TRUE;
22730 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022731
Bram Moolenaar071d4272004-06-13 20:20:40 +000022732 /* An error in a function call during evaluation of an expression in magic
22733 * braces should not cause the function not to be defined. */
22734 saved_did_emsg = did_emsg;
22735 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022736
22737 /*
22738 * ":function func" with only function name: list function.
22739 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022740 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022741 {
22742 if (!ends_excmd(*skipwhite(p)))
22743 {
22744 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022745 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022746 }
22747 eap->nextcmd = check_nextcmd(p);
22748 if (eap->nextcmd != NULL)
22749 *p = NUL;
22750 if (!eap->skip && !got_int)
22751 {
22752 fp = find_func(name);
22753 if (fp != NULL)
22754 {
22755 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022756 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022757 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022758 if (FUNCLINE(fp, j) == NULL)
22759 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022760 msg_putchar('\n');
22761 msg_outnum((long)(j + 1));
22762 if (j < 9)
22763 msg_putchar(' ');
22764 if (j < 99)
22765 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022766 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022767 out_flush(); /* show a line at a time */
22768 ui_breakcheck();
22769 }
22770 if (!got_int)
22771 {
22772 msg_putchar('\n');
22773 msg_puts((char_u *)" endfunction");
22774 }
22775 }
22776 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022777 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022778 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022779 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022780 }
22781
22782 /*
22783 * ":function name(arg1, arg2)" Define function.
22784 */
22785 p = skipwhite(p);
22786 if (*p != '(')
22787 {
22788 if (!eap->skip)
22789 {
22790 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022791 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022792 }
22793 /* attempt to continue by skipping some text */
22794 if (vim_strchr(p, '(') != NULL)
22795 p = vim_strchr(p, '(');
22796 }
22797 p = skipwhite(p + 1);
22798
22799 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22800 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22801
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022802 if (!eap->skip)
22803 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022804 /* Check the name of the function. Unless it's a dictionary function
22805 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022806 if (name != NULL)
22807 arg = name;
22808 else
22809 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022810 if (arg != NULL && (fudi.fd_di == NULL
22811 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022812 {
22813 if (*arg == K_SPECIAL)
22814 j = 3;
22815 else
22816 j = 0;
22817 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22818 : eval_isnamec(arg[j])))
22819 ++j;
22820 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022821 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022822 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022823 /* Disallow using the g: dict. */
22824 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22825 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022826 }
22827
Bram Moolenaar071d4272004-06-13 20:20:40 +000022828 /*
22829 * Isolate the arguments: "arg1, arg2, ...)"
22830 */
22831 while (*p != ')')
22832 {
22833 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22834 {
22835 varargs = TRUE;
22836 p += 3;
22837 mustend = TRUE;
22838 }
22839 else
22840 {
22841 arg = p;
22842 while (ASCII_ISALNUM(*p) || *p == '_')
22843 ++p;
22844 if (arg == p || isdigit(*arg)
22845 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22846 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22847 {
22848 if (!eap->skip)
22849 EMSG2(_("E125: Illegal argument: %s"), arg);
22850 break;
22851 }
22852 if (ga_grow(&newargs, 1) == FAIL)
22853 goto erret;
22854 c = *p;
22855 *p = NUL;
22856 arg = vim_strsave(arg);
22857 if (arg == NULL)
22858 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022859
22860 /* Check for duplicate argument name. */
22861 for (i = 0; i < newargs.ga_len; ++i)
22862 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22863 {
22864 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022865 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022866 goto erret;
22867 }
22868
Bram Moolenaar071d4272004-06-13 20:20:40 +000022869 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22870 *p = c;
22871 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872 if (*p == ',')
22873 ++p;
22874 else
22875 mustend = TRUE;
22876 }
22877 p = skipwhite(p);
22878 if (mustend && *p != ')')
22879 {
22880 if (!eap->skip)
22881 EMSG2(_(e_invarg2), eap->arg);
22882 break;
22883 }
22884 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022885 if (*p != ')')
22886 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022887 ++p; /* skip the ')' */
22888
Bram Moolenaare9a41262005-01-15 22:18:47 +000022889 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022890 for (;;)
22891 {
22892 p = skipwhite(p);
22893 if (STRNCMP(p, "range", 5) == 0)
22894 {
22895 flags |= FC_RANGE;
22896 p += 5;
22897 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022898 else if (STRNCMP(p, "dict", 4) == 0)
22899 {
22900 flags |= FC_DICT;
22901 p += 4;
22902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903 else if (STRNCMP(p, "abort", 5) == 0)
22904 {
22905 flags |= FC_ABORT;
22906 p += 5;
22907 }
22908 else
22909 break;
22910 }
22911
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022912 /* When there is a line break use what follows for the function body.
22913 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22914 if (*p == '\n')
22915 line_arg = p + 1;
22916 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022917 EMSG(_(e_trailing));
22918
22919 /*
22920 * Read the body of the function, until ":endfunction" is found.
22921 */
22922 if (KeyTyped)
22923 {
22924 /* Check if the function already exists, don't let the user type the
22925 * whole function before telling him it doesn't work! For a script we
22926 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022927 if (!eap->skip && !eap->forceit)
22928 {
22929 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22930 EMSG(_(e_funcdict));
22931 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022932 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022934
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022935 if (!eap->skip && did_emsg)
22936 goto erret;
22937
Bram Moolenaar071d4272004-06-13 20:20:40 +000022938 msg_putchar('\n'); /* don't overwrite the function name */
22939 cmdline_row = msg_row;
22940 }
22941
22942 indent = 2;
22943 nesting = 0;
22944 for (;;)
22945 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022946 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022947 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022948 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022949 saved_wait_return = FALSE;
22950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022951 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022952 sourcing_lnum_off = sourcing_lnum;
22953
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022954 if (line_arg != NULL)
22955 {
22956 /* Use eap->arg, split up in parts by line breaks. */
22957 theline = line_arg;
22958 p = vim_strchr(theline, '\n');
22959 if (p == NULL)
22960 line_arg += STRLEN(line_arg);
22961 else
22962 {
22963 *p = NUL;
22964 line_arg = p + 1;
22965 }
22966 }
22967 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022968 theline = getcmdline(':', 0L, indent);
22969 else
22970 theline = eap->getline(':', eap->cookie, indent);
22971 if (KeyTyped)
22972 lines_left = Rows - 1;
22973 if (theline == NULL)
22974 {
22975 EMSG(_("E126: Missing :endfunction"));
22976 goto erret;
22977 }
22978
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022979 /* Detect line continuation: sourcing_lnum increased more than one. */
22980 if (sourcing_lnum > sourcing_lnum_off + 1)
22981 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22982 else
22983 sourcing_lnum_off = 0;
22984
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985 if (skip_until != NULL)
22986 {
22987 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22988 * don't check for ":endfunc". */
22989 if (STRCMP(theline, skip_until) == 0)
22990 {
22991 vim_free(skip_until);
22992 skip_until = NULL;
22993 }
22994 }
22995 else
22996 {
22997 /* skip ':' and blanks*/
22998 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22999 ;
23000
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023001 /* Check for "endfunction". */
23002 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023003 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023004 if (line_arg == NULL)
23005 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023006 break;
23007 }
23008
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023009 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023010 * at "end". */
23011 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23012 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023013 else if (STRNCMP(p, "if", 2) == 0
23014 || STRNCMP(p, "wh", 2) == 0
23015 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023016 || STRNCMP(p, "try", 3) == 0)
23017 indent += 2;
23018
23019 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023020 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023021 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023022 if (*p == '!')
23023 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023024 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023025 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23026 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023027 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023028 ++nesting;
23029 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030 }
23031 }
23032
23033 /* Check for ":append" or ":insert". */
23034 p = skip_range(p, NULL);
23035 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23036 || (p[0] == 'i'
23037 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23038 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23039 skip_until = vim_strsave((char_u *)".");
23040
23041 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23042 arg = skipwhite(skiptowhite(p));
23043 if (arg[0] == '<' && arg[1] =='<'
23044 && ((p[0] == 'p' && p[1] == 'y'
23045 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23046 || (p[0] == 'p' && p[1] == 'e'
23047 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23048 || (p[0] == 't' && p[1] == 'c'
23049 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023050 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23051 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023052 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23053 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023054 || (p[0] == 'm' && p[1] == 'z'
23055 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023056 ))
23057 {
23058 /* ":python <<" continues until a dot, like ":append" */
23059 p = skipwhite(arg + 2);
23060 if (*p == NUL)
23061 skip_until = vim_strsave((char_u *)".");
23062 else
23063 skip_until = vim_strsave(p);
23064 }
23065 }
23066
23067 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023068 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023069 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023070 if (line_arg == NULL)
23071 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023072 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023073 }
23074
23075 /* Copy the line to newly allocated memory. get_one_sourceline()
23076 * allocates 250 bytes per line, this saves 80% on average. The cost
23077 * is an extra alloc/free. */
23078 p = vim_strsave(theline);
23079 if (p != NULL)
23080 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023081 if (line_arg == NULL)
23082 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023083 theline = p;
23084 }
23085
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023086 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23087
23088 /* Add NULL lines for continuation lines, so that the line count is
23089 * equal to the index in the growarray. */
23090 while (sourcing_lnum_off-- > 0)
23091 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023092
23093 /* Check for end of eap->arg. */
23094 if (line_arg != NULL && *line_arg == NUL)
23095 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023096 }
23097
23098 /* Don't define the function when skipping commands or when an error was
23099 * detected. */
23100 if (eap->skip || did_emsg)
23101 goto erret;
23102
23103 /*
23104 * If there are no errors, add the function
23105 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023106 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023107 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023108 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023109 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023110 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023111 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023112 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023113 goto erret;
23114 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023115
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023116 fp = find_func(name);
23117 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023118 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023119 if (!eap->forceit)
23120 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023121 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023122 goto erret;
23123 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023124 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023125 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023126 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023127 name);
23128 goto erret;
23129 }
23130 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023131 ga_clear_strings(&(fp->uf_args));
23132 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023133 vim_free(name);
23134 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023136 }
23137 else
23138 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023139 char numbuf[20];
23140
23141 fp = NULL;
23142 if (fudi.fd_newkey == NULL && !eap->forceit)
23143 {
23144 EMSG(_(e_funcdict));
23145 goto erret;
23146 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023147 if (fudi.fd_di == NULL)
23148 {
23149 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023150 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023151 goto erret;
23152 }
23153 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023154 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023155 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023156
23157 /* Give the function a sequential number. Can only be used with a
23158 * Funcref! */
23159 vim_free(name);
23160 sprintf(numbuf, "%d", ++func_nr);
23161 name = vim_strsave((char_u *)numbuf);
23162 if (name == NULL)
23163 goto erret;
23164 }
23165
23166 if (fp == NULL)
23167 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023168 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023169 {
23170 int slen, plen;
23171 char_u *scriptname;
23172
23173 /* Check that the autoload name matches the script name. */
23174 j = FAIL;
23175 if (sourcing_name != NULL)
23176 {
23177 scriptname = autoload_name(name);
23178 if (scriptname != NULL)
23179 {
23180 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023181 plen = (int)STRLEN(p);
23182 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023183 if (slen > plen && fnamecmp(p,
23184 sourcing_name + slen - plen) == 0)
23185 j = OK;
23186 vim_free(scriptname);
23187 }
23188 }
23189 if (j == FAIL)
23190 {
23191 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23192 goto erret;
23193 }
23194 }
23195
23196 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023197 if (fp == NULL)
23198 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023199
23200 if (fudi.fd_dict != NULL)
23201 {
23202 if (fudi.fd_di == NULL)
23203 {
23204 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023205 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023206 if (fudi.fd_di == NULL)
23207 {
23208 vim_free(fp);
23209 goto erret;
23210 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023211 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23212 {
23213 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023214 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023215 goto erret;
23216 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023217 }
23218 else
23219 /* overwrite existing dict entry */
23220 clear_tv(&fudi.fd_di->di_tv);
23221 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023222 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023223 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023224 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023225
23226 /* behave like "dict" was used */
23227 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023228 }
23229
Bram Moolenaar071d4272004-06-13 20:20:40 +000023230 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023231 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023232 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23233 {
23234 vim_free(fp);
23235 goto erret;
23236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023237 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023238 fp->uf_args = newargs;
23239 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023240#ifdef FEAT_PROFILE
23241 fp->uf_tml_count = NULL;
23242 fp->uf_tml_total = NULL;
23243 fp->uf_tml_self = NULL;
23244 fp->uf_profiling = FALSE;
23245 if (prof_def_func())
23246 func_do_profile(fp);
23247#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023248 fp->uf_varargs = varargs;
23249 fp->uf_flags = flags;
23250 fp->uf_calls = 0;
23251 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023252 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023253
23254erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023255 ga_clear_strings(&newargs);
23256 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023257ret_free:
23258 vim_free(skip_until);
23259 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023260 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023261 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023262 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023263}
23264
23265/*
23266 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023267 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023268 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023269 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023270 * TFN_INT: internal function name OK
23271 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023272 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023273 * Advances "pp" to just after the function name (if no error).
23274 */
23275 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023276trans_function_name(
23277 char_u **pp,
23278 int skip, /* only find the end, don't evaluate */
23279 int flags,
23280 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023281{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023282 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023283 char_u *start;
23284 char_u *end;
23285 int lead;
23286 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023287 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023288 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023289
23290 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023291 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023292 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023293
23294 /* Check for hard coded <SNR>: already translated function ID (from a user
23295 * command). */
23296 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23297 && (*pp)[2] == (int)KE_SNR)
23298 {
23299 *pp += 3;
23300 len = get_id_len(pp) + 3;
23301 return vim_strnsave(start, len);
23302 }
23303
23304 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23305 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023306 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023307 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023308 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023309
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023310 /* Note that TFN_ flags use the same values as GLV_ flags. */
23311 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023312 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023313 if (end == start)
23314 {
23315 if (!skip)
23316 EMSG(_("E129: Function name required"));
23317 goto theend;
23318 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023319 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023320 {
23321 /*
23322 * Report an invalid expression in braces, unless the expression
23323 * evaluation has been cancelled due to an aborting error, an
23324 * interrupt, or an exception.
23325 */
23326 if (!aborting())
23327 {
23328 if (end != NULL)
23329 EMSG2(_(e_invarg2), start);
23330 }
23331 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023332 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023333 goto theend;
23334 }
23335
23336 if (lv.ll_tv != NULL)
23337 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023338 if (fdp != NULL)
23339 {
23340 fdp->fd_dict = lv.ll_dict;
23341 fdp->fd_newkey = lv.ll_newkey;
23342 lv.ll_newkey = NULL;
23343 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023344 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023345 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23346 {
23347 name = vim_strsave(lv.ll_tv->vval.v_string);
23348 *pp = end;
23349 }
23350 else
23351 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023352 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23353 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023354 EMSG(_(e_funcref));
23355 else
23356 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023357 name = NULL;
23358 }
23359 goto theend;
23360 }
23361
23362 if (lv.ll_name == NULL)
23363 {
23364 /* Error found, but continue after the function name. */
23365 *pp = end;
23366 goto theend;
23367 }
23368
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023369 /* Check if the name is a Funcref. If so, use the value. */
23370 if (lv.ll_exp_name != NULL)
23371 {
23372 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023373 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023374 if (name == lv.ll_exp_name)
23375 name = NULL;
23376 }
23377 else
23378 {
23379 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023380 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023381 if (name == *pp)
23382 name = NULL;
23383 }
23384 if (name != NULL)
23385 {
23386 name = vim_strsave(name);
23387 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023388 if (STRNCMP(name, "<SNR>", 5) == 0)
23389 {
23390 /* Change "<SNR>" to the byte sequence. */
23391 name[0] = K_SPECIAL;
23392 name[1] = KS_EXTRA;
23393 name[2] = (int)KE_SNR;
23394 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23395 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023396 goto theend;
23397 }
23398
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023399 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023400 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023401 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023402 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23403 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23404 {
23405 /* When there was "s:" already or the name expanded to get a
23406 * leading "s:" then remove it. */
23407 lv.ll_name += 2;
23408 len -= 2;
23409 lead = 2;
23410 }
23411 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023412 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023413 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023414 /* skip over "s:" and "g:" */
23415 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023416 lv.ll_name += 2;
23417 len = (int)(end - lv.ll_name);
23418 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023419
23420 /*
23421 * Copy the function name to allocated memory.
23422 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23423 * Accept <SNR>123_name() outside a script.
23424 */
23425 if (skip)
23426 lead = 0; /* do nothing */
23427 else if (lead > 0)
23428 {
23429 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023430 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23431 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023432 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023433 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023434 if (current_SID <= 0)
23435 {
23436 EMSG(_(e_usingsid));
23437 goto theend;
23438 }
23439 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23440 lead += (int)STRLEN(sid_buf);
23441 }
23442 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023443 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023444 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023445 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023446 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023447 goto theend;
23448 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023449 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023450 {
23451 char_u *cp = vim_strchr(lv.ll_name, ':');
23452
23453 if (cp != NULL && cp < end)
23454 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023455 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023456 goto theend;
23457 }
23458 }
23459
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023460 name = alloc((unsigned)(len + lead + 1));
23461 if (name != NULL)
23462 {
23463 if (lead > 0)
23464 {
23465 name[0] = K_SPECIAL;
23466 name[1] = KS_EXTRA;
23467 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023468 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023469 STRCPY(name + 3, sid_buf);
23470 }
23471 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023472 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023473 }
23474 *pp = end;
23475
23476theend:
23477 clear_lval(&lv);
23478 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023479}
23480
23481/*
23482 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23483 * Return 2 if "p" starts with "s:".
23484 * Return 0 otherwise.
23485 */
23486 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023487eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023488{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023489 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23490 * the standard library function. */
23491 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23492 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023493 return 5;
23494 if (p[0] == 's' && p[1] == ':')
23495 return 2;
23496 return 0;
23497}
23498
23499/*
23500 * Return TRUE if "p" starts with "<SID>" or "s:".
23501 * Only works if eval_fname_script() returned non-zero for "p"!
23502 */
23503 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023504eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023505{
23506 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23507}
23508
23509/*
23510 * List the head of the function: "name(arg1, arg2)".
23511 */
23512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023513list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023514{
23515 int j;
23516
23517 msg_start();
23518 if (indent)
23519 MSG_PUTS(" ");
23520 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023521 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023522 {
23523 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023524 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023525 }
23526 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023527 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023528 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023529 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023530 {
23531 if (j)
23532 MSG_PUTS(", ");
23533 msg_puts(FUNCARG(fp, j));
23534 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023535 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023536 {
23537 if (j)
23538 MSG_PUTS(", ");
23539 MSG_PUTS("...");
23540 }
23541 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023542 if (fp->uf_flags & FC_ABORT)
23543 MSG_PUTS(" abort");
23544 if (fp->uf_flags & FC_RANGE)
23545 MSG_PUTS(" range");
23546 if (fp->uf_flags & FC_DICT)
23547 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023548 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023549 if (p_verbose > 0)
23550 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023551}
23552
23553/*
23554 * Find a function by name, return pointer to it in ufuncs.
23555 * Return NULL for unknown function.
23556 */
23557 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023558find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023559{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023560 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023561
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023562 hi = hash_find(&func_hashtab, name);
23563 if (!HASHITEM_EMPTY(hi))
23564 return HI2UF(hi);
23565 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023566}
23567
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023568#if defined(EXITFREE) || defined(PROTO)
23569 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023570free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023571{
23572 hashitem_T *hi;
23573
23574 /* Need to start all over every time, because func_free() may change the
23575 * hash table. */
23576 while (func_hashtab.ht_used > 0)
23577 for (hi = func_hashtab.ht_array; ; ++hi)
23578 if (!HASHITEM_EMPTY(hi))
23579 {
23580 func_free(HI2UF(hi));
23581 break;
23582 }
23583}
23584#endif
23585
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023586 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023587translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023588{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023589 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023590 return find_internal_func(name) >= 0;
23591 return find_func(name) != NULL;
23592}
23593
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023594/*
23595 * Return TRUE if a function "name" exists.
23596 */
23597 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023598function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023599{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023600 char_u *nm = name;
23601 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023602 int n = FALSE;
23603
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023604 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23605 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023606 nm = skipwhite(nm);
23607
23608 /* Only accept "funcname", "funcname ", "funcname (..." and
23609 * "funcname(...", not "funcname!...". */
23610 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023611 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023612 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023613 return n;
23614}
23615
Bram Moolenaara1544c02013-05-30 12:35:52 +020023616 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023617get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023618{
23619 char_u *nm = name;
23620 char_u *p;
23621
23622 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23623
23624 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023625 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023626 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023627
Bram Moolenaara1544c02013-05-30 12:35:52 +020023628 vim_free(p);
23629 return NULL;
23630}
23631
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023632/*
23633 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023634 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23635 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023636 */
23637 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023638builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023639{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023640 char_u *p;
23641
23642 if (!ASCII_ISLOWER(name[0]))
23643 return FALSE;
23644 p = vim_strchr(name, AUTOLOAD_CHAR);
23645 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023646}
23647
Bram Moolenaar05159a02005-02-26 23:04:13 +000023648#if defined(FEAT_PROFILE) || defined(PROTO)
23649/*
23650 * Start profiling function "fp".
23651 */
23652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023653func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023654{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023655 int len = fp->uf_lines.ga_len;
23656
23657 if (len == 0)
23658 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023659 fp->uf_tm_count = 0;
23660 profile_zero(&fp->uf_tm_self);
23661 profile_zero(&fp->uf_tm_total);
23662 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023663 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023664 if (fp->uf_tml_total == NULL)
23665 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023666 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023667 if (fp->uf_tml_self == NULL)
23668 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023669 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023670 fp->uf_tml_idx = -1;
23671 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23672 || fp->uf_tml_self == NULL)
23673 return; /* out of memory */
23674
23675 fp->uf_profiling = TRUE;
23676}
23677
23678/*
23679 * Dump the profiling results for all functions in file "fd".
23680 */
23681 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023682func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023683{
23684 hashitem_T *hi;
23685 int todo;
23686 ufunc_T *fp;
23687 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023688 ufunc_T **sorttab;
23689 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023690
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023691 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023692 if (todo == 0)
23693 return; /* nothing to dump */
23694
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023695 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023696
Bram Moolenaar05159a02005-02-26 23:04:13 +000023697 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23698 {
23699 if (!HASHITEM_EMPTY(hi))
23700 {
23701 --todo;
23702 fp = HI2UF(hi);
23703 if (fp->uf_profiling)
23704 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023705 if (sorttab != NULL)
23706 sorttab[st_len++] = fp;
23707
Bram Moolenaar05159a02005-02-26 23:04:13 +000023708 if (fp->uf_name[0] == K_SPECIAL)
23709 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23710 else
23711 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23712 if (fp->uf_tm_count == 1)
23713 fprintf(fd, "Called 1 time\n");
23714 else
23715 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23716 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23717 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23718 fprintf(fd, "\n");
23719 fprintf(fd, "count total (s) self (s)\n");
23720
23721 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23722 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023723 if (FUNCLINE(fp, i) == NULL)
23724 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023725 prof_func_line(fd, fp->uf_tml_count[i],
23726 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023727 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23728 }
23729 fprintf(fd, "\n");
23730 }
23731 }
23732 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023733
23734 if (sorttab != NULL && st_len > 0)
23735 {
23736 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23737 prof_total_cmp);
23738 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23739 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23740 prof_self_cmp);
23741 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23742 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023743
23744 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023745}
Bram Moolenaar73830342005-02-28 22:48:19 +000023746
23747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023748prof_sort_list(
23749 FILE *fd,
23750 ufunc_T **sorttab,
23751 int st_len,
23752 char *title,
23753 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023754{
23755 int i;
23756 ufunc_T *fp;
23757
23758 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23759 fprintf(fd, "count total (s) self (s) function\n");
23760 for (i = 0; i < 20 && i < st_len; ++i)
23761 {
23762 fp = sorttab[i];
23763 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23764 prefer_self);
23765 if (fp->uf_name[0] == K_SPECIAL)
23766 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23767 else
23768 fprintf(fd, " %s()\n", fp->uf_name);
23769 }
23770 fprintf(fd, "\n");
23771}
23772
23773/*
23774 * Print the count and times for one function or function line.
23775 */
23776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023777prof_func_line(
23778 FILE *fd,
23779 int count,
23780 proftime_T *total,
23781 proftime_T *self,
23782 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023783{
23784 if (count > 0)
23785 {
23786 fprintf(fd, "%5d ", count);
23787 if (prefer_self && profile_equal(total, self))
23788 fprintf(fd, " ");
23789 else
23790 fprintf(fd, "%s ", profile_msg(total));
23791 if (!prefer_self && profile_equal(total, self))
23792 fprintf(fd, " ");
23793 else
23794 fprintf(fd, "%s ", profile_msg(self));
23795 }
23796 else
23797 fprintf(fd, " ");
23798}
23799
23800/*
23801 * Compare function for total time sorting.
23802 */
23803 static int
23804#ifdef __BORLANDC__
23805_RTLENTRYF
23806#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023807prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023808{
23809 ufunc_T *p1, *p2;
23810
23811 p1 = *(ufunc_T **)s1;
23812 p2 = *(ufunc_T **)s2;
23813 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23814}
23815
23816/*
23817 * Compare function for self time sorting.
23818 */
23819 static int
23820#ifdef __BORLANDC__
23821_RTLENTRYF
23822#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023823prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023824{
23825 ufunc_T *p1, *p2;
23826
23827 p1 = *(ufunc_T **)s1;
23828 p2 = *(ufunc_T **)s2;
23829 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23830}
23831
Bram Moolenaar05159a02005-02-26 23:04:13 +000023832#endif
23833
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023834/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023835 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023836 * Return TRUE if a package was loaded.
23837 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023838 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023839script_autoload(
23840 char_u *name,
23841 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023842{
23843 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023844 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023845 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023846 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023847
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023848 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023849 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023850 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023851 return FALSE;
23852
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023853 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023854
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023855 /* Find the name in the list of previously loaded package names. Skip
23856 * "autoload/", it's always the same. */
23857 for (i = 0; i < ga_loaded.ga_len; ++i)
23858 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23859 break;
23860 if (!reload && i < ga_loaded.ga_len)
23861 ret = FALSE; /* was loaded already */
23862 else
23863 {
23864 /* Remember the name if it wasn't loaded already. */
23865 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23866 {
23867 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23868 tofree = NULL;
23869 }
23870
23871 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023872 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023873 ret = TRUE;
23874 }
23875
23876 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023877 return ret;
23878}
23879
23880/*
23881 * Return the autoload script name for a function or variable name.
23882 * Returns NULL when out of memory.
23883 */
23884 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023885autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023886{
23887 char_u *p;
23888 char_u *scriptname;
23889
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023890 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023891 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23892 if (scriptname == NULL)
23893 return FALSE;
23894 STRCPY(scriptname, "autoload/");
23895 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023896 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023897 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023898 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023899 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023900 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023901}
23902
Bram Moolenaar071d4272004-06-13 20:20:40 +000023903#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23904
23905/*
23906 * Function given to ExpandGeneric() to obtain the list of user defined
23907 * function names.
23908 */
23909 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023910get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023911{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023912 static long_u done;
23913 static hashitem_T *hi;
23914 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023915
23916 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023917 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023918 done = 0;
23919 hi = func_hashtab.ht_array;
23920 }
23921 if (done < func_hashtab.ht_used)
23922 {
23923 if (done++ > 0)
23924 ++hi;
23925 while (HASHITEM_EMPTY(hi))
23926 ++hi;
23927 fp = HI2UF(hi);
23928
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023929 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023930 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023931
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023932 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23933 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023934
23935 cat_func_name(IObuff, fp);
23936 if (xp->xp_context != EXPAND_USER_FUNC)
23937 {
23938 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023939 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023940 STRCAT(IObuff, ")");
23941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023942 return IObuff;
23943 }
23944 return NULL;
23945}
23946
23947#endif /* FEAT_CMDL_COMPL */
23948
23949/*
23950 * Copy the function name of "fp" to buffer "buf".
23951 * "buf" must be able to hold the function name plus three bytes.
23952 * Takes care of script-local function names.
23953 */
23954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023955cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023956{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023957 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023958 {
23959 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023960 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023961 }
23962 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023963 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023964}
23965
23966/*
23967 * ":delfunction {name}"
23968 */
23969 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023970ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023971{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023972 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023973 char_u *p;
23974 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023975 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023976
23977 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023978 name = trans_function_name(&p, eap->skip, 0, &fudi);
23979 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023980 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023981 {
23982 if (fudi.fd_dict != NULL && !eap->skip)
23983 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023984 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023986 if (!ends_excmd(*skipwhite(p)))
23987 {
23988 vim_free(name);
23989 EMSG(_(e_trailing));
23990 return;
23991 }
23992 eap->nextcmd = check_nextcmd(p);
23993 if (eap->nextcmd != NULL)
23994 *p = NUL;
23995
23996 if (!eap->skip)
23997 fp = find_func(name);
23998 vim_free(name);
23999
24000 if (!eap->skip)
24001 {
24002 if (fp == NULL)
24003 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024004 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024005 return;
24006 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024007 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008 {
24009 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24010 return;
24011 }
24012
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024013 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024014 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024015 /* Delete the dict item that refers to the function, it will
24016 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024017 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024018 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024019 else
24020 func_free(fp);
24021 }
24022}
24023
24024/*
24025 * Free a function and remove it from the list of functions.
24026 */
24027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024028func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024029{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024030 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024031
24032 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024033 ga_clear_strings(&(fp->uf_args));
24034 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024035#ifdef FEAT_PROFILE
24036 vim_free(fp->uf_tml_count);
24037 vim_free(fp->uf_tml_total);
24038 vim_free(fp->uf_tml_self);
24039#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024040
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024041 /* remove the function from the function hashtable */
24042 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24043 if (HASHITEM_EMPTY(hi))
24044 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024045 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024046 hash_remove(&func_hashtab, hi);
24047
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024048 vim_free(fp);
24049}
24050
24051/*
24052 * Unreference a Function: decrement the reference count and free it when it
24053 * becomes zero. Only for numbered functions.
24054 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024055 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024056func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024057{
24058 ufunc_T *fp;
24059
24060 if (name != NULL && isdigit(*name))
24061 {
24062 fp = find_func(name);
24063 if (fp == NULL)
24064 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024065 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024066 {
24067 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024068 * when "uf_calls" becomes zero. */
24069 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024070 func_free(fp);
24071 }
24072 }
24073}
24074
24075/*
24076 * Count a reference to a Function.
24077 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024078 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024079func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024080{
24081 ufunc_T *fp;
24082
24083 if (name != NULL && isdigit(*name))
24084 {
24085 fp = find_func(name);
24086 if (fp == NULL)
24087 EMSG2(_(e_intern2), "func_ref()");
24088 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024089 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024090 }
24091}
24092
24093/*
24094 * Call a user function.
24095 */
24096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024097call_user_func(
24098 ufunc_T *fp, /* pointer to function */
24099 int argcount, /* nr of args */
24100 typval_T *argvars, /* arguments */
24101 typval_T *rettv, /* return value */
24102 linenr_T firstline, /* first line of range */
24103 linenr_T lastline, /* last line of range */
24104 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024105{
Bram Moolenaar33570922005-01-25 22:26:29 +000024106 char_u *save_sourcing_name;
24107 linenr_T save_sourcing_lnum;
24108 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024109 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024110 int save_did_emsg;
24111 static int depth = 0;
24112 dictitem_T *v;
24113 int fixvar_idx = 0; /* index in fixvar[] */
24114 int i;
24115 int ai;
24116 char_u numbuf[NUMBUFLEN];
24117 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024118 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024119#ifdef FEAT_PROFILE
24120 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024121 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024123
24124 /* If depth of calling is getting too high, don't execute the function */
24125 if (depth >= p_mfd)
24126 {
24127 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024128 rettv->v_type = VAR_NUMBER;
24129 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024130 return;
24131 }
24132 ++depth;
24133
24134 line_breakcheck(); /* check for CTRL-C hit */
24135
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024136 fc = (funccall_T *)alloc(sizeof(funccall_T));
24137 fc->caller = current_funccal;
24138 current_funccal = fc;
24139 fc->func = fp;
24140 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024141 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024142 fc->linenr = 0;
24143 fc->returned = FALSE;
24144 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024145 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024146 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24147 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024148
Bram Moolenaar33570922005-01-25 22:26:29 +000024149 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024150 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024151 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24152 * each argument variable and saves a lot of time.
24153 */
24154 /*
24155 * Init l: variables.
24156 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024157 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024158 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024159 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024160 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24161 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024162 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024163 name = v->di_key;
24164 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024165 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024166 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024167 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024168 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024169 v->di_tv.vval.v_dict = selfdict;
24170 ++selfdict->dv_refcount;
24171 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024172
Bram Moolenaar33570922005-01-25 22:26:29 +000024173 /*
24174 * Init a: variables.
24175 * Set a:0 to "argcount".
24176 * Set a:000 to a list with room for the "..." arguments.
24177 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024178 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024179 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024180 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024181 /* Use "name" to avoid a warning from some compiler that checks the
24182 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024183 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024184 name = v->di_key;
24185 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024186 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024187 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024188 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024189 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024190 v->di_tv.vval.v_list = &fc->l_varlist;
24191 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24192 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24193 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024194
24195 /*
24196 * Set a:firstline to "firstline" and a:lastline to "lastline".
24197 * Set a:name to named arguments.
24198 * Set a:N to the "..." arguments.
24199 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024200 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024201 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024202 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024203 (varnumber_T)lastline);
24204 for (i = 0; i < argcount; ++i)
24205 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024206 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024207 if (ai < 0)
24208 /* named argument a:name */
24209 name = FUNCARG(fp, i);
24210 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024211 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024212 /* "..." argument a:1, a:2, etc. */
24213 sprintf((char *)numbuf, "%d", ai + 1);
24214 name = numbuf;
24215 }
24216 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24217 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024218 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024219 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24220 }
24221 else
24222 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024223 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24224 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024225 if (v == NULL)
24226 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024227 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024228 }
24229 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024230 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024231
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024232 /* Note: the values are copied directly to avoid alloc/free.
24233 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024234 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024235 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024236
24237 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24238 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024239 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24240 fc->l_listitems[ai].li_tv = argvars[i];
24241 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024242 }
24243 }
24244
Bram Moolenaar071d4272004-06-13 20:20:40 +000024245 /* Don't redraw while executing the function. */
24246 ++RedrawingDisabled;
24247 save_sourcing_name = sourcing_name;
24248 save_sourcing_lnum = sourcing_lnum;
24249 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024250 /* need space for function name + ("function " + 3) or "[number]" */
24251 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24252 + STRLEN(fp->uf_name) + 20;
24253 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024254 if (sourcing_name != NULL)
24255 {
24256 if (save_sourcing_name != NULL
24257 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024258 sprintf((char *)sourcing_name, "%s[%d]..",
24259 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024260 else
24261 STRCPY(sourcing_name, "function ");
24262 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24263
24264 if (p_verbose >= 12)
24265 {
24266 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024267 verbose_enter_scroll();
24268
Bram Moolenaar555b2802005-05-19 21:08:39 +000024269 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024270 if (p_verbose >= 14)
24271 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024272 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024273 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024274 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024275 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024276
24277 msg_puts((char_u *)"(");
24278 for (i = 0; i < argcount; ++i)
24279 {
24280 if (i > 0)
24281 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024282 if (argvars[i].v_type == VAR_NUMBER)
24283 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024284 else
24285 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024286 /* Do not want errors such as E724 here. */
24287 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024288 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024289 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024290 if (s != NULL)
24291 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024292 if (vim_strsize(s) > MSG_BUF_CLEN)
24293 {
24294 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24295 s = buf;
24296 }
24297 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024298 vim_free(tofree);
24299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024300 }
24301 }
24302 msg_puts((char_u *)")");
24303 }
24304 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024305
24306 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024307 --no_wait_return;
24308 }
24309 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024310#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024311 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024312 {
24313 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24314 func_do_profile(fp);
24315 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024316 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024317 {
24318 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024319 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024320 profile_zero(&fp->uf_tm_children);
24321 }
24322 script_prof_save(&wait_start);
24323 }
24324#endif
24325
Bram Moolenaar071d4272004-06-13 20:20:40 +000024326 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024327 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024328 save_did_emsg = did_emsg;
24329 did_emsg = FALSE;
24330
24331 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024332 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024333 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24334
24335 --RedrawingDisabled;
24336
24337 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024338 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024339 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024340 clear_tv(rettv);
24341 rettv->v_type = VAR_NUMBER;
24342 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024343 }
24344
Bram Moolenaar05159a02005-02-26 23:04:13 +000024345#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024346 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024347 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024348 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024349 profile_end(&call_start);
24350 profile_sub_wait(&wait_start, &call_start);
24351 profile_add(&fp->uf_tm_total, &call_start);
24352 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024353 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024354 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024355 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24356 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024357 }
24358 }
24359#endif
24360
Bram Moolenaar071d4272004-06-13 20:20:40 +000024361 /* when being verbose, mention the return value */
24362 if (p_verbose >= 12)
24363 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024364 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024365 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024366
Bram Moolenaar071d4272004-06-13 20:20:40 +000024367 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024368 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024369 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024370 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024371 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024372 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024373 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024374 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024375 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024376 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024377 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024378
Bram Moolenaar555b2802005-05-19 21:08:39 +000024379 /* The value may be very long. Skip the middle part, so that we
24380 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024381 * truncate it at the end. Don't want errors such as E724 here. */
24382 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024383 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024384 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024385 if (s != NULL)
24386 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024387 if (vim_strsize(s) > MSG_BUF_CLEN)
24388 {
24389 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24390 s = buf;
24391 }
24392 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024393 vim_free(tofree);
24394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024395 }
24396 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024397
24398 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024399 --no_wait_return;
24400 }
24401
24402 vim_free(sourcing_name);
24403 sourcing_name = save_sourcing_name;
24404 sourcing_lnum = save_sourcing_lnum;
24405 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024406#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024407 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024408 script_prof_restore(&wait_start);
24409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024410
24411 if (p_verbose >= 12 && sourcing_name != NULL)
24412 {
24413 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024414 verbose_enter_scroll();
24415
Bram Moolenaar555b2802005-05-19 21:08:39 +000024416 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024417 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024418
24419 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024420 --no_wait_return;
24421 }
24422
24423 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024424 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024425 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024426
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024427 /* If the a:000 list and the l: and a: dicts are not referenced we can
24428 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024429 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24430 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24431 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24432 {
24433 free_funccal(fc, FALSE);
24434 }
24435 else
24436 {
24437 hashitem_T *hi;
24438 listitem_T *li;
24439 int todo;
24440
24441 /* "fc" is still in use. This can happen when returning "a:000" or
24442 * assigning "l:" to a global variable.
24443 * Link "fc" in the list for garbage collection later. */
24444 fc->caller = previous_funccal;
24445 previous_funccal = fc;
24446
24447 /* Make a copy of the a: variables, since we didn't do that above. */
24448 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24449 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24450 {
24451 if (!HASHITEM_EMPTY(hi))
24452 {
24453 --todo;
24454 v = HI2DI(hi);
24455 copy_tv(&v->di_tv, &v->di_tv);
24456 }
24457 }
24458
24459 /* Make a copy of the a:000 items, since we didn't do that above. */
24460 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24461 copy_tv(&li->li_tv, &li->li_tv);
24462 }
24463}
24464
24465/*
24466 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024467 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024468 */
24469 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024470can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024471{
24472 return (fc->l_varlist.lv_copyID != copyID
24473 && fc->l_vars.dv_copyID != copyID
24474 && fc->l_avars.dv_copyID != copyID);
24475}
24476
24477/*
24478 * Free "fc" and what it contains.
24479 */
24480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024481free_funccal(
24482 funccall_T *fc,
24483 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024484{
24485 listitem_T *li;
24486
24487 /* The a: variables typevals may not have been allocated, only free the
24488 * allocated variables. */
24489 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24490
24491 /* free all l: variables */
24492 vars_clear(&fc->l_vars.dv_hashtab);
24493
24494 /* Free the a:000 variables if they were allocated. */
24495 if (free_val)
24496 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24497 clear_tv(&li->li_tv);
24498
24499 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024500}
24501
24502/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024503 * Add a number variable "name" to dict "dp" with value "nr".
24504 */
24505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024506add_nr_var(
24507 dict_T *dp,
24508 dictitem_T *v,
24509 char *name,
24510 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024511{
24512 STRCPY(v->di_key, name);
24513 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24514 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24515 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024516 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024517 v->di_tv.vval.v_number = nr;
24518}
24519
24520/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024521 * ":return [expr]"
24522 */
24523 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024524ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024525{
24526 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024527 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024528 int returning = FALSE;
24529
24530 if (current_funccal == NULL)
24531 {
24532 EMSG(_("E133: :return not inside a function"));
24533 return;
24534 }
24535
24536 if (eap->skip)
24537 ++emsg_skip;
24538
24539 eap->nextcmd = NULL;
24540 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024541 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024542 {
24543 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024544 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024545 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024546 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024547 }
24548 /* It's safer to return also on error. */
24549 else if (!eap->skip)
24550 {
24551 /*
24552 * Return unless the expression evaluation has been cancelled due to an
24553 * aborting error, an interrupt, or an exception.
24554 */
24555 if (!aborting())
24556 returning = do_return(eap, FALSE, TRUE, NULL);
24557 }
24558
24559 /* When skipping or the return gets pending, advance to the next command
24560 * in this line (!returning). Otherwise, ignore the rest of the line.
24561 * Following lines will be ignored by get_func_line(). */
24562 if (returning)
24563 eap->nextcmd = NULL;
24564 else if (eap->nextcmd == NULL) /* no argument */
24565 eap->nextcmd = check_nextcmd(arg);
24566
24567 if (eap->skip)
24568 --emsg_skip;
24569}
24570
24571/*
24572 * Return from a function. Possibly makes the return pending. Also called
24573 * for a pending return at the ":endtry" or after returning from an extra
24574 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024575 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024576 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024577 * FALSE when the return gets pending.
24578 */
24579 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024580do_return(
24581 exarg_T *eap,
24582 int reanimate,
24583 int is_cmd,
24584 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024585{
24586 int idx;
24587 struct condstack *cstack = eap->cstack;
24588
24589 if (reanimate)
24590 /* Undo the return. */
24591 current_funccal->returned = FALSE;
24592
24593 /*
24594 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24595 * not in its finally clause (which then is to be executed next) is found.
24596 * In this case, make the ":return" pending for execution at the ":endtry".
24597 * Otherwise, return normally.
24598 */
24599 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24600 if (idx >= 0)
24601 {
24602 cstack->cs_pending[idx] = CSTP_RETURN;
24603
24604 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024605 /* A pending return again gets pending. "rettv" points to an
24606 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024607 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024608 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024609 else
24610 {
24611 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024612 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024613 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024614 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024615
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024616 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024617 {
24618 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024619 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024620 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024621 else
24622 EMSG(_(e_outofmem));
24623 }
24624 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024625 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024626
24627 if (reanimate)
24628 {
24629 /* The pending return value could be overwritten by a ":return"
24630 * without argument in a finally clause; reset the default
24631 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024632 current_funccal->rettv->v_type = VAR_NUMBER;
24633 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024634 }
24635 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024636 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024637 }
24638 else
24639 {
24640 current_funccal->returned = TRUE;
24641
24642 /* If the return is carried out now, store the return value. For
24643 * a return immediately after reanimation, the value is already
24644 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024645 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024646 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024647 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024648 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024649 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024650 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024651 }
24652 }
24653
24654 return idx < 0;
24655}
24656
24657/*
24658 * Free the variable with a pending return value.
24659 */
24660 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024661discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024662{
Bram Moolenaar33570922005-01-25 22:26:29 +000024663 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024664}
24665
24666/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024667 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024668 * is an allocated string. Used by report_pending() for verbose messages.
24669 */
24670 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024671get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024672{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024673 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024674 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024675 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024676
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024677 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024678 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024679 if (s == NULL)
24680 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024681
24682 STRCPY(IObuff, ":return ");
24683 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24684 if (STRLEN(s) + 8 >= IOSIZE)
24685 STRCPY(IObuff + IOSIZE - 4, "...");
24686 vim_free(tofree);
24687 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024688}
24689
24690/*
24691 * Get next function line.
24692 * Called by do_cmdline() to get the next line.
24693 * Returns allocated string, or NULL for end of function.
24694 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024695 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024696get_func_line(
24697 int c UNUSED,
24698 void *cookie,
24699 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024700{
Bram Moolenaar33570922005-01-25 22:26:29 +000024701 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024702 ufunc_T *fp = fcp->func;
24703 char_u *retval;
24704 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024705
24706 /* If breakpoints have been added/deleted need to check for it. */
24707 if (fcp->dbg_tick != debug_tick)
24708 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024709 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024710 sourcing_lnum);
24711 fcp->dbg_tick = debug_tick;
24712 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024713#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024714 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024715 func_line_end(cookie);
24716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024717
Bram Moolenaar05159a02005-02-26 23:04:13 +000024718 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024719 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24720 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024721 retval = NULL;
24722 else
24723 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024724 /* Skip NULL lines (continuation lines). */
24725 while (fcp->linenr < gap->ga_len
24726 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24727 ++fcp->linenr;
24728 if (fcp->linenr >= gap->ga_len)
24729 retval = NULL;
24730 else
24731 {
24732 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24733 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024734#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024735 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024736 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024737#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024739 }
24740
24741 /* Did we encounter a breakpoint? */
24742 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24743 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024744 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024745 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024746 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024747 sourcing_lnum);
24748 fcp->dbg_tick = debug_tick;
24749 }
24750
24751 return retval;
24752}
24753
Bram Moolenaar05159a02005-02-26 23:04:13 +000024754#if defined(FEAT_PROFILE) || defined(PROTO)
24755/*
24756 * Called when starting to read a function line.
24757 * "sourcing_lnum" must be correct!
24758 * When skipping lines it may not actually be executed, but we won't find out
24759 * until later and we need to store the time now.
24760 */
24761 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024762func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024763{
24764 funccall_T *fcp = (funccall_T *)cookie;
24765 ufunc_T *fp = fcp->func;
24766
24767 if (fp->uf_profiling && sourcing_lnum >= 1
24768 && sourcing_lnum <= fp->uf_lines.ga_len)
24769 {
24770 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024771 /* Skip continuation lines. */
24772 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24773 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024774 fp->uf_tml_execed = FALSE;
24775 profile_start(&fp->uf_tml_start);
24776 profile_zero(&fp->uf_tml_children);
24777 profile_get_wait(&fp->uf_tml_wait);
24778 }
24779}
24780
24781/*
24782 * Called when actually executing a function line.
24783 */
24784 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024785func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024786{
24787 funccall_T *fcp = (funccall_T *)cookie;
24788 ufunc_T *fp = fcp->func;
24789
24790 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24791 fp->uf_tml_execed = TRUE;
24792}
24793
24794/*
24795 * Called when done with a function line.
24796 */
24797 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024798func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024799{
24800 funccall_T *fcp = (funccall_T *)cookie;
24801 ufunc_T *fp = fcp->func;
24802
24803 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24804 {
24805 if (fp->uf_tml_execed)
24806 {
24807 ++fp->uf_tml_count[fp->uf_tml_idx];
24808 profile_end(&fp->uf_tml_start);
24809 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024810 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024811 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24812 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024813 }
24814 fp->uf_tml_idx = -1;
24815 }
24816}
24817#endif
24818
Bram Moolenaar071d4272004-06-13 20:20:40 +000024819/*
24820 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024821 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024822 */
24823 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024824func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024825{
Bram Moolenaar33570922005-01-25 22:26:29 +000024826 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024827
24828 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24829 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024830 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024831 || fcp->returned);
24832}
24833
24834/*
24835 * return TRUE if cookie indicates a function which "abort"s on errors.
24836 */
24837 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024838func_has_abort(
24839 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024840{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024841 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024842}
24843
24844#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24845typedef enum
24846{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024847 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24848 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24849 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024850} var_flavour_T;
24851
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024852static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024853
24854 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010024855var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024856{
24857 char_u *p = varname;
24858
24859 if (ASCII_ISUPPER(*p))
24860 {
24861 while (*(++p))
24862 if (ASCII_ISLOWER(*p))
24863 return VAR_FLAVOUR_SESSION;
24864 return VAR_FLAVOUR_VIMINFO;
24865 }
24866 else
24867 return VAR_FLAVOUR_DEFAULT;
24868}
24869#endif
24870
24871#if defined(FEAT_VIMINFO) || defined(PROTO)
24872/*
24873 * Restore global vars that start with a capital from the viminfo file
24874 */
24875 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024876read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024877{
24878 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024879 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024880 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024881 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024882
24883 if (!writing && (find_viminfo_parameter('!') != NULL))
24884 {
24885 tab = vim_strchr(virp->vir_line + 1, '\t');
24886 if (tab != NULL)
24887 {
24888 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024889 switch (*tab)
24890 {
24891 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024892#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024893 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024894#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024895 case 'D': type = VAR_DICT; break;
24896 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010024897 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024899
24900 tab = vim_strchr(tab, '\t');
24901 if (tab != NULL)
24902 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024903 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024904 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024905 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024906 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024907#ifdef FEAT_FLOAT
24908 else if (type == VAR_FLOAT)
24909 (void)string2float(tab + 1, &tv.vval.v_float);
24910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024911 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024912 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024913 if (type == VAR_DICT || type == VAR_LIST)
24914 {
24915 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24916
24917 if (etv == NULL)
24918 /* Failed to parse back the dict or list, use it as a
24919 * string. */
24920 tv.v_type = VAR_STRING;
24921 else
24922 {
24923 vim_free(tv.vval.v_string);
24924 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024925 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024926 }
24927 }
24928
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024929 /* when in a function use global variables */
24930 save_funccal = current_funccal;
24931 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024932 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024933 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024934
24935 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024936 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024937 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24938 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024939 }
24940 }
24941 }
24942
24943 return viminfo_readline(virp);
24944}
24945
24946/*
24947 * Write global vars that start with a capital to the viminfo file
24948 */
24949 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024950write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024951{
Bram Moolenaar33570922005-01-25 22:26:29 +000024952 hashitem_T *hi;
24953 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024954 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024955 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024956 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024957 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024958 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024959
24960 if (find_viminfo_parameter('!') == NULL)
24961 return;
24962
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024963 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024964
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024965 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024966 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024967 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024968 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024969 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024970 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024971 this_var = HI2DI(hi);
24972 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024973 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024974 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024975 {
24976 case VAR_STRING: s = "STR"; break;
24977 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024978 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024979 case VAR_DICT: s = "DIC"; break;
24980 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010024981 case VAR_SPECIAL: s = "XPL"; break;
24982
24983 case VAR_UNKNOWN:
24984 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010024985 case VAR_JOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +010024986 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000024987 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024988 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024989 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024990 if (p != NULL)
24991 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024992 vim_free(tofree);
24993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024994 }
24995 }
24996}
24997#endif
24998
24999#if defined(FEAT_SESSION) || defined(PROTO)
25000 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025001store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025002{
Bram Moolenaar33570922005-01-25 22:26:29 +000025003 hashitem_T *hi;
25004 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025005 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025006 char_u *p, *t;
25007
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025008 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025009 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025010 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025011 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025012 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025013 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025014 this_var = HI2DI(hi);
25015 if ((this_var->di_tv.v_type == VAR_NUMBER
25016 || this_var->di_tv.v_type == VAR_STRING)
25017 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025018 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025019 /* Escape special characters with a backslash. Turn a LF and
25020 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025021 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025022 (char_u *)"\\\"\n\r");
25023 if (p == NULL) /* out of memory */
25024 break;
25025 for (t = p; *t != NUL; ++t)
25026 if (*t == '\n')
25027 *t = 'n';
25028 else if (*t == '\r')
25029 *t = 'r';
25030 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025031 this_var->di_key,
25032 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25033 : ' ',
25034 p,
25035 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25036 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025037 || put_eol(fd) == FAIL)
25038 {
25039 vim_free(p);
25040 return FAIL;
25041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025042 vim_free(p);
25043 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025044#ifdef FEAT_FLOAT
25045 else if (this_var->di_tv.v_type == VAR_FLOAT
25046 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25047 {
25048 float_T f = this_var->di_tv.vval.v_float;
25049 int sign = ' ';
25050
25051 if (f < 0)
25052 {
25053 f = -f;
25054 sign = '-';
25055 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025056 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025057 this_var->di_key, sign, f) < 0)
25058 || put_eol(fd) == FAIL)
25059 return FAIL;
25060 }
25061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025062 }
25063 }
25064 return OK;
25065}
25066#endif
25067
Bram Moolenaar661b1822005-07-28 22:36:45 +000025068/*
25069 * Display script name where an item was last set.
25070 * Should only be invoked when 'verbose' is non-zero.
25071 */
25072 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025073last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025074{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025075 char_u *p;
25076
Bram Moolenaar661b1822005-07-28 22:36:45 +000025077 if (scriptID != 0)
25078 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025079 p = home_replace_save(NULL, get_scriptname(scriptID));
25080 if (p != NULL)
25081 {
25082 verbose_enter();
25083 MSG_PUTS(_("\n\tLast set from "));
25084 MSG_PUTS(p);
25085 vim_free(p);
25086 verbose_leave();
25087 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025088 }
25089}
25090
Bram Moolenaard812df62008-11-09 12:46:09 +000025091/*
25092 * List v:oldfiles in a nice way.
25093 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025094 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025095ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025096{
25097 list_T *l = vimvars[VV_OLDFILES].vv_list;
25098 listitem_T *li;
25099 int nr = 0;
25100
25101 if (l == NULL)
25102 msg((char_u *)_("No old files"));
25103 else
25104 {
25105 msg_start();
25106 msg_scroll = TRUE;
25107 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25108 {
25109 msg_outnum((long)++nr);
25110 MSG_PUTS(": ");
25111 msg_outtrans(get_tv_string(&li->li_tv));
25112 msg_putchar('\n');
25113 out_flush(); /* output one line at a time */
25114 ui_breakcheck();
25115 }
25116 /* Assume "got_int" was set to truncate the listing. */
25117 got_int = FALSE;
25118
25119#ifdef FEAT_BROWSE_CMD
25120 if (cmdmod.browse)
25121 {
25122 quit_more = FALSE;
25123 nr = prompt_for_number(FALSE);
25124 msg_starthere();
25125 if (nr > 0)
25126 {
25127 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25128 (long)nr);
25129
25130 if (p != NULL)
25131 {
25132 p = expand_env_save(p);
25133 eap->arg = p;
25134 eap->cmdidx = CMD_edit;
25135 cmdmod.browse = FALSE;
25136 do_exedit(eap, NULL);
25137 vim_free(p);
25138 }
25139 }
25140 }
25141#endif
25142 }
25143}
25144
Bram Moolenaar53744302015-07-17 17:38:22 +020025145/* reset v:option_new, v:option_old and v:option_type */
25146 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025147reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025148{
25149 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25150 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25151 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25152}
25153
25154
Bram Moolenaar071d4272004-06-13 20:20:40 +000025155#endif /* FEAT_EVAL */
25156
Bram Moolenaar071d4272004-06-13 20:20:40 +000025157
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025158#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025159
25160#ifdef WIN3264
25161/*
25162 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25163 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025164static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25165static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25166static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025167
25168/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025169 * Get the short path (8.3) for the filename in "fnamep".
25170 * Only works for a valid file name.
25171 * When the path gets longer "fnamep" is changed and the allocated buffer
25172 * is put in "bufp".
25173 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25174 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025175 */
25176 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025177get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025178{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025179 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025180 char_u *newbuf;
25181
25182 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025183 l = GetShortPathName(*fnamep, *fnamep, len);
25184 if (l > len - 1)
25185 {
25186 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025187 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025188 newbuf = vim_strnsave(*fnamep, l);
25189 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025190 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025191
25192 vim_free(*bufp);
25193 *fnamep = *bufp = newbuf;
25194
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025195 /* Really should always succeed, as the buffer is big enough. */
25196 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025197 }
25198
25199 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025200 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025201}
25202
25203/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025204 * Get the short path (8.3) for the filename in "fname". The converted
25205 * path is returned in "bufp".
25206 *
25207 * Some of the directories specified in "fname" may not exist. This function
25208 * will shorten the existing directories at the beginning of the path and then
25209 * append the remaining non-existing path.
25210 *
25211 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025212 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025213 * bufp - Pointer to an allocated buffer for the filename.
25214 * fnamelen - Length of the filename pointed to by fname
25215 *
25216 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025217 */
25218 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025219shortpath_for_invalid_fname(
25220 char_u **fname,
25221 char_u **bufp,
25222 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025223{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025224 char_u *short_fname, *save_fname, *pbuf_unused;
25225 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025226 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025227 int old_len, len;
25228 int new_len, sfx_len;
25229 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025230
25231 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025232 old_len = *fnamelen;
25233 save_fname = vim_strnsave(*fname, old_len);
25234 pbuf_unused = NULL;
25235 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025236
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025237 endp = save_fname + old_len - 1; /* Find the end of the copy */
25238 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025239
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025240 /*
25241 * Try shortening the supplied path till it succeeds by removing one
25242 * directory at a time from the tail of the path.
25243 */
25244 len = 0;
25245 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025246 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025247 /* go back one path-separator */
25248 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25249 --endp;
25250 if (endp <= save_fname)
25251 break; /* processed the complete path */
25252
25253 /*
25254 * Replace the path separator with a NUL and try to shorten the
25255 * resulting path.
25256 */
25257 ch = *endp;
25258 *endp = 0;
25259 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025260 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025261 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25262 {
25263 retval = FAIL;
25264 goto theend;
25265 }
25266 *endp = ch; /* preserve the string */
25267
25268 if (len > 0)
25269 break; /* successfully shortened the path */
25270
25271 /* failed to shorten the path. Skip the path separator */
25272 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025273 }
25274
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025275 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025276 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025277 /*
25278 * Succeeded in shortening the path. Now concatenate the shortened
25279 * path with the remaining path at the tail.
25280 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025281
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025282 /* Compute the length of the new path. */
25283 sfx_len = (int)(save_endp - endp) + 1;
25284 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025285
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025286 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025287 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025288 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025289 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025290 /* There is not enough space in the currently allocated string,
25291 * copy it to a buffer big enough. */
25292 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025293 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025294 {
25295 retval = FAIL;
25296 goto theend;
25297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025298 }
25299 else
25300 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025301 /* Transfer short_fname to the main buffer (it's big enough),
25302 * unless get_short_pathname() did its work in-place. */
25303 *fname = *bufp = save_fname;
25304 if (short_fname != save_fname)
25305 vim_strncpy(save_fname, short_fname, len);
25306 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025307 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025308
25309 /* concat the not-shortened part of the path */
25310 vim_strncpy(*fname + len, endp, sfx_len);
25311 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025312 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025313
25314theend:
25315 vim_free(pbuf_unused);
25316 vim_free(save_fname);
25317
25318 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025319}
25320
25321/*
25322 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025323 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025324 */
25325 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025326shortpath_for_partial(
25327 char_u **fnamep,
25328 char_u **bufp,
25329 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025330{
25331 int sepcount, len, tflen;
25332 char_u *p;
25333 char_u *pbuf, *tfname;
25334 int hasTilde;
25335
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025336 /* Count up the path separators from the RHS.. so we know which part
25337 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025338 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025339 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025340 if (vim_ispathsep(*p))
25341 ++sepcount;
25342
25343 /* Need full path first (use expand_env() to remove a "~/") */
25344 hasTilde = (**fnamep == '~');
25345 if (hasTilde)
25346 pbuf = tfname = expand_env_save(*fnamep);
25347 else
25348 pbuf = tfname = FullName_save(*fnamep, FALSE);
25349
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025350 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025351
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025352 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25353 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025354
25355 if (len == 0)
25356 {
25357 /* Don't have a valid filename, so shorten the rest of the
25358 * path if we can. This CAN give us invalid 8.3 filenames, but
25359 * there's not a lot of point in guessing what it might be.
25360 */
25361 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025362 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25363 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025364 }
25365
25366 /* Count the paths backward to find the beginning of the desired string. */
25367 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025368 {
25369#ifdef FEAT_MBYTE
25370 if (has_mbyte)
25371 p -= mb_head_off(tfname, p);
25372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025373 if (vim_ispathsep(*p))
25374 {
25375 if (sepcount == 0 || (hasTilde && sepcount == 1))
25376 break;
25377 else
25378 sepcount --;
25379 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025381 if (hasTilde)
25382 {
25383 --p;
25384 if (p >= tfname)
25385 *p = '~';
25386 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025387 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025388 }
25389 else
25390 ++p;
25391
25392 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25393 vim_free(*bufp);
25394 *fnamelen = (int)STRLEN(p);
25395 *bufp = pbuf;
25396 *fnamep = p;
25397
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025398 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025399}
25400#endif /* WIN3264 */
25401
25402/*
25403 * Adjust a filename, according to a string of modifiers.
25404 * *fnamep must be NUL terminated when called. When returning, the length is
25405 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025406 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025407 * When there is an error, *fnamep is set to NULL.
25408 */
25409 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025410modify_fname(
25411 char_u *src, /* string with modifiers */
25412 int *usedlen, /* characters after src that are used */
25413 char_u **fnamep, /* file name so far */
25414 char_u **bufp, /* buffer for allocated file name or NULL */
25415 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025416{
25417 int valid = 0;
25418 char_u *tail;
25419 char_u *s, *p, *pbuf;
25420 char_u dirname[MAXPATHL];
25421 int c;
25422 int has_fullname = 0;
25423#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025424 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025425 int has_shortname = 0;
25426#endif
25427
25428repeat:
25429 /* ":p" - full path/file_name */
25430 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25431 {
25432 has_fullname = 1;
25433
25434 valid |= VALID_PATH;
25435 *usedlen += 2;
25436
25437 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25438 if ((*fnamep)[0] == '~'
25439#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25440 && ((*fnamep)[1] == '/'
25441# ifdef BACKSLASH_IN_FILENAME
25442 || (*fnamep)[1] == '\\'
25443# endif
25444 || (*fnamep)[1] == NUL)
25445
25446#endif
25447 )
25448 {
25449 *fnamep = expand_env_save(*fnamep);
25450 vim_free(*bufp); /* free any allocated file name */
25451 *bufp = *fnamep;
25452 if (*fnamep == NULL)
25453 return -1;
25454 }
25455
25456 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025457 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025458 {
25459 if (vim_ispathsep(*p)
25460 && p[1] == '.'
25461 && (p[2] == NUL
25462 || vim_ispathsep(p[2])
25463 || (p[2] == '.'
25464 && (p[3] == NUL || vim_ispathsep(p[3])))))
25465 break;
25466 }
25467
25468 /* FullName_save() is slow, don't use it when not needed. */
25469 if (*p != NUL || !vim_isAbsName(*fnamep))
25470 {
25471 *fnamep = FullName_save(*fnamep, *p != NUL);
25472 vim_free(*bufp); /* free any allocated file name */
25473 *bufp = *fnamep;
25474 if (*fnamep == NULL)
25475 return -1;
25476 }
25477
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025478#ifdef WIN3264
25479# if _WIN32_WINNT >= 0x0500
25480 if (vim_strchr(*fnamep, '~') != NULL)
25481 {
25482 /* Expand 8.3 filename to full path. Needed to make sure the same
25483 * file does not have two different names.
25484 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25485 p = alloc(_MAX_PATH + 1);
25486 if (p != NULL)
25487 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025488 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025489 {
25490 vim_free(*bufp);
25491 *bufp = *fnamep = p;
25492 }
25493 else
25494 vim_free(p);
25495 }
25496 }
25497# endif
25498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025499 /* Append a path separator to a directory. */
25500 if (mch_isdir(*fnamep))
25501 {
25502 /* Make room for one or two extra characters. */
25503 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25504 vim_free(*bufp); /* free any allocated file name */
25505 *bufp = *fnamep;
25506 if (*fnamep == NULL)
25507 return -1;
25508 add_pathsep(*fnamep);
25509 }
25510 }
25511
25512 /* ":." - path relative to the current directory */
25513 /* ":~" - path relative to the home directory */
25514 /* ":8" - shortname path - postponed till after */
25515 while (src[*usedlen] == ':'
25516 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25517 {
25518 *usedlen += 2;
25519 if (c == '8')
25520 {
25521#ifdef WIN3264
25522 has_shortname = 1; /* Postpone this. */
25523#endif
25524 continue;
25525 }
25526 pbuf = NULL;
25527 /* Need full path first (use expand_env() to remove a "~/") */
25528 if (!has_fullname)
25529 {
25530 if (c == '.' && **fnamep == '~')
25531 p = pbuf = expand_env_save(*fnamep);
25532 else
25533 p = pbuf = FullName_save(*fnamep, FALSE);
25534 }
25535 else
25536 p = *fnamep;
25537
25538 has_fullname = 0;
25539
25540 if (p != NULL)
25541 {
25542 if (c == '.')
25543 {
25544 mch_dirname(dirname, MAXPATHL);
25545 s = shorten_fname(p, dirname);
25546 if (s != NULL)
25547 {
25548 *fnamep = s;
25549 if (pbuf != NULL)
25550 {
25551 vim_free(*bufp); /* free any allocated file name */
25552 *bufp = pbuf;
25553 pbuf = NULL;
25554 }
25555 }
25556 }
25557 else
25558 {
25559 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25560 /* Only replace it when it starts with '~' */
25561 if (*dirname == '~')
25562 {
25563 s = vim_strsave(dirname);
25564 if (s != NULL)
25565 {
25566 *fnamep = s;
25567 vim_free(*bufp);
25568 *bufp = s;
25569 }
25570 }
25571 }
25572 vim_free(pbuf);
25573 }
25574 }
25575
25576 tail = gettail(*fnamep);
25577 *fnamelen = (int)STRLEN(*fnamep);
25578
25579 /* ":h" - head, remove "/file_name", can be repeated */
25580 /* Don't remove the first "/" or "c:\" */
25581 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25582 {
25583 valid |= VALID_HEAD;
25584 *usedlen += 2;
25585 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025586 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025587 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025588 *fnamelen = (int)(tail - *fnamep);
25589#ifdef VMS
25590 if (*fnamelen > 0)
25591 *fnamelen += 1; /* the path separator is part of the path */
25592#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025593 if (*fnamelen == 0)
25594 {
25595 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25596 p = vim_strsave((char_u *)".");
25597 if (p == NULL)
25598 return -1;
25599 vim_free(*bufp);
25600 *bufp = *fnamep = tail = p;
25601 *fnamelen = 1;
25602 }
25603 else
25604 {
25605 while (tail > s && !after_pathsep(s, tail))
25606 mb_ptr_back(*fnamep, tail);
25607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025608 }
25609
25610 /* ":8" - shortname */
25611 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25612 {
25613 *usedlen += 2;
25614#ifdef WIN3264
25615 has_shortname = 1;
25616#endif
25617 }
25618
25619#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025620 /*
25621 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025622 */
25623 if (has_shortname)
25624 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025625 /* Copy the string if it is shortened by :h and when it wasn't copied
25626 * yet, because we are going to change it in place. Avoids changing
25627 * the buffer name for "%:8". */
25628 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025629 {
25630 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025631 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025632 return -1;
25633 vim_free(*bufp);
25634 *bufp = *fnamep = p;
25635 }
25636
25637 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025638 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025639 if (!has_fullname && !vim_isAbsName(*fnamep))
25640 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025641 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025642 return -1;
25643 }
25644 else
25645 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025646 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025647
Bram Moolenaardc935552011-08-17 15:23:23 +020025648 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025649 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025650 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025651 return -1;
25652
25653 if (l == 0)
25654 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025655 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025656 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025657 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025658 return -1;
25659 }
25660 *fnamelen = l;
25661 }
25662 }
25663#endif /* WIN3264 */
25664
25665 /* ":t" - tail, just the basename */
25666 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25667 {
25668 *usedlen += 2;
25669 *fnamelen -= (int)(tail - *fnamep);
25670 *fnamep = tail;
25671 }
25672
25673 /* ":e" - extension, can be repeated */
25674 /* ":r" - root, without extension, can be repeated */
25675 while (src[*usedlen] == ':'
25676 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25677 {
25678 /* find a '.' in the tail:
25679 * - for second :e: before the current fname
25680 * - otherwise: The last '.'
25681 */
25682 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25683 s = *fnamep - 2;
25684 else
25685 s = *fnamep + *fnamelen - 1;
25686 for ( ; s > tail; --s)
25687 if (s[0] == '.')
25688 break;
25689 if (src[*usedlen + 1] == 'e') /* :e */
25690 {
25691 if (s > tail)
25692 {
25693 *fnamelen += (int)(*fnamep - (s + 1));
25694 *fnamep = s + 1;
25695#ifdef VMS
25696 /* cut version from the extension */
25697 s = *fnamep + *fnamelen - 1;
25698 for ( ; s > *fnamep; --s)
25699 if (s[0] == ';')
25700 break;
25701 if (s > *fnamep)
25702 *fnamelen = s - *fnamep;
25703#endif
25704 }
25705 else if (*fnamep <= tail)
25706 *fnamelen = 0;
25707 }
25708 else /* :r */
25709 {
25710 if (s > tail) /* remove one extension */
25711 *fnamelen = (int)(s - *fnamep);
25712 }
25713 *usedlen += 2;
25714 }
25715
25716 /* ":s?pat?foo?" - substitute */
25717 /* ":gs?pat?foo?" - global substitute */
25718 if (src[*usedlen] == ':'
25719 && (src[*usedlen + 1] == 's'
25720 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25721 {
25722 char_u *str;
25723 char_u *pat;
25724 char_u *sub;
25725 int sep;
25726 char_u *flags;
25727 int didit = FALSE;
25728
25729 flags = (char_u *)"";
25730 s = src + *usedlen + 2;
25731 if (src[*usedlen + 1] == 'g')
25732 {
25733 flags = (char_u *)"g";
25734 ++s;
25735 }
25736
25737 sep = *s++;
25738 if (sep)
25739 {
25740 /* find end of pattern */
25741 p = vim_strchr(s, sep);
25742 if (p != NULL)
25743 {
25744 pat = vim_strnsave(s, (int)(p - s));
25745 if (pat != NULL)
25746 {
25747 s = p + 1;
25748 /* find end of substitution */
25749 p = vim_strchr(s, sep);
25750 if (p != NULL)
25751 {
25752 sub = vim_strnsave(s, (int)(p - s));
25753 str = vim_strnsave(*fnamep, *fnamelen);
25754 if (sub != NULL && str != NULL)
25755 {
25756 *usedlen = (int)(p + 1 - src);
25757 s = do_string_sub(str, pat, sub, flags);
25758 if (s != NULL)
25759 {
25760 *fnamep = s;
25761 *fnamelen = (int)STRLEN(s);
25762 vim_free(*bufp);
25763 *bufp = s;
25764 didit = TRUE;
25765 }
25766 }
25767 vim_free(sub);
25768 vim_free(str);
25769 }
25770 vim_free(pat);
25771 }
25772 }
25773 /* after using ":s", repeat all the modifiers */
25774 if (didit)
25775 goto repeat;
25776 }
25777 }
25778
Bram Moolenaar26df0922014-02-23 23:39:13 +010025779 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25780 {
25781 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25782 if (p == NULL)
25783 return -1;
25784 vim_free(*bufp);
25785 *bufp = *fnamep = p;
25786 *fnamelen = (int)STRLEN(p);
25787 *usedlen += 2;
25788 }
25789
Bram Moolenaar071d4272004-06-13 20:20:40 +000025790 return valid;
25791}
25792
25793/*
25794 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25795 * "flags" can be "g" to do a global substitute.
25796 * Returns an allocated string, NULL for error.
25797 */
25798 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025799do_string_sub(
25800 char_u *str,
25801 char_u *pat,
25802 char_u *sub,
25803 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025804{
25805 int sublen;
25806 regmatch_T regmatch;
25807 int i;
25808 int do_all;
25809 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025810 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025811 garray_T ga;
25812 char_u *ret;
25813 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025814 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025815
25816 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25817 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025818 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025819
25820 ga_init2(&ga, 1, 200);
25821
25822 do_all = (flags[0] == 'g');
25823
25824 regmatch.rm_ic = p_ic;
25825 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25826 if (regmatch.regprog != NULL)
25827 {
25828 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025829 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025830 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25831 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025832 /* Skip empty match except for first match. */
25833 if (regmatch.startp[0] == regmatch.endp[0])
25834 {
25835 if (zero_width == regmatch.startp[0])
25836 {
25837 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025838 i = MB_PTR2LEN(tail);
25839 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25840 (size_t)i);
25841 ga.ga_len += i;
25842 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025843 continue;
25844 }
25845 zero_width = regmatch.startp[0];
25846 }
25847
Bram Moolenaar071d4272004-06-13 20:20:40 +000025848 /*
25849 * Get some space for a temporary buffer to do the substitution
25850 * into. It will contain:
25851 * - The text up to where the match is.
25852 * - The substituted text.
25853 * - The text after the match.
25854 */
25855 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025856 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025857 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25858 {
25859 ga_clear(&ga);
25860 break;
25861 }
25862
25863 /* copy the text up to where the match is */
25864 i = (int)(regmatch.startp[0] - tail);
25865 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25866 /* add the substituted text */
25867 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25868 + ga.ga_len + i, TRUE, TRUE, FALSE);
25869 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025870 tail = regmatch.endp[0];
25871 if (*tail == NUL)
25872 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025873 if (!do_all)
25874 break;
25875 }
25876
25877 if (ga.ga_data != NULL)
25878 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25879
Bram Moolenaar473de612013-06-08 18:19:48 +020025880 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025881 }
25882
25883 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25884 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025885 if (p_cpo == empty_option)
25886 p_cpo = save_cpo;
25887 else
25888 /* Darn, evaluating {sub} expression changed the value. */
25889 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025890
25891 return ret;
25892}
25893
25894#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */